-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.html
637 lines (583 loc) · 20.7 KB
/
game.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
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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body onload="startGame()" style="background-color: #565e61; color: white;">
<script>
var SPEED_INCREMENT = 8;
var SPEED_DECREMENT = SPEED_INCREMENT / 2;
var MAX_SPEED = SPEED_INCREMENT * 3;
var CURSOR_COUNTDOWN = 20;
var LEAF_HEIGHT = 100;
var LEAF_WIDTH = 45;
var POWERUP_STRENGTH = 3;
var wattImage = new Image();
var mongoImage = new Image();
var mongoImage2 = new Image();
var mongoImage3 = new Image();
var redgateImage = new Image();
var redgateBulletImage = new Image();
var backgroundImage = new Image();
var birstImage = new Image();
var birstLogoImage = new Image();
var birstHitImage = new Image();
var wattLoaded = false;
var mongoLoaded = false;
var mongoLoaded2 = false;
var mongoLoaded3 = false;
var redgateLoaded = false;
var redgateBulletLoaded = false;
var backgroundLoaded = false;
var birstLoaded = false;
var birstLogoLoaded = false;
var birstHitLoaded = false;
var canvas = document.createElement('canvas');
var context;
var canvasWidth = window.innerWidth - 25;
var canvasHeight = window.innerHeight - 100;
var wattWidth = 50;
var currentShotId = 0;
var currentEnemyId = 0;
var currentEnemies = [];
var currentShots = [];
var currentBombs = [];
var currentPowerup = null;
var drawInterval;
var spawnEnemyInterval;
var spawnPowerupInterval;
var timerInterval;
var wattSpeedX = 0;
var wattSpeedY = 0;
var shootInterval = 0;
var timer = 0;
var enemiesKilled = 0;
var totalEnemies = 0;
var lastBomb;
var isBombActive = false;
var bossKilled = false;
var bossSpawned = false;
var button = []; //Button map to keep track of which buttons are pressed
onkeydown = function(k) { // key down state handler
button[k.which - 32] = 1; // set key flag
},
onkeyup = function(k) { // key up state handler
button[k.which - 32] = 0; // clear key flag
}
function startGame() {
this.context = this.canvas.getContext("2d");
document.body.appendChild(canvas);
wattImage.src = "watt.jpg";
wattImage.onload = function () {
wattLoaded = true;
}
mongoImage.src = "mongo.png";
mongoImage.onload = function() {
mongoLoaded = true;
}
mongoImage2.src = "mongo2.png";
mongoImage2.onload = function() {
mongoLoaded2 = true;
}
mongoImage3.src = "mongo3.png";
mongoImage3.onload = function() {
mongoLoaded3 = true;
}
backgroundImage.src = "sqlbackground.jpg";
backgroundImage.onload = function() {
backgroundLoaded = true;
}
redgateImage.src = "redgate.png";
redgateImage.onload = function() {
redgateLoaded = true;
}
redgateBulletImage.src = "redgateBullet.png";
redgateBulletImage.onload = function() {
redgateBulletLoaded = true;
}
birstImage.src = "birst.jpg";
birstImage.onload = function() {
birstLoaded = true;
}
birstLogoImage.src = "birstLogo.png";
birstLogoImage.onload = function() {
birstLogoLoaded = true;
}
birstHitImage.src = "birstHit.jpg";
birstHitImage.onload = function() {
birstHitLoaded = true;
}
drawInterval = setInterval(drawAllEntities, 50);
spawnEnemyInterval = setInterval(spawnEnemiesIfNeeded, 500);
spawnPowerUpInterval = setInterval(spawnPowerUpIfNeeded, 1000);
timerInterval = setInterval(function() {
timer++;
}, 1000);
}
function drawAllEntities() {
gameArea.clear();
gameArea.draw();
// Handle user input
// button[0] = fire [space bar]
// button[5] = thrust left [left arrow]
// button[7] = thrust right [right arrow]
// button[6] = climb [up arrow]
// button[8] = dive [down arrow]
// button[35] = cursor [c key]
if (button[0]) {
if (shootInterval == 0) {
watt.shoot();
shootInterval = 3;
}
}
if (button[5]) {
wattSpeedX = wattSpeedX - SPEED_INCREMENT;
}
if (button[7]) {
wattSpeedX = wattSpeedX + SPEED_INCREMENT;
}
if (button[6]) {
wattSpeedY = wattSpeedY - SPEED_INCREMENT;
}
if (button[8]) {
wattSpeedY = wattSpeedY + SPEED_INCREMENT;
}
if (button[35]) {
watt.dropBomb();
}
//Cap the max speed
if (wattSpeedX > MAX_SPEED) {
wattSpeedX = MAX_SPEED;
}
if (wattSpeedX < -MAX_SPEED) {
wattSpeedX = -MAX_SPEED;
}
if (wattSpeedY > MAX_SPEED) {
wattSpeedY = MAX_SPEED;
}
if (wattSpeedY < -MAX_SPEED) {
wattSpeedY = -MAX_SPEED;
}
// Move Watt
watt.xPos = watt.xPos + wattSpeedX;
watt.yPos = watt.yPos + wattSpeedY;
if (watt.xPos < 0) {
watt.xPos = 0;
}
if (watt.xPos > canvasWidth - wattWidth) {
watt.xPos = canvasWidth - wattWidth;
}
if (watt.yPos < 0) {
watt.yPos = 0;
}
if (watt.yPos > canvasHeight - wattWidth) {
watt.yPos = canvasHeight - wattWidth;
}
watt.draw();
//Slow Watt down
if (wattSpeedX > 0) {
wattSpeedX = wattSpeedX - SPEED_DECREMENT > 0 ? wattSpeedX - SPEED_DECREMENT : 0;
}
else if (wattSpeedX < 0) {
wattSpeedX = wattSpeedX + SPEED_DECREMENT < 0 ? wattSpeedX + SPEED_DECREMENT : 0;
}
if (wattSpeedY > 0) {
wattSpeedY = wattSpeedY - SPEED_DECREMENT > 0 ? wattSpeedY - SPEED_DECREMENT : 0;
}
else if (wattSpeedY < 0) {
wattSpeedY = wattSpeedY + SPEED_DECREMENT < 0 ? wattSpeedY + SPEED_DECREMENT : 0;
}
//Decrement the shoot interval
if (shootInterval > 0) {
shootInterval = shootInterval - 1;
}
//Draw everything
for(var i = 0; i < this.currentShots.length; i++) {
currentShots[i].draw();
}
for(var i =0; i < this.currentEnemies.length; i++) {
currentEnemies[i].draw();
currentEnemies[i].doMove();
}
for(var i = 0; i < this.currentBombs.length; i++) {
currentBombs[i].draw();
}
if (currentPowerup != null) {
currentPowerup.draw();
currentPowerup.checkStatus();
}
}
var gameArea = {
draw: function () {
canvas.width = canvasWidth;
canvas.height = canvasHeight;
canvas.style.position = "absolute";
canvas.style.border = "1px solid";
if (backgroundLoaded) {
context.drawImage(backgroundImage, 0, 0, canvasWidth, canvasHeight);
}
document.getElementById("mongosKilled").innerHTML = enemiesKilled + "/" + totalEnemies;
document.getElementById("mongosKilledPercentage").innerHTML = totalEnemies > 0 ? (((enemiesKilled / totalEnemies).toFixed(2) * 100) + '%') : '0%';
document.getElementById("timeOnSoapBox").innerHTML = timer;
if (lastBomb != null && timer - lastBomb < 20) {
document.getElementById("cursorStatus").innerHTML = 'Full Table Scan (' + (CURSOR_COUNTDOWN - (timer - lastBomb)) + ' seconds remaining)';
}
else {
document.getElementById("cursorStatus").innerHTML = 'Available';
}
if (currentPowerup != null && currentPowerup.activated) {
document.getElementById("redgateStatus").innerHTML = 'Trial License (' + (currentPowerup.endTime - timer) + ' seconds remaining)';
}
else {
document.getElementById("redgateStatus").innerHTML = 'License Expired';
}
},
drawGameOver: function() {
context.fillStyle = 'white';
context.font = '25pt Calibri';
wrapText(context, "Transaction (Process ID 'Watt') was deadlocked with another process and has been chosen as the deadlock victim. Watt fought valiantly, but alas - he could only protect SQL Server for " + timer + " seconds. He was able to fight off " + enemiesKilled + " MongoDB instances but it's neverending list of features and infinite scalability eventually overwhelmed him. Webscale Complete! Do you want to play again? Yes(y), No(n), Maybe(m)?", 100, 100, canvasWidth - 100, 100);
window.addEventListener("keypress", function(e){
console.log(e.keyCode);
if(e.keyCode === 121) { //Y
location.reload();
}
else if(e.keyCode === 110) { //N
window.location.href = "http://www.mongodb.com";
}
else if(e.keyCode === 109) { //M
window.location.href = "http://yesnomaybe.info/?question=Should%20we%20switch%20to%20MongoDB?";
}
});
},
clear: function () {
context.clearRect(0, 0, canvas.width, canvas.height);
}
}
var watt = {
yPos: this.canvas.height / 2,
xPos: 10,
draw: function() {
if(wattLoaded) {
context.drawImage(wattImage, watt.xPos, watt.yPos, wattWidth, wattWidth);
}
},
shoot: function() {
var shot = {
id: currentShotId++,
xPos : watt.xPos + (wattWidth / 2) + 10,
yPos : watt.yPos + (wattWidth / 2),
power : (currentPowerup != null && currentPowerup.activated) ? POWERUP_STRENGTH : 1 ,
height : (currentPowerup != null && currentPowerup.activated) ? 25 : 10,
draw: function() {
if (this.power == POWERUP_STRENGTH) {
if(redgateBulletLoaded) {
context.drawImage(redgateBulletImage, this.xPos, this.yPos, 32, 25);
}
}
else {
context.fillStyle = 'white';
context.font = '12pt Calibri';
context.fillText("DACPAC", this.xPos, this.yPos);
}
this.xPos = this.xPos + 50;
if(this.xPos > canvasWidth) {
removeBullet(this);
}
for(var i =0; i < currentEnemies.length; i++) {
if (currentEnemies[i].xPos < this.xPos + this.height &&
currentEnemies[i].xPos + LEAF_WIDTH > this.xPos &&
currentEnemies[i].yPos < this.yPos + this.height &&
LEAF_HEIGHT + currentEnemies[i].yPos > this.yPos) {
enemyHit(i, this.power);
removeBullet(this);
}
}
}
}
currentShots.push(shot);
},
dropBomb: function() {
if (lastBomb == null || timer - lastBomb > CURSOR_COUNTDOWN) {
lastBomb = timer;
for(var x = -20; x <= 20; x = x + 5) {
for(var y = -20; y <= 20; y = y + 5) {
currentBombs.push(createBomb(x,y));
}
}
enemiesKilled += currentEnemies.length;
if(!bossSpawned || bossKilled) {
currentEnemies = [];
}
}
}
}
function createBomb(movementX, movementY) {
return bomb = {
xPos: watt.xPos,
yPos: watt.yPos,
movementX: movementX,
movementY: movementY,
bombTime: timer,
draw: function() {
isBombActive = true;
//Clear bomb from array
if(timer - this.bombTime > 5) {
currentBombs = [];
isBombActive = false;
}
context.fillStyle = 'white';
context.font = '16pt Calibri';
context.fillText("CURSOR", this.xPos, this.yPos);
this.xPos = this.movementX + this.xPos;
this.yPos = this.movementY + this.yPos;
}
}
}
function createMongoEnemy() {
return mongoEnemy = {
id: currentEnemyId++,
health: 3,
xPos: canvasWidth + 10,
yPos: Math.floor((Math.random() * canvasHeight) + 1),
speed: Math.floor(Math.random() * (30 - 10 + 1)) + 10 + (Math.floor(timer)),
ySpeed: Math.floor(Math.random() * (20)) * (Math.floor(Math.random() * (2)) == 0 ? -1 : 1),
draw: function () {
if (this.health == 3) {
if(mongoLoaded) {
context.drawImage(mongoImage, this.xPos, this.yPos, LEAF_WIDTH, LEAF_HEIGHT);
}
}
else if (this.health == 2) {
if(mongoLoaded2) {
context.drawImage(mongoImage2, this.xPos, this.yPos, LEAF_WIDTH, LEAF_HEIGHT);
}
}
else {
if(mongoLoaded3) {
context.drawImage(mongoImage3, this.xPos, this.yPos, LEAF_WIDTH, LEAF_HEIGHT);
}
}
},
doMove: function() {
if(this.xPos < 0) {
var index = currentEnemies.indexOf(this);
if(index != -1) {
currentEnemies.splice(index, 1);
}
}
//Check if Watt was destroyed
if (this.xPos < watt.xPos + wattWidth &&
this.xPos + LEAF_WIDTH > watt.xPos &&
this.yPos < watt.yPos + wattWidth &&
LEAF_HEIGHT + this.yPos > watt.yPos) {
gameOver();
}
this.xPos = this.xPos - this.speed;
this.yPos += this.ySpeed;
if (this.yPos < 0) {
this.yPos = 0;
this.ySpeed = -this.ySpeed;
}
else if (this.yPos > (canvasHeight - LEAF_HEIGHT)) {
this.yPos = canvasHeight - LEAF_HEIGHT;
this.ySpeed = -this.ySpeed;
}
}
}
}
function createBirstBoss() {
return birstBoss = {
id: currentEnemyId++,
health: 20,
xPos: canvasWidth - 300,
yPos: 100,
height: 200,
width: 300,
lastBomb: null,
moveDown: true,
moveUp: false,
draw: function() {
if(this.hit && birstHitLoaded) {
context.drawImage(birstHitImage, this.xPos, this.yPos, this.width, this.height);
this.hit = false;
} else if(birstLoaded) {
context.drawImage(birstImage, this.xPos, this.yPos, this.width, this.height);
}
if(isHit(this.xPos, this.yPos, this.width, this.height, watt.xPos, watt.yPos, wattWidth, wattWidth)) {
gameOver();
}
if(this.yPos + this.height > canvasHeight && !this.moveUp) {
this.moveDown = false;
this.moveUp = true;
} else if(this.yPos < 0 && !this.moveDown) {
this.moveDown = true;
this.moveUp = false;
} else if(this.moveDown == true) {
this.yPos = this.yPos + 20;
} else if(this.moveUp == true) {
this.yPos = this.yPos - 20;
}
},
doMove: function() {
//Shotgun like burst towards player
if(currentShots.length < 20) {
currentShots.push(createBirstShot(this.xPos, this.yPos + 50));
currentShots.push(createBirstShot(this.xPos, this.yPos));
currentShots.push(createBirstShot(this.xPos, this.yPos - 50));
}
}
}
}
function createBirstShot (currentX, currentY) {
//Shoot towards player
var shot = {
id: currentShotId++,
xPos: currentX,
yPos: currentY,
width: 50,
height: 50,
timeFired: timer,
lengthPath: null,
speedX: null,
speedY: null,
draw: function () {
if(birstLogoLoaded) {
context.drawImage(birstLogoImage, this.xPos, this.yPos, this.width, this.height);
}
if(isHit(this.xPos, this.yPos, this.width, this.height, watt.xPos, watt.yPos, wattWidth, wattWidth)) {
gameOver();
}
if(this.xPos > canvasWidth || this.xPos < 0 || this.yPos > canvasHeight || this.yPos < 0) {
removeBullet(this);
}
this.xPos = this.xPos + this.speedX;
this.yPos = this.yPos + this.speedY;
}
}
shot.lengthPath = Math.sqrt((watt.xPos - shot.xPos)*(watt.xPos - shot.xPos) + (watt.yPos - shot.yPos)*(watt.yPos - shot.yPos));
shot.speedX = (watt.xPos - shot.xPos) / shot.lengthPath * 50;
shot.speedY = (watt.yPos - shot.yPos) /shot.lengthPath * 50;
return shot;
}
function createPowerup() {
return powerUp = {
xPos: Math.floor((Math.random() * (canvasWidth-200)) + 100),
yPos: Math.floor((Math.random() * (canvasHeight-200)) + 100),
endTime: timer + 5,
visible: true,
activated: false,
draw: function () {
if(redgateLoaded && this.visible) {
context.drawImage(redgateImage, this.xPos, this.yPos, 140, 70);
}
},
checkStatus: function() {
if (this.visible &&
this.xPos < watt.xPos + wattWidth &&
this.xPos + 140 > watt.xPos &&
this.yPos < watt.yPos + wattWidth &&
70 + this.yPos > watt.yPos) {
this.visible = false;
this.activated = true;
this.endTime = timer + 5;
}
else {
if (this.endTime <= timer) {
currentPowerup = null;
console.log('powerup expired - ' + timer);
}
}
}
}
}
function removeBullet(currentShot) {
currentShots.splice(currentShots.indexOf(currentShot), 1);
}
function enemyHit(enemyIndex, shotPower) {
if(bossSpawned && !bossKilled) {
currentEnemies[enemyIndex].hit = true;
}
currentEnemies[enemyIndex].health = currentEnemies[enemyIndex].health - shotPower;
if(currentEnemies[enemyIndex].health <= 0) {
currentEnemies.splice(enemyIndex, 1);
enemiesKilled += 1;
if(bossSpawned && !bossKilled) {
bossSpawned = false;
bossKilled = true;
}
}
}
function spawnEnemiesIfNeeded() {
if (this.currentEnemies.length < 40 && !isBombActive && (timer < 30 || bossKilled)) {
var mongoEnemy = createMongoEnemy();
this.currentEnemies.push(mongoEnemy);
totalEnemies += 1;
} else if(timer > 30 && !bossKilled && !bossSpawned) {
this.currentEnemies = [];
this.currentEnemies.push(createBirstBoss());
bossSpawned = true;
}
}
function spawnPowerUpIfNeeded() {
if (currentPowerup == null) {
if (Math.floor(Math.random() * 6) == 1) { //1 in 6 chance to generate a power up
currentPowerup = createPowerup();
}
}
}
function gameOver() {
clearInterval(drawInterval);
clearInterval(spawnEnemyInterval);
clearInterval(timerInterval);
setInterval(function () {
gameArea.clear();
gameArea.drawGameOver();
}, 500)
}
function isHit(xPos1, yPos1, width1, height1, xPos2, yPos2, width2, height2) {
if (xPos1 < xPos2 + width2 &&
xPos1 + width1 > xPos2 &&
yPos1 < yPos2 + height2 &&
height1 + yPos1 > yPos2) {
return true;
}
return false;
}
function wrapText(context, text, x, y, maxWidth, lineHeight) {
var words = text.split(' ');
var line = '';
for(var n = 0; n < words.length; n++) {
var testLine = line + words[n] + ' ';
var metrics = context.measureText(testLine);
var testWidth = metrics.width;
if (testWidth > maxWidth && n > 0) {
context.fillText(line, x, y);
line = words[n] + ' ';
y += lineHeight;
}
else {
line = testLine;
}
}
context.fillText(line, x, y);
}
</script>
<div style="height:80px">
<div style="float: left; width: 33%;">
<b>Score</b>
<div>Time on Soap Box: <span id='timeOnSoapBox' style="font-weight:bold">0</span><span style="font-weight:bold"> seconds</span></div>
<div>MongoDB Instances Killed: <span id='mongosKilled' style="font-weight:bold">0/0</span></div>
<div>MongoDB Kill Rate: <span id='mongosKilledPercentage' style="font-weight:bold">0%</span></div>
</div>
<div style="display: inline-block; width: 33%;">
<div style='font-size:40px;'>SQL WARS</div>
</div>
<div style="float: right; width: 33%;">
<b>Controls</b>
<div>Arrow keys to move, Space to shoot, C to run a cursor</div>
<div>Cursor Status: <span id='cursorStatus' style="font-weight:bold">Available</span></div>
<div>Redgate Status: <span id='redgateStatus' style="font-weight:bold">License Expired</span></div>
</div>
</div>
</body>
</html>