-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alien.js
236 lines (193 loc) · 6.41 KB
/
Alien.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
function Alien(descr) {
this.setup(descr);
this.type = "Alien";
this.spriteType = descr.type;
this.sprite = this.sprite || g_sprites.aliens[this.spriteType];
this.width = this.sprite.width;
this.height = this.sprite.height;
let gapLeftWall = this.width * 2;
let gapTopWall = this.height * 1.5;
let gapBetween = this.width / 10;
this.column = descr.x; // its index in the _aliens_x_position grid in entityManager
this.row = descr.y;
this.originalY = gapTopWall + descr.y * (gapBetween + this.height);
if(descr.isRespawning == false)
this.cy = this.originalY;
else
this.cy = 10;
this.velYStandard = 0.5;
this.velY = 0.5;
this.acceleration = 0;
this.maxAcceleration = 3;
this.sidePush = 2;
this.velX = 0.1;
this.accelerationY = 0;
this.dir = 1;
this.animationInterval = 0.25 * SECS_TO_NOMINALS;
this.animationTimer = 0;
this.isExploding = false;
this.isAttacking = false;
this.isRotated = false;
this.animationFreeze = false;
this.rotation = 0;
this.loopCount = 0;
};
Alien.prototype = new Entity();
Alien.prototype.explosionAlien = new Audio("sounds/explosionAlien.wav");
Alien.prototype.explosionAlien.volume = 0.2;
Alien.prototype.attackingAlien = new Audio("sounds/attackingAlien.wav");
Alien.prototype.attackingAlien.volume = 0.2;
Alien.prototype.update = function (du) {
spatialManager.unregister(this);
if (this._isDeadNow) return entityManager.KILL_ME_NOW;
if(this.animationFreeze == false)
this.animationTimer += du;
if (this.isExploding) {
if (this.sprite.frame === this.sprite.numFrames-1) {
entityManager.setAlienGrid(this.column, this.row, 0); // remove enemy from grid
return entityManager.KILL_ME_NOW;
}
return;
}
let nextY = this.cy;
// Updating x position
if(this.isAttacking == false)
this.cx = entityManager.getAlienPosition(this.column);
// Updating y position
if(this.cy < this.originalY){
nextY = this.cy + (this.velY * du);
}
// Check if this enemy should start attack round
if(this.isExploding == false)
this.maybeAttack();
if (this.isAttacking) {
this.attackingAlien.play();
// Animation, if the attack animation has gone through all frames ONCE
// the sprite should be rotated by 90 degrees, and then played again ONCE
if (this.sprite.frame === 0 && this.loopCount == 1){
this.loopCount = 2;
this.rotation = 90 * Math.PI / 180;
this.isRotated = true;
}
if (this.sprite.frame === this.sprite.numFrames-1){
if(this.loopCount == 0)
this.loopCount = 1;
if(this.loopCount == 2){
this.animationFreeze = true;
}
// rotate by 90 degrees
}
this.maybeFireBullet();
this.velY = 1.5;
this.accelerationY += 0.1;
if(this.accelerationY > 1.5)
this.accelerationY = 1.5;
nextY = this.cy + (this.accelerationY * du);
// Move enemy to the side, with friction
// The alien is not allowed to change direction from the start when driving upwards
if(this.accelerationY == 1.5){
this.dir = 1
if(this.cx > entityManager.getShipCoords().x)
this.dir = -1;
}
this.acceleration += this.velX * this.dir;
if(this.acceleration > this.maxAcceleration)
this.acceleration = this.maxAcceleration;
if(this.acceleration < - this.maxAcceleration)
this.acceleration = - this.maxAcceleration;
this.cx += this.acceleration * du;
let hitEntity = this.isColliding();
if (hitEntity && hitEntity.type === "Ship") {
//this.kill();
this.takeBulletHit({ type: "playerBullet" });
hitEntity.takeBulletHit({ type: "enemyBullet" });
}
}
// If enemy is out of bounds reset it
if(this.cy > g_canvas.height + 100){
nextY = -20;
this.isAttacking = false;
this.velY = this.velYStandard;
this.sprite.setAnimation("default");
if(this.isRotated){
this.isRotated = false;
this.animationFreeze = false;
this.loopCount = 0;
this.rotation = 0;
// Rotate back by 90 degres counter clockwise!
}
}
this.cy = nextY;
spatialManager.register(this);
};
Alien.prototype.render = function (ctx) {
this.sprite.drawCentredAt(ctx, this.cx, this.cy, this.rotation);
if (this.animationTimer > this.animationInterval) {
this.sprite.nextFrame();
this.animationTimer = 0;
}
};
Alien.prototype.getRadius = function () {
return (this.sprite.width / 2) * 0.65;
};
Alien.prototype.takeBulletHit = function (bullet) {
// Aliens can't kill aliens
if (bullet.type === "enemyBullet") {
return;
}
let mode = (this.isAttacking) ? "charger" : "convoy";
scoreManager.increasePlayerScore(mode, this.spriteType);
this.explosionAlien.play();
this.sprite.setAnimation("explosion");
this.isExploding = true;
this.isAttacking = false;
this.animationFreeze = false;
this.rotation = 0;
this.animationInterval = 0.10 * SECS_TO_NOMINALS
};
Alien.prototype.maybeFireBullet = function () {
let probability = util.randRange(0, 200);
let bulletSpeed = 3;
if (probability < 2) {
entityManager.fireEnemyBullet(this.cx, this.cy + this.sprite.height / 2, bulletSpeed)
}
};
// Checks the condition for initiatng an attack
// The conditions are: The alien should not have any neighbours in one direction
Alien.prototype.maybeAttack = function() {
// If player is dead, stop attacking
let ship = entityManager.getShip();
if(typeof ship !== 'undefined' && ship.isRespawning)
return;
let alienGrid = entityManager.getAlienGrid();
let leftNeighboursDead = true;
let rightNeighboursDead = true;
// Check that all enemies to the left is dead
for(let i = 0; i < this.column; i++){
if(alienGrid[this.row][i] != 0){
leftNeighboursDead = false
break;
}
}
// Check that all enemies to the right is dead
for(let i = alienGrid[this.row].length - 1; i > this.column; i--){
if(alienGrid[this.row][i] != 0){
rightNeighboursDead = false
break;
}
}
if(this.column == 0 || this.column == alienGrid[0].length -1 || leftNeighboursDead || rightNeighboursDead){
let prob = entityManager.getPhaseProbability();
let probability = util.randRange(0, prob); //former 5000
if (probability < 2) {
this.sprite.setAnimation("attacking");
this.isAttacking = true;
this.accelerationY = -2;
this.dir = 1;
this.acceleration = this.sidePush;
if(leftNeighboursDead)
this.acceleration = - this.sidePush;
this.dir = -1;
}
}
}