-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpaul.js
462 lines (422 loc) · 14.8 KB
/
paul.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
$(document).ready(function() {
$(document).everyTime(100, 'adjust', adjustPanes, 0);
preloadImages();
initializeControls();
initializeGame();
});
/*************************************************
*************Initializing Game Features*********
************************************************/
function initializeControls() {
$('#help').click(helpWindow);
$('#help_window').hide();
$('.node').click(constructTurret);
$('.turret').hide();
$('.construct_button').click(function(){selectType(this)});
$('#send_wave').click(generateWave);
}
function initializeGame() {
//Initial Resources
setResource('dereks', 100);
setResource('sacs', 100);
setResource('bravos', 100);
//Recurring gamewide timer for movement
$(document).everyTime(50, 'update', gameUpdate, 0);
$(document).attr('paused', false);
$(document).attr('selected', null);
//name, damage, dereks, sacs, bravos
var nodeData = [];
for(var i = 0; i < TURRETDATA.length; i++) {
var type = TURRETDATA[i][0];
var name = TURRETDATA[i][1] + " " + TURRETDATA[i][2];
nodeData[type] = [];
nodeData[type].push(name);
for(var j = 3; j <= 6; j++) {
nodeData[type].push(TURRETDATA[i][j]);
}
}
$(document).attr('node_data', nodeData);
//Set of enemy indexes to whether or not they are alive.
$(document).attr('enemy_count', 0);
$(document).attr('lifelist', ['dummy']);
$(document).attr('enemies', []);
$(document).attr('turrets', []);
$(document).attr('lives', 20);
$(document).attr('level', 1);
initializeBullets();
helpWindow();
}
function preloadImages() {
images = []
for(var i = 0; i < IMAGEDATA.length; i++) {
images[i] = new Image()
images[i].src = IMAGEDATA[i]
}
}
//Creates 32*4 precreated bullets for turrets to cycle through.
function initializeBullets() {
var bullets = [];
var turrets = $('.turret')
for(var i = 0; i < 32; i++) {
var round = [0]
$(turrets[i]).attr('turret_id', i);
for(var j = 0; j < 4; j++) {
var bullet = document.createElement('div');
$('#sky').append(bullet);
$(bullet).addClass('bullet');
$(bullet).hide();
round.push(bullet);
}
bullets.push(round);
}
$(document).attr('bullets', bullets);
}
/**************************************************
*************Code for reorganizing the layout********
***********************************************/
function adjustPanes() {
if(!$(document).attr('paused') && (window.innerHeight < 800 || window.innerWidth < 1050))
pauseGame('Window too Small. Please Resize. You might even consider holding CTRL and scrolling the mousewheel down', function(){return true;});
if($('#footer').css('top') != (window.innerHeight - 300) + 'px') {
$('#footer').css('top', (window.innerHeight - 250) + 'px');
$('#sky').css('height', (window.innerHeight - 600) + 'px');
$('#stage').css('top', (window.innerHeight - 500) + 'px');
}
if($('#footer').css('width') != (window.innerWidth) + 'px') {
$('#footer').css('width', window.innerWidth + 'px');
$('#sky').css('width', window.innerWidth + 'px');
$('#stage').css('width', window.innerWidth + 'px');
$('#middle_bar').css('left', parseInt((window.innerWidth - 720) / 2) + 'px');
$('#controls').css('left', parseInt((window.innerWidth - 500) / 2) + 'px');
$('#left_pane').css('width', parseInt((window.innerWidth - 700) / 2) + 90 + 'px');
$('#right_pane').css('width', parseInt((window.innerWidth - 700) / 2) + 90 + 'px');
$('#left_pane_content').css('left', parseInt((window.innerWidth - 700) / 2) - 450 + 'px');
}
}
/*****************************************
**********Code for controlling the game***********
*******************************************/
function gameUpdate() {
var enemies = $(document).attr('enemies');
for(var i = 0; i < enemies.length; i++) {
var result = move(enemies[i]);
updateHealthBar(enemies[i]);
if(!result) {
$(enemies[i]).remove();
enemies.splice(i, 1);
i--;
}
}
var turrets = $(document).attr('turrets');
for(var i = 0; i < turrets.length; i++) {
attack(turrets[i]);
}
}
function hasResource(resource, n) {
return $(document).attr(resource) >= n;
}
function setResource(resource, n) {
$(document).attr(resource, n);
$('#' + resource)[0].innerHTML = $(document).attr(resource);
}
function addResource(resource, n) {
$(document).attr(resource, n + $(document).attr(resource));
$('#' + resource)[0].innerHTML = $(document).attr(resource);
}
function selectType(dataObject) {
if(checkPause()) {
return false;
}
var nodeData = [];
if(dataObject == null) {
nodeData = ['No Selection', '', '', '', ''];
} else {
nodeData = $(document).attr('node_data')[$(dataObject).attr('type')];
}
var selectTable = $('#left_pane_content table td')
for(var i = 0; i < 5; i++) {
selectTable[i].innerHTML = nodeData[i];
}
if(dataObject == null) {
$('#selected_image').attr('src', 'images/no_paul.png');
$(document.attr('selected', null));
return;
}
if($(dataObject).is('.construct_button')) {
$('#destroy_turret').hide();
$('#turret_id_row').hide();
} else {
$('#destroy_turret').show();
$('#turret_id_row').show();
selectTable[5].innerHTML = $(dataObject).attr('turret_id');
$('#destroy_turret').attr('onclick', '').unbind('click');
$('#destroy_turret').click(function(){destroyTurret(dataObject);});
}
$('#selected_image').attr('src', 'images/select_' + $(dataObject).attr('type') + '.png');
$(document).attr('selected', $(dataObject).attr('type'));
}
function constructTurret() {
if(checkPause()) {
return;
}
var selected = $(document).attr('selected');
if(!selected) {
return;
}
if($(this.children[0]).css('display') != 'none') {
return;
}
if(!subtractResources($(document).attr('node_data')[selected])) {
return;
}
$(this).click(function(){selectType(turret);});
var turret = this.children[0];
$(turret).addClass(selected);
$(turret).show();
$(turret).attr('damage', $(document).attr('node_data')[selected][1]);
$(turret).attr('type', selected);
$(document).attr('turrets').push(turret);
}
function destroyTurret(turret) {
if(checkPause()) {
return false;
}
var turrets = $(document).attr('turrets');
for(var i = 0; i < turrets.length; i++) {
if(turrets[i] == turret) {
turrets.splice(i, 1);
$(turret).hide();
$(turret).removeClass($(turret).attr('type'));
selectType(null);
return true;
}
}
return false;
}
function subtractResources(necessary) {
var dereks = necessary[2];
var sacs = necessary[3];
var bravos = necessary[4];
if(!(hasResource('dereks', dereks) && hasResource('sacs', sacs) && hasResource('bravos', bravos))) {
return false;
}
addResource('dereks', -dereks);
addResource('sacs', -sacs);
addResource('bravos', -bravos);
return true;
}
function reward(target) {
var funds = parseInt($(target).attr('worth'));
addResource('dereks', funds);
addResource('sacs', funds);
addResource('bravos', funds);
}
function reduceLife() {
$(document).attr('lives', $(document).attr('lives') - 1);
$('#lives')[0].innerHTML = $(document).attr('lives');
if($(document).attr('lives') == 0) {
pauseGame('You just lost. You can keep playing though...', function(){});
}
}
/*********************************************
******************Combat*********************
*******************************************/
/******************
******Enemy*******
****************/
function generateWave() {
if(checkPause()) {
return;
}
var difficulty = $(document).attr('level');
difficulty = parseInt((Math.pow(difficulty * 0.9, 2) + 5 * difficulty) / 2);
var enemies = DIFFICULTIES.map(function(x){return x;});
while(difficulty > 0 && enemies.length > 0) {
var choice = parseInt(Math.random() * enemies.length);
if(enemies[choice][1] > difficulty) {
enemies.splice(choice, 1);
} else {
generateEnemy(enemies[choice]);
difficulty -= enemies[choice][1];
}
}
$('#wave_no')[0].innerHTML = $(document).attr('level');
$(document).attr('level', $(document).attr('level') + 1);
}
function generateEnemy(enemyData) {
if(checkPause()) {
return;
}
var enemy = document.createElement('div');
$(enemy).attr('x', parseInt(Math.random() * 200) + 2000);
$(enemy).attr('y', parseInt(Math.random() * 100));
$(enemy).css('left', $(enemy).attr('x') + 'px');
$(enemy).css('top', $(enemy).attr('y') + 'px');
$(enemy).attr('xVelocity', parseInt(-enemyData[3]));
$(enemy).attr('count', $(document).attr('enemy_count'));
$(enemy).attr('original-health', enemyData[2]);
$(enemy).attr('health', enemyData[2]);
$(enemy).attr('worth', enemyData[1]);
var healthBar = document.createElement('div');
$(healthBar).addClass('health_container');
$(enemy).append(healthBar);
var health = document.createElement('div');
$(health).addClass('health_bar');
$(healthBar).append(health);
$(document).attr('lifelist')[$(document).attr('enemy_count')] = enemy;
$(enemy).addClass(enemyData[0]);
$(enemy).addClass('enemy');
$('#sky').append(enemy);
$(document).attr('enemies').push(enemy);
$(document).attr("enemy_count", $(document).attr('enemy_count') + 1);
}
function move(enemy) {
var x = parseInt($(enemy).attr('x'));
var xVelocity = parseInt($(enemy).attr('xVelocity'));
x += xVelocity;
$(enemy).attr('x', x);
$(enemy).css('left', x + "px");
if(enemy.offsetLeft < -100 || parseFloat($(enemy).attr('health')) < 0) {
$(document).attr('lifelist')[$(enemy).attr('count')] = null;
if(enemy.offsetLeft < -100) {
reduceLife();
} else {
reward(enemy);
}
return false;
}
return true;
}
function updateHealthBar(enemy) {
var health = enemy.children[0].children[0];
$(health).css('width', 80 * parseInt($(enemy).attr('health')) / parseInt($(enemy).attr('original-health')) + 'px');
}
/****************
*****Turrets*****
****************/
function attack(turret) {
if($(document).attr('enemies').length == 0) {
return false;
}
var enemy = $(turret).attr('target');
if(!enemy || !inRange(turret, enemy) || !$(document).attr('lifelist')[$(enemy).attr('count')]) {
var enemies = $(document).attr('enemies');
for(var i = 0; i < enemies.length; i++) {
if(inRange(turret, enemies[i])) {
$(turret).attr('target', $(enemies[i]).attr('count'));
}
}
}
enemy = $(document).attr('lifelist')[$(turret).attr('target')];
if(!enemy || !inRange(turret, enemy)) {
return false;
}
sendProjectile(turret, enemy);
}
function inRange(turret, other) {
var yDistance = 100 - parseInt($(other).attr('y'));
var xDistance = turret.offsetLeft - other.offsetLeft;
var distance = Math.sqrt(yDistance * yDistance + xDistance * xDistance);
return distance <= 400;
}
function sendProjectile(turret, other) {
var round = $(document).attr('bullets')[parseInt($(turret).attr('turret_id'))];
var bullet = round[round[0]];
round[0] = (round[0] + 1) % 4;
var turretOffset = $(turret).offset();
var otherOffset = $(other).offset();
$(bullet).show();
var yDistance = otherOffset['top'] - turretOffset['top'];
var xDistance = otherOffset['left'] - (turretOffset['left'] - (4 * parseInt($(other).attr('xVelocity'))));
$(bullet).attr('x', turretOffset['left'] + parseInt((parseInt($(turret).css('width')) / 2)));
$(bullet).attr('y', turretOffset['top'] + parseInt((parseInt($(turret).css('height')) / 2)));
$(bullet).css('left', parseInt($(bullet).attr('x')) + 'px');
$(bullet).css('top', parseInt($(bullet).attr('y')) + 'px');
var xVelocity = parseInt(xDistance / 4);
var yVelocity = parseInt(yDistance / 4);
setTimeout(function() {moveProjectile(bullet, xVelocity, yVelocity, 3, turret, other)}, 10);
}
function moveProjectile(bullet, xVelocity, yVelocity, depth, turret, other) {
if(depth == 0) {
$(bullet).hide();
damage(turret, other);
return;
}
$(bullet).attr('x', parseInt($(bullet).attr('x')) + xVelocity);
$(bullet).attr('y', parseInt($(bullet).attr('y')) + yVelocity);
$(bullet).css('left', parseInt($(bullet).attr('x')) + 'px');
$(bullet).css('top', parseInt($(bullet).attr('y')) + 'px');
setTimeout(function() {moveProjectile(bullet, xVelocity, yVelocity, depth - 1, turret, other)}, 10);
}
function damage(turret, other) {
var damage = .25 * parseInt($(turret).attr('damage'));
var enemyHealth = parseFloat($(other).attr('health')) - damage;
$(other).attr('health', enemyHealth);
}
/***********************************************
***********************Misc*******************
********************************************/
function pauseGame(reason, onReturn) {
$(document).stopTime('update');
$(document).attr('paused', true);
$('#unpause')[0].innerHTML = reason + "... Press to Return to the Game";
$('#unpause').show();
$('#unpause').click(function() {returnToGame(onReturn);});
}
function returnToGame(onReturn) {
$('#unpause').hide();
onReturn();
$(document).everyTime(50, 'update', gameUpdate, 0);
$(document).attr('paused', false);
}
function checkPause() {
if($(document).attr('paused')) {
return true;
}
return false;
}
function helpWindow() {
if(checkPause()) {
return;
}
onReturn = function() {
$('#help_window').hide();
//$('#help').click(helpWindow);
};
pauseGame('You have opened the help window', onReturn);
//$('#help').click(function() {returnToGame(onReturn);});
$('#help_window').show();
}
/**********************************************
*********************Constants**********************
*********************************************/
var DIFFICULTIES =
('isaac 45 2500 8\n' +
'dyal 30 700 16\n' +
'will 22 450 13\n' +
'derek 14 280 14\n' +
'mah 7 200 15\n' +
'bravo 1 30 18' +
'').split("\n").map(
function(x){
var temp = x.split(" ")
return [temp[0], parseInt(temp[1]), parseInt(temp[2]), parseInt(temp[3])];
});
var TURRETDATA =
('basic_paul Basic Paul 10 30 30 30\n' +
'boring_paul Boring Paul 15 100 30 0\n' +
'squatty_paul Squatty Paul 19 50 100 50\n' +
'thinky_paul Thinky Paul 26 50 50 200\n' +
'sad_paul Sad Paul 35 100 100 100\n' +
'covert_paul Covert Paul 50 200 200 300\n' +
'gnome_paul Gnome Paul 65 300 300 300\n' +
'kissy_paul Kissy Paul 80 500 500 500\n' +
'chipmunk_paul Chipmunk Paul 169 1200 1200 1200').split("\n").map(function(x){ return x.split(" ") });
var IMAGEDATA =
('basic_paul.png boring_paul.png bravo.png chipmunk_paul.png covert_paul.png ' +
'derek.png dyal.png gnome_paul.png isaac.png kissy_paul.png mah.png ' +
'no_paul.png sad_paul.png select_basic_paul.png select_boring_paul.png ' +
'select_chipmunk_paul.png select_covert_paul.png select_gnome_paul.png ' +
'select_kissy_paul.png select_sad_paul.png select_squatty_paul.png ' +
'select_thinky_paul.png select_undercover_paul.png squatty_paul.png ' +
'thinky_paul.png undercover_paul.png will.png ').split(/ +/).map(function(x){ return "/paulville/images/" + x; });