generated from hi337/Canvas-Game-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
components.js
391 lines (381 loc) · 11.3 KB
/
components.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
//components declaration
var mainCharacter;
var borderTop;
var borderBottom;
var borderLeft;
var myScore;
var topScore;
var borderRight;
let border_bullet_arr = []; //array to store all the bullets shot from border
let mainChar_bullet_arr = []; //array to store all the bullets shot from mainChar
let interval = 70; //initial number of miliseconds to wait for next shot, gets incrementaly smaller
let time_before_next_shot = 0; //counts until interval, hits a shot, goes back to zero
//health variable
let health = 3; //health
let chosen_border = ""; //variable denoting the next border to shoot from
let mainCharx = 315; //mainCharx, global for bullet positioning
let mainChary = 200; //mainChary, global for bullet positioning
let border_arr = ["top", "bottom", "left", "right"]; //array of border components
let particles = []; //array for the firework effect after bullet-bullet collision
//function causes one of the borders to shoot a bullet from a random spot on the side facing inwards and at a random degree. They start of slowly shooting, but the interval shrinks to a certain number, until it shoots 4-5 per minuite.
function choose_shooting_border() {
if (interval > 20) {
interval -= 1;
}
chosen_border = border_arr[Math.floor(Math.random() * border_arr.length)];
}
//function for the firework particles
//mainChar bullet component
function mainChar_bullet_comp(x, y, name, angle, speedX, speedY) {
this.speedX = speedX;
this.speedY = speedY;
this.angle = angle;
this.name = name;
this.x = x;
this.y = y;
this.width = 12;
this.height = 24;
this.image = new Image();
this.image.src = "./img/mainCharBullet.png";
this.update = function () {
ctx = myGameArea.context;
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(this.angle);
ctx.drawImage(
this.image,
this.width / -2,
this.height / -2,
this.width,
this.height
);
ctx.restore();
};
//changes the position of the component
this.newPos = function () {
this.x += this.speedX;
this.y += this.speedY;
};
//collision detection
this.crashWith = function (otherobj) {
var myleft = this.x;
var myright = this.x + this.width;
var mytop = this.y;
var mybottom = this.y + this.height;
var otherleft = otherobj.x;
var otherright = otherobj.x + otherobj.width;
var othertop = otherobj.y;
var otherbottom = otherobj.y + otherobj.height;
var crash = true;
if (
mybottom < othertop ||
mytop > otherbottom ||
myright < otherleft ||
myleft > otherright
) {
crash = false;
}
return crash;
};
}
//border shooting bullet component
function bullet_comp(x, y, name, angle, speedX, speedY) {
this.speedX = speedX;
this.speedY = speedY;
this.angle = angle;
this.name = name;
this.x = x;
this.y = y;
this.width = 10;
this.height = 10;
this.image = new Image();
this.image.src = "./img/fireball.png";
this.update = function () {
ctx = myGameArea.context;
ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
};
//changes the position of the component
this.newPos = function () {
this.x += this.speedX;
this.y += this.speedY;
};
//collision detection
this.crashWith = function (otherobj) {
var myleft = this.x;
var myright = this.x + this.width;
var mytop = this.y;
var mybottom = this.y + this.height;
var otherleft = otherobj.x;
var otherright = otherobj.x + otherobj.width;
var othertop = otherobj.y;
var otherbottom = otherobj.y + otherobj.height;
var crash = true;
if (
mybottom < othertop ||
mytop > otherbottom ||
myright < otherleft ||
myleft > otherright
) {
crash = false;
}
return crash;
};
}
//border component, which will be shooting out the flames
function border_comp(width, height, x, y, name) {
this.name = name;
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.image = new Image();
if (name == "bottom" || name == "top") {
this.image.src = "./img/border_bottom.png";
} else if (name == "left") {
this.image.src = "./img/border_left.png";
} else {
this.image.src = "./img/border_right.png";
}
this.update = function () {
ctx = myGameArea.context;
ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
};
//shooting bullets from the border
this.shoot = function () {
let nom = makeid(5);
if (this.name == "top") {
let random_int = getRandomInt(30, 670);
let ang = Math.atan(
Math.abs(mainCharx - random_int) / Math.abs(mainChary - 20)
);
let slope = (mainChary - 20) / (mainCharx - random_int);
if (mainCharx < random_int) slope = -slope;
border_bullet_arr.push(
new bullet_comp(random_int, 20, nom, ang, slope, 1)
);
} else if (this.name == "bottom") {
let random_int = getRandomInt(20, 680);
let ang = Math.atan(
Math.abs(mainCharx - random_int) / Math.abs(mainChary - 360)
);
let slope = Math.abs(mainChary - 360) / Math.abs(mainCharx - random_int);
if (mainCharx < random_int) slope = -slope;
border_bullet_arr.push(
new bullet_comp(random_int, 360, nom, ang, slope, -1)
);
} else if (this.name == "left") {
let random_int = getRandomInt(20, 370);
let ang = Math.atan(
Math.abs(mainCharx - 20) / Math.abs(mainChary - random_int)
);
let slope = Math.abs(mainChary - random_int) / Math.abs(mainCharx - 20);
if (mainChary < random_int) slope = -slope;
border_bullet_arr.push(
new bullet_comp(20, random_int, nom, ang, 1, slope)
);
} else if (this.name == "right") {
let random_int = getRandomInt(20, 370);
let ang = Math.atan(
Math.abs(mainCharx - 660) / Math.abs(mainChary - random_int)
);
let slope = Math.abs(mainChary - random_int) / Math.abs(mainCharx - 660);
if (mainChary < random_int) slope = -slope;
border_bullet_arr.push(
new bullet_comp(660, random_int, nom, ang, -1, slope)
);
}
};
}
//Text component for the score and initial screen
function text_comp(size, font, color, x, y) {
this.x = x;
this.y = y;
this.update = function () {
ctx.font = size + " " + font;
ctx.fillStyle = color;
ctx.fillText(this.text, this.x, this.y);
};
}
//default component for main characters and borders
function component(width, height, x, y) {
this.width = width;
this.height = height;
this.hitBoxWidth = 30;
this.hitBoxHeight = 40;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.centerX = this.x + 15;
this.centerY = this.y + 11;
this.image = north;
this.facing = "N";
this.update_direction_facing = function () {
if (this.speedX > 0) {
if (this.speedY == 0) {
this.image = east;
this.facing = "E";
this.hitBoxWidth = 40;
this.hitBoxHeight = 30;
} else if (this.speedY < 0) {
this.image = northeast;
this.facing = "NE";
} else if (this.speedY > 0) {
this.image = southeast;
this.facing = "SE";
}
} else if (this.speedX < 0) {
if (this.speedY == 0) {
this.image = west;
this.facing = "W";
this.hitBoxWidth = 40;
this.hitBoxHeight = 30;
} else if (this.speedY < 0) {
this.image = northwest;
this.facing = "NW";
} else if (this.speedY > 0) {
this.image = southwest;
this.facing = "SW";
}
} else if (this.speedX == 0) {
if (this.speedY < 0) {
this.image = north;
this.facing = "N";
this.hitBoxWidth = 30;
this.hitBoxHeight = 40;
} else if (this.speedY > 0) {
this.image = south;
this.facing = "S";
this.hitBoxWidth = 30;
this.hitBoxHeight = 40;
}
}
};
this.update = function () {
let ctx = myGameArea.context;
ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
};
//changes the position of the component
this.newPos = function () {
this.x += this.speedX;
this.y += this.speedY;
mainCharx = this.x;
mainChary = this.y;
this.centerX = this.x + 15;
this.centerY = this.y + 11;
};
//shooting mechanism
this.shoot = function () {
let nom = makeid(5);
if (this.facing == "N") {
mainChar_bullet_arr.push(
new mainChar_bullet_comp(this.x + 30, this.y + 10, nom, 0, 0, -5.5)
);
} else if (this.facing == "NE") {
mainChar_bullet_arr.push(
new mainChar_bullet_comp(
this.x + 47,
this.y + 10,
nom,
0.785398,
5.5,
-5.5
)
);
} else if (this.facing == "E") {
mainChar_bullet_arr.push(
new mainChar_bullet_comp(this.x + 30, this.y + 30, nom, 1.5708, 5.5, 0)
);
} else if (this.facing == "SE") {
mainChar_bullet_arr.push(
new mainChar_bullet_comp(
this.x + 35,
this.y + 35,
nom,
2.35619,
5.5,
5.5
)
);
} else if (this.facing == "S") {
mainChar_bullet_arr.push(
new mainChar_bullet_comp(this.x + 30, this.y + 40, nom, 3.14159, 0, 5.5)
);
} else if (this.facing == "SW") {
mainChar_bullet_arr.push(
new mainChar_bullet_comp(
this.x + 20,
this.y + 40,
nom,
3.92699,
-5.5,
5.5
)
);
} else if (this.facing == "W") {
mainChar_bullet_arr.push(
new mainChar_bullet_comp(
this.x + 15,
this.y + 30,
nom,
4.71239,
-5.5,
0
)
);
} else if (this.facing == "NW") {
mainChar_bullet_arr.push(
new mainChar_bullet_comp(this.x, this.y, nom, 5.49779, -5.5, -5.5)
);
}
shot_sound.play();
};
//collision detection
this.crashWith = function (otherobj) {
let myleft = this.centerX;
let myright = this.centerX + this.hitBoxWidth;
let mytop = this.centerY;
let mybottom = this.centerY + this.hitBoxHeight;
let otherleft = otherobj.x;
let otherright = otherobj.x + otherobj.width;
let othertop = otherobj.y;
let otherbottom = otherobj.y + otherobj.height;
let crash = true;
if (
mybottom <= othertop ||
mytop >= otherbottom ||
myright <= otherleft ||
myleft >= otherright
) {
crash = false;
}
return crash;
};
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function makeid(length) {
let result = "";
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charactersLength = characters.length;
let counter = 0;
while (counter < length) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
return result;
}
/**
* @todo remove all depreciated libraries like requestAnimaitonFrame()
* @todo make the shaking effect on nuke and not on hit
* @todo ask gpt how to improve code
* @todo make maincharacter rotate based on the direction he is facing.
* @todo change maincharacter's width and height and replace with an image of a spaceship.
* @todo add textures to the bullet, the background, and the borders.
* @todo add start screen
* @todo fix the shooting mechanism, detect hits with the border_bullets, get new texture.
*/