-
Notifications
You must be signed in to change notification settings - Fork 0
/
ii2d_main.js
158 lines (128 loc) · 4.59 KB
/
ii2d_main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
var canvas;
var ctx; // !!! context 2D (drawing)
var engine;
var mouseIsDown;
var oldMouse = new Vector();
var repulse;
window.addEventListener("load",main);
//window.addEventListener("load",scene_tp3); //Question 4
//window.addEventListener("load",scene_tp3_12); //Question 12
function main() {
canvas=document.getElementById("canvas");
canvas.addEventListener('mousedown', handleMouseDown, false);
canvas.addEventListener('mousemove', handleMouseMove, false);
canvas.addEventListener('mouseup', handleMouseUp, false);
ctx=canvas.getContext("2d");
repulse=document.getElementById('repulseur');
repulse.addEventListener('click', repulseClick);
engine=new Engine();
var gen1 = new GeneratorBox();
gen1.birthRate = 3;
gen1.min.setXY(50,50); // setXY à faire dans la classe Vector
gen1.max.setXY(55,55); //100, 200 , (55,55) min
var gen2 = new GeneratorBox();
gen2.birthRate = 3;
gen2.min.setXY(150,150);
gen2.max.setXY(155,155); //250, 300 , (155,155) min
//creation des cercles
let circle1 = new Circle(new Vector(100, 400), 50);
let circle2 = new Circle(new Vector(250, 220), 10);
//creation des segments
let seg1=new Segment(new Vector(100,200),new Vector(250,300));
let seg2=new Segment(new Vector(350,420),new Vector(400,30));
engine.particleManager.generatorList.push(gen1, gen2); // ajoute au tableau generatorList
engine.obstacleManager.all.push(circle1, circle2); //ajout des cercles
engine.obstacleManager.all.push(seg1, seg2); //ajout des segments
engine.start();
}
function scene_tp3(){
//retrait volontaire des obstacles pour montrer les générateurs.
canvas=document.getElementById("canvas");
ctx=canvas.getContext("2d");
engine=new Engine();
var gen1 = new GeneratorBox();
gen1.birthRate = 8;
gen1.min.setXY(100,100);
gen1.max.setXY(105,105);
var gen2 = new GeneratorBox();
gen2.birthRate = 6;
gen2.min.setXY(220,220);
gen2.max.setXY(225,225);
engine.particleManager.generatorList.push(gen1,gen2);
engine.start();
}
function scene_tp3_12() {
canvas=document.getElementById("canvas");
ctx=canvas.getContext("2d");
engine=new Engine();
var gen1 = new GeneratorBox();
gen1.birthRate = 3;
gen1.min.setXY(50,50); // setXY à faire dans la classe Vector
gen1.max.setXY(55,55); //100, 200 , (55,55) min
var gen2 = new GeneratorBox();
gen2.birthRate = 3;
gen2.min.setXY(150,150);
gen2.max.setXY(155,155); //250, 300 , (155,155) min
//creation des cercles
let circle1 = new Circle(new Vector(100, 400), 50);
let circle2 = new Circle(new Vector(250, 220), 10);
//creation des segments
let seg1=new Segment(new Vector(100,200),new Vector(250,300));
let seg2=new Segment(new Vector(350,420),new Vector(400,30));
engine.particleManager.generatorList.push(gen1, gen2); // ajoute au tableau generatorList
engine.obstacleManager.all.push(circle1, circle2); //ajout des cercles
engine.obstacleManager.all.push(seg1, seg2); //ajout des segments
engine.start();
}
function handleMouseDown(){
// get the mouse relative to canvas
mouseIsDown = true;
let mouseX = event.offsetX;
let mouseY = event.offsetY;
let mouse = new Vector(mouseX, mouseY);
oldMouse.set(mouse);
engine.particleManager.select(mouse);
engine.obstacleManager.select(mouse);
if (engine.obstacleManager.selected){
//console.log(engine.obstacleManager.selected);
engine.obstacleManager.selected.color = "#00FF00";
}
//console.log(engine.particleManager.selected);
//console.log(engine.obstacleManager.all[0].distance(mouse));
}
function handleMouseMove(){
if (mouseIsDown){
let mouseX = event.offsetX;
let mouseY = event.offsetY;
let mouse = new Vector(mouseX, mouseY);
if (engine.particleManager.selected){ //pour ne pas avoir d'erreur si null
let m = new Vector(mouse.x-oldMouse.x,mouse.y-oldMouse.y);
engine.particleManager.selected.move(m);
oldMouse.set(mouse);
}
if (engine.obstacleManager.selected){
let m = new Vector(mouse.x-oldMouse.x,mouse.y-oldMouse.y);
engine.obstacleManager.selected.move(m);
oldMouse.set(mouse);
}
}
if(engine.repulseur){
}
}
function handleMouseUp(){
mouseIsDown = false;
if (engine.obstacleManager.selected){
engine.obstacleManager.selected.color = "#FF0000";
}
}
function repulseClick(){
console.log('pas encore implémenté');
if(engine.repulseur){
repulse.innerHTML="Répulseur : OFF";
engine.repulseur = false;
} else {
repulse.innerHTML="Répulseur : ON";
engine.repulseur = true;
}
//console.log(engine.repulseur);
}