-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrenderer.js
1271 lines (1146 loc) · 50.9 KB
/
renderer.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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @constructor
*/
function Renderer(sceneryCanvas, overlayCanvas, background, cover, spriteImg, textCanvas, game, showTrapOnHover)
{
this.game = game;
this.sceneryCanvas = sceneryCanvas;
this.overlayCanvas = overlayCanvas;
this.textCanvas = textCanvas;
this.sceneryContext = sceneryCanvas.getContext("2d");
this.overlayContext = overlayCanvas.getContext("2d");
this.textContext = textCanvas.getContext("2d");
this.background = background;
this.cover = cover;
this.coverDown = false;
this.showTrapOnHover = showTrapOnHover; // true to show the trap upon hovering (mouse), false not to show it (touch screen)
this.scrollingInProgress = 0; // -1 or 1 for direction, used only to draw arrows (smaller upon scrolling)
this.windowLayout = {
pixelRatio : 1,
playArea : [],
textArea : [],
toolBar : [],
controlBar : [],
titleScreen : []
};
// create a mirrored version of the sprite sheet
this.spriteSheet = document.createElement("canvas");
this.spriteSheet.width=300; //spriteImg.width*2;
this.spriteSheet.height=600; // spriteImg.height+40;
var bufferContext = this.spriteSheet.getContext('2d');
bufferContext.drawImage(spriteImg, 0, 0);
bufferContext.scale(-1, 1);
bufferContext.drawImage(spriteImg, 0, 0, 140, 200, -140*2, 0, 140, 200);
bufferContext.drawImage(spriteImg, 0, 300, 150, 240, -150*2, 300, 150, 240);
bufferContext.scale(-1, 1);
// add icons background to sprite sheet
bufferContext.fillStyle = "#22F";
bufferContext.fillRect(1,561,46,38);
bufferContext.fillStyle = "#008";
bufferContext.fillRect(49,561,46,38);
bufferContext.fillStyle = "#C62";
bufferContext.fillRect(97,561,46,38);
bufferContext.fillStyle = "#620";
bufferContext.fillRect(145,561,46,38);
var gradient = this.overlayContext.createLinearGradient(0,560,0,590);
gradient.addColorStop(0, "rgba(255,255,255,.6)");
gradient.addColorStop(1, "rgba(255,255,255,0)");
bufferContext.fillStyle = gradient;
bufferContext.fillRect(4,563,40,34);
bufferContext.fillRect(52,563,40,34);
bufferContext.fillRect(100,563,40,34);
bufferContext.fillRect(148,563,40,34);
this.sceneryCanvas.height=256;
this.sceneryCanvas.width=1000;
this.sceneryOffsetX = 0;
this.textCanvas.height=64;
this.resizeWindow(); // define the appropriate pixel zoom for the play area
this.frameCount = 0;
this.animationStartFrame = 0; // used to animate main screen
this.titleZoomFactor = 1;
this.smoke = [];
// draw the title on a separate canvas, to simplify the animation on the main screen
this.titleBuffer=document.createElement("canvas");
this.titleBuffer.width=1000;
this.titleBuffer.height=256;
/*
this.sceneryContext.imageSmoothingEnabled = false;
this.sceneryContext.mozImageSmoothingEnabled = false;
this.sceneryContext.oImageSmoothingEnabled = false;
this.sceneryContext.webkitImageSmoothingEnabled = false;
this.overlayContext.imageSmoothingEnabled = false;
this.overlayContext.mozImageSmoothingEnabled = false;
this.overlayContext.oImageSmoothingEnabled = false;
this.overlayContext.webkitImageSmoothingEnabled = false;
*/
}
Renderer.prototype = {
/**
* Returns the bitmap buffer (2D context image data) associated to the scenery context
* Used by the game engine to test for collisions and modify (diggers, explosions, stairs).
*/
getSceneryImageData : function() {
return this.sceneryContext.getImageData(0, 0, this.sceneryCanvas.width, this.sceneryCanvas.height);
},
/**
* Handler for global window resize event, also called once at init time
* Defines the zoom factor for the canvases and (re)aligns everything
* Zoom level is the largest integer so that 320 (game height) times zoom level fits vertically in window
*/
resizeWindow : function() {
var oldWidth = this.overlayCanvas.width;
// recompute zoom level
this.windowLayout.pixelRatio = Math.max(1, Math.floor(window.innerHeight/320));
var iconWidth = 48, iconHeight = 40, margin = 8, textHeight = 16; // icon size, in unzoomed pixels
var overlayWidth = Math.ceil(window.innerWidth/this.windowLayout.pixelRatio);
this.windowLayout.textArea = [272, 272, 272, "center", 5, .7*overlayWidth, overlayWidth-5, 272];
this.windowLayout.toolBar = [0, 280, 6*iconWidth, iconHeight, iconWidth, 0];
this.windowLayout.controlBar = [Math.ceil(window.innerWidth/this.windowLayout.pixelRatio)-4*iconWidth, 280, 4*iconWidth, iconHeight, iconWidth, 0];
this.splitIconLine = false;
if (window.innerWidth < (iconWidth*this.windowLayout.pixelRatio*10)) // 6 icons for traps, 4 for game controls
{ // not enough room to show all icons on one line
this.windowLayout.pixelRatio = Math.max(1, Math.floor(window.innerHeight/(320+margin+iconHeight+textHeight)));
this.splitIconLine = true;
overlayWidth = Math.ceil(window.innerWidth/this.windowLayout.pixelRatio);
this.windowLayout.textArea = [272, 288, 288, "left", 5, 5, overlayWidth-5, 272];
this.windowLayout.toolBar = [0, 296, 6*iconWidth, iconHeight, iconWidth, 0];
this.windowLayout.controlBar = [Math.ceil(window.innerWidth/this.windowLayout.pixelRatio)-4*iconWidth, 344, 4*iconWidth, iconHeight, iconWidth, 0];
}
var overlayHeight = Math.ceil(window.innerHeight/this.windowLayout.pixelRatio);
this.windowLayout.playArea = [0, 0, overlayWidth, 256];
this.windowLayout.titleScreen = [overlayWidth, 256, 1];
if (window.innerHeight<640 && window.innerHeight>478) {
// special layout for FirefoxOS devices (Flame, ZTE Open C : 480 px ; Revolution : 540 px)
// to avoid falling back to a game area 320px high and a big black bar on the bottom
this.windowLayout.pixelRatio = 2;
overlayWidth = Math.ceil(window.innerWidth/this.windowLayout.pixelRatio);
overlayHeight = Math.ceil(window.innerHeight/this.windowLayout.pixelRatio);
this.windowLayout.playArea = [iconWidth, 0, overlayWidth-2*iconWidth, Math.min(256, overlayHeight)];
this.windowLayout.textArea = [16, 32, 16, "left", iconWidth+5, iconWidth+5, overlayWidth-iconWidth-5, overlayHeight-48];
this.windowLayout.toolBar = [0, 0, iconWidth, 6*iconHeight, 0, iconHeight];
this.windowLayout.controlBar = [overlayWidth-iconWidth, 0, iconWidth, 4*iconHeight, 0, iconHeight];
this.windowLayout.titleScreen = [overlayWidth, overlayHeight-64, 1];
}
this.windowLayout.titleScreen[2] = Math.min(this.windowLayout.titleScreen[1]/256, this.windowLayout.titleScreen[0]/500);
// set text canvas real size, rendered size and position
this.textCanvas.width = overlayWidth;
this.textCanvas.style.width = (overlayWidth*this.windowLayout.pixelRatio)+"px";
this.textCanvas.style.height = (64*this.windowLayout.pixelRatio)+"px";
this.textCanvas.style.bottom = (512-Math.min(window.innerHeight, 640))+"px";
// set overlay canvas real size
// At least 270 px, even if this means overflowing offscreen, to draw water at the bottom
overlayHeight = Math.max(overlayHeight, 270);
this.overlayCanvas.height=overlayHeight;
this.overlayCanvas.width=overlayWidth;
var overflowOffsetY = this.windowLayout.playArea[3]-256;
this.sceneryCanvas.style.top = (this.windowLayout.pixelRatio*overflowOffsetY)+"px";
// and rendered size for both canvases and background
this.overlayCanvas.style.width=(overlayWidth*this.windowLayout.pixelRatio)+"px";
this.overlayCanvas.style.height=(overlayHeight*this.windowLayout.pixelRatio)+"px"; // keep a margin to draw water
this.sceneryCanvas.style.width=(1000*this.windowLayout.pixelRatio)+"px";
this.background.style.height=this.sceneryCanvas.style.height=(256*this.windowLayout.pixelRatio)+"px";
this.cover.style.height=(258*this.windowLayout.pixelRatio)+"px";
this.cover.style.top = this.coverDown?"0px":(-322*this.windowLayout.pixelRatio)+"px";
// resize and recenter cover image to match
var endImageContainer = this.cover.firstChild;
endImageContainer.style.top=(this.windowLayout.pixelRatio*88)+"px";
endImageContainer.style.width=(this.windowLayout.pixelRatio*160)+"px";
endImageContainer.style.height=(this.windowLayout.pixelRatio*80)+"px";
endImageContainer.style.marginLeft=(-this.windowLayout.pixelRatio*80)+"px";
// set scrolling bounds + adjust location to keep the view center constant
this.minSceneryOffsetX = this.windowLayout.playArea[0]+this.windowLayout.playArea[2]-this.sceneryCanvas.width; // offset when scrolling to far right
this.maxSceneryOffsetX = this.windowLayout.playArea[0];
this.game.layoutChanged(this.windowLayout);
var newOffset = this.sceneryOffsetX+(overlayWidth-oldWidth)/2;
if (this.game.state == 0) {
newOffset = Math.min(0, (overlayWidth-1000)/2);
}
this.scrollScenery(newOffset, true);
},
/**
* Scroll the scenery (and whole playing area) laterally.
* Scroll is checked against bounds.
* Controls is informed to translate into world coordinates
* @param dx pixel count, in canvas coordinates (e.g. zoomed)
* @param absolute true means dx is an absolute value (scroll to dx), false for a relative value (scroll by dx)
*/
scrollScenery : function(dx, absolute) {
if (absolute) {
this.sceneryOffsetX = 0;
} else {
this.scrollingInProgress = (dx<0?1:-1);
}
this.game.controls.onHScroll(this.sceneryOffsetX = Math.floor(Math.min(this.maxSceneryOffsetX, Math.max(this.sceneryOffsetX+dx, this.minSceneryOffsetX))));
this.sceneryCanvas.style.left = (this.windowLayout.pixelRatio*this.sceneryOffsetX)+"px";
},
/**
* Draw text on the text canvas
* @param text The text to write
* @param x X-coordinate of the text, left/center/right depending on the textAlign property of the canvas
* @param x Y-coordinate of the text
*/
drawShadedText : function(text, x, y)
{
this.textContext.shadowOffsetX = -1;
this.textContext.shadowOffsetY = -1;
this.textContext.fillText(text, x, y);
this.textContext.shadowOffsetX = 2;
this.textContext.shadowOffsetY = 2;
this.textContext.fillText(text, x, y);
},
/**
* Draw text on the overlay canvas
* @param text The text to write
* @param x X-coordinate of the text, left/center/right depending on the textAlign property of the canvas
* @param x Y-coordinate of the text
*/
drawShadedTextOnOverlay : function(text, x, y)
{
this.overlayContext.shadowOffsetX = -1;
this.overlayContext.shadowOffsetY = -1;
this.overlayContext.fillText(text, x, y);
this.overlayContext.shadowOffsetX = 2;
this.overlayContext.shadowOffsetY = 2;
this.overlayContext.fillText(text, x, y);
},
/**
* Draw balloons (trap) on the overlay canvas
* (either on the play area or icon bar)
* @param x x-coordinate of the center of the image
* @param y y-coordinate of the bottom of the image
* @param timer animation frame
*/
drawBalloons : function(x, y, timer) {
this.overlayContext.drawImage(this.spriteSheet, 59, 135, 12, 17, Math.floor(x-11+4*Math.sin(timer/30)), y-32, 12, 17);
this.overlayContext.drawImage(this.spriteSheet, 47, 135, 12, 17, Math.floor(x+6+3*Math.sin(1+timer/28)), y-33, 12, 17);
this.overlayContext.drawImage(this.spriteSheet, 1, 401, 12, 17, Math.floor(x-4+3*Math.sin(2+timer/25)), y-31, 12, 17);
this.overlayContext.lineWidth=1;
this.overlayContext.strokeStyle = "#410";
this.overlayContext.beginPath();
this.overlayContext.moveTo(x, y);
this.overlayContext.lineTo(Math.floor(x-6+4*Math.sin(timer/30)), y-17);
this.overlayContext.moveTo(x, y);
this.overlayContext.lineTo(Math.floor(x+11+3*Math.sin(1+timer/28)), y-18);
this.overlayContext.moveTo(x, y);
this.overlayContext.lineTo(Math.floor(x+1+3*Math.sin(2+timer/25)), y-16);
this.overlayContext.stroke();
},
/**
* Draw both scenery and overlay canvas
*/
drawMain : function() {
this.overlayContext.clearRect(0, 0, this.overlayCanvas.width, this.overlayCanvas.height);
if (this.coverDown && (this.game.state == 0 || this.game.state == 2 || this.game.state == 3))
{
this.coverDown = false;
this.cover.style.top = (-322*this.windowLayout.pixelRatio)+"px";
if (this.game.state == 2 || this.game.state == 3)
{
this.cover.style.transition = "top 0.3s ease-in";
}
}
if (!this.coverDown && (this.game.state == 1 | this.game.state == 4))
{
this.coverDown = true;
this.cover.style.top = "0px";
}
if (this.game.state == 0)
{
this.cover.style.transition = "none";
}
if (this.game.state >= 1)
{ // Level intro, playing/pause, tutorial, end : display scenery, traps and critters
this.game.world.playField.render(this.sceneryContext);
// shake the play area + sprites whenever a mine was blown
// - by moving all the playfield canvas with css
// - by translating the sprites in the overlay canvas. Not the whole canvas as the lower part (icons, text) does not move
// combine this with a vertical offset for FFOS layout if the screen represents less than 256 zoomed pixels
// we hide the top of the play area in this case, the bottom is more important
var dt = this.game.world.timer - this.game.world.lastExplosionTime;
var jolt = Math.floor(Math.floor(20*Math.exp(-.2*dt))*Math.cos(dt));
var overflowOffsetY = this.windowLayout.playArea[3]-256;
this.sceneryCanvas.style.top = (this.windowLayout.pixelRatio*(jolt+overflowOffsetY))+"px";
this.overlayContext.translate(this.sceneryOffsetX, jolt+overflowOffsetY);
this.overlayContext.clearRect(0, 0, this.overlayCanvas.width, this.overlayCanvas.height);
for (var i=0; i<this.game.world.traps.length; ++i)
{ // display traps
var trap = this.game.world.traps[i];
if (trap.type == 3) // balloons
{
this.drawBalloons(trap.x, trap.y, this.frameCount);
} else {
var w = this.game.world.trapSize[trap.type][0];
var h = this.game.world.trapSize[trap.type][1];
var frames = [4, 4, 1, 1, 1, 1, 4, 4, 1, 1, 1][trap.type];
var currentFrame = (this.frameCount>>(trap.type==1?1:2));
this.overlayContext.drawImage(this.spriteSheet, trap.dir*35*(currentFrame%frames)+(trap.dir==-1?280-2*w:0), 27*trap.type, w*2, h, trap.x-w, trap.y-h+1, w*2, h);
}
if (trap.type==8 && trap.timer<=trap.hitTime) // cannon tower, with flying pellet
{ // draw pellet
this.overlayContext.drawImage(this.spriteSheet, 35, 5*27, 3, 3, trap.pelletX-1, trap.pelletY-1, 3, 3);
}
if (trap.type==9) // dynamite
{
var length = (trap.timer == -1 ? 10 : trap.timer/5);
this.overlayContext.strokeStyle = "#000";
this.overlayContext.lineWidth = 1;
this.overlayContext.beginPath();
this.overlayContext.moveTo(trap.x-.5, trap.y-17);
this.overlayContext.lineTo(trap.x-.5, trap.y-17-length);
this.overlayContext.stroke();
if (trap.timer>-1) { // ignited
this.overlayContext.strokeStyle = "#FD4";
for (var j=0; j<10; ++j)
{
var angle = .5*j-1;
var r = 5+((10+trap.timer-j)%5);
this.overlayContext.lineWidth = 2;
this.overlayContext.beginPath();
this.overlayContext.moveTo(trap.x-.5+r*Math.cos(angle), trap.y-17-length-r*Math.sin(angle));
this.overlayContext.lineTo(trap.x-.5+(r+2)*Math.cos(angle), trap.y-17-length-(r+2)*Math.sin(angle));
this.overlayContext.stroke();
}
}
}
if (trap.type==2 && this.game.world.draggedTrap != i) { // flamethrower
this.smoke.push(0, true, 1, trap.x + 10*trap.dir, trap.y-12, 2*trap.dir, 0); // flames
}
}
for (var i=0; i<this.game.world.predators.length; ++i)
{ // display weasels
var critter = this.game.world.predators[i];
if (critter.activity >= 0 && (critter.activity!=8 || critter.timer<10)) { // alive critters only
var act = (critter.activity == 5 ? 2 : critter.activity);
// rolling animation for parachute and balloon
var currentFrame = act==2 ? [5, 6, 7, 8, 9, 8, 7, 6][(critter.timer>>1)%8] : ((critter.timer)%10);
// no animation for flying and burned
currentFrame = (act==6 || act==7 ? 0 : currentFrame);
// animation for smok^H^H^H^H blockers : thump the ground for 96 frames, then get a smoke for 32 frames
if (act == 3)
{
var animFrame = (critter.timer&127)>>2;
currentFrame = (animFrame>23) ? 2+(animFrame&7) : (animFrame&1);
if (animFrame<24)
{ // smoke from cigarette held in extended hand
this.smoke.push(0, false, .5, critter.x-5*critter.dir, critter.y-8, 0, 0);
}
if (animFrame>26 && animFrame<30)
{
this.smoke.push(4, false, 1, critter.x+critter.dir, critter.y-9, .5*critter.dir, -.5);
}
}
// use frames 0-9 if heading right (dir==1), 10-19 if heading left
currentFrame = (critter.dir==-1?19:0)+critter.dir*currentFrame;
var critterStartY = 300; // in sprite sheet
this.overlayContext.drawImage(this.spriteSheet, currentFrame*15, critterStartY+20*act, 15, 20, Math.floor(critter.x)-7, Math.floor(critter.y)-19, 15, 20);
if (act==2) // umbrella/parachute or balloon
{
this.overlayContext.drawImage(this.spriteSheet, (critter.dir==-1?285:0), critterStartY+20*critter.activity, 15, 20, Math.floor(critter.x)-7, Math.floor(critter.y)-31, 15, 20);
}
if (critter.activity==7&&critter.timer<10) // torched umbrella
{
this.overlayContext.drawImage(this.spriteSheet, (critter.dir==-1?285:0)+15*critter.dir*(critter.timer>>1), critterStartY+40, 15, 20, Math.floor(critter.x)-7, Math.floor(critter.y)-31, 15, 20);
}
if (critter.shield)
{
this.overlayContext.drawImage(this.spriteSheet, 15*critter.shield, critterStartY+100, 15, 20, critter.x-7, critter.y-19, 15, 20);
}
if (this.game.world.timer < 25+critter.lastWound)
{
var alpha = (25+critter.lastWound-this.game.world.timer)/25;
var dy = (act==2 ||act==7 ? 31 : 24);
this.overlayContext.lineWidth = 1;
this.overlayContext.strokeStyle = "rgba(0,0,0,"+alpha+")";
this.overlayContext.strokeRect(.5+Math.floor(critter.x-5), .5+Math.floor(critter.y-dy-1), 10, 4);
this.overlayContext.fillStyle = "rgba(0,255,0,"+alpha+")";
this.overlayContext.fillRect(Math.floor(critter.x-4), Math.floor(critter.y-dy), .09*critter.life, 3);
}
}
}
// trap being added
if (this.game.world.currentToolIndex > -1
&& this.game.world.tools[this.game.world.currentToolIndex] > 0
&& this.game.controls.mouseInPlayArea
&& this.game.world.draggedTrap < (this.showTrapOnHover ? 0 : -1) // -1 (hovering only) or -2 (LMB down, adding new trap)
&& this.game.world.draggedTrap != -3 // scrolling
&& this.game.world.trapUnderMouse < 0 // click will add a new trap, not drag an existing one
&& this.game.state == 2) // only during game
{
var w = this.game.world.trapSize[this.game.world.currentTool][0];
var h = this.game.world.trapSize[this.game.world.currentTool][1];
if (this.game.world.currentTool==3) { // balloons
this.drawBalloons(this.game.world.controls.worldX, this.game.world.controls.worldY, 5);
} else {
// drawing first frame, not animated, and always facing right
this.overlayContext.drawImage(this.spriteSheet,
0, 27*this.game.world.currentTool,
w*2, h,
this.game.world.controls.worldX-w, this.game.world.controls.worldY-h+1, w*2, h);
}
}
/*
// debug : draw hazards
this.overlayContext.strokeStyle = "#F00";
this.overlayContext.lineWidth = 1;
for (var i=0; i<this.game.world.strategy.hazards.length; ++i)
{
var hazard = this.game.world.strategy.hazards[i];
this.overlayContext.beginPath();
this.overlayContext.rect(hazard[3], hazard[4], hazard[5], hazard[6]);
this.overlayContext.stroke();
}
*/
this.drawSmoke();
this.drawWater();
this.overlayContext.translate(-this.sceneryOffsetX, -jolt-overflowOffsetY);
}
if (this.game.state == 0)
{ // main menu
this.drawTitleScreen();
} else
{ // so that animationStartFrame is equal to the frame the screen is changed to main menu
this.animationStartFrame = this.frameCount;
}
if (this.game.state == 1)
{ // level intro : display level description
this.drawLevelDescription();
this.smoke = []; // clean all smoke from previous level
}
if (this.game.state == 2 || this.game.state == 3)
{ // playing/pause or tutorial : show icons, timer, level data, scrolling arrows
this.drawIcons();
this.drawWorldInfo();
}
if (this.game.state == 2)
{ // playing/pause : show scrolling arrows
if (!this.game.controls.scrollOnSwipe)
{ // Draw scrolling arrows if not using a touch screen, and not in tutorial
this.overlayContext.fillStyle="#FFF";
if (this.sceneryOffsetX < 0)
{
var arrowSize = (this.scrollingInProgress<0 ? 6: 10);1
this.overlayContext.translate(this.windowLayout.playArea[0], this.windowLayout.playArea[1]);
this.overlayContext.beginPath();
this.overlayContext.moveTo(12, 128);
this.overlayContext.lineTo(12, 128-arrowSize);
this.overlayContext.lineTo(12-arrowSize, 128);
this.overlayContext.lineTo(12, 128+arrowSize);
this.overlayContext.fill();
this.overlayContext.translate(-this.windowLayout.playArea[0], -this.windowLayout.playArea[1]);
}
if (this.sceneryOffsetX > this.minSceneryOffsetX)
{
var arrowSize = (this.scrollingInProgress>0 ? 6 : 10);
this.overlayContext.translate(this.windowLayout.playArea[0]+this.windowLayout.playArea[2], this.windowLayout.playArea[1]);
this.overlayContext.beginPath();
this.overlayContext.moveTo(-12, 128);
this.overlayContext.lineTo(-12, 128-arrowSize);
this.overlayContext.lineTo(-12+arrowSize, 128);
this.overlayContext.lineTo(-12, 128+arrowSize);
this.overlayContext.fill();
this.overlayContext.translate(-this.windowLayout.playArea[0]-this.windowLayout.playArea[2], -this.windowLayout.playArea[1]);
}
}
this.drawMouseCursor(); // on top, so drawn last
}
if (this.game.state == 3)
{ // tutorial
this.drawTutorialInfo();
}
if (this.game.state == 4)
{ // level ended
this.drawLevelEndText();
}
++this.frameCount;
},
/**
* Draw the fire and smoke from the flamethrowers,
* and from the smoking blockers if it gets implemented.
* Moves and ages flames and smoke.
*/
drawSmoke : function()
{
for (var i=0; i<this.smoke.length; i+=7)
{
var radius = 1;
if (this.smoke[i+1])
{ // heavy smoke : blowtorch or explosion
var red = Math.floor(Math.min(255, Math.max(0, 186+50*this.smoke[i]-5*this.smoke[i]*this.smoke[i])));
var green = Math.floor(Math.min(255, Math.max(0, 229+11*this.smoke[i]-2.83*this.smoke[i]*this.smoke[i])));
var blue = Math.floor(Math.min(255, Math.max(0, 203+14*this.smoke[i]-4.55*this.smoke[i]*this.smoke[i])));
var alpha = Math.min(1, Math.max(0, 1.25-.02*this.smoke[i]));
this.overlayContext.fillStyle = "rgba("+red+","+green+","+blue+","+alpha+")";
radius = this.smoke[i+2]*(this.smoke[i]<10 ? Math.min(2, .5*this.smoke[i]) : .5+.15*this.smoke[i]);
} else { // light smoke : cigarette
var alpha = Math.min(1, Math.max(0, .8-.05*this.smoke[i]));
this.overlayContext.fillStyle = "rgba(160, 160, 160,"+alpha+")";
radius = this.smoke[i+2]*(.5+.05*this.smoke[i]);
}
this.overlayContext.beginPath();
this.overlayContext.arc(this.smoke[i+3], this.smoke[i+4], radius, 0, 7);
this.overlayContext.fill();
if (this.smoke[i]>=64) {
this.smoke.splice(i,7);
i-=7;
} else {
++this.smoke[i];
this.smoke[i+3]+=this.smoke[i+5];
this.smoke[i+4]+=this.smoke[i+6];
this.smoke[i+5]=this.smoke[i+5]*.98+.2*Math.random()-.1;
this.smoke[i+6]=(this.smoke[i+1] ? this.smoke[i+6]+(this.smoke[i]>10?0:.1)-.2*Math.random() : Math.max(-1.5, this.smoke[i+6]-.1));
}
}
},
/**
* Draw the water (ahem..) pool at the bottom of the play area
*/
drawWater : function()
{
this.overlayContext.fillStyle = "#138";
for (var x=0; x<this.overlayCanvas.width; x+=10)
{
this.overlayContext.fillRect(x-this.sceneryOffsetX, this.sceneryCanvas.height+2, 10, -12-8*Math.sin(.2*this.frameCount)*Math.sin((this.frameCount+x-this.sceneryOffsetX)/10));
}
},
/**
* Displays on the overlay canvas information related to the current game
* - number of critters inside and eliminated
* - time remaining
*/
drawWorldInfo : function() {
this.overlayContext.lineWidth=1;
this.overlayContext.font="bold 14px sans-serif";
this.overlayContext.shadowColor="#060";
this.overlayContext.fillStyle = "#8F8";
// critters inside and remaining
this.overlayContext.textAlign="center";
var text = this.game.world.crittersFragged + (this.game.world.fragTarget>0?"/"+this.game.world.fragTarget:"")+" fragged, "+this.game.world.crittersInside+" in, "+this.game.world.crittersExited+" out";
this.overlayContext.textAlign=this.windowLayout.textArea[3];
this.drawShadedTextOnOverlay(text, this.windowLayout.textArea[5], this.windowLayout.textArea[1]);
// time left
this.overlayContext.textAlign="right";
var time = Math.max(0, Math.floor((this.game.world.totalTime - this.game.world.timer)/25)); // floored to zero, for the tutorial
text = Math.floor(time/60)+":"+((time%60)<10 ? "0":"")+(time%60);
this.drawShadedTextOnOverlay(text, this.windowLayout.textArea[6], this.windowLayout.textArea[2]);
// current tool
this.overlayContext.textAlign="left";
var prefix = "";
var toolIndex = this.game.world.currentTool;
var suffix = (this.game.world.tools[this.game.world.currentToolIndex]==0 ? " (none left)" : "");
if (this.game.world.highlightedTool > -1) {
// mouse over tool in the tool or control bar : name the tool
toolIndex = this.game.world.highlightedTool == 4 ? this.game.world.variableTool : this.game.world.highlightedTool;
suffix = (this.game.world.tools[this.game.world.highlightedTool]==0 ? " (none left)" : "");
}
if (this.game.world.trapUnderMouse>-1 && this.game.world.trapUnderMouse<this.game.world.traps.length) {
// mouse over an ingame trap : indicate whether it can be moved or not
toolIndex = this.game.world.traps[this.game.world.trapUnderMouse].type;
if (this.game.world.canMoveTrap(toolIndex)) {
suffix = " (click and hold to move)";
} else {
suffix = " (cannot be moved)";
}
}
if (this.game.world.draggedTrap > -1 && this.game.world.draggedTrap<this.game.world.traps.length) {
// dragging a trap
toolIndex = this.game.world.traps[this.game.world.draggedTrap].type;
if (this.game.world.dragging) {
prefix = "Dragging ";
suffix = "";
} else {
prefix = "";
suffix = " (turn around or move)";
}
}
if (this.game.world.highlightedTool==-1 && this.game.lastButtonClicked>1) {
// Fix for issue #10
// touch screen : keep the message about a "double click" button
// even after the first click is released (no hover on touch screen)
toolIndex = 16+this.game.lastButtonClicked;
}
if (toolIndex > 17)
{
suffix = (this.game.lastButtonClicked == toolIndex - 16 ? " (click again to confirm)" : " (click twice)");
}
if (toolIndex == this.game.world.tools.length-1 && this.game.world.shotgunReloadTime>0)
{
suffix = " (reloading)";
}
text = prefix + ["", "Landmine", "Fan", "Flamethrower", "Balloons", "Variable", "Shotgun",
"Exit", "Entrance", "Cannon Tower", "Dynamite", "Building Block", "", "", "", "", "",
"Pause", "Fast forward", "Restart level", "Return to main menu"][1+toolIndex] + suffix;
this.drawShadedTextOnOverlay(text, this.windowLayout.textArea[4], this.windowLayout.textArea[0]);
this.overlayContext.shadowOffsetX = 0;
this.overlayContext.shadowOffsetY = 0;
},
/**
* Draws the icons for tools and game flow controls on the overlay canvas.
* Hovered icon (one only) is highlighted
* Selected icon (one tool only) is bordered
* Tool icons shown charges (number of uses) remaining.
*/
drawIcons : function() {
// trap toolbar (blue background)
var tools = this.game.world.tools;
this.overlayContext.textAlign="center";
this.overlayContext.font="bold italic 14px sans-serif";
for (var i=0; i<tools.length; ++i)
{
var iconX = this.windowLayout.toolBar[0]+i*this.windowLayout.toolBar[4];
var iconY = this.windowLayout.toolBar[1]+i*this.windowLayout.toolBar[5];
this.overlayContext.drawImage(this.spriteSheet, this.game.world.highlightedTool == i ? 48 : 0, 560, 48, 40, iconX, iconY, 48, 40);
if (i==3) {
this.drawBalloons(iconX+17, iconY+37, 5);
} else {
this.overlayContext.drawImage(this.spriteSheet, (i?0:81), (i==4?this.game.world.variableTool*27:(i?27*i:135)), 35, 27, iconX+10, iconY+8, 35, 27);
}
var text = (tools[i]<0 ? "oo" : tools[i]);
this.overlayContext.strokeStyle="#000";
this.overlayContext.lineWidth=3;
this.overlayContext.strokeText(text, iconX+33, iconY+36);
this.overlayContext.fillStyle="#FFF";
this.overlayContext.fillText(text, iconX+33, iconY+36);
}
// selected tool
this.overlayContext.strokeStyle="#F00";
if (this.game.world.currentToolIndex>-1) {
this.overlayContext.beginPath();
this.overlayContext.rect(this.windowLayout.toolBar[0] + this.game.world.currentToolIndex*this.windowLayout.toolBar[4]+1,
this.windowLayout.toolBar[1] + this.game.world.currentToolIndex*this.windowLayout.toolBar[5]+1, 46, 38);
this.overlayContext.stroke();
}
// controls toolbar (brown background)
this.overlayContext.strokeStyle="#000";
for (var i=0; i<4; ++i)
{
var iconX = this.windowLayout.controlBar[0]+i*this.windowLayout.controlBar[4];
var iconY = this.windowLayout.controlBar[1]+i*this.windowLayout.controlBar[5];
this.overlayContext.drawImage(this.spriteSheet, this.game.world.highlightedTool == i+16 ? 144 : 96, 560, 48, 40, iconX, iconY, 48, 40);
var text = ["| |", ">>", "reset", "menu"][i];
if ((i==0 && this.game.pause) // paused
|| (i==1 && this.game.fastForward)) // accelerated game
{
if (this.frameCount&32) { // blink the resume symbol in pause or fast forward
text = ">";
}
}
this.overlayContext.strokeText(text, iconX+23, iconY+30);
this.overlayContext.fillStyle="#FFF";
this.overlayContext.fillText(text, iconX+23, iconY+30);
}
},
/**
* Draw the mouse cursor,
* either by using one of the browser presets
* or by hiding it entirely and drawing at its location on the overlay canvas
* - in playfield, shotgun selected : crosshair
* - in playfield, over trap than can be picked up : hand
* - in playfield, dragging trap, location OK to drop : pointer
* - in playfield, dragging trap, location not ok to drop : no-drop
* - all others : default
*/
drawMouseCursor : function()
{
var cursorId = "default";
if (this.game.controls.mouseInPlayArea)
{
if (this.game.world.draggedTrap > -1 || this.game.world.draggedTrap == -2)
{ // LMB down, moving existing trap (>=0) or adding new trap (-2)
cursorId = (this.game.world.dragValid ? "pointer" : "ew-resize");
}
else if (this.game.world.draggedTrap == -3)
{ // LMB down, dragging to scroll (-3, touch screen only)
cursorId = (this.game.world.dragValid ? "ew-resize" :
(this.game.world.currentToolIndex == this.game.world.tools.length-1 ?
(this.game.world.shotgunReloadTime ? "wait" : "crosshair") : "pointer"));
}
else if (this.game.world.trapUnderMouse != -1)
{ // ready to pickup trap and move it
cursorId = "pointer";
} else if (this.game.world.currentToolIndex == this.game.world.tools.length-1)
{ // shotgun tool
cursorId = (this.game.world.shotgunReloadTime ? "wait" : "crosshair");
}
}
this.overlayCanvas.style.cursor = cursorId;
},
/**
* Display the intro text for a level
* - goal
* - time
*/
drawLevelDescription : function()
{
this.textContext.clearRect(0, 0, this.textCanvas.width, this.textCanvas.height);
var textY = 12;
this.textContext.lineWidth=1;
this.textContext.textAlign="left";
this.textContext.font="bold 14px sans-serif";
this.textContext.shadowColor="#060";
this.textContext.fillStyle = "#8F8";
var text = "Level "+(1+this.game.level)+" - "+this.game.world.levelTitle;
this.drawShadedText(text, 20, textY);
textY+=16;
text = this.game.world.fragTarget
? "Get rid of "+(this.game.world.fragTarget == this.game.world.crittersWaitingAtSource ? "all "+this.game.world.fragTarget+" weasels"
: this.game.world.fragTarget+" weasels out of "+ this.game.world.crittersWaitingAtSource)
: "Do not let any weasel out";
this.drawShadedText(text, 20, textY);
textY+=16;
time = Math.floor((this.game.world.totalTime - this.game.world.timer)/50);
text = "Time "+Math.floor(time/60)+":"+((time%60)<10 ? "0":"")+(time%60);
this.drawShadedText(text, 20, textY);
textY+=16;
text = "Click to start";
this.drawShadedText(text, 20, textY);
this.overlayContext.shadowOffsetX = 0;
this.overlayContext.shadowOffsetY = 0;
},
/**
* Display the different steps of the tutorial
* - highlight or the different tools
* - write the associated explanation text
*/
drawTutorialInfo : function()
{
var allTutorialTexts = [
[
"Your goal is to prevent weasels from",
"reaching the exit by any means.",
"A mission statement is given for each level,",
"you may be asked to eliminate some or",
"all of them, or block the way and hold for",
"a given time."
],
[
"Weasels are smarter than the average",
"lemming and will evade traps, find their way",
"around and avoid falling to their demise.",
"Blockers prevent them from jumping off",
"a cliff. Builders create stairs that they can",
"all climb to reach higher platforms."
],
[
"This is an entrance. Weasels come this way",
"at a regular pace. In difficult levels there",
"can be several of them throughout",
"the place. Entrances cannot be moved."
],
[
"This is an exit and weasels will endeavor to",
"reach it. There may also be several ways out",
"of the level. Exits can usually not be moved."
],
[
"These traps will help you defend against",
"weasels. Their availability depends on",
"the level. Drag a trap from the toolbar to",
"the playfield, or select it first then tap",
"or click to place it. You can also drag",
"an already installed trap to relocate it."
],
[
"Landmines explode when a weasel comes",
"nearby. They obey gravity so you can drop",
"them from above. While unexploded they",
"can be freely moved around."
],
[
"Fans give lateral speed to already airborne",
"weasels. The closer they are, the stronger",
"the push. They are ineffective against walkers.",
"Click on an already installed fan to",
"turn it around."
],
[
"Flamethrowers deal lots of damage at close",
"range but produce a heavy smoke that",
"obscures the view. Click to turn them",
"around. Weasels are afraid of fire and thus",
"will try to walk or climb around these."
],
[
"Weasels will grab balloons on their way",
"and become airborne. They will let them go",
"once they are satisfied with the flight.",
"Some levels come with balloon stands",
"already installed."
],
[
"Cannon towers shoot at the closest weasel.",
"They are a slow yet efficient weapon.",
"Pay attention as they cannot be moved",
"once installed."
],
[
"Your shotgun is a decent weapon,",
"however weasels carry a heavy shield",
"which makes it inefficient against them.",
"It still serves a purpose though."
],
[
"New traps will be provided in the upper levels.",
"They are up to you to discover."
],
[
"The toolbar on the right hand side is used",
"to pause the game, fast forward, restart",
"the level or return to the main menu."
],
[
"Actual levels are wider than the screen,",
"and can be scrolled left and right. Bring",
"your mouse cursor to the edge of",
"the screen, or swipe on touch screens."
],
[
"I hear weasels coming. It is now up to you !"
]
];
var allTextLocations = [80, 80, 110, 150, 50,
80, 80, 80, 80, 80,
80, 80, 80, 80, 80];
var allHighlightedAreas = [
0,
0,
[400+this.sceneryOffsetX, 74+this.windowLayout.playArea[3]-256, 23],
[639+this.sceneryOffsetX, 119+this.windowLayout.playArea[3]-256, 25],
this.windowLayout.toolBar,
[this.windowLayout.toolBar[0], this.windowLayout.toolBar[1], 48, 40],
[this.windowLayout.toolBar[0]+this.windowLayout.toolBar[4], this.windowLayout.toolBar[1]+this.windowLayout.toolBar[5], 48, 40],
[this.windowLayout.toolBar[0]+2*this.windowLayout.toolBar[4], this.windowLayout.toolBar[1]+2*this.windowLayout.toolBar[5], 48, 40],
[this.windowLayout.toolBar[0]+3*this.windowLayout.toolBar[4], this.windowLayout.toolBar[1]+3*this.windowLayout.toolBar[5], 48, 40],
[this.windowLayout.toolBar[0]+4*this.windowLayout.toolBar[4], this.windowLayout.toolBar[1]+4*this.windowLayout.toolBar[5], 48, 40],
[this.windowLayout.toolBar[0]+5*this.windowLayout.toolBar[4], this.windowLayout.toolBar[1]+5*this.windowLayout.toolBar[5], 48, 40],
0,
this.windowLayout.controlBar,
0,
0
];
var tutorialText = allTutorialTexts[this.game.tutorialPage];
var textLocation = allTextLocations[this.game.tutorialPage];
var highlightedArea = allHighlightedAreas[this.game.tutorialPage];
this.overlayContext.fillStyle = "rgba(0, 0, 0, .7)";
this.overlayContext.beginPath();
this.overlayContext.rect(0, 0, this.overlayCanvas.width, this.overlayCanvas.height);
if (highlightedArea !== false) {
if (highlightedArea.length > 3)
{
this.overlayContext.moveTo(highlightedArea[0], highlightedArea[1]);
this.overlayContext.lineTo(highlightedArea[0], highlightedArea[1]+highlightedArea[3]);
this.overlayContext.lineTo(highlightedArea[0]+highlightedArea[2], highlightedArea[1]+highlightedArea[3]);
this.overlayContext.lineTo(highlightedArea[0]+highlightedArea[2], highlightedArea[1]);
this.overlayContext.lineTo(highlightedArea[0], highlightedArea[1]);
} else {
this.overlayContext.arc(highlightedArea[0], highlightedArea[1], highlightedArea[2], 0, 2*Math.PI, true);
}
}
this.overlayContext.closePath();
this.overlayContext.fill();
this.overlayContext.lineWidth=1;
this.overlayContext.font="bold 14px sans-serif";
this.overlayContext.shadowColor="#060";
this.overlayContext.fillStyle = "#8F8";
this.overlayContext.textAlign="center";
for (var i=0; i<tutorialText.length; ++i)
{
this.drawShadedTextOnOverlay(tutorialText[i], this.overlayCanvas.width>>1, textLocation+30*i);
}
this.overlayContext.shadowOffsetX = 0;
this.overlayContext.shadowOffsetY = 0;
},
/**
* Display level debriefing
* - result obtained
* - result expected
*/
drawLevelEndText : function()
{
this.textContext.clearRect(0, 0, this.textCanvas.width, this.textCanvas.height);
var textY = 12;
this.textContext.lineWidth=1;
this.textContext.textAlign="left";
this.textContext.font="bold 14px sans-serif";
this.textContext.shadowColor="#060";
this.textContext.fillStyle = "#8F8";
var won = this.game.world.won();
var text = this.game.world.fragTarget ? (this.game.world.crittersFragged ? this.game.world.crittersFragged : "Not a single")+" weasel"+(this.game.world.crittersFragged?"s":"")+" killed."
: (won ? "No weasel made it to the exit." : "A weasel escaped ! ");
this.drawShadedText(text, 20, textY);
textY+=16;
if (this.game.world.fragTarget)
{
if (this.game.world.crittersFragged==0) {
text = "Sitting duck !";
} else if (this.game.world.crittersFragged*2 < this.game.world.fragTarget) {
text = "Not enough";
} else if (!won) {
text = "Close, but no cigar";
} else {
text = "Fixed'em once for all !"
}
} else {
text = won ? "Great job! " : "Slick devils, ain't them ?";
}
this.drawShadedText(text, 20, textY);
textY+=16;
text = won ? "Click for next level" : "Click to try again";
this.drawShadedText(text, 20, textY);
this.overlayContext.shadowOffsetX = 0;
this.overlayContext.shadowOffsetY = 0;
},
/**
* Display title and main menu
*/
drawTitleScreen : function()
{
var time = this.frameCount - this.animationStartFrame;
if (time==1) {
var bufferContext = this.titleBuffer.getContext('2d');
bufferContext.clearRect(0, 0, this.titleBuffer.width, this.titleBuffer.height);
bufferContext.strokeStyle="#080";
bufferContext.lineWidth=10;
bufferContext.font="40pt Verdana";
bufferContext.textAlign="center";
bufferContext.strokeText("PEST CONTROL", 500, 100);