-
Notifications
You must be signed in to change notification settings - Fork 2
/
TD.html
318 lines (260 loc) · 10.8 KB
/
TD.html
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
<!DOCTYPE html>
<html>
<head>
<style>
canvas, #stats{
border: 1px solid black;
}
canvas{
background: #2B6E36;
}
#stats{
background: #003D99;
}
div canvas, p{
float: left;
}
div p{
width: 50%;
margin : 5px;
}
</style>
</head>
<body>
<div>
<canvas id="ctx" width="500" height="500"></canvas>
<canvas id="stats" width="200" height="500"></canvas>
<p>
Clickear el boton "play" para empezar una oleada. Solo se puede activar 1 oleada a la vez. La oleada termina cuando todos los monstruos se eliminan o salen del mapa.<br><br>
Mantener precionado 1, 2 o 3 para elegir una torre y clickear en el mapa para colocarla.
Si no tenes oro suficiente el valor de la torre se va a mostrar en rojo.
<br><br>Seleccionar una torre y usar la letra "q" para mejorarla, 2 mejoras maximo.
<br><br>Seleccionar una torre y usar la letra "x" para venderla. Si la torre se compra y vende sin haber empezado la oleada esta devuelve el 100% del oro,
Si se coloco la torre y se comenzo una oleada la torre valdra solo el 75% a partir de ese momento</p>
</div>
<script>
var ctx = document.getElementById("ctx").getContext("2d");
var stats = document.getElementById("stats").getContext("2d");
var currentActiveWave = 0;
var totalWavesPassed = 1;
var globalTimer = (new Date()).getTime();
var fps = 10;
var C_HEIGHT = 500;
var C_WIDTH = 500;
var outBoundaries = C_WIDTH + 25; //el ancho y alto de la pantalla es 500, por eso fuera es un poco mas alla de 500
var selectedTower;
var typesOfMonsters = ["knight", "chomp" , "wizard", "dragon"];
</script>
<script src="js/player_ent.js"></script>
<script src="js/wave_ent.js"></script>
<!--<script src="js/sprite.js"></script>-->
<script src="js/enemy_ent.js"></script>
<script src="js/towers_ent.js"></script>
<script src="js/ghostTower_ent.js"></script>
<script src="js/bullet_ent.js"></script>
<script src="js/genMap_ent.js"></script>
<script src="js/docFunctions.js"></script>
<script src="js/drawFunctions.js"></script>
<script>
map1 = genMap();
waves = [];
enemies = [];
towers = [];
towersForStats = {};
iceTowers = [];
explotions = [];
gTower = ghostTower(-50,-50); //posx, posy
player = player();
/*
var imagen = new Image();
imagen.src = "img/monster2.png";
frameIndex = 0;
tickCount = 0;
ticksPerFrame = 100;
numberOfFrames = 4 ;
sprwidth = 160;
sprheight = 40;
*/
towerForStatsInit();
window.addEventListener('focus',setInterval(update,fps)); //update solo se debe ejecutar si la tab esta visible
function update(){
stats.clearRect(0,0,200,500);
drawStats(player.gold, player.lives);
drawTowerOptions(player.gold);
ctx.clearRect(0,0,500,500);
drawMap(map1);
drawPlayButton();
if (waves[currentActiveWave] != undefined && waves[currentActiveWave].active){
for (var key in towers)
{
towers[key].setUsed(); //Cuando arranca una oleada seteo todas las torres como "usadas" para que se vendan por el 75% de su valor en vez de 100%
}
if ( /*(globalTimer - waves[currentActiveWave].lastCreatedDate) > waves[currentActiveWave].spawnTime &&*/
waves[currentActiveWave].amountEnemies > waves[currentActiveWave].enemies.length && waves[currentActiveWave].keepCreating)
{
waves[currentActiveWave].generateEnemies();
waves[currentActiveWave].lastCreatedDate = globalTimer;
if ( waves[currentActiveWave].amountEnemies == waves[currentActiveWave].enemies.length){
waves[currentActiveWave].keepStopCreating();
//keepCreating va a pasar a false una vez que consegui generar todos los bichos y no va a crear mas
}
}
for(var key in waves[currentActiveWave].enemies)
{
if ( waves[currentActiveWave].enemies[key].direction == "horizontal" &&
typeof waves[currentActiveWave].enemies[key].nextX !== 'undefined')
{
(waves[currentActiveWave].enemies[key].nextX - waves[currentActiveWave].enemies[key].posX > 0)?
waves[currentActiveWave].enemies[key].posX += waves[currentActiveWave].enemies[key].spd :
waves[currentActiveWave].enemies[key].posX -= waves[currentActiveWave].enemies[key].spd;
if ( Math.abs(waves[currentActiveWave].enemies[key].nextX - waves[currentActiveWave].enemies[key].posX) <
waves[currentActiveWave].enemies[key].spd)
waves[currentActiveWave].enemies[key].posX = waves[currentActiveWave].enemies[key].nextX;
waves[currentActiveWave].enemies[key].nextX == waves[currentActiveWave].enemies[key].posX ?
waves[currentActiveWave].enemies[key].direction = "vertical":
waves[currentActiveWave].enemies[key].direction ;
}
else if ( waves[currentActiveWave].enemies[key].direction == "vertical" &&
typeof waves[currentActiveWave].enemies[key].nextY !== 'undefined')
{
(waves[currentActiveWave].enemies[key].nextY - waves[currentActiveWave].enemies[key].posY > 0)?
waves[currentActiveWave].enemies[key].posY += waves[currentActiveWave].enemies[key].spd :
waves[currentActiveWave].enemies[key].posY -= waves[currentActiveWave].enemies[key].spd;
if ( Math.abs(waves[currentActiveWave].enemies[key].nextY - waves[currentActiveWave].enemies[key].posY) <
waves[currentActiveWave].enemies[key].spd)
waves[currentActiveWave].enemies[key].posY = waves[currentActiveWave].enemies[key].nextY;
waves[currentActiveWave].enemies[key].nextY == waves[currentActiveWave].enemies[key].posY ?
waves[currentActiveWave].enemies[key].direction = "horizontal":
waves[currentActiveWave].enemies[key].direction ;
}
else;
if ( waves[currentActiveWave].enemies[key].posY == waves[currentActiveWave].enemies[key].nextY &&
waves[currentActiveWave].enemies[key].posX == waves[currentActiveWave].enemies[key].nextX &&
waves[currentActiveWave].enemies[key].movement + 1 < map1.x.length)
{
waves[currentActiveWave].enemies[key].movement++;
waves[currentActiveWave].enemies[key].posX = waves[currentActiveWave].enemies[key].nextX;
waves[currentActiveWave].enemies[key].posY = waves[currentActiveWave].enemies[key].nextY;
waves[currentActiveWave].enemies[key].nextX = map1.x[waves[currentActiveWave].enemies[key].movement+1];
waves[currentActiveWave].enemies[key].nextY = map1.y[waves[currentActiveWave].enemies[key].movement+1];
}
waves[currentActiveWave].enemies[key].updateHealthBarPosition();
waves[currentActiveWave].enemies[key].drawEntity();
if (waves[currentActiveWave].enemies[key].posY > outBoundaries || waves[currentActiveWave].enemies[key].posX > outBoundaries)
{
player.reduceLives(waves[currentActiveWave].enemies[key].damage);
waves[currentActiveWave].enemies.splice(key,1); //la funcion que borra el elemento tiene que estar a lo ultimo del for()
}
if(waves[currentActiveWave].enemies.length > 0 && waves[currentActiveWave].enemies[key].health <= 0){
player.addingMoney(waves[currentActiveWave].enemies[key].gold);
waves[currentActiveWave].enemies.splice(key,1); //la funcion que borra el elemento tiene que estar a lo ultimo del for()
}
} //end for()
if ( waves[currentActiveWave].enemies.length == 0)
{
waves[currentActiveWave].setActiveInactive();
currentActiveWave++;
totalWavesPassed++;
//Una vez que todos los enemigos son destruidos o cruzan el camino la wave actual se desactiva y se pasa a la siguiente que se
//va a activar con un nuevo click
if (waves.length >= 2)
{ //Si hay mas de 2 oleadas borro la 1ra y dejo la anterior. Se deja la anterior porque en base a los valores de los enemigos (Vida y daño) se va generar la nueva
//oleada con valores aumentados. ej:vida de enemigos de esta oleada = vida de enemigos de oleada anterior * 1.2
waves.splice(0,1);
currentActiveWave--;
for ( var index in waves)
{
waves[index].id = index;
}
}
}
} //end if para wave active
for ( var key in towers){
towers[key].usingNotUsing(); //la pongo en uso, por si el usuario la quiere borrar dentro de este for, que el sistema no lo deje
towers[key].drawEntity();
if( waves[currentActiveWave] != undefined)
{
for(var key2 in waves[currentActiveWave].enemies){
if(towers[key].detectEnemy(waves[currentActiveWave].enemies[key2]))
{
if ( towers[key].type != "iceTower" && new Date().getTime() - towers[key].lastAttack > towers[key].atkSpeed)
{
towers[key].shootBullet(waves[currentActiveWave].enemies[key2]);
towers[key].lastAttack = new Date().getTime();
}
}
for (var key4 in iceTowers){
if(iceTowers[key4].detectEnemy(waves[currentActiveWave].enemies[key2]))
{
waves[currentActiveWave].enemies[key2].slowDownBy(iceTowers[key4].bulletDamage);
break; //en el momento que reduzco la velocidad a un enemigo tengo que hacer un brake, ya que si sigo buscando puedo llegar a encontrar otra torre de hielo
//que no tienen al enemigo en rango y restauraria su velocidad
}
else
waves[currentActiveWave].enemies[key2].normalSpeed();
}
}
}
if ( towers[key].type != "iceTower")
//si la torre no es de hielo disparon normalmente
for(var key3 in towers[key].myBullets)
{
towers[key].myBullets[key3].persuitEnemy();
towers[key].myBullets[key3].drawEntity();
if(towers[key].myBullets[key3].collideWithEnemy())
{
towers[key].myBullets.splice(key3,1);
}
}
towers[key].usingNotUsing(); //la dejo en desuso, la funcion es un switch
}
if ( typeof gTower !== "undefined" && player.optionTowerSelected != "0"){
switch (player.optionTowerSelected){
case "1":
gTower.setRange(towersForStats.archer.range);
break;
case "2":
gTower.setRange(towersForStats.cannon.range);
break;
case "3":
gTower.setRange(towersForStats.iceTower.range);
break;
}
gTower.drawEntity();
}
globalTimer = (new Date()).getTime();
}
function towerForStatsInit(){
//estas torres se crean solo para poder tener los valores y mostrarlos en otros lugares
towersForStats = {
archer: tower(0, "archer", 0, 0, 75, "red", 4, 2 , 200, 250),
cannon: cannonTower(0, "cannon", 0, 0, 50, "orange", 4, 6 , 50 ,1000, 750),
iceTower: iceTower(0, "iceTower", 0, 0, 40, "#3399ff" /*Celeste hielo*/, 0, .8 ,0, 1000)
}
}
/*
function writeMessage(canvas, message) {
var context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
context.font = '18pt Calibri';
context.fillStyle = 'black';
context.fillText(message, 10, 450);
}
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
var canvas = document.getElementById('stats');
var context = canvas.getContext('2d');
canvas.addEventListener('mousemove', function(evt) {
var mousePos = getMousePos(canvas, evt);
var message = 'MP' + mousePos.x + ',' + mousePos.y;
writeMessage(canvas, message);
}, false);
*/
</script>
</body>
</html>