-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnemyController.js
150 lines (133 loc) · 4.17 KB
/
EnemyController.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
import Enemy from "./Enemy.js";
import MovingDirection from "./MovingDirection.js";
export default class EnemyController {
enemyMap = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[2, 2, 2, 3, 3, 3, 3, 2, 2, 2],
[2, 2, 2, 3, 3, 3, 3, 2, 2, 2],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
];
enemyRows = [];
currentDirection = MovingDirection.right;
xVelocity = 0;
yVelocity = 0;
defaultXVelocity = 1;
defaultYVelocity = 1;
moveDownTimerDefault = 30;
moveDownTimer = this.moveDownTimerDefault;
fireBulletTimerDefault = 100;
fireBulletTimer = this.fireBulletTimerDefault;
constructor(canvas, enemyBulletController, playerBulletController) {
this.canvas = canvas;
this.enemyBulletController = enemyBulletController;
this.playerBulletController = playerBulletController;
this.enemyDeathSound = new Audio("sounds/enemy-death.wav");
this.enemyDeathSound.volume = 0.1;
this.createEnemies();
}
draw(ctx) {
this.decrementMoveDownTimer();
this.updateVelocityAndDirection();
this.collisionDetection();
this.drawEnemies(ctx);
this.resetMoveDownTimer();
this.fireBullet();
}
collisionDetection() {
this.enemyRows.forEach((enemyRow) => {
enemyRow.forEach((enemy, enemyIndex) => {
if (this.playerBulletController.collideWith(enemy)) {
this.enemyDeathSound.currentTime = 0;
this.enemyDeathSound.play();
enemyRow.splice(enemyIndex, 1);
}
});
});
this.enemyRows = this.enemyRows.filter((enemyRow) => enemyRow.length > 0);
}
fireBullet() {
this.fireBulletTimer--;
if (this.fireBulletTimer <= 0) {
this.fireBulletTimer = this.fireBulletTimerDefault;
const allEnemies = this.enemyRows.flat();
const enemyIndex = Math.floor(Math.random() * allEnemies.length);
const enemy = allEnemies[enemyIndex];
this.enemyBulletController.shoot(enemy.x + enemy.width / 2, enemy.y, -3);
}
}
resetMoveDownTimer() {
if (this.moveDownTimer <= 0) {
this.moveDownTimer = this.moveDownTimerDefault;
}
}
decrementMoveDownTimer() {
if (
this.currentDirection === MovingDirection.downLeft ||
this.currentDirection === MovingDirection.downRight
) {
this.moveDownTimer--;
}
}
updateVelocityAndDirection() {
for (const enemyRow of this.enemyRows) {
if (this.currentDirection == MovingDirection.right) {
this.xVelocity = this.defaultXVelocity;
this.yVelocity = 0;
const rightMostEnemy = enemyRow[enemyRow.length - 1];
if (rightMostEnemy.x + rightMostEnemy.width >= this.canvas.width) {
this.currentDirection = MovingDirection.downLeft;
break;
}
} else if (this.currentDirection === MovingDirection.downLeft) {
if (this.moveDown(MovingDirection.left)) {
break;
}
} else if (this.currentDirection === MovingDirection.left) {
this.xVelocity = -this.defaultXVelocity;
this.yVelocity = 0;
const leftMostEnemy = enemyRow[0];
if (leftMostEnemy.x <= 0) {
this.currentDirection = MovingDirection.downRight;
break;
}
} else if (this.currentDirection === MovingDirection.downRight) {
if (this.moveDown(MovingDirection.right)) {
break;
}
}
}
}
moveDown(newDirection) {
this.xVelocity = 0;
this.yVelocity = this.defaultYVelocity;
if (this.moveDownTimer <= 0) {
this.currentDirection = newDirection;
return true;
}
return false;
}
drawEnemies(ctx) {
this.enemyRows.flat().forEach((enemy) => {
enemy.move(this.xVelocity, this.yVelocity);
enemy.draw(ctx);
});
}
happy = () => {};
createEnemies() {
this.enemyMap.forEach((row, rowIndex) => {
this.enemyRows[rowIndex] = [];
row.forEach((enemyNubmer, enemyIndex) => {
if (enemyNubmer > 0) {
this.enemyRows[rowIndex].push(
new Enemy(enemyIndex * 50, rowIndex * 35, enemyNubmer)
);
}
});
});
}
collideWith(sprite) {
return this.enemyRows.flat().some((enemy) => enemy.collideWith(sprite));
}
}