-
Notifications
You must be signed in to change notification settings - Fork 2
/
heart.js
149 lines (131 loc) · 4.71 KB
/
heart.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
var mapData = [];
jQuery(document).ready(function()
{
var xScale = $(window).width() / 640;
var yScale = $(window).height() / 530;
var scale = Math.floor(Math.max(1,Math.min(xScale, yScale)));
$('#wrapper').css('zoom', scale);
if (location.search != "")
{
var queryString = location.search.split("=");
data.currentLevel = parseInt(queryString[1]);
}
else
{
location.href = "start.html";
}
var mapVersion = data.currentLevel;
renderMap(mapData[mapVersion].layers.mapData, mapData[mapVersion].layers.objectData);
initiateLevel();
buildBuilderMenu();
heartBeat = window.setInterval(updateGame, 25);
//window.setInterval(updateGame, 25);
$("#map div[data-blocker = 'false']").on('click', function(){
var mapNodes = $('#map').children();
for (var i = 1; i < mapNodes.length; i++) {
$(mapNodes[i]).removeClass('blueBoxShadow')
}
$('#buildMenu').css('display', 'block');
var clicked = this;
$(this).addClass('blueBoxShadow');
var nodes = $('#buildMenu').children();
for (var i = 1; i < nodes.length; i++) {
var towerName = nodes[i].dataset.name;
if(clicked.dataset.blocker == "true" || data.currentCredits < data.towers[towerName].costs[data.towers[towerName].level])
{
$(nodes[i]).addClass('redState');
}
}
$("#buildMenu .towerStoreItem").unbind().on('click', function() {
var offsetTop = clicked.offsetTop;
var offsetLeft = clicked.offsetLeft;
var towerName = this.dataset.name;
if(clicked.dataset.blocker == "false" && data.currentCredits >= data.towers[towerName].costs[0])
{
spawnTower(offsetTop, offsetLeft, towerName);
$('#buildMenu').css('display', 'none');
$(clicked).removeClass('blueBoxShadow');
}
});
});
$("#buildMenu .close").on('click', function (){
$('#buildMenu').css('display', 'none');
});
$(document).on("click", "#overlayLost .button", function(e) {
location.href = "index.html?level=" + data.currentLevel;
});
$(document).on("click", "#overlayNextLevel .button", function(e) {
location.href = "index.html?level=" + (data.currentLevel + 1);
});
});
function updateGame ()
{
$('#fullTitleBar').css('width', (Math.floor(data.kills / data.requiredKills * 330)) + "px");
$('#lifeIcon').text(data.life);
$('#creditIcon').text(data.currentCredits);
// update movement of all enemies
for(key in data.currentEnemies)
{
checkForNextWaypoint(key);
var speed = data.currentEnemies[key].speed;
if (data.currentEnemies[key].slowedTurns > 0)
{
speed *= 0.6;
data.currentEnemies[key].slowedTurns--;
}
newPos = moveEntity (data.currentEnemies[key].posX, data.currentEnemies[key].posY, speed, data.currentEnemies[key].angle);
updatePosition(key, newPos);
}
// update movement of all projectiles
for(key in data.currentProjectiles)
{
if (typeof data.currentEnemies[data.currentProjectiles[key].targetID] != "undefined")
{
var angle = calculateAngle (data.currentProjectiles[key].posX, data.currentProjectiles[key].posY, data.currentEnemies[data.currentProjectiles[key].targetID].posX, data.currentEnemies[data.currentProjectiles[key].targetID].posY);
data.currentProjectiles[key].angle = angle;
rotate(data.currentProjectiles[key].domElement, angle);
var newPos = moveEntity (data.currentProjectiles[key].posX, data.currentProjectiles[key].posY, data.currentProjectiles[key].speed, angle);
data.currentProjectiles[key].posX = newPos[0];
data.currentProjectiles[key].posY = newPos[1];
data.currentProjectiles[key].domElement.css('left', (Math.floor(newPos[0])) + "px");
data.currentProjectiles[key].domElement.css('top', (Math.floor(newPos[1])) + "px");
}
checkForHittingProjectile (key);
}
// rotate the moneymakers!
for(key in data.currentTowers)
{
if (data.currentTowers[key].special == "moneyBoost")
{
data.currentTowers[key].angle += 1;
rotate($("#tower" + key + " > .turret"), data.currentTowers[key].angle);
}
}
processEnemiesToDelete();
processProjectilesToDelete();
//check win condition
if (data.kills >= data.requiredKills)
{
// next level possible
if (data.currentLevel < data.maxLevels)
{
winLevel();
}
else
{
winGame();
}
}
}
function initiateLevel(){
data.currentCredits += data.startCredits[data.currentLevel];
setTimeout (function() { generateCredits(50); }, 2500);
for(key in data.waves[data.currentLevel])
(function(key) {
setTimeout(function ()
{
spawnEnemies(data.waypoints[data.currentLevel][data.waves[data.currentLevel][key][0]][0], data.waves[data.currentLevel][key], 0);
}, key);
})(key);
determineRequiredKills();
}