-
Notifications
You must be signed in to change notification settings - Fork 1
/
TimePilot.EnemyFactory.js
200 lines (180 loc) · 5.64 KB
/
TimePilot.EnemyFactory.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
/* global define */
define("TimePilot.EnemyFactory", [
"TimePilot.CONSTANTS",
"TimePilot.dataStore",
"TimePilot.Enemy",
"engine/Sound",
"engine/helpers"
], function (
CONSTS,
dataStore,
Enemy,
SoundEngine,
helpers
) {
/**
* Construct an enemy factory for managing creation, movement, rendering and removal of enemies.
* @constructor
* @returns {Enemy Factory Instance}
*/
var EnemyFactory = function () {
this._level = dataStore._level;
this._player = dataStore._player;
this._bullets = dataStore._bullets;
this._explosionSound = new SoundEngine(this.getLevelData().explosion.sound.src);
this._enemies = [];
};
EnemyFactory.prototype = {
/**
* Create an enemy instance and keep a record of it in the factory.
* @method
* @param {Number} posX - X coordinate to start from.
* @param {Number} posY - Y coordinate to start from.
* @param {Number} heading - Heading to start from.
*/
create: function (posX, posY, heading) {
this._enemies.push(
new Enemy(posX, posY, heading)
);
},
/**
* Get current data for this level
* @method
* @param {String} [key] [description]
* @returns {object}
*/
getLevelData: function (key) {
var data = CONSTS.levels[this._level].enemies.basic;
if (key) {
if (data[key]) {
return data[key];
} else {
return;
}
} else {
return data;
}
},
/**
* Get the current number of spawned entities.
* @method
* @returns {Number}
*/
getCount: function () {
return this._enemies.length;
},
/**
* Boolean flag reporting if there are spawns available for enemies.
* @method
* @returns {Boolean}
*/
isUnderLimit: function () {
return this._enemies.length < this.getLevelData('spawnLimit');
},
/**
* Return the data for all entities in an array.
* @method
* @returns {Array}
*/
getData: function () {
var data = [],
i = 0;
for (i in this._enemies) {
if (this._enemies.hasOwnProperty(i)) {
data.push(this._enemies[i].getData());
}
}
return data;
},
/**
* Run player collision calculations on all entities.
* @method
*/
detectCollision: function () {
var bullets = this._bullets.getData(),
playerData = this._player.getData();
for (var i in this._enemies) {
if (this._enemies.hasOwnProperty(i) && this._enemies[i].isAlive && playerData.isAlive) {
if (this._enemies[i].detectCollision(
playerData.posX,
playerData.posY,
CONSTS.player.hitRadius
)) {
this._enemies[i].kill();
this._explosionSound.stop();
this._explosionSound.play();
this._player.kill();
}
for (var j in bullets) {
if (bullets.hasOwnProperty(j) &&
this._enemies[i].detectCollision(
bullets[j].posX + this._player.getData().posX,
bullets[j].posY + this._player.getData().posY,
CONSTS.player.projectile.size
)
) {
this._enemies[i].kill();
this._explosionSound.stop();
this._explosionSound.play();
}
}
}
}
},
/**
* If an entity declares it is to be removed, remove it.
* @method
*/
cleanup: function () {
var i;
for (i in this._enemies) {
if (this._enemies.hasOwnProperty(i) && this._enemies[i].removeMe) {
this._despawn(i);
}
}
},
/**
* Run all reposition logic.
* @method
*/
reposition: function () {
var i;
for (i in this._enemies) {
if (this._enemies.hasOwnProperty(i)) {
this._enemies[i].reposition();
}
}
},
/**
* Render all enemies on the gameArena.
* @method
*/
render: function () {
var i = 0;
for (i in this._enemies) {
if (this._enemies.hasOwnProperty(i)) {
this._enemies[i].render();
}
}
},
/**
* De-spawn specified entity.
* @method
* @param {Number} entityId - Index ID of entity you wish to remove.
*/
_despawn: function (entityId) {
this._enemies.splice(entityId, 1);
},
/**
* Clear all enemies from memory.
*/
clearAll: function () {
for (var i in this._enemies) {
if (this._enemies.hasOwnProperty(i)) {
this._despawn(i);
}
}
}
};
return EnemyFactory;
});