-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenemypool.js
539 lines (500 loc) · 17.7 KB
/
enemypool.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
import { HeroDeathLeft, HeroDeathRight, HeroTakeHitWhiteLeft, HeroTakeHitWhiteRight } from "./hero.js";
import { Character } from "./character.js";
import { FloatingMessage } from "./floatingMessage.js"
export class Enemy extends Character{
constructor(game){
super(game);
this.game = game;
this.image = this.game.assetManager.images[10];
this.damage = 1;
this.lives = 50;
this.maxLives = this.lives;
this.currentState = "idleLeft";
this.lastDirection = "left";
this.frameX = 0;
this.frameY = 9;
this.frames = 4;
this.speedX = 0;
this.speedBufferX = 80;
this.hitCount = 0;
this.free = true;
this.state = null;
//attack properties
this.attackCounter = 0;
this.attackInterval = 3000;
this.attackUpdate = false;
this.enterState(new EnemyIdleRight());
}
updatehitboxes(){
this.collisionbox = {
x: this.x+135,
y: this.y+118,
width: this.width-170,
height: this.height-130
}
//adjust attack box for proper collision detection
if(this.lastDirection=="right"){
this.attackbox = {
x: this.collisionbox.x+this.collisionbox.width,
y: this.collisionbox.y,
width: this.collisionbox.width+50,
height: this.height-180
}
}
else{
this.attackbox = {
x: this.collisionbox.x-this.collisionbox.width-50,
y: this.collisionbox.y,
width: this.collisionbox.width+50,
height: this.game.player.attackbox.height
}
}
}
render(ctx){
if( (!this.free) ){
super.render(ctx);
};
}
animationExecuted(){
return this.frameX === this.frames - 1;
}
update(deltaTime){
if( (!this.free)) {
super.update(deltaTime);
//enemy changes behaviour
this.state.changeBehaviour(this.game, this, deltaTime);
//new code for player's behavioural change called here
//changes state
this.game.heroStateMachine.currentState.changeBehaviour(this, deltaTime);
}
//collision
//enemy and hero
//while player is attacking and truly within attack range
if(!this.free && this.game.rectangularCollision(this.game.player.attackbox, this.collisionbox) &&
( this.game.player.state == "attack1Right" ||
this.game.player.state == "attack2Right" ||
this.game.player.state == "attack1Left" ||
this.game.player.state == "attack2Left"
) ){
//play takeHit animation when player is idle
if(this.currentState == "idleRight"){
this.enterState(new EnemyTakeHitRight())
}
else if(this.currentState == "idleLeft"){
this.enterState(new EnemyTakeHitLeft())
}
//enemy takes hit
if(this.game.input.keypressed){
this.takenHit();
this.game.input.keypressed = false;
}
//enemy dies
if(this.lives < 1){
//play player-collects-coins audio
this.game.audio.play(this.game.audio.coinSound);
this.game.player.score+= this.maxLives;
//push a floating score to the scoreboard
const rightMargin = 30;
const topMargin = 50;
this.game.floatingMessages.push(new FloatingMessage(this.game,
"+"+this.maxLives,
this.collisionbox.x,
this.collisionbox.y,
this.game.width+this.game.camera.viewportX-rightMargin,
this.game.camera.viewportY+ topMargin
))
this.free = true;
}
}
}
start(x, y){
const offsetY = (this.y+this.height) - (this.collisionbox.y+this.collisionbox.height)
const offsetX = this.collisionbox.x - this.x;
this.x = x;
this.y = y;
this.lives = this.maxLives;
this.state = new EnemyIdleRight();
this.free = false;
}
reset(){
this.x = this.y = 0;
this.free = true;
}
takenHit() {
this.lives -= this.game.player.damage;
//play takenHit audio
//this.game.audio.play(this.game.audio.winSong);
}
enterState(state){
this.state = state;
this.state.enter(this);
}
updateAttack(deltaTime){
if(this.attackCounter < this.attackInterval){
this.attackCounter+= deltaTime;
this.attackUpdate = false;
}
else{
this.attackCounter = 0;
this.attackUpdate = true;
}
}
}
class EnemyState{
constructor(){
}
animationExecuted(enemy){
return enemy.frameX === enemy.frames - 1;
}
}
class EnemyIdleRight extends EnemyState{
enter(enemy){
enemy.frameX = 0;
enemy.currentState = "idleRight";
enemy.lastDirection = "right";
enemy.frameY = 8;
enemy.frames = 4;
enemy.speedX = 0;
enemy.speedBufferX = 80;
enemy.staggerSpeed = 80;
}
changeBehaviour(game, enemy, deltaTime){
enemy.updateAttack(deltaTime);
const player = game.player;
//when in attack range and the sword actually collides with player
if(enemy.attackbox.x + enemy.attackbox.width >= player.collisionbox.x &&
enemy.attackbox.x <= player.collisionbox.x + player.collisionbox.width &&
enemy.withinAttackRangeY(player)){
//enemy has the following options:
//(a) attack every x seconds if enemy is alive
if(!player.alive) return;
if(enemy.attackUpdate){
const randomNumber = Math.random();
if(randomNumber < 0.5){
enemy.enterState(new EnemyAttack1Right());
}
else{
enemy.enterState(new EnemyAttack2Right());
}
}
}
//when player tries to run away, rightwards, enemy chases
if(enemy.withinBattleZone(player) &&
enemy.attackbox.x + enemy.attackbox.width < player.collisionbox.x){
enemy.enterState(new EnemyRunRight());
}
//when player tries running to the other side
else if(enemy.withinBattleZone(player) &&
enemy.attackbox.x > player.collisionbox.x + player.collisionbox.width){
enemy.enterState(new EnemyIdleLeft());
}
}
}
class EnemyIdleLeft extends EnemyState{
enter(enemy){
enemy.frameX = 0;
enemy.currentState = "idleLeft";
enemy.lastDirection = "left";
enemy.frameY = 9;
enemy.frames = 4;
enemy.speedX = 0;
enemy.speedBufferX = 80;
enemy.staggerSpeed = 80;
}
changeBehaviour(game, enemy, deltaTime){
enemy.updateAttack(deltaTime);
const player = game.player;
if(enemy.attackbox.x <= player.collisionbox.x + player.collisionbox.width &&
enemy.attackbox.x + enemy.attackbox.width >= player.collisionbox.x &&
game.rectangularCollision(player.collisionbox, enemy.attackbox)
){
if(!player.alive) return;
if(enemy.attackUpdate){
if(Math.random() < 0.5){
enemy.enterState(new EnemyAttack1Left());
}
else{
enemy.enterState(new EnemyAttack2Left());
}
}
}
//when player tries to run out of battle zone
if(enemy.withinBattleZone(player) &&
enemy.attackbox.x > player.collisionbox.x + player.collisionbox.width){
enemy.enterState(new EnemyRunLeft(this.game));
}
//when player tries running to the other side
else if(enemy.attackbox.x + enemy.attackbox.width < player.collisionbox.x &&
enemy.withinBattleZone(player)){
enemy.enterState(new EnemyIdleRight());
}
}
}
export class EnemyRunRight extends EnemyState{
enter(enemy){
//this.game.enemy.frameX = 0;
enemy.currentState = "runRight";
enemy.lastDirection = "right";
enemy.frameY = 12;
enemy.frames = 8;
enemy.speedX = 1;
enemy.speedBufferX = 80;
enemy.staggerSpeed = 80;
}
changeBehaviour(game, enemy, deltaTime){
const player = game.player;
//idle when in attack range
if(enemy.attackbox.x + enemy.attackbox.width >= player.collisionbox.x &&
enemy.attackbox.x < player.collisionbox.x + player.collisionbox.width){
enemy.enterState(new EnemyIdleRight());
}
//code for when in run right and player suddenly runs away from battlezone, rightwards
if(!enemy.withinBattleZone(player) &&
enemy.attackbox.x + enemy.attackbox.width < player.collisionbox.x){
enemy.enterState(new EnemyIdleRight());
}
}
}
export class EnemyRunLeft extends EnemyState{
enter(enemy){
//enemy.frameX = 0;
enemy.currentState = "runLeft";
enemy.lastDirection = "left";
enemy.frameY = 13;
enemy.frames = 8;
enemy.speedX = -1;
enemy.speedBufferX = 80;
enemy.staggerSpeed = 80;
}
changeBehaviour(game, enemy, deltaTime){
const player = game.player;
//while running and player suddenly runs away from battlezone, leftwards
if(!enemy.withinBattleZone(player) &&
enemy.attackbox.x > player.collisionbox.x + player.collisionbox.width){
enemy.enterState(new EnemyIdleLeft(this.game));
}
//when it reaches attack range
if(enemy.attackbox.x <= player.collisionbox.x + player.collisionbox.width &&
enemy.attackbox.x + enemy.attackbox.width >= player.collisionbox.x){
enemy.enterState(new EnemyIdleLeft(this.game));
}
}
}
export class EnemyAttack1Right extends EnemyState{
enter(enemy){
enemy.frameX = 0;
enemy.currentState = "attack1Right";
enemy.lastDirection = "right";
enemy.frameY = 0;
enemy.frames = 4;
enemy.speedX = 0;
enemy.speedBufferX = 80;
enemy.staggerSpeed = 80;
}
changeBehaviour(game, enemy, deltaTime){
const player = game.player;
if(player.lives > 0){
if(player.lastDirection ==="left"){
// player.lives -= enemy.damage;
game.heroStateMachine.enterState(new HeroTakeHitWhiteLeft(game));
}
else{
// player.lives -= enemy.damage;
game.heroStateMachine.enterState(new HeroTakeHitWhiteRight(game));
}
}
else{
if(player.lastDirection ==="left")
game.heroStateMachine.enterState(new HeroDeathLeft(game));
else
game.heroStateMachine.enterState(new HeroDeathRight(game));
}
if(this.animationExecuted(enemy)){
player.lives -= enemy.damage;
enemy.enterState(new EnemyIdleLeft());
}
}
}
export class EnemyAttack1Left extends EnemyState{
enter(enemy){
enemy.frameX = 0;
enemy.currentState = "attack1Left";
enemy.lastDirection = "left";
enemy.frameY = 1;
enemy.frames = 4;
enemy.speedX = 0;
enemy.speedBufferX = 80;
enemy.staggerSpeed = 80;
}
changeBehaviour(game, enemy, deltaTime){
const player = game.player;
if(player.lives > 0){
if(player.lastDirection ==="left"){
// player.lives -= enemy.damage;
game.heroStateMachine.enterState(new HeroTakeHitWhiteLeft(game));
}
else{
// player.lives -= enemy.damage;
game.heroStateMachine.enterState(new HeroTakeHitWhiteRight(game));
}
}
else{
if(player.lastDirection ==="left")
game.heroStateMachine.enterState(new HeroDeathLeft(game));
else
game.heroStateMachine.enterState(new HeroDeathRight(game));
}
if(this.animationExecuted(enemy)){
player.lives -= enemy.damage;
enemy.enterState(new EnemyIdleLeft(game))
}
}
}
export class EnemyAttack2Right extends EnemyState{
enter(enemy){
enemy.frameX = 0;
enemy.currentState = "attack2Right";
enemy.lastDirection = "right";
enemy.frameY = 2;
enemy.frames = 4;
enemy.speedX = 0;
enemy.speedBufferX = 80;
enemy.staggerSpeed = 80;
}
changeBehaviour(game, enemy, deltaTime){
const player = game.player;
if(player.lives > 0){
if(player.lastDirection ==="left"){
// player.lives -= enemy.damage;
game.heroStateMachine.enterState(new HeroTakeHitWhiteLeft(game));
}
else{
// player.lives -= enemy.damage;
game.heroStateMachine.enterState(new HeroTakeHitWhiteRight(game));
}
}
else{
if(player.lastDirection ==="left")
game.heroStateMachine.enterState(new HeroDeathLeft(game));
else
game.heroStateMachine.enterState(new HeroDeathRight(game));
}
if(this.animationExecuted(enemy)){
player.lives -= enemy.damage;
enemy.enterState(new EnemyIdleLeft(game))
}
}
}
export class EnemyAttack2Left extends EnemyState{
enter(enemy){
enemy.frameX = 0;
enemy.currentState = "attack2Left";
enemy.lastDirection = "left";
enemy.frameY = 3;
enemy.frames = 4;
enemy.speedX = 0;
enemy.speedBufferX = 80;
enemy.staggerSpeed = 80;
}
changeBehaviour(game, enemy, deltaTime){
const player = game.player;
if(player.lives > 0){
if(player.lastDirection ==="left"){
// player.lives -= enemy.damage;
game.heroStateMachine.enterState(new HeroTakeHitWhiteLeft(game));
}
else{
// player.lives -= enemy.damage;
game.heroStateMachine.enterState(new HeroTakeHitWhiteRight(game));
}
}
else{
if(player.lastDirection ==="left")
game.heroStateMachine.enterState(new HeroDeathLeft(game));
else
game.heroStateMachine.enterState(new HeroDeathRight(game));
}
if(this.animationExecuted(enemy)){
player.lives -= enemy.damage;
enemy.enterState(new EnemyIdleLeft(this.game))
}
}
}
export class EnemyDeathRight extends EnemyState {
enter(enemy) {
enemy.frameX = 0;
enemy.currentState = "deathRight";
enemy.lastDirection = "right";
enemy.frameY = 4;
enemy.frames = 5;
enemy.speedX = 0;
enemy.staggerSpeed = 160;
}
changeBehaviour(game, enemy, deltaTime) {
const player = game.player;
if (this.animationExecuted(enemy)) {
enemy.reset();
}
}
}
export class EnemyDeathLeft extends EnemyState {
enter(enemy) {
enemy.frameX = 0;
enemy.currentState = "deathLeft";
enemy.lastDirection = "left";
enemy.frameY = 5;
enemy.frames = 5;
enemy.speedX = 0;
enemy.staggerSpeed = 160;
}
changeBehaviour(game, enemy, deltaTime) {
const player = game.player;
if (this.animationExecuted(enemy)) {
enemy.reset();
}
}
}
export class EnemyTakeHitRight extends EnemyState{
enter(enemy){
enemy.frameX = 0;
enemy.currentState = "takeHitRight";
enemy.lastDirection = "right";
enemy.frameY = 14;
enemy.frames = 3;
enemy.speedX = 0;
enemy.staggerSpeed = 140;
}
changeBehaviour(game, enemy, deltaTime){
const player = game.player;
if(enemy.lives <=0){
enemy.enterState(new EnemyDeathRight());
}
else{
if(this.animationExecuted(enemy)){
enemy.enterState(new EnemyIdleRight())
}
}
}
}
export class EnemyTakeHitLeft extends EnemyState{
enter(enemy){
enemy.frameX = 0;
enemy.currentState = "takeHitLeft";
enemy.lastDirection = "left";
enemy.frameY = 15;
enemy.frames = 3;
enemy.speedX = 0;
enemy.staggerSpeed = 140;
}
changeBehaviour(game, enemy, deltaTime){
const player = game.player;
if(enemy.lives <= 0){
enemy.enterState(new EnemyDeathLeft());
}
else{
if(this.animationExecuted(enemy)){
enemy.enterState(new EnemyIdleLeft());
}
}
}
}