-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeteore.js
More file actions
232 lines (205 loc) · 5.93 KB
/
Copy pathmeteore.js
File metadata and controls
232 lines (205 loc) · 5.93 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
var AstArray = [];
var NbAst = 2;
var cunrrentNB = NbAst;
var tab = [];
var etoilenoire = new Image(110, 95);
etoilenoire.src = "deathstar.png";
var tie = new Image(50, 50);
tie.src = "tie.png";
var destroyer = new Image(100, 100);
destroyer.src = "destroyer.png";
destroyer.style.transform = 'rotate(180deg)';
// Change this number to get more meteores
createMeteore(NbAst);
function createMeteore(numberOfMeteore) {
for (var i = 0; i < numberOfMeteore; i++) {
// Create a meteore with random position and speed
var meteore = new Meteore((Math.random() * 1200),
(Math.random() * 800),
(2 * Math.random()) - 1,
(2 * Math.random()) - 1,
0); // radius, change if ou like.
// Add it to the array
AstArray[i] = meteore;
}
}
function collisionTestWithWalls(meteore) {
if (meteore.x < 0) {
meteore.x = width;
//meteore.vx *= -1;
}
if (meteore.x > width) {
meteore.x = 0;
//meteore.vx *= -1;
}
if (meteore.y < 0) {
meteore.y = height;
//meteore.vy *= -1;
}
if (meteore.y > height) {
meteore.y = 0;
//meteore.vy *= -1;
}
}
function Meteore(x, y, vx, vy, id) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.id = id;
this.rayon = 20;
if (this.id == 0) {
this.boundingBox = {
x: x,
y: y,
width: 110,
height: 95
}
}
if (this.id == 1) {
this.boundingBox = {
x: x,
y: y,
width: 80,
height: 95
}
}
if (this.id == 2) {
this.boundingBox = {
x: x,
y: y,
width: 80,
height: 95
}
}
if (this.id == 3) {
this.vx = (Math.random() * 10) - 5; // returns a random integer from -5 to 5;
this.vy = (Math.random() * 10) - 5;
this.boundingBox = {
x: x,
y: y,
width: 40,
height: 40
}
}
this.drawBoundingBox = function (ctx) {
ctx.save();
ctx.strokeStyle = 'red';
ctx.strokeRect(this.boundingBox.x, this.boundingBox.y, this.boundingBox.width, this.boundingBox.height);
ctx.restore();
}
//Fonction de dessin
this.draw = function () {
if (afficher_bounding == true) {
this.drawBoundingBox(ctx);
}
ctx.beginPath();
ctx.strokeStyle = "black";
ctx.lineWidth = 3;
for (var i = 0; i < 1; i++) {
if (this.id == 0) {
if (swmode == true) {
ctx.drawImage(etoilenoire, this.x + 10, this.y + 5, 110, 95);
} else {
ctx.beginPath();
ctx.strokeStyle = "green";
ctx.lineWidth = 3;
ctx.lineJoin = 'round';
ctx.lineTo(this.x + 40, this.y + 10);
ctx.lineTo(this.x + 10, this.y + 50);
ctx.lineTo(this.x + 20, this.y + 80);
ctx.lineTo(this.x + 40, this.y + 100);
ctx.lineTo(this.x + 100, this.y + 90);
ctx.lineTo(this.x + 120, this.y + 50)
ctx.lineTo(this.x + 90, this.y + 5);
ctx.closePath();
}
}
if (this.id == 1) {
if (swmode == true) {
ctx.save();
ctx.translate(this.boundingBox.x+87, this.boundingBox.y+30);
ctx.rotate(22.5*Math.PI);
ctx.translate(-this.boundingBox.x, -this.boundingBox.y);
ctx.drawImage(destroyer, this.x + 10, this.y + 5, 110, 95);
ctx.restore();
} else {
ctx.beginPath();
ctx.strokeStyle = "red";
ctx.lineWidth = 3;
ctx.lineJoin = 'round';
ctx.lineTo(this.x + 120, this.y + 50);
ctx.lineTo(this.x + 100, this.y + 90);
ctx.lineTo(this.x + 40, this.y + 100);
ctx.lineTo(this.x + 50, this.y + 90);
ctx.lineTo(this.x + 60, this.y + 50);
ctx.lineTo(this.x + 90, this.y + 5);
ctx.closePath();
ctx.stroke();
}
}
if (this.id == 2) {
if (swmode == true) {
ctx.save();
ctx.translate(this.boundingBox.x+87, this.boundingBox.y);
ctx.rotate(22.5*Math.PI);
ctx.translate(-this.boundingBox.x, -this.boundingBox.y);
ctx.drawImage(destroyer, this.x + 10, this.y + 5, 110, 95);
ctx.restore();
} else {
ctx.beginPath();
ctx.strokeStyle = "#0080ff";
ctx.lineWidth = 3;
ctx.lineJoin = 'round';
ctx.lineTo(this.x + 40, this.y + 10);
ctx.lineTo(this.x + 10, this.y + 50);
ctx.lineTo(this.x + 20, this.y + 80);
ctx.lineTo(this.x + 40, this.y + 100);
ctx.lineTo(this.x + 50, this.y + 90);
ctx.lineTo(this.x + 60, this.y + 50)
ctx.lineTo(this.x + 90, this.y + 5);
ctx.closePath();
ctx.stroke();
}
}
if (this.id == 3) {
if (swmode == true) {
ctx.drawImage(tie, this.x + 5, this.y + 15, 50, 50);
} else {
ctx.strokeStyle = "orange";
ctx.lineWidth = 3;
ctx.lineJoin = 'round';
ctx.beginPath();
ctx.lineTo(this.x + 40, this.y + 20);
ctx.lineTo(this.x + 50, this.y + 40);
ctx.lineTo(this.x + 40, this.y + 50);
ctx.lineTo(this.x + 20, this.y + 60);
ctx.lineTo(this.x + 10, this.y + 55);
ctx.lineTo(this.x + 30, this.y + 20);
ctx.closePath();
}
}
}
ctx.stroke();
}
this.move = function () {
this.x += this.vx;
this.y += this.vy;
if (this.id == 0) {
this.boundingBox.x = this.x + 10;
this.boundingBox.y = this.y + 5;
}
if (this.id == 1) {
this.boundingBox.x = this.x + 40;
this.boundingBox.y = this.y + 5;
}
if (this.id == 2) {
this.boundingBox.x = this.x + 10;
this.boundingBox.y = this.y + 5;
}
if (this.id == 3) {
this.boundingBox.x = this.x + 10;
this.boundingBox.y = this.y + 20;
}
};
}