-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemies.js
More file actions
365 lines (327 loc) ยท 12.6 KB
/
enemies.js
File metadata and controls
365 lines (327 loc) ยท 12.6 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
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
/*
######################################
## JS Canvas Game - Quadrant Patrol ##
###################################### */
const enemyColors = ['blue', 'green', 'orange', 'cyan', 'red'];
const enemyImage = new Image();
enemyImage.src = './images/ships_enemies.png';
const powerUpsImage = new Image();
powerUpsImage.src = './images/power_ups.png';
const specialExplosionImage = new Image();
specialExplosionImage.src = './images/explosion_special.png';
const enemyExplosionImage = new Image();
enemyExplosionImage.src = './images/explosion_enemies.png';
const razorImage = new Image();
razorImage.src = './images/bouncing_razor.png';
const projectileExplosionImage = new Image();
projectileExplosionImage.src = './images/explosion_projectiles.png'
class Enemy {
constructor(game, x, y) {
this.game = game;
this.x = x;
this.y = y;
this.moveDir = Math.random() < 0.5 ? -1 : 1;
this.width = 60;
this.height = 36;
this.widthXT = 16;
this.heightXT = 20;
this.frame = 0;
this.frameStep = 7;
this.animationFrame = this.moveDir < 0 ? 9 : 1;
this.color = Math.floor(Math.random() * 5);
this.health = 500;
this.lastEnemyShotTimestamp = Date.now();
this.enemyShotInterval = 1000;
}
runLogic() {
this.y += 0.5;
this.x += 2 * this.moveDir;
if (this.x <= 0) {
this.moveDir *= -1;
this.frame = 0;
}
if (this.x >= this.game.canvas.width - this.width) {
this.moveDir *= -1;
this.frame = 0;
}
// ### Check shot interval on enemy ###
if ((Date.now() - this.lastEnemyShotTimestamp) > this.enemyShotInterval) {
this.lastEnemyShotTimestamp = Date.now();
this.shoot();
}
this.frame++;
this.animation();
}
animation() {
// ### Enemy rolls left/right during direction change ###
if (this.frame > this.frameStep) {
if (this.x > this.game.canvas.width - 120 && this.animationFrame < 9) {
this.animationFrame++;
this.frame = 0;
} else if (this.x < 60 && this.animationFrame > 1) {
this.animationFrame--;
this.frame = 0;
}
}
}
shoot() {
const enemyShot = new EnemyProjectile(this.game, this.x + this.width / 2 - 3, this.y + 32);
this.game.enemyProjectiles.push(enemyShot);
}
checkIntersection(element) {
return (
// turns true if right side of element is beyond left side of enemy base shape
element.x + element.width >= this.x &&
// turns true if left side of element is beyond right side of enemy base shape
element.x <= this.x + this.width &&
// turns true if bottom side of element is beyond top side of enemy base shape
element.y + element.height >= this.y &&
// turns true if top side of element is above bottom edge of enemy base shape
element.y <= this.y + this.height
) || (
// turns true if right side of element is beyond left side of enemy cockpit
element.x + element.width >= this.x + 22 &&
// turns true if left side of element is beyond right side of enemy cockpit
element.x <= this.x + 22 + this.widthXT &&
// turns true if bottom side of element is beyond top side of enemy cockpit
element.y + element.height >= this.y + 36 &&
// turns true if top side of element is above bottom edge of enemy cockpit
element.y <= this.y + 36 + this.heightXT
);
}
drawEnemy() {
// this.game.context.fillStyle = 'beige';
// this.game.context.fillRect(this.x, this.y, this.width, this.height);
// this.game.context.fillRect(this.x + 22, this.y + 36, this.widthXT, this.heightXT);
this.game.context.drawImage(enemyImage, 343 * this.animationFrame - 343, this.color * 383, 343, 383, Math.floor(this.x - 2), Math.floor(this.y), 64, 64);
}
}
const rockImage_light = new Image();
const rockImage_dark = new Image();
rockImage_light.src = './images/rocks_light.png';
rockImage_dark.src = './images/rocks_dark.png';
class Rock extends Enemy {
constructor(game, x, y) {
super(game, x, y);
this.color = '#1C2833'
this.width = 50;
this.height = 50;
this.health = 150;
this.frame = 0;
this.animations = 31;
this.animationFrame = 1;
// spawn lighter or darker rocks
this.rockBrightness = Math.random() < 0.5;
// random rotation left/right (clip different images)
this.rotationDirection = Math.floor(Math.random() * 2);
// random rotation speed (between 1.5 and 3.5)
this.animationFrameSteps = Number(((Math.random() * 2) + 1.5).toPrecision(3));
// random y velocity between 0.5 and 2
this.vy = Math.floor((Math.random() + 2) * 100) / 100;
// random destination (bottom) x between 0 and canvas.width
this.destination = Math.floor(Math.random() * (this.game.canvas.width - this.width));
// calculate amount of updates to reach bottom regarding random vy
this.travelUpdatesY = Math.floor(this.game.canvas.height / this.vy);
// calculate amount of needed x/update to reach destination
this.vx = Math.floor(((this.destination - this.x) / this.travelUpdatesY) * 100) / 100;
}
drawRock() {
// ### Clip light image left/right // dark image top/bottom ###
if (this.rockBrightness === true) {
// ### Light rock animation logic
if ((this.frame > this.animationFrameSteps)) {
this.animationFrame++;
this.frame = 0;
}
if (this.animationFrame > this.animations) {
this.animationFrame = 1;
}
this.game.context.drawImage(rockImage_light, (128 * this.animationFrame - 128) - ((Math.ceil(this.animationFrame / 8)) * 1024 - 1024), (0 + Math.floor((this.animationFrame - 1) / 8) * 128) + this.rotationDirection * 512, 128, 128, Math.floor(this.x - 15), Math.floor(this.y - 15), 80, 80);
}
else {
// ### Dark rock animation logic
if ((this.frame > this.animationFrameSteps)) {
this.animationFrame++;
this.frame = 0;
}
if (this.animationFrame > this.animations) {
this.animationFrame = 1;
}
this.game.context.drawImage(rockImage_dark, (384 - Math.floor((this.animationFrame - 1) / 8) * 128) + this.rotationDirection * 512, (this.animationFrame * 128 - 128) - ((Math.ceil(this.animationFrame / 8)) * 1024 - 1024), 128, 128, Math.floor(this.x - 15), Math.floor(this.y) - 15, 80, 80);
}
}
runLogic() {
this.x += this.vx;
this.y += this.vy;
}
}
class PowerUp {
constructor(game, x, y, direction, bonus) {
this.game = game;
this.direction = direction;
this.bonus = bonus;
this.x = x;
this.y = y;
this.vx = 2.25 * this.direction;
this.vy = -3;
this.width = 50;
this.height = 50;
this.frame = 0;
this.frameStep = 2;
this.animation = 1;
this.gravity = 0.15;
}
drawPowerUp() {
// this.game.context.fillStyle = `${this.color}`;
// this.game.context.fillRect(this.x, this.y, this.width, this.height);
this.game.context.drawImage(powerUpsImage, this.animation * 52 - 52, this.colorRow * 52 - 52, 52, 52, Math.floor(this.x), Math.floor(this.y), 50, 50);
// if (this.bounced === true && (Date.now() - this.timeSpawned < 9000)) {
// this.game.context.globalCompositeOperation = 'source-over'
// this.game.context.fillStyle = 'white';
// this.game.context.font = '24px Arial';
// this.game.context.fillText(`${10 - ((Date.now() - this.timeSpawned) / 1000).toFixed()}`, this.x + 8, this.y + this.height - 7);
// }
}
drawRazor() {
// ### Bouncing razor animation ###
if (this.razorFrame < 63) {
this.razorFrame++;
} else {
this.razorFrame = 0;
}
this.game.context.drawImage(razorImage, (128 * this.razorFrame - 128) - ((Math.ceil(this.razorFrame / 8)) * 1024 - 1024), (0 + Math.floor((this.razorFrame - 1) / 8) * 128), 128, 128, Math.floor(this.x), Math.floor(this.y), 50, 50);
}
runLogic() {
this.x += this.vx;
this.vy += this.gravity;
this.y += this.vy;
this.frame++;
this.rotatePowerUp();
}
rotatePowerUp() {
// ### Power-up animation ###
if (this.frame > this.frameStep - 1) {
this.frame = 0;
if (this.animation < 20) {
this.animation++;
} else {
this.animation = 1;
}
}
}
checkIntersection(element) {
return (
// turns true if right side of element is beyond left side of powerup
element.x + element.width >= this.x &&
// turns true if left side of element is beyond right side of powerup
element.x <= this.x + this.width &&
// turns true if bottom side of element is beyond top side of powerup
element.y + element.height >= this.y &&
// turns true if top side of element is above bottom edge of powerup
element.y <= this.y + this.height
);
}
}
class HealthUp extends PowerUp {
constructor(game, x, y, direction, bonus, colorRow) {
super(game, x, y, direction, bonus, colorRow)
this.colorRow = colorRow;
}
}
class ShieldUp extends PowerUp {
constructor(game, x, y, direction, bonus, colorRow) {
super(game, x, y, direction, bonus, colorRow)
this.colorRow = colorRow;
}
}
class WingsUp extends PowerUp {
constructor(game, x, y, direction, bonus, colorRow) {
super(game, x, y, direction, bonus, colorRow)
this.colorRow = colorRow;
}
}
class BounceUp extends PowerUp {
constructor(game, x, y, direction, bonus, colorRow) {
super(game, x, y, direction, bonus, colorRow)
this.colorRow = colorRow;
this.bounced = false;
this.timeSpawned = Date.now();
this.razorFrame = 0;
}
runLogic() {
if (this.bounced === false) {
this.x += (2.25 * this.direction);
this.vy += this.gravity;
this.y += this.vy;
}
if (this.bounced === true) {
this.x += this.vx * 1;
this.y -= this.vy * 0.4;
if (this.y < 60 && Date.now() - this.timeSpawned < 15000) {
this.vy *= -1;
}
if (this.x < 0 || this.x + this.width > this.game.canvas.width) {
this.vx *= -1;
}
if (game.player.checkIntersectionShield(this)) {
this.vy *= -1;
}
this.game.enemies.forEach((enemy, index) => {
if (this.checkIntersection(enemy)) {
// Spawn razor/enemy explosion
const enemyBoom = new Explosion(this, enemy.x, enemy.y, enemy.width);
this.game.razorExplosions.push(enemyBoom);
this.game.enemies.splice(index, 1);
this.game.score += 100;
}
});
this.game.rocks.forEach((rock, index) => {
if (this.checkIntersection(rock)) {
// Spawn razor/rock explosion
const rockBoom = new Explosion(this, rock.x, rock.y, rock.width);
this.game.razorExplosions.push(rockBoom);
this.game.rocks.splice(index, 1);
this.game.score += 50;
}
});
if (this.x + this.width < 0 || this.x > this.game.canvas.width || this.y + this.height < 60) {
this.game.powerUpSpawned = false;
}
}
this.frame++;
this.rotatePowerUp();
}
}
class ScoreUp extends PowerUp {
constructor(game, x, y, direction, bonus, colorRow) {
super(game, x, y, direction, bonus, colorRow)
this.colorRow = colorRow;
}
}
class Explosion {
constructor(game, x, y, width) {
this.game = game;
this.x = x;
this.y = y;
this.animationFrame = 1;
this.xOffset = width / 2;
this.explosionType = Math.floor(Math.random() * 4);
}
runLogic() {
if (this.animationFrame < 64) {
this.animationFrame++;
}
}
drawPlayerExplosion() {
this.game.context.drawImage(specialExplosionImage, (256 * this.animationFrame - 256) - ((Math.ceil(this.animationFrame / 8)) * 2048 - 2048), (0 + Math.floor((this.animationFrame - 1) / 8) * 256), 256, 256, Math.floor(this.x - 200 + this.xOffset), Math.floor(this.y - 200 + this.xOffset), 400, 400)
}
drawEnemyExplosion() {
this.game.context.drawImage(enemyExplosionImage, (256 * this.animationFrame - 256) - ((Math.ceil(this.animationFrame / 8)) * 2048 - 2048), (0 + Math.floor((this.animationFrame - 1) / 8) * 256) + this.explosionType * 2048, 256, 256, Math.floor(this.x - 170 + this.xOffset), Math.floor(this.y - 170 + this.xOffset), 340, 340)
}
drawRazorExplosion() {
game.context.drawImage(specialExplosionImage, (256 * this.animationFrame - 256) - ((Math.ceil(this.animationFrame / 8)) * 2048 - 2048), (0 + Math.floor((this.animationFrame - 1) / 8) * 256) + 2048, 256, 256, Math.floor(this.x - 175 + this.xOffset), Math.floor(this.y - 175 + this.xOffset), 300, 300)
}
drawPlayerProjectileHits() {
this.game.context.drawImage(projectileExplosionImage, (96 * this.animationFrame - 96) - ((Math.ceil(this.animationFrame / 8)) * 768 - 768), (0 + Math.floor((this.animationFrame - 1) / 8) * 96) + this.explosionType * 768, 96, 96, Math.floor(this.x - 48 + this.xOffset), Math.floor(this.y - 48 + this.xOffset), 96, 96)
}
}