forked from MattSkala/html5-bombergirl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bombergirl.js
1812 lines (1440 loc) · 55.9 KB
/
Bombergirl.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
var Bombergirl = {
/*
* iniReady is used to only load resources once
* */
initReady: false,
touch: false,
// to limit showing off
showOffTimes: 0,
showingOff: false,
/*
* Game states
* */
paused: false,
mute: false,
quited: false,
// number of tiles must be odd
nrTilesWidth: 17,
nrTilesHeight: 13,
// the container element on which everything will be rendered
canvas: null,
notificationsArea: null,
playerNotification1: null,
playerNotification2: null,
pausedIndicator: null,
manualPaused: false,
// the container on which everything will be rendered
stage: null,
spaceRatio: 1,
// holds references to all the loaded images used in the game
images: {},
oaOperands: null,
oaNumbers: null,
// an array with the numbers player 1 must collect
correctNumbers1: [],
// numberOrder1 is a string which can be 'ascending'/'descending'
numberOrder1: null,
// an array with the numbers player 2 must collect
correctNumbers2: [],
numberOrder2: null,
// input holds methods and variables which deal with input and actions
input: null,
players: [],
bots: [],
player1: null,
player2: null,
// the number of players currently playing the game
nrPlayers: null,
yellow: '#FFE900',
red: '#FC0003',
// a matrix of elements on the game map
tiles: null,
woodsPositions: [],
bonuses: [],
bonusesPercent: 16,
bonusTypes: {
0: 'speed_bonus',
1: 'bomb_bonus',
2: 'fire_bonus'
},
bombs: [],
possibleFires: [],
fires: [],
soundtrack: null,
soundtrackStarted: false,
onKeyDown: null,
onKeyUp: null,
prepare: function () {
this.canvas = dom.createCanvas( 'bomberman_map', null, "", gSize * this.nrTilesWidth, gSize * this.nrTilesHeight);
// document.getElementById( "quit").addEventListener( "click", this.handleClickQuitButtonEvent);
this.touch = ('ontouchstart' in window)
|| (navigator.MaxTouchPoints > 0)
|| (navigator.msMaxTouchPoints > 0);
this.input = new InputEngine();
this.input.addListener( 'mute', this.toggleSound);
this.input.addListener( 'pause', this.togglePause);
this.input.addListener( 'escape', this.restart);
this.onKeyDown = this.input.onKeyDown.bind( this.input);
this.onKeyUp = this.input.onKeyUp.bind( this.input);
this.onTouchStart = this.input.onTouchStart.bind( this.input);
this.onTouchMove = this.input.onTouchMove.bind( this.input);
this.onTouchEnd = this.input.onTouchEnd.bind( this.input);
},
start: function () {
// change the number of tiles on width to be lower than the number of tiles
// on height to take advantage of the available space
var containerSize = this.getContainerSizes();
if (containerSize.width < containerSize.height) {
if ( this.nrTilesWidth > this.nrTilesHeight) {
var aux = this.nrTilesHeight;
this.nrTilesHeight = this.nrTilesWidth;
this.nrTilesWidth = aux;
}
} else if (containerSize.height < containerSize.width){
if ( this.nrTilesHeight > this.nrTilesWidth) {
var aux = this.nrTilesHeight;
this.nrTilesHeight = this.nrTilesWidth;
this.nrTilesWidth = aux;
}
}
document.addEventListener( 'keydown', this.onKeyDown);
document.addEventListener( 'keyup', this.onKeyUp);
window.addEventListener( 'resize', this.scale);
window.addEventListener( 'focus', this.unPause);
window.addEventListener( 'blur', this.pause);
// load resources only if it's the first time entering the game
if (!this.initReady) {
// display a loading text while the resources are loading
var loadingText1 = dom.createSpan('', 'bomberman_loadingText redText', 'Bombergirl');
var br = document.createElement( 'br');
var loadingText2 = dom.createSpan('', 'bomberman_loadingText', 'Loading, please wait...');
var renderBox = document.getElementById( 'renderBox');
renderBox.appendChild( loadingText1);
renderBox.appendChild( br);
renderBox.appendChild( loadingText2);
var loadQueue = new createjs.LoadQueue();
loadQueue.installPlugin( createjs.Sound);
var self = this;
loadQueue.addEventListener( 'complete', function () {
self.images['shadow'] = loadQueue.getResult( 'shadow');
// player images
self.images['betty'] = loadQueue.getResult( 'betty');
self.images['betty2'] = loadQueue.getResult( 'betty2');
// bots images
self.images['george'] = loadQueue.getResult( 'george');
self.images['george2'] = loadQueue.getResult( 'george2');
self.images['george3'] = loadQueue.getResult( 'george3');
self.images['wall'] = loadQueue.getResult( 'wall');
self.images['bomb'] = loadQueue.getResult( 'bomb');
self.images['wood'] = loadQueue.getResult( 'wood');
self.images['fire'] = loadQueue.getResult( 'fire');
self.images['speed_bonus'] = loadQueue.getResult( 'speed_bonus');
self.images['bomb_bonus'] = loadQueue.getResult( 'bomb_bonus');
self.images['fire_bonus'] = loadQueue.getResult( 'fire_bonus');
self.initReady = true;
renderBox.removeChild( loadingText1);
renderBox.removeChild( br);
renderBox.removeChild( loadingText2);
self.startGame();
});
loadQueue.loadManifest([
{id: 'shadow', src: 'resources/img/bomberman_shadow.png'},
{id: 'betty', src: 'resources/img/bomberman_betty.png'},
{id: 'betty2', src: 'resources/img/bomberman_betty2.png'},
{id: 'george', src: 'resources/img/bomberman_george.png'},
{id: 'george2', src: 'resources/img/bomberman_george2.png'},
{id: 'george3', src: 'resources/img/bomberman_george3.png'},
{id: 'wall', src: 'resources/img/bomberman_wall.png'},
{id: 'bomb', src: 'resources/img/bomberman_bomb.png'},
{id: 'wood', src: 'resources/img/bomberman_wood.png'},
{id: 'fire', src: 'resources/img/bomberman_fire.png'},
{id: 'speed_bonus', src: 'resources/img/bomberman_speed_bonus.png'},
{id: 'bomb_bonus', src: 'resources/img/bomberman_bomb_bonus.png'},
{id: 'fire_bonus', src: 'resources/img/bomberman_fire_bonus.png'},
{data: 1, id: 'game', src: 'resources/sound/bomberman_soundtrack.mp3|media/music/bomberman_soundtrack.ogg'},
{data: 8, id: 'correct', src: 'resources/sound/bomberman_collect.mp3|media/music/bomberman_collect.ogg'},
{data: 8, id: 'wrong', src: 'resources/sound/bomberman_collect_wrong.mp3|media/music/bomberman_collect_wrong.ogg'},
{data: 8, id: 'bonus', src: 'resources/sound/bomberman_bonus.mp3|media/music/bomberman_bonus.ogg'},
{data: 30, id: 'bombsound', src: 'resources/sound/bomberman_bomb.mp3|media/music/bomberman_bomb.ogg'},
{data: 8, id: 'die', src: 'resources/sound/bomberman_die.mp3|media/music/bomberman_die.ogg'}
]);
} else {
this.startGame();
}
},
startGame: function () {
this.stage = new createjs.Stage(this.canvas);
createjs.Touch.enable(this.stage);
var nrPlayers = 1;
// If we are not on a small screen
if (this.nrTilesWidth > this.nrTilesHeight) {
nrPlayers = 2;
}
this.input.removeListeners( 'pause');
var menu = new Menu( ['Bomber Girl'], nrPlayers, this.nrTilesWidth, this.nrTilesHeight, this.images);
this.stage.addChild( menu.content);
var self = this;
menu.onSinglePlayer( function () {
createjs.Sound.play('correct').setVolume(0.7);
self.stage.removeAllChildren();
self.cleanUp();
self.nrPlayers = 1;
self.initGame();
self.scale();
self.initEvents();
// bring the notifications area into view
self.notificationsArea.style.top = '0';
// hide the cursor
self.canvas.style.cursor = 'none';
setTimeout( function () {
document.addEventListener( 'touchstart', self.onTouchStart);
document.addEventListener( 'touchmove', self.onTouchMove);
document.addEventListener( 'touchend', self.onTouchEnd);
}, 500);
});
if (this.nrTilesWidth > this.nrTilesHeight) {
// Add the listener for the multiplayer button only if there's enough space on the screen for multiplayer gameplay
menu.onMultiPlayer( function () {
createjs.Sound.play('correct').setVolume(0.7);
self.stage.removeAllChildren();
self.cleanUp();
self.nrPlayers = 2;
self.initGame();
self.scale();
self.initEvents();
// bring the notifications area into view
self.notificationsArea.style.top = '0';
// hide the cursor
self.canvas.style.cursor = 'none';
setTimeout( function () {
document.addEventListener( 'touchstart', self.onTouchStart);
document.addEventListener( 'touchmove', self.onTouchMove);
document.addEventListener( 'touchend', self.onTouchEnd);
}, 500);
});
}
// scale the game before appending it to the DOM, otherwise a bug would occur on chrome
this.scale();
// The canvas element must be inserted before the notifications area for proper display
try {
document.getElementById( 'renderBox').insertBefore( this.canvas, this.notificationsArea);
} catch (e) {
document.getElementById( 'renderBox').appendChild( this.canvas);
}
createjs.Ticker.setFPS( 60);
createjs.Ticker.addEventListener('tick', function () {
// Set the ticker because the menu contains 2 animation images
self.stage.update();
});
if (!this.soundtrackStarted) {
if (this.quited) {
createjs.Sound.setMute(false);
this.mute = false;
this.quited = false;
}
this.soundtrack = createjs.Sound.play('game', 'none', 0, 0, -1);
this.soundtrack.setVolume(0.5);
this.soundtrackStarted = true;
}
},
initGame: function () {
this.showingOff = false;
this.tiles = new Array( this.nrTilesWidth + 2);
for (var i = 0; i < this.tiles.length; i++) {
this.tiles[i] = new Array( this.nrTilesHeight + 2);
}
var map = [
[ null, null, 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', null, null],
[ null, 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', null],
[ 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood'],
[ 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood'],
[ 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood'],
[ 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood'],
[ 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood'],
[ 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood'],
[ 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood'],
[ 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood'],
[ 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood'],
[ null, 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', 'wood', 'wall', null],
[ null, null, 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', 'wood', null, null]
];
for ( var i = 0; i < 6; i++) {
map[Utils.getRandomInt(4, map.length - 3)][Utils.getRandomInt(4, map[0].length - 3)] = 'wall';
}
//this.oaOperands = ap.operands;
this.oaNumbers = [];
this.createWoods( map);
this.createBonuses();
this.createWalls( map);
this.createBorders();
this.createNotificationsArea();
this.createBots();
this.createPlayers();
this.buildRewardMap();
this.buildDangerMap();
this.buildAuxDangerMap();
},
createWoods: function ( map) {
this.woodsPositions = [];
var tilesOnHeight = this.nrTilesHeight;
var tilesOnWidth = this.nrTilesWidth;
var i, j, tall = false;
if ( tilesOnHeight > tilesOnWidth) {
tall = true;
}
for ( j = 0; j < tilesOnHeight; j++ ) {
for ( i = 0; i < tilesOnWidth; i++) {
if ( !tall && map[j][i] === 'wood') {
var wood = new Wood( this.images['wood'], {x: i + 1, y: j + 1});
this.tiles[i + 1][j + 1] = wood;
this.stage.addChild( wood.bmp);
this.woodsPositions.push({x: i + 1, y: j + 1});
} else if ( tall && map[i][j] === 'wood') {
var wood = new Wood( this.images['wood'], {x: i + 1, y: j + 1});
this.tiles[i + 1][j + 1] = wood;
this.stage.addChild( wood.bmp);
this.woodsPositions.push({x: i + 1, y: j + 1});
}
}
}
},
createBonuses: function () {
var bonusesCount = Math.round(this.woodsPositions.length * this.bonusesPercent * 0.01 / 4);
this.woodsPositions.sort(function () {
return 0.2 - Math.random();
});
for (var j = 0; j < 4; j++) {
var count = 0;
for (var i = 1; i < this.woodsPositions.length; i += 4) {
if (count > bonusesCount) {
break;
}
var woodPos = this.woodsPositions[i];
if ((j == 0 && woodPos.x < this.nrTilesWidth / 2 && woodPos.y < this.nrTilesHeight / 2) ||
(j == 1 && woodPos.x < this.nrTilesWidth / 2 && woodPos.y > this.nrTilesHeight / 2) ||
(j == 2 && woodPos.x > this.nrTilesWidth / 2 && woodPos.y < this.nrTilesHeight / 2) ||
(j == 3 && woodPos.x > this.nrTilesWidth / 2 && woodPos.y > this.nrTilesWidth / 2)) {
var typePercentage = Math.floor(Math.random() * 100);
var typeNr;
if (typePercentage < 20) {
typeNr = 0; // speed
} else if (typePercentage < 60) {
typeNr = 1; // bomb
} else {
typeNr = 2; // fire
}
var bonusType = this.bonusTypes[typeNr];
var bonus = new Bonus( woodPos, bonusType, this.images[bonusType]);
this.bonuses.push(bonus);
this.stage.addChildAt(bonus.bmp, 0);
count++;
}
}
}
},
createWalls: function ( map) {
var tall = false;
var tilesOnHeight = this.nrTilesHeight;
var tilesOnWidth = this.nrTilesWidth;
if ( tilesOnHeight > tilesOnWidth) {
tall = true;
}
var i,j;
for ( j = 0; j < tilesOnHeight; j++) {
for ( i = 0; i < tilesOnWidth; i++) {
if ( !tall && map[j][i] === 'wall') {
var wall = new Wall( {x: i + 1, y: j + 1}, this.images['wall']);
this.tiles[i + 1][j + 1] = wall;
this.stage.addChild(wall.bmp);
} else if ( tall && map[i][j] === 'wall') {
var wall = new Wall( {x: i + 1, y: j + 1}, this.images['wall']);
this.tiles[i + 1][j + 1] = wall;
this.stage.addChild(wall.bmp);
}
}
}
},
createBorders: function () {
// + 2 for the exterior ,invisible, walls (the ones outside the game area)
tilesOnHeight = this.nrTilesHeight + 2;
tilesOnWidth = this.nrTilesWidth + 2;
// Create the outer, invisible, walls
for (var leftright = 0; leftright < tilesOnHeight; leftright++) {
// the left wall
wall = new Wall( {x: 0, y: leftright});
this.tiles[0][leftright] = wall;
// the right wall
wall = new Wall( {x: tilesOnWidth - 1, y: leftright});
this.tiles[tilesOnWidth - 1][leftright] = wall;
}
for (var updown = 0; updown < tilesOnWidth; updown++) {
// the upper wall
wall = new Wall( {x: updown, y: 0});
this.tiles[updown][0] = wall;
// the lower wall
wall = new Wall( {x: updown, y: tilesOnHeight - 1});
this.tiles[updown][tilesOnHeight - 1] = wall;
}
},
/**
* @author Gabriel Titerlea
*
* Todo: the players may collect some special bonuses for more points in the future
*/
createOANumbers: function () {
var owner = 1;
var color = this.yellow;
this.makeOANumber( 0, 3, 3, color, owner);
this.makeOANumber( 1, this.nrTilesWidth - 2, 3, color, owner);
if (this.nrTilesWidth > this.nrTilesHeight) {
this.makeOANumber( 2, 7, 5, color, owner);
this.makeOANumber( 3, 11, 5, color, owner);
} else {
this.makeOANumber( 3, 5, 7, color, owner);
this.makeOANumber( 3, 9, 7, color, owner);
}
this.makeOANumber( 9, Math.floor(this.nrTilesWidth / 2) + 1, 2, color, owner);
if (this.nrPlayers === 2) {
owner = 2;
color = this.red;
}
this.makeOANumber( 5, 3, this.nrTilesHeight - 2, color, owner);
this.makeOANumber( 6, this.nrTilesWidth - 2, this.nrTilesHeight - 2, color, owner);
if (this.nrTilesWidth > this.nrTilesHeight) {
this.makeOANumber( 7, 7, this.nrTilesHeight - 4, color, owner);
this.makeOANumber( 8, 11, this.nrTilesHeight - 4, color, owner);
} else {
this.makeOANumber( 7, 5, this.nrTilesHeight - 6, color, owner);
this.makeOANumber( 8, 9, this.nrTilesHeight - 6, color, owner);
}
this.makeOANumber( 4, Math.floor(this.nrTilesWidth / 2) + 1, this.nrTilesHeight - 1, color, owner);
// set the order in which player 1 will collect numbers
var order = Math.round(Math.random());
switch (order) {
case 0:
this.correctNumbers1.sort( function ( a, b) {
return a > b;
});
this.numberOrder1 = '(asc)';
break;
case 1:
this.correctNumbers1.sort( function ( a, b) {
return a < b;
});
this.numberOrder1 = '(desc)';
break;
}
// set the order in which player 2 will collect numbers
order = Math.round(Math.random());
switch (order) {
case 0:
this.correctNumbers2.sort( function ( a, b) {
return a > b;
});
this.numberOrder2 = '(asc)';
break;
case 1:
this.correctNumbers2.sort( function ( a, b) {
return a < b;
});
this.numberOrder2 = '(desc)';
break;
}
},
makeOANumber: function ( numberIndex, gridPosX, gridPosY, color, owner) {
this['correctNumbers' + owner].push(this.oaOperands[numberIndex]);
var number = new OANumber( this.oaOperands[numberIndex], color, {x: gridPosX, y: gridPosY}, owner);
this.oaNumbers.push(number);
// the hidden bg is only seen when there is no wood on the number position
// making the number look like it's glowing
this.stage.addChildAt(number.hiddenBG, 0);
this.stage.addChild(number.bg);
this.stage.addChild(number.txt);
},
createNotificationsArea: function () {
this.notificationsArea = dom.createDiv( 'bomberman_notifications');
var notificationsAreaForeground = dom.createDiv();
this.notificationsArea.appendChild(notificationsAreaForeground);
this.playerNotification1 = dom.createSpan( 'bomberman_p1_score', '', this.numberOrder1);
this.notificationsArea.appendChild(this.playerNotification1);
if (this.nrPlayers === 2) {
this.playerNotification2 = dom.createSpan( 'bomberman_p2_score', '', this.numberOrder2);
this.notificationsArea.appendChild(this.playerNotification2);
}
document.getElementById( 'renderBox').appendChild( this.notificationsArea);
this.pausedIndicator = dom.createSpan( 'pausedIndicator', '', 'Paused');
document.getElementById( 'renderBox').appendChild( this.pausedIndicator);
},
createBots: function () {
var bot1 = new Bot( this.images['george'], this.images['shadow'], {x: this.nrTilesWidth, y: 1});
this.stage.addChild( bot1.shadow);
this.stage.addChild(bot1.bmp);
this.bots.push(bot1);
var bot2 = new Bot( this.images['george2'], this.images['shadow'], {x: 1, y: this.nrTilesHeight});
this.stage.addChild( bot2.shadow);
this.stage.addChild(bot2.bmp);
this.bots.push(bot2);
if (this.nrPlayers !== 2) {
var bot3 = new Bot( this.images['george3'], this.images['shadow'], {x: this.nrTilesWidth, y: this.nrTilesHeight});
this.stage.addChild( bot3.shadow);
this.stage.addChild(bot3.bmp);
this.bots.push(bot3);
}
},
createPlayers: function () {
var player1 = new Player( this.images['betty'], this.images['shadow'], {x: 1, y: 1}, 1);
this.stage.addChild( player1.shadow);
this.stage.addChild( player1.bmp);
this.player1 = player1;
this.players.push( player1);
if (this.nrPlayers === 2) {
var player2 = new Player( this.images['betty2'], this.images['shadow'], {x: this.nrTilesWidth, y: this.nrTilesHeight}, 2);
this.stage.addChild( player2.shadow);
this.stage.addChild( player2.bmp);
this.player2 = player2;
this.players.push( player2);
}
},
buildRewardMap: function () {
var i, j;
var width = this.nrTilesWidth + 2;
var height = this.nrTilesHeight + 2;
this.rewardMap = [];
for ( i = 0; i < width; i++) {
this.rewardMap[i] = [];
for ( j = 0; j < height; j++) {
if ( this.tiles[i][j] && ((this.tiles[i][j] instanceof Wall) || (this.tiles[i][j] instanceof Wood) || (this.tiles[i][j] instanceof Bomb))) {
this.rewardMap[i][j] = -1;
} else {
this.rewardMap[i][j] = 0;
}
}
}
},
buildDangerMap: function () {
var i;
var width = this.nrTilesWidth + 2;
var height = this.nrTilesHeight + 2;
this.dangerMap = [];
for ( i = 0; i < width; i++) {
this.dangerMap[i] = [];
for ( var j = 0; j < height; j++) {
this.dangerMap[i][j] = 0;
}
}
},
buildAuxDangerMap: function () {
this.auxDangerMap = [];
var width = this.nrTilesWidth + 2;
var height = this.nrTilesHeight + 2;
for ( var i = 0; i < width; i++) {
this.auxDangerMap[i] = [];
}
},
initEvents: function () {
this.input.addListener( 'pause', this.togglePause);
createjs.Ticker.addEventListener( 'tick', this.update);
},
togglePause: function () {
var self = Bombergirl;
if (self.paused) {
self.manualPaused = false;
self.unPause();
} else {
self.pause();
self.manualPaused = true;
}
},
unPause: function () {
var self = Bombergirl;
if (self.manualPaused) {
return;
}
createjs.Ticker.removeAllEventListeners();
if (self.paused) {
if (!self.mute) {
createjs.Sound.setMute(false);
}
// Add the tick listener back so that objects can move
if ( !self.showingOff) {
createjs.Ticker.addEventListener('tick', self.update);
}
self.paused = false;
try {
self.canvas.style.opacity = '1';
self.notificationsArea.style.opacity = '1';
self.pausedIndicator.style.display = 'none';
} catch (e) {}
}
},
pause: function () {
var self = Bombergirl;
if (self.manualPaused) {
return;
}
if (!self.paused) {
if (!self.mute) {
createjs.Sound.setMute(true);
}
// Removing the ticker listener
createjs.Ticker.removeAllEventListeners();
self.paused = true;
// cancel all input commands so that when unpausing the game
// players will not move in the last direction given
self.input.cancelAll();
try {
self.canvas.style.opacity = '0.6';
self.notificationsArea.style.opacity = '0.6';
self.pausedIndicator.style.display = 'block';
} catch (e) {}
}
},
toggleSound: function () {
var self = Bombergirl;
if (!self.paused) {
createjs.Sound.setMute(!createjs.Sound.getMute());
self.mute = createjs.Sound.getMute();
}
},
/**
* @author Gabriel Titerlea
*
* is called on every tick
* */
update: function () {
var self = Bombergirl;
self.processInput();
self.collisionHandling();
self.botsThink();
self.animateObjects();
self.endGame();
},
processInput: function () {
// for each controllable player
for (var i = 1; i <= this.nrPlayers; i++) {
// check movement input
if (this.input.actions['up' + i] === true) {
this['player' + i].move( Direction.UP);
} else if (this.input.actions['down' + i] === true) {
this['player' + i].move( Direction.DOWN);
} else if (this.input.actions['left' + i] === true) {
this['player' + i].move( Direction.LEFT);
} else if (this.input.actions['right' + i] === true) {
this['player' + i].move( Direction.RIGHT);
} else {
this['player' + i].move( Direction.NONE);
}
// check bomb input
if (this.input.actions['bomb' + i] === true) {
var bomb = this['player' + i].placeBomb( this.images['bomb'], this.images['shadow']);
if (bomb) {
this.bombs.push(bomb);
this.tiles[bomb.gridPosition.x][bomb.gridPosition.y] = bomb;
for (var k = 0; k < this.players.length; k++) {
var playerEsc = this.players[k];
var pGridPos = Utils.convertToGridPosition(playerEsc.position);
if (pGridPos.x === bomb.gridPosition.x && pGridPos.y === bomb.gridPosition.y) {
playerEsc.escapeBomb = bomb;
}
}
this.stage.addChildAt(bomb.bmp, 0);
this.stage.addChildAt(bomb.shadow, 0);
}
if (this.touch) {
this.input.actions['bomb' + i] = false;
}
}
}
},
collisionHandling: function () {
for (var i = 0; i < this.players.length; i++) {
var player = this.players[i];
this.tileCollision( player);
if ( !player.dead) {
this.bonusCollision( player);
// this.OANumbersCollision( this['player' + (i + 1)]);
this.playerFireCollision( player);
}
}
for (var i = 0; i < this.bots.length; i++) {
var bot = this.bots[i];
this.botFireCollision( bot);
this.bonusCollision( bot);
}
},
tileCollision: function ( player) {
// check wall collision only if the player is moving
if ( player.direction !== Direction.NONE) {
var collision = false;
player.willCollide = false;
// auxiliary position, to see if moving will cause a collision
var auxPlayerPos = {
x: player.position.x,
y: player.position.y
};
// make the auxiliary move
auxPlayerPos.x += player.direction.x * player.speed;
auxPlayerPos.y += player.direction.y * player.speed;
var playerGridPos = Utils.convertToGridPosition( auxPlayerPos);
// iterate in a spiral, the tiles in the vicinity of the player to check for collision
var x = playerGridPos.x;
var y = playerGridPos.y;
if (this.checkCollision( x - 1, y - 1, player, auxPlayerPos)) { collision = true;}
if (this.checkCollision( x, y - 1, player, auxPlayerPos)) { collision = true;}
if (this.checkCollision( x + 1, y - 1, player, auxPlayerPos)) { collision = true;}
if (this.checkCollision( x + 1, y, player, auxPlayerPos)) { collision = true;}
if (this.checkCollision( x + 1, y + 1, player, auxPlayerPos)) { collision = true;}
if (this.checkCollision( x, y + 1, player, auxPlayerPos)) { collision = true;}
if (this.checkCollision( x - 1, y + 1, player, auxPlayerPos)) { collision = true;}
if (this.checkCollision( x - 1, y, player, auxPlayerPos)) { collision = true;}
if (this.checkCollision( x, y, player, auxPlayerPos)) { collision = true;}
if (!collision) {
// if there was no collision it means we have escaped the escapeBomb, if there was one
player.escapeBomb = null;
}
}
},
checkCollision: function ( i, j, player, auxPlayerPos) {
var collision = false;
var tile = this.tiles[i][j];
if (tile && Utils.intersectPlayerAndTile( auxPlayerPos, tile.position)) {
if (tile !== player.escapeBomb) {
player.willCollide = true;
// --- CORNER FIX --- //
var auxPlayerPosFix = { x: auxPlayerPos.x, y: auxPlayerPos.y};
if (player.direction === Direction.UP) {
auxPlayerPosFix.x -= 27;
if (!this.tiles[i - 1][j] && (!this.tiles[i - 1][j + 1] || this.tiles[i - 1][j + 1] === player.escapeBomb) && !Utils.intersectPlayerAndTile(auxPlayerPosFix, tile.position)) { // if the tile is empty we can move there
player.move( Direction.LEFT, true);
player.willCollide = false;
}
auxPlayerPosFix.x += 54;
if (!this.tiles[i + 1][j] && (!this.tiles[i + 1][j + 1] || this.tiles[i + 1][j + 1] === player.escapeBomb) && !Utils.intersectPlayerAndTile(auxPlayerPosFix, tile.position)) { // if the tile is empty we can move there
player.move( Direction.RIGHT, true);
player.willCollide = false;
}
} else if (player.direction === Direction.DOWN) {
auxPlayerPosFix.x -= 27;
if (!this.tiles[i - 1][j] && (!this.tiles[i - 1][j - 1] || this.tiles[i - 1][j - 1] === player.escapeBomb) && !Utils.intersectPlayerAndTile(auxPlayerPosFix, tile.position)) { // if the tile is empty we can move there
player.move( Direction.LEFT, true);
player.willCollide = false;
}
auxPlayerPosFix.x += 54;
if (!this.tiles[i + 1][j] && (!this.tiles[i + 1][j - 1] || this.tiles[i + 1][j - 1] === player.escapeBomb) && !Utils.intersectPlayerAndTile(auxPlayerPosFix, tile.position)) { // if the tile is empty we can move there
player.move( Direction.RIGHT, true);
player.willCollide = false;
}
} else if (player.direction === Direction.LEFT) {
auxPlayerPosFix.y -= 27;
if (!this.tiles[i][j - 1] && (!this.tiles[i + 1][j - 1] || this.tiles[i + 1][j - 1] === player.escapeBomb) && !Utils.intersectPlayerAndTile(auxPlayerPosFix, tile.position)) { // if the tile is empty we can move there
player.move( Direction.UP, true);
player.willCollide = false;
}
auxPlayerPosFix.y += 54;
if (!this.tiles[i][j + 1] && (!this.tiles[i + 1][j + 1] || this.tiles[i + 1][j + 1] === player.escapeBomb) && !Utils.intersectPlayerAndTile(auxPlayerPosFix, tile.position)) { // if the tile is empty we can move there
player.move( Direction.DOWN, true);
player.willCollide = false;
}
} else if (player.direction === Direction.RIGHT) {
auxPlayerPosFix.y -= 27;
if (!this.tiles[i][j - 1] && (!this.tiles[i - 1][j - 1] || this.tiles[i - 1][j - 1] === player.escapeBomb) && !Utils.intersectPlayerAndTile(auxPlayerPosFix, tile.position)) { // if the tile is empty we can move there
player.move( Direction.UP, true);
player.willCollide = false;
}
auxPlayerPosFix.y += 54;
if (!this.tiles[i][j + 1] && (!this.tiles[i - 1][j + 1] || this.tiles[i - 1][j + 1] === player.escapeBomb) && !Utils.intersectPlayerAndTile(auxPlayerPosFix, tile.position)) { // if the tile is empty we can move there
player.move( Direction.DOWN, true);
player.willCollide = false;
}
}
}
collision = true;
}
return collision;
},
bonusCollision: function ( player) {
var i = this.bonuses.length;
while (i--) {
var bonus = this.bonuses[i];
if ( Utils.intersectPlayerAndTile( player.position, bonus.position)) {
player.applyBonus( bonus);
// if this is not a bot
if (player.id && player.id === 1) {
player.score += 10;
this['playerNotification' + player.id].innerHTML = player.score;
} else if (player.id && player.id === 2) {
player.score += 10;
this['playerNotification' + player.id].innerHTML = player.score;
}
createjs.Sound.play('bonus').setVolume(0.6);
// remove bonus from reward map
this.spreadRewards( bonus, true);
this.stage.removeChild(bonus.bmp);
this.bonuses.splice(i, 1);
}
}
this.calculateRewardMap();
},
/**
* @author Gabriel Titerlea
*
* Handles the collision of the players with the OANumbers
* */
OANumbersCollision: function (player) {
if (player) {
var i = this.oaNumbers.length;
while (i--) {
var oaNumber = this.oaNumbers[i];
if ( Utils.intersectPlayerAndTile(player.position, oaNumber.position)) {
// if this OANumber belongs to this player
if (oaNumber.owner === player.id) {
if (this['correctNumbers' + player.id][0] === oaNumber.nr) {
createjs.Sound.play('correct').setVolume(0.5);
player.score += 100 + player.streak;
player.streak += 50;
} else {
createjs.Sound.play('wrong').setVolume(0.5);
player.score -= 50;
player.streak = 0;
if (player.score < 0) {
player.score = 0;
}
}