-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemy2.js
130 lines (99 loc) · 3.43 KB
/
Enemy2.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
// ====
// ENEMY2
// ====
"use strict";
/* jshint browser: true, devel: true, globalstrict: true */
/*
0 1 2 3 4 5 6 7 8
12345678901234567890123456789012345678901234567890123456789012345678901234567890
*/
// A generic contructor which accepts an arbitrary descriptor object
function Enemy2(descr) {
// Common inherited setup logic from Entity
this.setup(descr);
// Default sprite and scale, if not otherwise specified
this.sprite = g_sprites.enemy2[8];
this.scale = this.scale || 2;
/*
// Diagnostics to check inheritance stuff
this._Enemy1Property = true;
console.dir(this);
*/
};
Enemy2.prototype = new Entity();
Enemy2.prototype.interval = 70 / NOMINAL_UPDATE_INTERVAL; //interval for the animation
Enemy2.prototype.cel = 8; //sprite cells
Enemy2.prototype.eInterval = 50 / NOMINAL_UPDATE_INTERVAL; //explosion animation interval
Enemy2.prototype.update = function (du) {
// unregister enemy2 from spatial manager
spatialManager.unregister(this);
//check if the enemy is dead
if (this._isDeadNow || this.cx < 0) {
return entityManager.KILL_ME_NOW;
//trigger explosion
//when the interval for the explosion animation goes below zero then change the sprite and reset the interval
} else if (this.isExploding) {
this.eInterval -= du;
if(this.eInterval < 0){
this.nextExplodingSprite();
this.eInterval = 50 / NOMINAL_UPDATE_INTERVAL;
}
//enemy is alive
} else {
//update the enemy 2 velocity and make it move in a wave like motion
this.xVel = -3;
this.yVel = 4 * Math.cos(this.cx / 100);
//get previous position
var prevX = this.cx;
var prevY = this.cy;
//calculate next position
var nextX = prevX + this.xVel * du;
var nextY = prevY + this.yVel * du;
//animate enemy 2
this.interval -= du;
if (this.interval < 0) {
//if the next position is above the previous position, animate the enemy to go up
if (nextY > this.cy) {
if (this.cel > 5)
this.cel--;
this.sprite = g_sprites.enemy2[this.cel];
} else {
//if the next position is below the previous position, animate the enemy to go down
if (this.cel < 11)
this.cel++;
this.sprite = g_sprites.enemy2[this.cel];
}
this.interval = 70 / NOMINAL_UPDATE_INTERVAL;
}
//actually move enemy 2 according to the velocity
this.cx += this.xVel * du;
this.cy += this.yVel * du;
// register enemy 2 in the spatial manager
spatialManager.register(this);
}
};
Enemy2.prototype.getRadius = function () {
return this.scale * (this.sprite.width / 2) * 0.9;
};
// HACKED-IN AUDIO (no preloading)
Enemy2.prototype.evaporateSound = new Audio("sounds/explosion.mp3");
//function for taking a bullet hit, increment the score and trigger the explosion
Enemy2.prototype.takeBulletHit = function () {
entityManager._hud[0].incrementScore(50);
//Check if we should generate powerup
if(entityManager._hud[0].killCount % 50 == 0) {
entityManager.generatePowerup(this.cx, this.cy, 'multiGun');
}
this.isExploding = true;
// play the sound of enemy exploding
this.evaporateSound.pause();
this.evaporateSound.currentTime = 0;
this.evaporateSound.play();
};
//render enemy 2
Enemy2.prototype.render = function (ctx) {
this.sprite.scale = this.scale;
this.sprite.drawCentredAt(
ctx, this.cx, this.cy, this.rotation
);
};