-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnemy.js
346 lines (333 loc) · 10.1 KB
/
Enemy.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
//Enemy: Defines a new enemy object
Enemy.id = 0;
/* Constructor for Enemy
* @param {String} type The type of enemy
* @param {Number} health The health remaining
* @param {Number} armor The armor remaining
* @param {Number} money Money given when they die
* @param {Number} speed the speed they move each frame
* @param {Number} damage The damage they do if get to end path?
* @param {String} color1 The primary color
* @param {String} color2 The secondary color
* @param {String} color3 The tertiary color
* @param {String} color4 The quaternary color
* @param {Integer} drawingFunction which drawing function should we use?
* @param {Integer} The current frame
* @param {Path} The monster's path
* @param {Array of MonsterTypes} deathSplit What the monster splits into when it dies
* @param {Number} distanceTravelled (used for spawning monsters during split)
*/
function Enemy(
type,
health,
armor,
money,
speed,
damage,
color1,
color2,
color3,
color4,
drawingFunction,
frame,
path,
deathSplit,
distanceTravelled
) {
//set id
this.id = Enemy.id;
Enemy.id++;
//given variables
this.type = type;
this.health = health;
this.healthArray = new Array(); //used to track life for each frame
this.isDead = false; //will the enemy die before it crosses the finish line?
this.armor = armor;
this.armorArray = new Array(); //used to track armor for each frame
this.money = money;
this.speed = speed + Math.random() * 0.01;
this.damage = damage;
this.color1 = color1;
this.color2 = color2;
this.color3 = color3;
this.color4 = color4;
this.drawingFunction = drawingFunction;
this.frame = frame;
this.deathSplit = deathSplit;
this.distanceTravelled = distanceTravelled;
this.path = path;
//derived variables
//movement
this.positions = new Array();
this.rotations = new Array();
this.lastFrame = frame;
this.scale = 1;
this.calculatePositions(); //calculates the position and rotation at every frame
this.currentRotation = this.rotations[0];
this.transition = 0;
this.transitionAngle = 0;
this.hitRadius = this.path.width * 0.9;
this.hitRadiusSquared = this.hitRadius * this.hitRadius;
}
/* Calculates the position at every frame step
* @param {Path} the current path
*/
Enemy.prototype.calculatePositions = function (path) {
//make a variable for distance travelled
var pathSegment = 0; //The current path segment
//find the correct path segment
var currentFrame = this.frame - 1;
//how far have we travelled on the current path?
var currentPathTravelled = this.distanceTravelled;
while (this.distanceTravelled <= this.path.totalLength) {
currentFrame++;
//see if we need a new path?
while (currentPathTravelled > this.path.vectorLengths[pathSegment]) {
//move to the next path
currentPathTravelled -= this.path.vectorLengths[pathSegment];
pathSegment++;
}
//calculate x and y based on current path segment
var x =
this.path.points[pathSegment].x +
this.path.vectors[pathSegment].x * currentPathTravelled;
var y =
this.path.points[pathSegment].y +
this.path.vectors[pathSegment].y * currentPathTravelled;
//make a new point for our current location
var p = new Point(x, y);
//push current location onto this.positions Array
this.positions.push(p);
//add to the health array also
this.healthArray.push(this.health);
//add to the armor array also
this.armorArray.push(this.armor);
//push the path segments rotation onto the rotations array
this.rotations.push(this.path.rotations[pathSegment]);
this.distanceTravelled += this.speed;
currentPathTravelled += this.speed;
}
//last frame before it reaches end of track
this.lastFrame = currentFrame;
};
/*Draws the enemy (We will need more than one function here)
* @param {Canvas Drawing Context} ctx
* @param {Integer} currentFrame (our currentFrame)
* @return {Boolean} Has the enemy reached the end of the frame? If so, it should be deleted
*/
Enemy.prototype.draw = function (ctx, currentFrame) {
//Find the monster's position index and rotation
var currentIndex = currentFrame - this.frame;
//if they have reached the end, damage player
if (currentIndex < this.positions.length) {
var x = this.positions[currentIndex].x;
var y = this.positions[currentIndex].y;
var rotation = this.rotations[currentIndex];
if (this.currentRotation != rotation) {
if (this.transition == 0) {
this.transition = 5;
this.transitionAngle =
(rotation - this.currentRotation) / this.transition;
}
this.transition--;
rotation = this.currentRotation + this.transitionAngle;
}
this.currentRotation = rotation;
var pathWidth = this.path.width;
if (this.drawingFunction == 1) {
EnemyDrawing.draw1(
ctx,
x,
y,
rotation,
pathWidth,
this.color1,
this.color2,
this.color3,
this.color4
);
} else if (this.drawingFunction == 2) {
EnemyDrawing.draw2(
ctx,
x,
y,
rotation,
pathWidth,
this.color1,
this.color2,
this.color3,
this.color4
);
} else if (this.drawingFunction == 3) {
EnemyDrawing.draw3(
ctx,
x,
y,
rotation,
pathWidth,
this.color1,
this.color2,
this.color3,
this.color4
);
} else if (this.drawingFunction == 4) {
EnemyDrawing.draw4(
ctx,
x,
y,
rotation,
pathWidth,
this.color1,
this.color2,
this.color3,
this.color4
);
} else if (this.drawingFunction == 5) {
EnemyDrawing.draw5(
ctx,
x,
y,
rotation,
pathWidth,
this.color1,
this.color2,
this.color3,
this.color4
);
} else if (this.drawingFunction == 6) {
EnemyDrawing.draw6(
ctx,
x,
y,
rotation,
pathWidth,
this.color1,
this.color2,
this.color3,
this.color4
);
} else if (this.drawingFunction == 7) {
EnemyDrawing.draw7(
ctx,
x,
y,
rotation,
pathWidth,
this.color1,
this.color2,
this.color3,
this.color4
);
} else if (this.drawingFunction == 8) {
EnemyDrawing.draw8(
ctx,
x,
y,
rotation,
pathWidth,
this.color1,
this.color2,
this.color3,
this.color4
);
} else if (this.drawingFunction == 9) {
EnemyDrawing.draw9(
ctx,
x,
y,
rotation,
pathWidth,
this.color1,
this.color2,
this.color3,
this.color4
);
} else if (this.drawingFunction == 10) {
EnemyDrawing.draw10(
ctx,
x,
y,
rotation,
pathWidth,
this.color1,
this.color2,
this.color3,
this.color4
);
}
//console.log(this.healthArray[currentFrame]);
//if we are dead, delete the enemy
if (this.healthArray[currentIndex] <= 0) {
return true;
}
//don't delete enemy
return false;
} else {
//delete enemy if we have reached the end of the track
return true;
}
};
/*Applies Damage to a Specific Enemy
* @param {Integer} frame The frame to start applying the damage
* @param {Integer} damage How much damage to apply
*/
Enemy.prototype.applyDamage = function (frame, damage, armorDamage) {
if (frame <= this.lastFrame) {
var index = frame - this.frame;
//apply the damage to every frame after target frame... if it kills the enemy, flag it
for (var i = index, n = this.healthArray.length; i < n; i++) {
var damageApplied = damage;
//apply damage to armor array
if (this.armorArray[i] > 0) {
//console.log("Here1" + damageApplied + " " + this.armorArray[i]);
this.armorArray[i] = Math.max(0, this.armorArray[i] - armorDamage);
}
//apply health damage to armor
if (this.armorArray[i] > 0) {
//console.log("here2" + damageApplied + " " + this.armorArray[i]);
if (damageApplied >= this.armorArray[i]) {
damageApplied -= this.armorArray[i];
this.armorArray[i] = 0;
} else {
this.armorArray[i] -= damageApplied;
damageApplied = 0;
}
}
//apply health damage to health
//console.log("here3" + damageApplied + " " + this.armorArray[i]);
this.healthArray[i] -= damageApplied;
if (this.healthArray[i] <= 0) {
this.isDead = true;
this.healthArray[i] = 0;
}
}
}
};
/*Returns whether or not this object is within a specific area
* @param {Point} center The center point of the radius
* @param {Number} radius The radius of the area
* @param {Integer} The frame to target
* @return {Boolean} true if in area, false if not
*/
Enemy.prototype.inRadius = function (p, r, frame) {
if (frame > this.lastFrame) return false;
//real radius accounts for character width
var r2 = r + this.path.width;
//calculate the index into the positions array
var targetIndex = frame - this.frame;
//quick check of distance
var xDistance = Math.abs(this.positions[targetIndex].x - p.x);
var yDistance = Math.abs(this.positions[targetIndex].y - p.y);
if (xDistance > r2 || xDistance > r2) return false;
//Distance formula
var distanceSquared = xDistance * xDistance + yDistance * yDistance;
var r2Squared = r2 * r2;
return distanceSquared < r2Squared;
};
/*Returns how long it will be before this object reaches the end of the path
* @param {Integer} frame The current Frame
* @return The number of frames before the object is finished
*/
Enemy.prototype.framesTillEnd = function (frame) {
return this.positions.length - (frame - this.frame);
};