-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
741 lines (653 loc) · 21.5 KB
/
main.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
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
// HTML5 Mr. Driller
// Authors:
// Nathan Hamal <[email protected]>
// Dylan Mikus <[email protected]>
// Global variables
//colors used for blocks.
// Everything in this array can be drilled.
var colors= ["red","blue","green","purple"];
function canDrill(block) {
return (colors.indexOf(block.type) > -1);
}
//maximum rows of blocks stored
//blocks dissappear if they are 15 above the bottom of the screen
var numRows = 15;
// The number of columns of blocks (x width)
var numColumns = 7;
// variables so keycodes are more transparent
var downarrow = 40;
var uparrow = 38;
var leftarrow = 37;
var rightarrow = 39;
var spacebar = 32;
var rKey = 82;
var inGame = false;
var introScreen = true;
var worldWidth = 420;
var score = 0;
var driller;
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var infoScreen = new Image();
infoScreen.src = "infoScreen.jpg";
infoScreen.onload = function(){
drawIntroScreen();
}
// A 2D array of blocks. Block at pos (0,0) is one below canvas display on
// left side. This is like cartesian plane coordinates
var blocks = [];
for(i=0;i<numColumns;i++){
blocks.push([]); // add second dimensional arrays to each index
}
// Sets up the world and draws objects on canvas
function main() {
// adding listeners to control driller
// Focusing canvas so it can register events
canvas.setAttribute('tabindex','0');
canvas.focus();
canvas.addEventListener('keydown', onKeyDown, false);
intro();
//setUpWorld(); // does not handle drawings of objects
// Creating timer to draw screen
var timerDelay = 50;
var intervalId = setInterval(onTimer, timerDelay);
}
function intro(){
window.introScreen= true;
}
function setUpWorld() {
addEmptyBlocks(2); // add empty blocks at the bottom of the screen
addBottomBlocks(5,0,0); // add blocks to bottom of screen, pushing them up
fillEmpty(); // fill the rest of the grid with empty blocks
driller = new Driller(3,5);
}
function gameOver(){
window.inGame=false;
drawGameOver();
}
// Stuff that happens every time the timer fires
function onTimer() {
if(window.inGame=== true){
drawDisplay(); // draws objects on screen
gravity();
if(driller.alive === true){
driller.breathe();
}else{
driller.deathTime();
}
blocks = animate(blocks);
// Check if Mr. Driller is in an air pocket
if (blocks[driller.column][driller.row].type==="air") {
blocks[driller.column][driller.row].type= "empty";
driller.airPocket();
}
blocks = animate(blocks);
}
}
//checks all things that can fall to see if they should be falling,
//then makes them fall
function gravity() {
//check if the driller should fall
if(blocks[driller.column][driller.row-1].type === "empty" ||
blocks[driller.column][driller.row-1].type === "air"){
if (driller.countdown === 0) {
addBottomBlocks(1,
.015,
//this argument is the probability of a durable block
//essentially this is the function from depth to
//difficulty, since durable blocks make it harder
Math.pow(driller.depth/100,2)/
(5*Math.pow((driller.depth+300)/100,2)));
driller.depth+=5;
if(window.blocks[driller.column][driller.row].type === "air"){
driller.airPocket();
blocks[driller.column][driller.row].type = "empty";
}
driller.resetCountdown();
} else {
driller.countdown -= 1;
}
}
var fallObj = blockGravity(blocks);
blocks = fallObj.blockGrid;
//check if driller was crushed
if(window.blocks[driller.column][driller.row].type !== "empty"
&& window.blocks[driller.column][driller.row].type !== "air"
&& driller.alive===true){
driller.kill();
}
}
function animate(blocks) {
var x = 0;
var y = 0;
// If block is in middle, shake left
// If block is left, shake right
function shakeBlock(block) {
blockOffset = 5;
if (block.xOffset >= 0) {
block.xOffset = -blockOffset;
} else if (block.xOffset < 0) {
block.xOffset = blockOffset;
}
return block;
}
for (y = 0; y < numRows; y++) {
for (x = 0; x < numColumns; x++) {
if (blocks[x][y].state === "shaking") {
if (blocks[x][y].countdown % 2 === 0) {
blocks[x][y] = shakeBlock(blocks[x][y]);
}
} else {
blocks[x][y].xOffset = 0;
}
}
}
return blocks;
}
// The player's dude
function Driller(column,row) {
var countdownFactor = 4;
this.countdown = countdownFactor;
this.column = column;
this.row = row;
this.lives = 2;
this.alive = true;
this.depth = 0;
this.air = 100;
this.timeDead = 0;
// Possibilities: left, right, up, down
this.drillDirection = "down";
// Receives input to move around digger
// Also does object collision detection
// Pretty sure this will only be called with dx or dy non-zero. Not both
this.move = function (dx, dy) {
if(this.alive === false){//prevent moving while driller is dead
return;
}
// Checks for object collision for moving
if(this.column+dx>=0 && this.column+dx<numColumns
&& this.row+dy>0 && this.row+dy < blocks[this.column].length
&& (blocks[this.column+dx][this.row+dy].type==="empty"
|| blocks[this.column+dx][this.row+dy].type==="air")){
this.column += dx;
if(blocks[this.column][this.row].type==="air"){
blocks[this.column][this.row].type="empty";
this.airPocket();
}
}
if (dx < 0) this.drillDirection = "left";
else if (dx > 0) this.drillDirection = "right";
else if (dy < 0) this.drillDirection = "down";
else if (dy > 0) this.drillDirection = "up";
}
this.airPocket = function(){
this.air = Math.min(this.air+30,100);
}
this.deathTime = function(){
this.timeDead +=1;
if(this.timeDead > 30){
this.timeDead =0;
this.revive();
}
}
this.breathe = function(){
this.air -= .07;
if(this.air<0) {
this.kill();
}
}
this.kill = function(){//kills the driller
this.alive= false;
this.lives--;
if(this.lives<0){
gameOver();
}
}
this.revive = function(){
for(var i= this.column-1;i<=this.column+1;i++){
if(i<0 || i>=numColumns)
continue;
for(var j = this.row; j<numRows;j++){
blocks[i][j].type="empty";
}
this.air =100;
this.alive=true;
}
}
this.fall = function(){
this.row--;
}
this.drill = function () {
var pos;
if(this.alive===false){
return;
}
if (this.drillDirection === "left")
pos = [this.column - 1, this.row];
else if (this.drillDirection === "right")
pos = [this.column + 1, this.row];
else if (this.drillDirection === "up")
pos = [this.column, this.row + 1];
else if (this.drillDirection === "down")
pos = [this.column, this.row - 1];
// Check that block is within the bounds of the grid,
// and disable player from drilling blocks that are currently falling
if (pos[0] >= 0 && pos[0] < numColumns
&& pos[1] >= 0 && pos[1] < numRows
&& blocks[pos[0]][pos[1]].state !== "falling") {
var toDrill = blocks[pos[0]][pos[1]];
// Checks if the thing we are drilling is a drillable block.
// Everything in colors can be drilled.
if (canDrill(toDrill)) {
// Get the group of blocks to be drilled
var drillGroup = getBlockGroup(blocks,
pos[0], pos[1], toDrill.type);
// Drill that group of blocks
drillGroup.forEach(function (point) {
blocks[point.x][point.y] = new Block("empty");
window.score+=1;
});
}else if(toDrill.type==="durable"){
toDrill.health--;
if(toDrill.health===0){
blocks[pos[0]][pos[1]] = new Block("empty");
this.air-= 10;
}
}
}
}
this.resetCountdown = function () {
this.countdown = countdownFactor;
}
}
//adds a line of empty blocks at the bottom
//used for initiating the screen
function addEmptyBlocks(depth){
for(var d=0; d<window.depth; d++){
for(var x=0; x<window.numColumns;x++){
// pushes a new item onto the beginning of the array
window.blocks[x].unshift(new Block("empty"));
}
if(blocks[x].length>numRows){
blocks[x].pop();
}
}
return blocks;
}
// Called whenever Mr. Driller moves down or whenever we want to add a new row
// of blocks to the bottom of the array
function addBottomBlocks(depth, airProbability, durableProbability){
var d;
for(d=0; d<depth; d++){
var x;
for(x=0; x<numColumns;x++){
// pushes a new item onto the beginning of the array
window.blocks[x].unshift(new Block(colors[Math.floor(Math.random()*colors.length)]));
if(Math.random()<airProbability){
blocks[x][0].type = "air";
}else if(Math.random()<durableProbability){
blocks[x][0].type = "durable";
}
if(blocks[x].length>numRows){
blocks[x].pop();
}
}
}
return blocks;
}
function fillEmpty() {
var x;
for (x=0; x < numColumns; x++) {
var y;
while (blocks[x].length < numRows) {
blocks[x].push(new Block("empty"));
}
}
}
function onKeyDown(event) {
var keycode = event.keyCode;
// Variables for where Mr. Driller moves
var dx = 0;
var dy = 0;
if (keycode === leftarrow) dx--
else if (keycode === rightarrow) dx++;
else if (keycode === downarrow) dy--;
else if (keycode === uparrow) dy++;
// Shouldn't be able to move up or down by keypresses.
if (dx !== 0 || dy !== 0) {
driller.move(dx, dy); // TODO: this is probably messed up
}
// Drilling stuff
if (keycode === spacebar && introScreen ===false) {
driller.drill();
}
if (keycode === spacebar && introScreen ===true) {
setUpWorld();
introScreen = false;
inGame= true;
}
// Restart
if (keycode === rKey
&& window.inGame ===false){
restartGame();
}
}
function restartGame(){
window.blocks = [[],[],[],[],[],[],[]];
setUpWorld();
window.score = 0;
window.depth = 0;
window.inGame = true;
}
///////// graphics and drawing stuff /////////
function drawScoreboard(width, height) {
ctx.fillStyle = "black";
ctx.fillRect(0,0,worldWidth,canvas.height);
ctx.fillRect(canvas.width - width,0,width,height);
ctx.fillStyle = "green";
drawRoundedRectangle(ctx,canvas.width - width + 5,
height/9 -30,
width-5,35,5);
ctx.fillStyle = "white";
ctx.font = "35px Arial";
ctx.fillText("LIVES ",
canvas.width - width + 10 , height/9);
ctx.textAlign="right";
ctx.fillText(""+driller.lives,
canvas.width - 10 , 2*height/9);
ctx.textAlign= "left";
ctx.fillStyle = "red";
drawRoundedRectangle(ctx,canvas.width - width + 5,
3*height/9 -30,
width-5,35,5);
ctx.fillStyle = "white";
ctx.fillText("DEPTH: ",
canvas.width - width + 10 , 3*height/9);
ctx.textAlign="right";
ctx.fillText(""+driller.depth+"ft",
canvas.width -10 , 4*height/9);
ctx.textAlign= "left";
ctx.fillStyle = "blue";
drawRoundedRectangle(ctx,canvas.width - width + 5,
5*height/9 -30,
width-5,35,5);
ctx.fillStyle = "white";
ctx.fillText("AIR: ",
canvas.width - width + 10 , 5*height/9);
drawRoundedRectangle(ctx,canvas.width - width + 10,
5.7*height/9,
width-20,20,5);
ctx.fillStyle= "blue";
drawRoundedRectangle(ctx,
(canvas.width - width + 15)+(1-driller.air/100)*(width - 30),
5.7*height/9 +5,
(width-30)*(driller.air/100),10,1);
ctx.fillStyle= "purple";
drawRoundedRectangle(ctx,canvas.width - width + 5,
7*height/9 -30,
width-5,35,5);
ctx.fillStyle = "white";
ctx.fillText("SCORE: ",
canvas.width - width + 10, 7*height/9);
ctx.textAlign="right";
ctx.fillText(""+window.score+"",
canvas.width -10 , 8*height/9);
ctx.textAlign= "left";
}
function drawGameOver(){
ctx.textAlign="left";
ctx.fillStyle= "rgba(0,0,0,.5)";
ctx.fillRect(0,0,canvas.width,canvas.height);
ctx.fill();
ctx.fillStyle= "rgba(255,255,255,.5)";
ctx.font = "60px Arial";
ctx.textAlign = "center";
ctx.fillText("GAME OVER", canvas.width/2, canvas.height/2);
ctx.font = "40px Arial";
ctx.fillText("Press R to play again", canvas.width/2, canvas.height/2 + 70);
}
function drawDriller(){
// Draws Mr. Driller body
ctx.beginPath();
ctx.fillStyle = "pink";
ctx.arc(driller.column*60+30,
canvas.height - driller.row*60+15, 15, 0, 2*Math.PI, true);
ctx.fill()
drawRoundedRectangle(ctx, driller.column*60+24,
canvas.height-driller.row*60+25,12,20,2);
ctx.strokeStyle = "pink";
ctx.lineWidth = 6;
ctx.beginPath();
ctx.moveTo(driller.column*60+14,
canvas.height-driller.row*60+60);
ctx.lineTo(driller.column*60+30,
canvas.height-driller.row*60+33);
ctx.lineTo(driller.column*60+46,
canvas.height-driller.row*60+60);
ctx.stroke();
ctx.closePath();
if(driller.alive === false){
ctx.strokeStyle = "black";
ctx.lineWidth = 4;
ctx.beginPath();
ctx.moveTo(driller.column*60+20,
canvas.height - driller.row*60+10);
ctx.lineTo(driller.column*60+40,
canvas.height - driller.row*60+20);
ctx.stroke();
ctx.moveTo(driller.column*60+20,
canvas.height - driller.row*60+20);
ctx.lineTo(driller.column*60+40,
canvas.height - driller.row*60+10);
ctx.stroke();
ctx.closePath();
ctx.strokeStyle = "pink";
}
// Draw Mr. Driller's drill
ctx.fillStyle = "#E01B6A";
var drillOffset = 15;
if (driller.drillDirection === "down") {
ctx.beginPath();
ctx.moveTo(driller.column*60+25,
canvas.height-driller.row*60+45);
ctx.lineTo(driller.column*60+30,
canvas.height-driller.row*60+60);
ctx.lineTo(driller.column*60+35,
canvas.height-driller.row*60+45);
ctx.fill();
}
else if (driller.drillDirection === "up") {
ctx.beginPath();
ctx.moveTo(driller.column*60+25,
canvas.height-driller.row*60+15);
ctx.lineTo(driller.column*60+30,
canvas.height-driller.row*60);
ctx.lineTo(driller.column*60+35,
canvas.height-driller.row*60+15);
ctx.fill();
}
else if (driller.drillDirection === "left") {
ctx.beginPath();
ctx.moveTo(driller.column*60+15,
canvas.height-driller.row*60+25);
ctx.lineTo(driller.column*60,
canvas.height-driller.row*60+30);
ctx.lineTo(driller.column*60+15,
canvas.height-driller.row*60+35);
ctx.fill();
}
else if (driller.drillDirection === "right") {
ctx.beginPath();
ctx.moveTo(driller.column*60+45,
canvas.height-driller.row*60+25);
ctx.lineTo(driller.column*60+60,
canvas.height-driller.row*60+30);
ctx.lineTo(driller.column*60+45,
canvas.height-driller.row*60+35);
ctx.fill(); }
}
function drawWorld() {
reset();
drawBlocks();
drawDriller();
}
// This is the drawing function that happens every time
function drawDisplay() {
if( window.introScreen){
drawIntroScreen();
}else{
drawScoreboard(canvas.width - worldWidth, canvas.height);
drawWorld();
}
}
function drawIntroScreen(){
ctx.drawImage(window.infoScreen, 0, 0, 600, 600, 0, 0, 600, 600);
}
// Just offshores (to China) the drawing of blocks and figuring out whether to
// connect blocks visually
function drawBlocks(){
for(column=0; column<numColumns; column++){
for(index=0; index<blocks[column].length;index++){
drawBlock(column,index,blocks[column][index].type);
}
}
}
// If blocks are adjacent and same color, connects them
function drawBlock(column,row,type){
function drawNormal(fillStyle) {
ctx.fillStyle = fillStyle;
var hasAdjacent = false;
//detects overlap
if(column>0 && blocks[column-1][row].type===type) {
drawRoundedRectangle(ctx,
(column-1)*60+5 + blocks[column][row].xOffset,
canvas.height-row*60+5,
110, 50, 5);
hasAdjacent = true;
}
if(row>0 && blocks[column][row-1].type===type) {
drawRoundedRectangle(ctx,
column*60+5 + blocks[column][row].xOffset,
canvas.height-row*60+5,
50, 110, 5);
hasAdjacent = true;
}
if(hasAdjacent===false) {
drawRoundedRectangle(ctx,
column*60+5 + blocks[column][row].xOffset,
canvas.height-row*60+5,
50, 50, 5);
}
}
function drawAir() {
ctx.fillStyle = "lightblue";
var radius = 12;
circle(ctx,
column*60 + (1.5 * radius) + blocks[column][row].xOffset,
canvas.height - (row-1)*60 - (1.5 * radius),
radius);
circle(ctx,
(column)*60 + 30 + blocks[column][row].xOffset,
canvas.height - (row)*60 + 30,
radius);
circle(ctx,
(column+1)*60 - (1.5 * radius) + blocks[column][row].xOffset,
canvas.height - (row)*60 + (1.5 * radius),
radius);
}
function drawDurable(block) {
var r = 122;
var g = 71;
var b = 20;
var a = 1 - (3 - block.health)*0.25;
ctx.fillStyle = rgbToString(r,g,b,a);
drawRoundedRectangle(ctx,
column*60+5 + blocks[column][row].xOffset,
canvas.height-row*60+5,
50, 50, 5);
var innerTopLeft = {"x": column*60+10 + blocks[column][row].xOffset,
"y": canvas.height-row*60+10};
ctx.fillStyle = rgbToString(r-20,g-20,b-20, a);
drawRoundedRectangle(ctx,
innerTopLeft.x,
innerTopLeft.y,
40, 40, 5);
var oldWidth = ctx.lineWidth;
ctx.fillStyle = rgbToString(r+10, g+10, b+10, a);
ctx.lineWidth = 5;
drawLine(innerTopLeft.x+5, innerTopLeft.y+5,
innerTopLeft.x + 35, innerTopLeft.y + 35);
drawLine(innerTopLeft.x+5, innerTopLeft.y + 35,
innerTopLeft.x + 35, innerTopLeft.y+5);
ctx.lineWidth = oldWidth;
}
//dont draw anything for empty blocks
if(type ==="empty")
return;
if(type==="blue") {
drawNormal("blue");
// drawDurable();
}
else if(type==="green") {
drawNormal("green");
}
else if(type==="red") {
drawNormal("red");
}
else if(type==="purple") {
drawNormal("purple");
}
// drawing air and durables should be different
// they don't connect
else if(type==="air") {
// ctx.fillStyle = "lightblue";
drawAir();
}
else if(type==="durable") {
drawDurable(blocks[column][row]);
}
}
function reset(){
ctx.fillStyle= "black";
ctx.fillRect(0,0,worldWidth,canvas.height);
}
function drawRoundedRectangle(ctx,x,y,width,height,radius){
ctx.beginPath();
ctx.moveTo(x,y+radius);
ctx.lineTo(x,y+height-radius);
ctx.quadraticCurveTo(x,y+height,x+radius,y+height);
ctx.lineTo(x+width-radius,y+height);
ctx.quadraticCurveTo(x+width,y+height,x+width,y+height-radius);
ctx.lineTo(x+width,y+radius);
ctx.quadraticCurveTo(x+width,y,x+width-radius,y);
ctx.lineTo(x+radius,y);
ctx.quadraticCurveTo(x,y,x,y+radius);
ctx.fill();
}
function circle(ctx, cx, cy, radius) {
ctx.beginPath();
ctx.moveTo(cx, cy);
ctx.arc(cx, cy, radius, 0, 2*Math.PI, true);
ctx.fill();
}
function drawLine(x1, y1, x2, y2) {
ctx.beginPath();
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}
function rgbToString(r, g, b,a) {
var retStr;
if (a === undefined) {
retStr = ("rgb(" + String(r) + ","
+ String(g) + ","
+ String(b) + ")");
} else {
retStr = ("rgba(" + String(r) + ","
+ String(g) + ","
+ String(b) + ","
+ String(a) + ")");
}
return retStr;
}
main();