-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathgame.js
1375 lines (1227 loc) · 40.9 KB
/
game.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 loadState = {
loading_label: null,
preload: function(){
game.physics.startSystem(Phaser.Physics.ARCADE);
var cx = game.world.centerX;
var cy = game.world.centerY;
this.loading_label = game.add.text(cx, cy, 'loading ...',
{font: '64px Arial', fill: '#ffffff'});
this.loading_label.anchor.setTo(0.5,0.5);
this.loading_label.alpha = 0;
var loading_tween = game.add.tween(this.loading_label).to({ alpha: 1 }, 1000, "Linear", true, 0, -1);
loading_tween.yoyo(true, 1500);
// load json
game.load.json('data', 'assets/data.json');
game.load.json('maps', 'assets/maps.json');
// load atlas
game.load.atlasJSONHash('image', 'assets/image.png', 'assets/image.json');
game.load.image('tileset', 'assets/tileset.png');
// load sound
game.load.audiosprite('sound', 'assets/sound_effect.mp3', 'assets/sound_effect.json');
},
create: function(){
// creat sound
sound = game.add.audioSprite('sound');
// fade out
this.camera.fade('#000', 500);
this.camera.onFadeComplete.addOnce(function() {
theme_music = sound.play('theme_music');
this.game.state.start('menu');
}, this);
}
};
function object(o){
function F(){};
F.prototype = o;
return new F();
}
function inheritPrototype(subType, superType){
var prototype = object(superType.prototype);
prototype.constructor = subType;
subType.prototype = prototype;
}
function selectFrom(lowerValue, upperValue) {
var choices = upperValue - lowerValue + 1;
return Math.floor(Math.random() * choices + lowerValue);
}
// button prefabs
function ButtonPrefab(x, y, name, callback, obj){
var bt_data = {
'resume':{'normal':'01','over':'05','pressed':'09'},
'return':{'normal':'02','over':'06','pressed':'10'},
'start':{'normal':'03','over':'07','pressed':'11'},
'about':{'normal':'04','over':'08','pressed':'12'}
};
var dat = bt_data[name];
this.button = game.add.button(x, y, 'image', callback, obj,
'buttons/button_' + dat.over, 'buttons/button_' + dat.normal, 'buttons/button_' + dat.pressed);
this.button.anchor.setTo(0.5,0.5);
/*this.button.onInputOver.add((e) => {
console.log('over');
var butenter_sound = game.add.audio('butenter');
butenter_sound.play();
});*/
this.button.onInputDown.add((e) => {
sound.play('butpressed');
});
}
ButtonPrefab.prototype.setAnchor = function(x,y){
this.button.anchor.setTo(x,y);
}
ButtonPrefab.prototype.enable = function(){
this.button.inputEnabled = true;
}
ButtonPrefab.prototype.disable = function(){
this.button.inputEnabled = false;
}
var menuState = {
menus : null,
start_button: null,
about_button: null,
about_panel:null,
startCallback : function(){
// fade out
this.camera.fade('#000', 500);
this.camera.onFadeComplete.addOnce(function() {
this.game.state.start('level');
}, this);
},
aboutReturnCallback: function(){
this.about_panel.visible = false;
this.start_button.enable();
this.about_button.enable();
},
aboutCallback : function(){
this.start_button.disable();
this.about_button.disable();
this.about_panel = game.add.group();
var cx = game.world.centerX;
var cy = game.world.centerY;
var aboutbg = game.add.sprite(cx, cy, 'image','window');
aboutbg.anchor.setTo(0.5, 0.5);
this.about_panel.add(aboutbg);
var return_button = new ButtonPrefab(cx, cy + 128, 'return', menuState.aboutReturnCallback, this);
this.about_panel.add(return_button.button);
// text
var content = "Author: lianera\n"+
"Link: lianera.com\n" +
"Powered by Phaser";
var style = { font: "28px Consolas", fill: "#fff", stroke: '#555',strokeThickness:6};
var text = game.add.text(cx, cy - 64, content, style);
text.anchor.setTo(0.5,0.5);
//text.setTextBounds(cx - 128, cy - 128, 256, 256);
this.about_panel.add(text);
},
create: function(){
// fade in
this.camera.flash('#000000', 500);
var menu_sprite = game.add.sprite(0, 0, 'image', 'menu');
//var enter_key = game.input.keyboard.addKey(Phaser.Keyboard.W);
//enter_key.onDown.addOnce(this.start, this);
this.menus = game.add.group();
var cx = game.world.centerX;
var cy = game.world.centerY;
this.start_button = new ButtonPrefab(cx, cy, 'start', menuState.startCallback, this);
this.menus.add(this.start_button.button);
this.about_button = new ButtonPrefab(cx, cy + 96, 'about', menuState.aboutCallback, this);
this.menus.add(this.about_button.button);
},
};
var levelState = {
level_background: null,
levels : null,
preload: function(){
},
levelCallback : function(i){
var e = this.levels[i];
theme_music.fadeOut(500);
// fade out
this.camera.fade('#000', 500);
this.camera.onFadeComplete.addOnce(function() {
this.game.state.start('play', true, false, e.map, i);
}, this);
},
returnCallback: function(){
// fade out
this.camera.fade('#000', 500);
this.camera.onFadeComplete.addOnce(function() {
this.game.state.start('menu');
}, this);
},
addButton:function(i, type){
var e = this.levels[i];
var b = null;
if(type == 'passed')
b = game.add.button(e.iconx, e.icony, 'image', () => this.levelCallback(i), this, 'system/system_06', 'system/system_05', 'system/system_07');
else if(type == 'next')
b = game.add.button(e.iconx, e.icony, 'image', () => this.levelCallback(i), this, 'system/system_02', 'system/system_01', 'system/system_03');
else
b = game.add.sprite(e.iconx, e.icony, 'image', 'system/system_04');
b.anchor.setTo(0.5, 0.5);
var style = { font: "32px Arial", fill: "#ffffff", stroke: '#555555',strokeThickness:4, wordWrap: true, wordWrapWidth: 64};
var t = game.add.text(e.iconx, e.icony, e.name, style);
t.anchor.setTo(0.5, 0.45);
},
create: function(){
this.camera.flash('#000000', 500);
this.level_background = game.add.sprite(0, 0, 'image', 'level');
var data = game.cache.getJSON('data');
this.levels = data.levels;
var is_next = new Array(this.levels.length);
var is_passed = new Array(this.levels.length);
var passed_count = 0;
for(let i in this.levels){
var e = this.levels[i];
var storage = localStorage.getItem('level'+i);
if(storage == 'true'){
this.addButton(i, 'passed');
passed_count++;
for(var j in e.next){
var n = e.next[j];
is_next[n] = true;
}
is_passed[i] = true;
}
}
for(let i in this.levels){
if(is_passed[i])
continue;
if(is_next[i]){
this.addButton(i, 'next');
}else{
this.addButton(i, 'unlocked');
}
}
if(passed_count == 0)
this.addButton(0, 'next');
// add return button
this.return_button = new ButtonPrefab(game.world.width - 16, game.world.height - 16, 'return', levelState.returnCallback, this);
this.return_button.setAnchor(1,1);
}
};
var playState = {
panel: null,
player: null,
input: null,
popup_menu: null,
map: null,
level: null,
battle_music: null,
init: function(map, level){
this.map = map;
this.level = level;
},
preload: function(){
game.time.advancedTiming = true;
// load map
var maps = game.cache.getJSON('maps');
var map_obj = maps[this.map];
game.load.tilemap(this.map, null, map_obj, Phaser.Tilemap.TILED_JSON);
},
resumeCallback: function(){
this.popup_menu.visible = false;
game.paused = false;
sound.play('butpressed');
},
returnToLevel: function(){
if(this.battle_music)
this.battle_music.fadeOut(500);
// fade out
this.camera.fade('#000', 500);
this.camera.onFadeComplete.addOnce(function() {
sound.play('theme_music');
this.game.state.start('level');
}, this);
},
returnCallback: function(){
this.popup_menu.visible = false;
game.paused = false;
sound.play('butpressed');
this.returnToLevel();
},
onMenu: function(){
game.paused = true;
// popup menu
this.popup_menu = game.add.group();
var cx = game.world.centerX;
var cy = game.world.centerY;
var popbg = game.add.sprite(cx, cy, 'image', 'popup');
popbg.anchor.setTo(0.5, 0.5);
this.popup_menu.add(popbg);
this.resume_button = new ButtonPrefab(cx, cy, 'resume', playState.resumeCallback, this);
this.popup_menu.add(this.resume_button.button);
this.return_button = new ButtonPrefab(cx, cy + 96, 'return', playState.returnCallback, this);
this.popup_menu.add(this.return_button.button);
},
create: function(){
// fade in
this.camera.flash('#000000', 500);
this.esc = game.input.keyboard.addKey(Phaser.Keyboard.ESC);
this.esc.onDown.add(this.onMenu, this);
panel = new Panel(this.map);
panel.createCharacters();
game.time.events.add(Phaser.Timer.SECOND * 2.5, function(){
// play music
this.battle_music = sound.play('battle_music');
}, this);
},
showReadyGo : function(){
// 'ready' 'go' text
var cx = game.world.centerX;
var cy = game.world.centerY;
this.ready_label = game.add.text(cx, cy, 'READY',
{font: '64px Arial', fill: '#ffffff', stroke: '#a50',strokeThickness:10});
this.ready_label.anchor.setTo(0.5, 0.5);
this.ready_count = 0;
this.timer = game.time.events.loop(250, function(){
var t = ['READY', 'READY','READY', 'READY','GO'];
if(this.ready_count < t.length){
game.world.bringToTop(this.ready_label);
this.ready_label.text = t[this.ready_count];
//this.ready_label.scale.set(1,1);
this.ready_label.alpha = 0;
game.add.tween(this.ready_label).to( { alpha: 1 }, 200, Phaser.Easing.Quartic.InOut, true);
//game.add.tween(this.ready_label.scale).to( { x: 1.1, y:1.1 }, 300, Phaser.Easing.Quartic.Out, true);
}else{
game.time.events.remove(this.timer);
this.ready_label.visible = false;
panel.status = 'playing';
}
if(this.ready_count == 0){
sound.play('readygo');
}
this.ready_count++;
}, this);
panel.status = 'ready_waiting';
},
showResult : function(text){
panel.player.standStill();
for(var e in panel.enemies){
panel.enemies[e].standStill();
}
this.battle_music.stop();
var cx = game.world.centerX;
var cy = game.world.centerY;
var menu_sprite = game.add.sprite(cx, cy, 'image', 'window');
menu_sprite.scale.set(0.5,0.5);
menu_sprite.anchor.setTo(0.5,0.5);
if(text == '胜利'){
var emitter = game.add.emitter(cx, cy, 100);
emitter.setXSpeed(-300, 300);
emitter.setYSpeed(-300, 300);
var ani = Phaser.Animation.generateFrameNames('effects/effect_', 41, 48, '', 2);
emitter.makeParticles('image', ani);
emitter.gravity = 150;
emitter.start(true, this.explosion_time, null, 100);
}
this.result_label = game.add.text(cx, cy, text,
{font: '72px Arial', fill: '#ffffff', stroke: '#850',strokeThickness:10});
this.result_label.anchor.setTo(0.5, 0.5);
this.result_label.scale.set(0.1,0.1);
var tween = game.add.tween(this.result_label.scale).to( { x:1, y:1 }, 1500, Phaser.Easing.Quartic.InOut, true);
//tween.yoyo(true, 500);
panel.status = 'ending';
game.time.events.add(Phaser.Timer.SECOND * 4, function(){
this.returnToLevel();
}, this);
},
update: function(){
panel.update();
if(panel.status == 'ready'){
this.showReadyGo();
// manually victory/failed
// panel.enemies = [];
// panel.player.state = 'dead';
}if(panel.status == 'victory'){
localStorage.setItem('level'+this.level, 'true');
this.showResult('胜利');
// play sound
sound.play('victory');
}else if(panel.status == 'failed'){
this.showResult('失败');
// play sound
sound.play('failed');
}
},
render: function() {
//game.debug.text('fps:'+game.time.fps, 2, 14, "#fff");
}
};
function Character(data, panel, mapx, mapy, type){
this.panel = panel;
this.type = type;
this.health = data.health;
this.attack = data.attack;
this.speed = data.speed;
this.mapx = mapx;
this.mapy = mapy;
this.stand_frame = 'characters/'+type+'_down_1';
this.bomb_type = data.bomb_type;
this.bomb_range = data.bomb_range;
this.bomb_shape = data.bomb_shape;
this.goal_mapx = this.mapx;
this.goal_mapy = this.mapy;
// states: stand, walking_left, walking_right, walking_up, walking_down
this.state = 'stand';
// world position
var coord = panel.mapToCoord(this.mapx, this.mapy);
this.sprite = game.add.sprite(coord.x, coord.y, 'image', this.stand_frame);
var style = { font: "Arial", fill: "#ffffff", stroke: '#880000',strokeThickness:3};
this.health_text = game.add.text(coord.x, coord.y, String(this.health), style);
game.physics.enable(this.sprite,Phaser.Physics.ARCADE);
// constants
this.dirs = ['left', 'right', 'up', 'down'];
// animations
for(var d in this.dirs){
var ani = new Array(3);
for(var i = 0; i < 3; i++)
ani[i] = 'characters/' + type + '_' + this.dirs[d] + '_' + i;
this.sprite.animations.add('walking_' + this.dirs[d], ani, 10, true);
}
this.dir_action = {
'left' : {'dx':-1, 'dy': 0, 'vx':-1, 'vy': 0, 'ani': 'walking_left', 'state': 'walking_left'},
'right' : {'dx': 1, 'dy': 0, 'vx': 1, 'vy': 0, 'ani': 'walking_right', 'state': 'walking_right'},
'up' : {'dx': 0, 'dy':-1, 'vx': 0, 'vy':-1, 'ani': 'walking_up', 'state': 'walking_up'},
'down' : {'dx': 0, 'dy': 1, 'vx': 0, 'vy': 1, 'ani': 'walking_down', 'state': 'walking_down'}
};
this.dir_frames = {'left': 0, 'right': 0, 'up': 0, 'down': 0};
}
Character.prototype.walk = function(direction){
var action = this.dir_action[direction];
var gx = this.mapx + action.dx;
var gy = this.mapy + action.dy;
// check boarder & map
if(gx < 0 || gx >= this.panel.width || gy < 0 || gy >= this.panel.height
|| !this.panel.walkable(gx, gy)){
this.standStill();
return false;
}
this.goal_mapx = gx;
this.goal_mapy = gy;
// turn
if(this.dir_action[direction].state != this.state){
// reset coordination
var coord = this.panel.mapToCoord(this.mapx, this.mapy);
this.sprite.body.x = coord.x;
this.sprite.body.y = coord.y;
// set velocity
this.sprite.body.velocity.x = this.speed*action.vx;
this.sprite.body.velocity.y = this.speed*action.vy;
this.sprite.animations.play(action.ani);
this.state = action.state;
}// else continue
return true;
}
Character.prototype.putBomb = function(){
if(panel.canPutBomb(this.mapx, this.mapy)){
var bomb = new Bomb(panel, this.mapx, this.mapy, this.attack, this.bomb_type, this.bomb_range, this.bomb_shape);
panel.putBomb(bomb);
return true;
}
return false;
}
Character.prototype.standStill = function(){
if(this.state == 'dead')
return;
var coord = this.panel.mapToCoord(this.mapx, this.mapy);
this.goal_mapx = this.mapx;
this.goal_mapy = this.mapy;
this.sprite.body.velocity.x = 0;
this.sprite.body.velocity.y = 0;
this.sprite.body.x = coord.x;
this.sprite.body.y = coord.y;
this.sprite.animations.stop();
this.sprite.frame = this.stand_frame;
this.state = 'stand';
}
Character.prototype.update = function(){
if(this.state == 'dead')
return;
// update map position
var pos_map = this.panel.coordToMap(this.sprite.body.x, this.sprite.body.y);
this.mapx = pos_map.mapx;
this.mapy = pos_map.mapy;
// goal reach check
var goal_coord = this.panel.mapToCoord(this.goal_mapx, this.goal_mapy);
var dx = goal_coord.x - this.sprite.body.x;
var dy = goal_coord.y - this.sprite.body.y;
if(this.state == 'walking_left' && dx >= 0
|| this.state == 'walking_right' && dx <= 0
|| this.state == 'walking_up' && dy >= 0
|| this.state == 'walking_down' && dy <= 0){
this.onReachGoal();
}
// update health text
this.health_text.x = this.sprite.body.x;
this.health_text.y = this.sprite.body.y;
this.health_text.text = String(Math.ceil(this.health));
}
Character.prototype.onReachGoal = function(){
this.mapx = this.goal_mapx;
this.mapy = this.goal_mapy;
this.panel.checkProp(this);
}
Character.prototype.onAttack = function(atk_val){
this.health -= atk_val;
// death check
if(this.health <= 0){
this.health = 0;
this.onDead();
}
}
Character.prototype.onDead = function(){
if(this.state == 'dead')
return;
this.sprite.body.velocity.x = 0;
this.sprite.body.velocity.y = 0;
this.sprite.animations.stop();
this.sprite.frame = this.stand_frame;
this.state = 'dead';
this.sprite.destroy();
this.health_text.destroy();
// dead effects
var coord = this.panel.mapToCoord(this.mapx, this.mapy);
var emitter = game.add.emitter(coord.x + 32, coord.y + 32);
emitter.setXSpeed(-20, 20);
emitter.setYSpeed(-20, 20);
emitter.setAlpha(0.1, 0.3, 50);
var ani = Phaser.Animation.generateFrameNames('effects/effect_', 17, 24, '', 2);
emitter.makeParticles('image', ani);
emitter.gravity = 0;
emitter.start(true, 2000, null, 30);
this.emitter = emitter;
game.time.events.add(2000, this.destroyEmitter, this);
}
Character.prototype.destroyEmitter = function(){
this.emitter.destroy();
}
function Player(data, panel, mapx, mapy){
Character.call(this, data, panel, mapx, mapy, 'player');
var input = game.input.keyboard.createCursorKeys();
input.spacebar = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);
this.input = input;
this.bomb_cd = 0;
this.last_dir = null;
// key events
input.left.onDown.add(function(){
this.last_dir = 'left';
}, this);
this.key_record = {'left': Infinity, 'right': Infinity, 'up': Infinity, 'down': Infinity};
}
inheritPrototype(Player, Character);
Player.prototype.onReachGoal = function(){
Character.prototype.onReachGoal.call(this);
var d = this.getDirectionKey();
if(!d || !this.walk(d))
this.standStill();
}
Player.prototype.onAttack = function(atk_val){
Character.prototype.onAttack.call(this, atk_val);
}
Player.prototype.onDead = function(){
Character.prototype.onDead.call(this);
// return to level state
}
Player.prototype.getDirectionKey = function(){
var min = Infinity;
var k = null;
for(var d in this.dirs){
var dir = this.dirs[d];
if(this.key_record[dir] < min){
min = this.key_record[dir];
k = dir;
}
}
return k;
}
Player.prototype.update = function(){
// key record
for(var d in this.dirs){
var dir = this.dirs[d];
if(this.input[dir].isDown){
if(isFinite(this.key_record[dir]))
this.key_record[dir]++;
else
this.key_record[dir] = 0;
}else
this.key_record[dir] = Infinity;
}
// walk check
if(this.state == 'stand'){
var d = this.getDirectionKey();
if(d){
this.walk(d);
}
}
// put bomb check
if(this.input.spacebar.isDown){
if(this.bomb_cd > 0){
this.bomb_cd-=game.time.elapsed;
}else{
if(this.putBomb()){
sound.play('putbomb');
}
this.bomb_cd = 200;
}
}else{
this.bomb_cd = 0;
}
Character.prototype.update.call(this);
};
// map: 2d array, return path
function AStar(map_array, sx, sy, tx, ty, faraway = false){
var dirs = [
{'x': -1, 'y': 0},
{'x': 1, 'y': 0},
{'x': 0, 'y': -1},
{'x': 0, 'y': 1},
];
var width = map_array[0].length;
var height = map_array.length;
var nodedir = new Array(height);
for(var ni = 0; ni < height; ni ++){
nodedir[ni] = new Array(width);
}
var openset = [];
var closedset = [];
var first = {'x':sx, 'y':sy, 'g':0};
var global_min_node = first;
var global_min_cost = Infinity;
closedset.push(first);
// untile last node reach (tx, ty)
while(faraway? closedset.length < width*height : closedset[closedset.length-1].x != tx ||
closedset[closedset.length -1].y != ty){
var last = closedset[closedset.length - 1];
// check around
for(var d in dirs){
var x = last.x+dirs[d].x;
var y = last.y+dirs[d].y;
// check boarder
if(x < 0 || x >= width || y < 0 || y >= height)
continue;
var valid = true;
// find in open set
for(var i in openset){
if(openset[i].x == x && openset[i].y == y){
valid = false;
break;
}
}
if(!valid)
continue;
// find in closed set
for(var i in closedset){
if(closedset[i].x == x && closedset[i].y == y){
valid = false;
break;
}
}
if(!valid)
continue;
// valid
nodedir[y][x] = d;
openset.push({'x':x,'y':y, 'g': last.g + 1});
}
// find lowest cost in open set
var min_cost = Infinity;
var minid = 0;
for(var i in openset){
var x = openset[i].x;
var y = openset[i].y;
// computer cost function: f(x) = g(x) + h(x) + obs(x)
var h = 0;
if(faraway)
h = -Math.abs(tx - x) - Math.abs(ty - y);
else
h = Math.abs(tx - x) + Math.abs(ty - y);
var cost = openset[i].g + h*2 + map_array[y][x];
if(cost <= min_cost){
min_cost = cost;
minid = i;
}
}
if(!isFinite(min_cost))
break;
var min_node = openset[minid];
// remove the lowest cost node from open set
openset.splice(minid, 1);
// add to closed set
closedset.push(min_node);
if(min_cost < global_min_cost){
global_min_cost = min_cost;
global_min_node = min_node;
}
}
// trace back
var path = [];
var nx, ny;
if(faraway){
nx = global_min_node.x;
ny = global_min_node.y;
}else{
nx = closedset[closedset.length - 1].x;
ny = closedset[closedset.length - 1].y;
}
while(nx != sx || ny != sy){
path.push({'x':nx,'y':ny});
var thedir = nodedir[ny][nx];
nx -= dirs[thedir].x;
ny -= dirs[thedir].y;
}
path.push({'x':sx,'y':sy});
return path.reverse();
}
function pathCost(map_array, path){
var block_cost = 0;
for(var b in path){
var x = path[b].x;
var y = path[b].y;
block_cost += map_array[y][x];
}
return block_cost + path.length;
}
function Enemy(data, panel, mapx, mapy, type){
Character.call(this, data, panel, mapx, mapy, type);
this.escape_mapx = null;
this.escape_mapy = null;
this.action_state = 'idle';
}
inheritPrototype(Enemy, Character);
Enemy.prototype.update = function(){
Character.prototype.update.call(this);
if(this.action_state == 'idle'){
this.takeAction();
}
}
Enemy.prototype.onReachGoal = function(){
Character.prototype.onReachGoal.call(this);
this.takeAction();
}
Enemy.prototype.takeAction = function(){
// estimate behaviors
var behaviors = this.behaviorEstimate();
// find minimum cost behavior
var min_b = 'idle';
for(var b in behaviors){
if(behaviors[b].cost < behaviors[min_b].cost){
min_b = b;
}
}
var player = this.panel.player;
//console.log(min_b);
this.action_state = min_b;
// take action
switch(min_b){
case 'idle':
this.standStill();
break;
case 'chase':
this.walkTo(player.mapx, player.mapy);
break;
case 'escape':
this.escape(behaviors.escape.bombs);
break;
case 'getprop':
var prop = behaviors['getprop'].prop;
this.walkTo(prop.x, prop.y);
break;
case 'putbomb':
this.putBomb();
this.action_state = 'idle';
break;
}
}
Enemy.prototype.behaviorEstimate = function(){
// precompute data
var props = this.panel.getProps();
var map_array = this.panel.toArray();
var player = this.panel.player;
// idle
var idle_behavior = {'cost':Infinity};
// escape
var near_bombs = this.panel.getNearBombs(this.mapx, this.mapy);
var escape_cost = 0;
for(var b in near_bombs){
escape_cost += near_bombs[b].attack;
}
var escape_behavior = {
'cost': 1000 + 10 / escape_cost,
'bombs': near_bombs
};
// get prop
var min_prop = null;
var min_prop_cost = Infinity;
for(var p in props){
var prop = props[p];
var path = AStar(map_array, this.mapx, this.mapy, prop.x, prop.y);
var cost = pathCost(map_array,path);
if(cost < min_prop_cost){
min_prop_cost = cost;
min_prop = prop;
}
}
var getprop_behavior = {'cost': 1000 + min_prop_cost, 'prop': min_prop};
// chase
var chase_path = AStar(map_array, this.mapx, this.mapy, player.mapx, player.mapy);
var chase_cost = 1000 + pathCost(map_array, chase_path);
var chase_behavior = {'cost':chase_cost};
// put bomb
var putbomb_cost =
(this.isNearChest() || this.isNearPlayer())
&& !this.panel.hasBomb(this.mapx, this.mapy) ?
-100:Infinity;
var putbomb_behavior = {'cost':putbomb_cost};
var behaviors = {'idle':idle_behavior,
'chase':chase_behavior,
'escape':escape_behavior,
'getprop':getprop_behavior,
'putbomb':putbomb_behavior};
return behaviors;
}
// give a target, make next step
Enemy.prototype.walkTo = function(targetx, targety){
var map_arr = this.panel.walkableArray();
// follow player
var sx = this.mapx;
var sy = this.mapy;
//map_arr[sy][sx] = 0;
var path = AStar(map_arr, sx, sy, targetx, targety);
if(path.length <= 1){
this.standStill();
this.action_state = 'idle';
return false;
}
var dx = path[1].x - path[0].x;
var dy = path[1].y - path[0].y;
var r = false;
if(dx>0) r = this.walk('right');
else if(dx<0) r = this.walk('left');
else if(dy>0) r = this.walk('down');
else if(dy<0) r = this.walk('up');
else{
console.log('logic error');
}
if(!r){
// the tile blocked the way?
var block_health = this.panel.getBlockHealth(path[1].x, path[1].y);
if(block_health && isFinite(block_health))
this.putBomb();
this.standStill();
this.action_state = 'idle';
}else{
//this.action_state = 'walking';
}
return r;
}
Enemy.prototype.isNearPlayer = function(){
var player = this.panel.player;
var dx = player.mapx - this.mapx;
var dy = player.mapy - this.mapy;
return dx*dx + dy*dy <= 1;
}
Enemy.prototype.isNearChest = function(){
var dx = [-1,0,1,0];
var dy = [0,-1,0,1];
for(var i = 0; i < 4; i++){
var x = this.mapx + dx[i];
var y = this.mapy + dy[i];
var type = this.panel.getBlockType(x, y);
if(type == 'chest')
return true;
}
return false;
}
Enemy.prototype.escape = function(bombs){
var sx = this.mapx;
var sy = this.mapy;
//get nearest bomb
var bomb = bombs[0];
for(var i in bombs){
if(Math.abs(bombs[i].mapx - sx)+Math.abs(bombs[i].mapy - sy)
< Math.abs(bomb.mapx - sx)+Math.abs(bomb.mapy - sy))
bomb = bombs[i];
}
var tx = bomb.mapx;
var ty = bomb.mapy;
var map_arr = this.panel.walkableArray();
this.escape_bomb = bomb;
var path = AStar(map_arr, sx, sy, tx, ty, true);
if(path.length <= 1){
this.standStill();
this.action_state = 'idle';
return false;
}
var dx = path[1].x - path[0].x;
var dy = path[1].y - path[0].y;
var r = false;
if(dx>0) r = this.walk('right');
else if(dx<0) r = this.walk('left');
else if(dy>0) r = this.walk('down');
else if(dy<0) r = this.walk('up');
else{
console.log('logic error');
}
if(!r){
// the tile blocked the way?
var block_health = this.panel.getBlockHealth(path[1].x, path[1].y);
if(block_health && isFinite(block_health))
this.putBomb();
this.standStill();
this.action_state = 'idle';
}else{
//this.action_state = 'walking';
}
return r;
}
function Bomb(panel, mapx, mapy, base_attack, type, range, shape){
this.mapx = mapx;
this.mapy = mapy;
this.panel = panel;
this.type = type;
this.range = range;