forked from MasterIV/ggj2015
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
533 lines (476 loc) · 19.2 KB
/
helpers.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
// DOsplays the build menu for towers
function buildBuilderMenu()
{
var buildTowerHTML = "";
$.each(data.towers, function(key, value) {
buildTowerHTML = buildTowerHTML +
'<div data-name="'+key+'" class="towerStoreItem">'+
'<img src="images/turrets/'+ key +'.png" />'+
'<div id="price">Price: '+value.costs[value.level]+'</div>'+
'<h4 class="name">'+ key +'</h4>'+
'<ul>'+
'<li>'+
'<div class="damageIcon"></div><span class="damage">Damage: '+ value.damage[value.level] +'</span>'+
'</li>'+
'<li>'+
'<div class="firerateIcon"></div><span class="firerate">Firerate: '+ value.firerate[value.level] +'</span>'+
'</li>'+
'<li>'+
'<div class="rangeIcon"></div><span class="range">Range: '+ value.range[value.level] +'</span>'+
'</li>'+
'<li>'+
'<div class="weaknessIcon"></div><span class="weakness">Weakness: '+ value.damageType +'</span>'+
'</li>'+
'</ul>'+
'</div>';
});
$('.close').after(buildTowerHTML);
}
// Calculates the pixel-distance between two objects
function calculateDistance (oneX, oneY, twoX, twoY)
{
xDist = Math.abs(oneX - twoX);
yDist = Math.abs(oneY - twoY);
return Math.sqrt((xDist * xDist) + (yDist * yDist));
}
// Calculates the looking angle from object ONE at object TWO. Returned value is in DEGREE.
function calculateAngle (oneX, oneY, twoX, twoY)
{
xDist = twoX - oneX;
yDist = twoY - oneY;
return 180 + 180 / Math.PI * Math.atan2(-xDist, -yDist);
}
// Returns the new position of an object after moving it
function moveEntity (xPos, yPos, speed, angle)
{
angle = toRadians(angle);
if (angle >= 0 && angle <= 90)
{
return [xPos + (speed * Math.sin(angle)), yPos + (speed * Math.cos(angle))];
}
if (angle > 90 && angle <= 180)
{
return [xPos + (speed * Math.sin(angle - 90)), yPos - (speed * Math.cos(angle - 90))];
}
if (angle > 180 && angle <= 270)
{
return [xPos - (speed * Math.sin(angle - 180)), yPos - (speed * Math.cos(angle - 180))];
}
if (angle > 270 && angle < 360)
{
return [xPos - (speed * Math.sin(angle - 270)), yPos + (speed * Math.cos(angle - 270))];
}
}
function updatePosition (enemyID, newPos)
{
data.currentEnemies[enemyID].posX = newPos[0];
data.currentEnemies[enemyID].posY = newPos[1];
data.currentEnemies[enemyID].domElement.css('left', Math.floor(newPos[0]) + "px");
data.currentEnemies[enemyID].domElement.css('top', Math.floor(newPos[1]) + "px");
data.currentEnemies[enemyID].domLifebar.css('left', Math.floor(newPos[0]) + 6 + "px");
data.currentEnemies[enemyID].domLifebar.css('top', Math.floor(newPos[1]) - 6 + "px");
}
// Checks if two DOM objects have collided. Tolerance states the pixel-distance for collision
function checkCollision (objectOne, objectTwo, tolerance)
{
// Center points of both objects are taken into account
oneX = parseInt(objectOne.css('left').split("px")) + objectOne.width() / 2;
oneY = parseInt(objectOne.css('top').split("px")) + objectOne.height() / 2;
twoX = parseInt(objectTwo.css('left').split("px")) + objectTwo.width() / 2;
twoY = parseInt(objectTwo.css('top').split("px")) + objectTwo.height() / 2;
if (calculateDistance (oneX, oneY, twoX, twoY) <= tolerance)
{
return true;
}
else
{
return false;
}
}
// Initiates the animation of a sprite. First call is to cause a delay
function animateSprite (target, frames, loop, playSpeed)
{
window.setTimeout(function(){ animateSpriteHelper(target, frames, 1, loop, playSpeed) }, playSpeed);
}
// Animates a sprite.
// Target -> JQUERY DIV Object
// frames -> total amount of frames in the animation, including the start frame!
// currentFrame -> currently displayed frame
// loop -> true / false (true: animation will be looped infinitely)
// playSpeed -> pause in milliseconds between animations
function animateSpriteHelper (target, frames, currentFrame, loop, playSpeed)
{
if (target.length > 0 && data.killAllTimers == false)
{
if (currentFrame == 1)
{
target.css("background-position", "0px 0px");
}
target.css("background-position", (target.width() * (currentFrame - 1) * (-1)) + "px 0px");
currentFrame++;
if (currentFrame <= frames)
{
window.setTimeout(function(){ animateSpriteHelper(target, frames, currentFrame, loop, playSpeed) }, playSpeed);
}
if (currentFrame > frames && loop)
{
window.setTimeout(function(){ animateSpriteHelper(target, frames, 1, loop, playSpeed) }, playSpeed);
}
}
}
// Spawns all enemies from a given list one by one in a 500ms interval
function spawnEnemies (startingWaypoint, enemyList, unitPath)
{
if (data.killAllTimers == false)
{
if (unitPath == 0)
{
// Waypoint information, which is always element 0, needs to be killed and stored differently!
var unitPath = enemyList.shift();
}
var domRepresentative = $("<div class='enemy " + enemyList[0] + "' id='" + data.currentEnemyID + "'></div>");
var lifeBar = $("<div class='lifebar' id='lifebar" + data.currentEnemyID + "'><div class='currentLife'></div></div>");
data.currentEnemies[data.currentEnemyID] = jQuery.extend(true, {domElement : domRepresentative, domLifebar : lifeBar }, data.enemies[enemyList.shift()]);
// give initial position and rotation
variation = Math.floor((Math.random() * 5) + 1);
data.currentEnemies[data.currentEnemyID].posX = startingWaypoint[0]*32 + variation;
data.currentEnemies[data.currentEnemyID].posY = startingWaypoint[1]*32 + variation;
// give list of waypoints to entity
data.currentEnemies[data.currentEnemyID].nextWaypoints = data.waypoints[data.currentLevel][unitPath];
// calculate looking angle for next waypoint
nextWaypointPos = calculateNextWaypointPosition(data.currentEnemyID);
data.currentEnemies[data.currentEnemyID].angle = calculateAngle (data.currentEnemies[data.currentEnemyID].posX, data.currentEnemies[data.currentEnemyID].posY, nextWaypointPos[0], nextWaypointPos[1]);
// TODO - add initial CSS rotation
rotate(domRepresentative, data.currentEnemies[data.currentEnemyID].angle);
domRepresentative.css('left', (startingWaypoint[0]*32 + variation) + "px");
domRepresentative.css('top', (startingWaypoint[1]*32 + variation) + "px");
lifeBar.css('left', (startingWaypoint[0]*32 + variation + 6) + "px");
lifeBar.css('top', (startingWaypoint[1]*32 + variation - 6) + "px");
$("#objects").append(domRepresentative);
$("#objects").append(lifeBar);
// initiate animation
animateSprite(domRepresentative, data.currentEnemies[data.currentEnemyID].animationFrames, true, data.currentEnemies[data.currentEnemyID].animationSpeed);
data.currentEnemyID++;
if (enemyList.length > 0)
{
window.setTimeout(function(){ spawnEnemies(startingWaypoint, enemyList, unitPath) }, 500);
}
}
}
// Checks if unit has reached its next waypoint
function checkForNextWaypoint (enemyID)
{
nextWaypointPos = calculateNextWaypointPosition(enemyID);
distance = calculateDistance (data.currentEnemies[enemyID].posX, data.currentEnemies[enemyID].posY, nextWaypointPos[0], nextWaypointPos[1]);
if (distance <= 10)
{
data.currentEnemies[enemyID].currentWaypoint++;
// enemy has reached last waypoint
if (data.currentEnemies[enemyID].currentWaypoint == data.currentEnemies[enemyID].nextWaypoints.length)
{
removeEnemy (enemyID);
reduceLife();
data.kills++;
}
// enemy is still on the go
else
{
nextWaypointPos = calculateNextWaypointPosition(enemyID);
// calculate looking angle for next waypoint
data.currentEnemies[enemyID].angle = calculateAngle (data.currentEnemies[enemyID].posX, data.currentEnemies[enemyID].posY, nextWaypointPos[0], nextWaypointPos[1]);
// also change rotation for DOM Element
rotate(data.currentEnemies[enemyID].domElement, data.currentEnemies[enemyID].angle);
}
return true;
}
else
{
return false;
}
}
// provides X and Y coordinates of the next waypoint
function calculateNextWaypointPosition (enemyID)
{
var nextWaypoint = data.currentEnemies[enemyID].nextWaypoints[data.currentEnemies[enemyID].currentWaypoint];
var nextWaypointX = nextWaypoint[0] * 32;
var nextWaypointY = nextWaypoint[1] * 32;
return [nextWaypointX, nextWaypointY];
}
// Tower tries to emit a projectile
function towerShoots (towerID)
{
var target = scanForTargets(towerID);
if (target.id >= 0)
{
// Tower turns towards target on shooting
var angle = calculateAngle (data.currentTowers[towerID].posX, data.currentTowers[towerID].posY, data.currentEnemies[target.id].posX, data.currentEnemies[target.id].posY);
rotate($("#tower" + towerID + " > .turret"), angle);
spawnProjectile (towerID, target.id);
sound.play("sound/" + data.currentTowers[towerID].sound);
}
}
// Scans for all targets within a towers range
function scanForTargets (towerID)
{
var bestTarget = {
id: -1,
waypoint: 0,
};
for (key in data.currentEnemies)
{
if (checkCollision(data.currentEnemies[key].domElement, data.currentTowers[towerID].domElement, data.currentTowers[towerID].range[data.currentTowers[towerID].level]))
{
if (data.currentEnemies[key].currentWaypoint > bestTarget.waypoint)
{
bestTarget.id = key;
bestTarget.waypoint = data.currentEnemies[key].currentWaypoint;
}
}
}
return bestTarget;
}
// Spawns a projectile from a turret's position
function spawnProjectile (towerID, targetID)
{
var domRepresentative = $("<div class='projectile " + data.currentTowers[towerID].projectile + "' id='projectile" + data.currentProjectileID + "'></div>");
data.currentProjectiles[data.currentProjectileID] = jQuery.extend(true, {domElement : domRepresentative }, data.projectiles[data.currentTowers[towerID].projectile]);
data.currentProjectiles[data.currentProjectileID].targetID = targetID;
data.currentProjectiles[data.currentProjectileID].damage = data.currentTowers[towerID].damage[data.currentTowers[towerID].level];
// give initial position and rotation
data.currentProjectiles[data.currentProjectileID].posX = data.currentTowers[towerID].posX;
data.currentProjectiles[data.currentProjectileID].posY = data.currentTowers[towerID].posY;
domRepresentative.css('left', (data.currentTowers[towerID].posX) + "px");
domRepresentative.css('top', (data.currentTowers[towerID].posY) + "px");
// calculate looking angle for next waypoint
var angle = calculateAngle (data.currentProjectiles[data.currentProjectileID].posX, data.currentProjectiles[data.currentProjectileID].posY, data.currentEnemies[targetID].posX, data.currentEnemies[targetID].posY);
data.currentProjectiles[data.currentProjectileID].angle = angle;
rotate(domRepresentative, angle);
// put projectile ahead of turret
var newPos = moveEntity (data.currentProjectiles[data.currentProjectileID].posX, data.currentProjectiles[data.currentProjectileID].posY, 8, angle);
data.currentProjectiles[data.currentProjectileID].posX = newPos[0];
data.currentProjectiles[data.currentProjectileID].posY = newPos[1];
domRepresentative.css('left', (Math.floor(newPos[0])) + "px");
domRepresentative.css('top', (Math.floor(newPos[1])) + "px");
$("#objects").append(domRepresentative);
data.currentProjectileID++;
}
// Projectile might hit and damage target
function checkForHittingProjectile (projectileID)
{
// Check if target is still there
if (typeof data.currentEnemies[data.currentProjectiles[projectileID].targetID] != "undefined")
{
// reduce HP of the hit enemy & remove it if applicable
if (checkCollision(data.currentEnemies[data.currentProjectiles[projectileID].targetID].domElement, data.currentProjectiles[projectileID].domElement, 12))
{
if(data.currentEnemies[data.currentProjectiles[projectileID].targetID].resistence == data.currentProjectiles[projectileID].type)
{
data.currentEnemies[data.currentProjectiles[projectileID].targetID].hitpoints -= Math.floor(data.currentProjectiles[projectileID].damage / 3);
} else {
data.currentEnemies[data.currentProjectiles[projectileID].targetID].hitpoints -= data.currentProjectiles[projectileID].damage;
}
$("#lifebar" + data.currentProjectiles[projectileID].targetID + " .currentLife").css("width", (Math.floor(data.currentEnemies[data.currentProjectiles[projectileID].targetID].hitpoints / data.currentEnemies[data.currentProjectiles[projectileID].targetID].maxHitpoints * 20)) + "px");
// apply slow effect
if (data.currentProjectiles[projectileID].special == "slow")
{
data.currentEnemies[data.currentProjectiles[projectileID].targetID].slowedTurns = 80;
}
// display explosion
if (data.currentProjectiles[projectileID].special == "splash")
{
spawnEmitter ("explosion", 5, 40, 0, 0, data.currentEnemies[data.currentProjectiles[projectileID].targetID].posX, data.currentEnemies[data.currentProjectiles[projectileID].targetID].posY);
sound.play("sound/explosion.mp3");
for (var key in data.currentEnemies)
{
if (checkCollision(data.currentEnemies[key].domElement, data.currentProjectiles[projectileID].domElement, 50) && key != data.currentProjectiles[projectileID].targetID)
{
if(data.currentEnemies[key].resistence == data.currentProjectiles[projectileID].type)
{
data.currentEnemies[key].hitpoints -= Math.floor(data.currentProjectiles[projectileID].damage / 3);
} else {
data.currentEnemies[key].hitpoints -= data.currentProjectiles[projectileID].damage;
}
$("#lifebar" + key + " .currentLife").css("width", (Math.floor(data.currentEnemies[key].hitpoints / data.currentEnemies[key].maxHitpoints * 20)) + "px");
if( data.currentEnemies[key].hitpoints <= 0 )
{
removeEnemy (key);
spawnEmitter ("bloodSplash", 5, 40, 0, 0, data.currentEnemies[key].posX, data.currentEnemies[key].posY);
data.kills++;
}
}
}
}
if (data.currentEnemies[data.currentProjectiles[projectileID].targetID].hitpoints <= 0)
{
spawnEmitter ("bloodSplash", 5, 40, 0, 0, data.currentEnemies[data.currentProjectiles[projectileID].targetID].posX, data.currentEnemies[data.currentProjectiles[projectileID].targetID].posY);
removeEnemy (data.currentProjectiles[projectileID].targetID);
data.kills++;
}
// TODO add emitter here
removeProjectile (projectileID);
return true;
}
else
{
return false;
}
}
else
{
// TODO add emitter here
removeProjectile (projectileID);
return false;
}
}
// Spawns a temporary GFX emitter
function spawnEmitter (cssClass, frames, playSpeed, offsetX, offsetY, posX, posY)
{
var domRepresentative = $("<div class='" + cssClass + "' id='emitter" + data.currentEmitterID + "'></div>");
domRepresentative.css("left", posX + offsetX);
domRepresentative.css("top", posY + offsetY);
animateSprite (domRepresentative, frames, false, playSpeed);
$("#objects").append(domRepresentative);
// emitter terminates itself after being played
setTimeout(function(){ domRepresentative.remove(); }, frames * playSpeed);
}
// removes DOM objects and marks items for deletion
function removeEnemy (enemyID)
{
$("#" + enemyID).remove();
$("#lifebar" + enemyID).remove();
data.enemiesToDelete.push(enemyID);
}
// Actually removes enemies after the FOR loop in heart.js has run out
function processEnemiesToDelete ()
{
while (data.enemiesToDelete.length > 0)
{
delete data.currentEnemies[data.enemiesToDelete.pop()];
}
}
// removes DOM objects and marks items for deletion
function removeProjectile (projectileID)
{
$("#projectile" + projectileID).remove();
data.projectilesToDelete.push(projectileID);
}
// Actually removes projectiles after the FOR loop in heart.js has run out
function processProjectilesToDelete ()
{
while (data.projectilesToDelete.length > 0)
{
delete data.currentProjectiles[data.projectilesToDelete.pop()];
}
}
function reduceLife ()
{
data.life--;
// Adjust life display
// TODO
if (data.life <= 0)
{
data.killAllTimers = true;
$("#shroud").css("display", "block");
$("#overlayLost").css("display", "block");
}
}
function winLevel ()
{
$("#shroud").css("display", "block");
$("#overlayNextLevel").css("display", "block");
}
function winGame ()
{
$("#shroud").css("display", "block");
$("#overlayWon").css("display", "block");
}
function determineRequiredKills ()
{
data.requiredKills = 0;
for (key in data.waves[data.currentLevel])
{
data.requiredKills += data.waves[data.currentLevel][key].length - 1;
}
}
function rotate(target, degree)
{
// For webkit browsers: e.g. Chrome
target.css({ WebkitTransform: 'rotate(' + (-1) * degree + 'deg)'});
// For Mozilla browser: e.g. Firefox
target.css({ '-moz-transform': 'rotate(' + (-1) * degree + 'deg)'});
}
function generateCredits (amount)
{
if (data.killAllTimers == false)
{
data.currentCredits += amount;
setTimeout (function () { generateCredits(amount); }, 2500);
var childs = $('#buildMenu').children();
for (var i = 1; i < childs.length; i++)
{
var child = $(childs[i]).children()[1];
if((parseInt($('#creditIcon').text()) + amount) >= $(child).text().split(' ')[1])
{
$(childs[i]).removeClass('redState');
}
}
}
}
function toDegrees (radian)
{
return radian * (180 / Math.PI);
}
function toRadians (angle)
{
return angle * (Math.PI / 180);
}
function spawnTower(offsetTop, offsetLeft, towerName)
{
var tower =
$('<div id="tower'+ data.currentTowerID +'" class="tower '+towerName+'" style="top:'+offsetTop+'px; left:'+offsetLeft+'px;">'+
'<img class="turret" src="images/turrets/'+towerName+'.png"/>'+
'<img class="base" src="images/turrets/base.png"/>'+
'</div>');
var selectedTile = (offsetTop / 32) * (offsetLeft / 32);
$('#map').children().eq(selectedTile).data('blocker', true);
data.currentTowers[data.currentTowerID] = jQuery.extend(true, {domElement : tower}, data.towers[towerName]);
console.log(data.currentTowers);
data.currentTowers[data.currentTowerID].posY = offsetTop;
data.currentTowers[data.currentTowerID].posX = offsetLeft;
spawnEmitter ("summon", 7, 70, 0, 0, offsetLeft, offsetTop);
var towerID = data.currentTowerID;
data.currentTowers[data.currentTowerID].firePulse = setInterval(function() { towerShoots(towerID) }, data.currentTowers[data.currentTowerID].firerate[data.currentTowers[data.currentTowerID].level]);
if (data.currentTowers[data.currentTowerID].special == "moneyBoost")
{
setTimeout (function () { generateCredits(6);}, 2500);
}
data.currentCredits -= data.currentTowers[data.currentTowerID].costs[data.currentTowers[data.currentTowerID].level];
data.currentTowerID++;
$('#objects').append(tower);
}
var sound = {
sampels: [],
mp3: function()
{
var audio = new Audio();
return !!audio.canPlayType && audio.canPlayType('audio/mpeg') != "";
},
play: function( file )
{
var self = this;
//if(this.mp3()) file = file.replace(/ogg/g, "mp3");
if( !this.sampels[file] )
this.sampels[file] = [];
if( this.sampels[file].length )
{
var sound = this.sampels[file].pop();
sound.play();
return sound;
} else {
var sound = new Audio( file );
sound.onended = function() { self.sampels[file].push( this ); };
sound.play();
return sound;
}
},
}