-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
2582 lines (2263 loc) · 69.4 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
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
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<meta name="copyright" content= "丁超兵版权所有 Copyright (c) 2011" />
<meta name="viewport" content="minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0, width=device-width, user-scalable=no">
<title>Battle City 坦克大战 HTML5 复刻版</title>
<style type="text/css">
*{padding:0; margin:0;}
.cRed{ color:#B53120;}
.fb{ font-weight:900;}
.tr{ text-align:right;}
body{ text-align:center; font:100 15px/24px 'Microsoft YaHei'; color:#2b2b2b; background:#FBF9FF;}
body>canvas{ margin:20px 0; }
body>div{ width:46%; padding:15px; margin:0 auto; text-align:left; /* border:2px solid #f3f3f3; -webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; */}
body>div>h3{ padding-left:12px; color:#2E8B57;}
body>div label{ padding:2px 6px 2px 2px;}
body>div>p, body>div>table{ padding:6px 12px 12px 12px; margin:0 0 24px 0; border-bottom:1px dotted #ccc;}
body>div>table{ width:70%; padding:0; margin:12px; font-size:13px; border:1px solid #E8E8E8; border-collapse: collapse;}
body>div>table td, body>div>table th{ padding:3px; text-align:center;}
body>div>table td{ color:#666; border-right:1px solid #E8E8E8; border-bottom:1px solid #E8E8E8; background:#fff;}
body>div>table th{ font-size:15px; background:#F2F2F2;}
body>div>ul{ padding:8px 0 0 28px;}
</style>
</head>
<body>
<noscript>
<div class="noscript">
你好,需要浏览器开启 JavaScript 功能才能运行本程序。<br/>
请设置浏览器开启 JavaScript 功能,然后重试。
</div>
</noscript>
<script type="text/javascript">
if(typeof document.createElement('canvas').getContext !== 'function') {
alert('你的浏览器不支持 HTML5 Canvas,不能运行本程序。');
}
</script>
<canvas id="canvas"></canvas>
<div>
<h3>选项:</h3>
<p>
缩放
<label><input type="radio" name="scale" onclick="canvas.setScale(1);" />1.5倍</label>
<label><input type="radio" name="scale" onclick="canvas.setScale(2);" />2倍</label>
<label><input type="radio" name="scale" onclick="canvas.setScale(2.5);" checked="checked" />2.5倍</label>
<label><input type="radio" name="scale" onclick="canvas.setScale(3);" />3倍</label>
<br />
暂停
<label><input type="checkbox" name="pause" onclick="loop.active = this.checked ? false : true" /></label>
</p>
<h3>按键说明:</h3>
<table>
<tr>
<th>键位</th>
<th>说明</th>
</tr>
<tr>
<td>回车键</td>
<td>开始</td>
</tr>
<tr>
<td>H</td>
<td>开火,单发;选关画面跳至上一关</td>
</tr>
<tr>
<td>J</td>
<td>开火,连发;选关画面跳至下一关</td>
</tr>
<tr>
<td>W,S,A,D</td>
<td>分别为上、下、左、右移动</td>
</tr>
</table>
<p> </p>
<h3>介绍:</h3>
<p class="intro"> 精确重现红白机上的经典。复刻制作的过程,是逐步探掘红白机原版游戏设定的过程——子弹的飞行速度、雪地地形打滑的步数都完全照搬不误。做到最后,可以说已经解构了整个游戏。原版的一些有趣的东西,比如打薄砖块卡位这样的经典做法也可以在这个版本完全重现,原版的战术玩法,搬到这里亦照样适用。35 个关卡完整添加。</p>
<h3>运行环境:</h3>
<ul>
<li>Chrome</li>
<li>Opera</li>
<li>Firefox</li>
<li>Safari (需 Quicktime。)</li>
</ul>
<p class="cRed">暂不支持 IE9。</p>
<h3 class="tr" style="color:#EE873A;">作者:重装</h3>
</div>
<script type="text/javascript" src="base/propulsion.js"></script>
<script type="text/javascript" src="base/randomizer.js"></script>
<script type="text/javascript" src="data/stages.js"></script>
<script type="text/javascript">
// 全局变量
var spr = PP.spr, rm = PP.rm, obj = PP.obj, snd = PP.snd, al = PP.al,
global = PP.global, Alarm = PP.Alarm, collision = PP.collision,
draw = PP.draw, init = PP.init, key = PP.key, load = PP.load,
loop = PP.loop, mouse = PP.mouse, physics = PP.physics, Sound = PP.Sound,
snd = PP.snd, Sprite = PP.Sprite, view = PP.view, walkDown = PP.walkDown;
var unitStep = 2, gridLength = unitStep*8, unitLength = gridLength/2, fieldWidth = gridLength*13;
var fieldHeight = fieldWidth, fieldX = 1*gridLength, fieldY = 1*gridLength;
var canvasWidth = fieldWidth+gridLength+2*gridLength, canvasHeight = fieldHeight+2*gridLength;
var fieldColor = 'rgb(0, 0, 0)';
var backgroundColor = 'rgb(127, 127, 127)';
// 设置大小以及缩放
var canvas = document.getElementById('canvas');
canvas.width = canvasWidth;
canvas.height = canvasHeight;
canvas.setScale = function(factor) {
canvas.style.cssText = 'border:1px solid white; width:'+canvas.width*factor+'px; height:'+canvas.height*factor+'px';
}
canvas.setScale(2.5);
init('canvas');
loop.rate = 60;
// 图像资源
spr.tank = {};
spr.terrain = {};
spr.bullet = {};
spr.effect = {};
spr.tank.playerTank_1 = new Sprite('resources/player_tank_1.png', 2, 8, 8);
spr.tank.playerTank_2 = new Sprite('resources/player_tank_2.png', 2, 8, 8);
spr.tank.playerTank_3 = new Sprite('resources/player_tank_3.png', 2, 8, 8);
spr.tank.playerTank_4 = new Sprite('resources/player_tank_4.png', 2, 8, 8);
spr.tank.enemyTank_1 = new Sprite('resources/enemy_tank_1.png', 2, 8, 8);
spr.tank.enemyTank_1_red = new Sprite('resources/enemy_tank_1_red.png', 2, 8, 8);
spr.tank.enemyTank_2 = new Sprite('resources/enemy_tank_2.png', 2, 8, 8);
spr.tank.enemyTank_2_red = new Sprite('resources/enemy_tank_2_red.png', 2, 8, 8);
spr.tank.enemyTank_3 = new Sprite('resources/enemy_tank_3.png', 2, 8, 8);
spr.tank.enemyTank_3_red = new Sprite('resources/enemy_tank_3_red.png', 2, 8, 8);
spr.tank.enemyTank_4a = new Sprite('resources/enemy_tank_4a.png', 2, 8, 8);
spr.tank.enemyTank_4b = new Sprite('resources/enemy_tank_4b.png', 2, 8, 8);
spr.tank.enemyTank_4c = new Sprite('resources/enemy_tank_4c.png', 2, 8, 8);
spr.tank.enemyTank_4_red = new Sprite('resources/enemy_tank_4_red.png', 2, 8, 8);
spr.terrain.brick = new Sprite('resources/terrain_brick.png', 1, 4, 4);
spr.terrain.river = new Sprite('resources/terrain_river.png', 2, 4, 4);
spr.terrain.snow = new Sprite('resources/terrain_snow.png', 1, 4, 4);
spr.terrain.jungle = new Sprite('resources/terrain_jungle.png', 1, 4, 4);
spr.terrain.steel = new Sprite('resources/terrain_steel.png', 1, 4, 4);
spr.terrain.camp = new Sprite('resources/terrain_camp.png', 2, 8, 8);
spr.bullet = new Sprite('resources/bullet.png', 1, 2, 2);
spr.effect.exploding_1 = new Sprite('resources/effect_exploding_1.png', 3, 16, 16);
spr.effect.exploding_2 = new Sprite('resources/effect_exploding_2.png', 6, 16, 16);
spr.effect.born = new Sprite('resources/effect_born.png', 4, 8, 8);
spr.effect.divine = new Sprite('resources/effect_divine.png', 2, 8, 8); // 无敌状态光环
spr.tankCountIco = new Sprite('resources/tank_count_ico.png', 2, 4, 4);
spr.chars = new Sprite('resources/chars.png', 42, 4, 4);
spr.flag = new Sprite('resources/flag.png', 1, 8, 8);
spr.bonus = new Sprite('resources/bonus.png', 6, 8, 8);
spr.gameOver = new Sprite('resources/game_over.png', 1, 62, 40);
spr.points = new Sprite('resources/points.png', 5, 8, 8);
spr.arrow = new Sprite('resources/arrow.png', 1, 4, 4);
spr.logo = new Sprite('resources/logo.png', 1, 0, 0);
snd.begin = new Sound('resources/begin.wav');
snd.enemyDie = new Sound('resources/edie.wav');
snd.playerDie = new Sound('resources/wdie.wav');
snd.fire = new Sound('resources/fire.wav');
snd.get = new Sound('resources/get.wav');
//snd.walk = new Sound('resources/walk.wav');
snd.bulletExplode = new Sound('resources/bullet_explode.wav');
snd.snow = new Sound('resources/snow.wav');
load(function() {
window.clearInterval(loadingBar);
draw.rectangle(0, 0, canvasWidth, canvasHeight, false, 'rgb(0, 0, 0)');
draw.color = 'rgb(255, 255, 255)';
draw.text(canvasWidth/2, canvasHeight/2, 'Loading 100% ...');
global.stage = {
current: 0,
max: 35,
//current: 1,
toString: function() {
return 'stage_'+this.getActualStage();
},
getActualStage: function() {
return this.current%this.max != 0 ? this.current%this.max : this.max;
}
}
global.canChangeStage = false;
/* 全局方法 */
// 方向与夹角的转化。图片资源以东方向为初始方向
global.direction = {
directionAngle: {
up: 3*Math.PI/2,
down: 3*Math.PI/2,
left: 0,
right: 0
},
directionScale: {
up: [1, 1],
down: [-1, 1],
left: [-1, 1],
right: [1, 1]
},
getAngle: function(t) {
return global.direction.directionAngle[t.direction] !== undefined ?
global.direction.directionAngle[t.direction] : false;
},
getScales: function(t) {
return global.direction.directionScale[t.direction] !== undefined ?
global.direction.directionScale[t.direction] : false;
}
};
// 精确到毫秒的时间的生成
global.time = {
getNow: function() {
return Math.round(new Date().getTime());
}
};
global.position = {
setPos: function(t, gridX, gridY) {
t.x = this.getCoordinate(gridX, gridY)[0];
t.y = this.getCoordinate(gridX, gridY)[1];
},
getCoordinate: function(gridX, gridY) {
return [unitLength*gridX+fieldX, unitLength*gridY+fieldY];
}
};
// 分数
global.score = {
playerTank: 0,
getHiScore: function() {
return Math.max(this.playerTank, 20000);
}
};
global.life = {
playerTank: 2,
maxLife: 9
};
global.playerTank = {};
// 关卡结束前保存的画布
global.stageEndShot = null;
/* 坦克 */
obj.tank = {
parent: {
mask: [[-8,-8],[8,-8],[8,8],[-8,8]],
//mask: spr.tank.playerTank_1.mask,
moving: false,
step: 1,
// 已连续移动步数
movedStep: 0,
// 连续移动几步后间歇停止
stepInter: 3,
direction: 'up',
health: 1,
group: 1,
maxBullet: 1,
bullets: [],
shuttleIndex: 0,
lastCasted: 0,
castCd: 200,
lastBulletExploded: 0,
bulletExplodedCd: 50,
frame: 0,
bulletClass: 'slowBullet',
initialize: function(t) {
},
tick: function(t) {
},
// 步进
move: function(t) {
if(t.movedStep < t.stepInter || t.stepInter == 0) {
t.movedStep++;
switch(t.direction) {
case 'up':
t.y -= t.step;
break;
case 'down':
t.y += t.step;
break;
case 'left':
t.x -= t.step;
break;
case 'right':
t.x += t.step;
break;
}
} else {
t.movedStep = 0;
}
},
// 前进
forward: function(t) {
//snd.walk.play();
if( ! t.detectCollision(t)) {
t.move(t);
}
},
castBullet: function(t) {
if(t.shuttleIndex >= t.maxBullet && t.bullets.length == 0) {
t.shuttleIndex = 0;
} else if(t.shuttleIndex < t.bullets.length) {
t.shuttleIndex = t.bullets.length;
}
if(t.canCast(t)) {
var bullet = obj.bullet[t.bulletClass];
//if(t.bullets.push(bullet)) {
// 最近一次发弹时间
t.lastCasted = global.time.getNow();
var fixX = 0, fixY = 0;
switch(t.direction) {
case 'up':
fixY = -4;
break;
case 'down':
fixY = 4;
break;
case 'left':
fixX = -4;
break;
case 'right':
fixX = 4;
break;
}
bullet = loop.beget(bullet, t.x+fixX, t.y+fixY);
bullet.direction = t.direction;
bullet.host = t;
t.bullets.push(bullet);
//}
}
},
canCast: function(t) {
if(t.lastCasted == 0) {
return true;
}
/* 允许发射的三个条件:已发射炮弹数小于坦克允许同时最大炮弹数(一梭子最多的炮弹数)、
上一次发射时间延迟大于指定值、
如果是第一发炮弹,上一梭子炮弹的爆炸时间延迟大于指定值
*/
return t.bullets.length < t.maxBullet
&& global.time.getNow() - t.lastCasted > t.castCd
&& ((t.shuttleIndex > 0 && t.shuttleIndex < t.maxBullet)
|| (t.shuttleIndex == 0 && global.time.getNow() - t.lastBulletExploded > t.bulletExplodedCd));
},
// 坦克转向时的位置修正,使得坦克总处于布局方格上
fixPosY: function(t) {
if(t.direction == 'up' || t.direction == 'down') {
if(t.y%unitLength >= unitLength/2 && t.y%unitLength < unitLength) {
t.y = t.y + (unitLength-t.y%unitLength);
} else if(t.y%unitLength < unitLength/2 && t.y%unitLength > 0) {
t.y = t.y - t.y%unitLength;
}
return true;
}
return false;
},
fixPosX: function(t) {
if(t.direction == 'left' || t.direction == 'right') {
if(t.x%unitLength >= unitLength/2 && t.x%unitLength < unitLength) {
t.x = t.x + (unitLength-t.x%unitLength);
} else if(t.x%unitLength < unitLength/2 && t.x%unitLength > 0) {
t.x = t.x - t.x%unitLength;
}
return true;
}
return false;
},
detectCollision: function(t) {
var fixX = 0;
var fixY = 0;
// 边界碰撞检测
switch(t.direction) {
case 'up':
fixY = -unitStep;
if(collision.line(t.mask, t.x, t.y, 0, fieldX, fieldY, fieldX+fieldWidth, fieldY)) {
return true;
}
//result = t.y > this.y;
break;
case 'down':
fixY = unitStep;
if(collision.line(t.mask, t.x, t.y, 0, fieldX, fieldY+fieldHeight, fieldX+fieldWidth, fieldY+fieldHeight)) {
return true;
}
//result = t.y < this.y;
break;
case 'left':
fixX = -unitStep;
if(collision.line(t.mask, t.x, t.y, 0, fieldX, fieldY, fieldX, fieldY+fieldHeight)) {
return true;
}
//result = t.x > this.x;
break;
case 'right':
fixX = unitStep;
if(collision.line(t.mask, t.x, t.y, 0, fieldX+fieldWidth, fieldY, fieldX+fieldWidth, fieldY+fieldHeight)) {
return true;
}
//result = t.x < this.x;
break;
}
// 地形碰撞检测
result = false;
units = ['brick', 'river', 'steel', 'camp'];
for(var i=0; i<units.length; i++) {
walkDown(obj.terrain[units[i]], function() {
needDetectCollision = true;
// 两个单位距离大于一定值则不进行碰撞检测
range = 0;
if(units[i] != 'camp') {
range = unitLength+unitLength/2;
} else {
range = unitLength+gridLength/2;
}
if(Math.abs(t.x - this.x) >= range
|| Math.abs(t.y - this.y) >= range) {
needDetectCollision = false;
}
if(needDetectCollision) {
if(collision.masks(t.mask, t.x+fixX, t.y+fixY, 0, this.mask, this.x, this.y, 0)) {
result = this;
}
}
});
if(result) {
return result;
}
}
// 坦克碰撞检测
units = ['playerTank', 'enemyTank_1', 'enemyTank_2', 'enemyTank_3', 'enemyTank_4'];
for(var i=0; i<units.length; i++) {
walkDown(obj.tank[units[i]], function() {
if(this != t) {
needDetectCollision = true;
// 两个单位距离大于一定值则不进行碰撞检测
switch(t.direction) {
case 'up':
case 'down':
if(Math.abs(t.x - this.x) >= gridLength-3*unitStep) {
needDetectCollision = false;
}
break;
case 'left':
case 'right':
if(Math.abs(t.y - this.y) >= gridLength-3*unitStep) {
needDetectCollision = false;
}
break;
}
if(needDetectCollision) {
if(collision.masks(t.mask, t.x, t.y, 0, this.mask, this.x, this.y, 0)) {
switch(t.direction) {
case 'up':
if(t.y > this.y && Math.abs(t.y - this.y) <= gridLength) {
result = true;
}
break;
case 'down':
if(t.y < this.y && Math.abs(t.y - this.y) <= gridLength) {
result = true;
}
break;
case 'left':
if(t.x > this.x && Math.abs(t.x - this.x) <= gridLength) {
result = true;
}
break;
case 'right':
if(t.x < this.x && Math.abs(t.x - this.x) <= gridLength) {
result = true;
}
break;
}
}
}
}
});
}
if(result) {
return result;
}
// 奖励道具碰撞检测
if(typeof t.becomeDivine === 'function') {
units = ['star', 'forklift', 'life', 'bonus', 'hat', 'mine', 'timer'];
for(var i=0; i<units.length; i++) {
walkDown(obj.bonus[units[i]], function() {
if(collision.masks(t.mask, t.x, t.y, 0, this.mask, this.x, this.y, 0)) {
this.beGot(this, t);
}
});
}
}
return result;
},
endTick: function(t) {
},
beHit: function(t, bullet) {
t.health -= 1;
if( ! (t.health > 0)) {
t.over(t, true);
}
},
over: function(t, hasScore) {
loop.beget(obj.effect.exploding_2, t.x, t.y);
if(hasScore) {
s = loop.beget(obj.scorer, t.score, t);
s.delay = 35;
s.setScoreSource(s, t.score, t);
rm.play.scores.push(t);
}
loop.remove(t);
snd.enemyDie.play();
},
draw: function(t) {
angle = global.direction.getAngle(t);
scales = global.direction.getScales(t);
if(t.moving && t.movedStep > 0) {
t.frame = t.sprite.nextFrame(t.frame);
}
t.sprite.draw(t.x, t.y, t.frame, angle, scales[0], scales[1]);
t.drawCovers(t);
},
// 绘制遮蔽物,即森林地形
covers: [],
drawCovers: function(t) {
var covers = [];
walkDown(obj.terrain.jungle, function() {
if(Math.abs(t.x - this.x) <= unitLength/2
&& Math.abs(t.y - this.y) <= unitLength/2+unitLength
|| Math.abs(t.y - this.y) <= unitLength/2
&& Math.abs(t.x - this.x) <= unitLength/2+unitLength) {
covers.push(this);
this.sprite.draw(this.x, this.y);
//draw.line(this.x, this.y, this.x+1, this.y+1, 1, 'rgb(255, 0, 0)');
}
});
t.covers = covers;
}
},
/* 敌人坦克 */
enemyTank: {
// 阵营,玩家为 1,电脑为 2
group: 2,
moving: true,
direction: 'down',
hasBonus: false,
sprite2: {},
defaultSprite: {},
collided: false,
stepInter: 2,
score: 0,
initialize: function(t) {
t.bullets = [];
al.bonus = {};
//global.position.setPos(t, 6, 20);
},
beHit: function(t, bullet) {
t.generateBonus(t);
t.health -= 1;
if( ! (t.health > 0)) {
t.over(t, true);
}
},
generateBonus: function(t) {
if(t.hasBonus) {
t.hasBonus = false;
units = ['life', 'star', 'forklift', 'hat', 'mine', 'timer'];
for(var i=0; i<units.length; i++) {
walkDown(obj.bonus[units[i]], function() {
if(this.registered) {
loop.remove(this);
}
});
}
lifeRate = 0.1;
if(global.life.playerTank >= 1) {
lifeRate = 0.1;
} else {
lifeRate = 0.15;
}
if(rand(200) < 100*lifeRate) {
loop.register(obj.bonus.life);
} else {
loop.register(obj.bonus[Math.choose('star', 'forklift', 'hat', 'mine', 'timer')]);
}
//loop.register(obj.bonus['hat']);
}
},
tick: function(t) {
/* 敌方坦克的人工智能 */
// 坦克转向几率因子
if( ! rm.play.enemyFreeze) {
var factor = 1.6;
if(t.collided) {
// 发生碰撞则将转向几率调高
factor *= 0.15;
}
if(rand(factor*loop.rate) == 1) {
if( ! t.fixPosY(t)) {
t.fixPosX(t);
}
hOrV = Math.choose('h', 'v');
factor = 100;
// 向我方本营移动的倾向
if(hOrV == 'h') {
walkDown(obj.terrain.camp, function() {
angle = Math.pointDirection(this.x, this.y, t.x, t.y);
if(angle >= Math.PI && angle < Math.PI*3/2) {
leftOdds = 0.3;
} else {
leftOdds = 0.7;
}
});
if(rand(factor) < factor*leftOdds) {
t.direction = 'left';
} else {
t.direction = 'right';
}
} else if(hOrV == 'v') {
downOdds = 0.83;
if(rand(factor) < factor*downOdds) {
t.direction = 'down';
} else {
t.direction = 'up';
}
}
//t.direction = Math.choose('left', 'right');
}
t.moving = true;
t.forward(t);
// 坦克发炮弹几率因子
var factor = 0.9;
if(t.collided) {
// 发生碰撞则将转向发弹几率调高
factor *= 0.9;
// 如果方向为下,并且碰到砖头,发弹几率大幅调高
if(t.direction == 'down'
&& typeof t.collided == 'object'
&& t.collided.hasOwnProperty('masks')) {
factor *= 0.4;
}
}
/*
walkDown(obj.tank.playerTank, function() {
if(Math.abs(this.x - t.x) < unitLength
|| Math.abs(this.y - t.y) < unitLength) {
factor = 0.1;
}
});
*/
if(rand(factor*loop.rate) == 1) {
t.castBullet(t);
}
} else {
t.moving = false;
}
},
// 前进
forward: function(t) {
if( ! (collided = t.detectCollision(t))) {
t.move(t);
t.collided = false;
} else {
t.collided = collided;
}
},
setBonus: function(t) {
t.hasBonus = true;
al.bonus = new Alarm(function() {
if(t.hasBonus) {
tmp = t.sprite;
t.sprite = t.sprite2;
t.sprite2 = tmp;
this.time = loop.rate*.3;
} else {
this.stop();
t.sprite = t.defaultSprite;
}
});
al.bonus.time = 0;
}
},
enemyTank_1: {
sprite: spr.tank.enemyTank_1,
defaultSprite: spr.tank.enemyTank_1,
sprite2: spr.tank.enemyTank_1_red,
stepInter: 1,
score: 100
},
enemyTank_2: {
sprite: spr.tank.enemyTank_2,
defaultSprite: spr.tank.enemyTank_2,
sprite2: spr.tank.enemyTank_2_red,
stepInter: 0,
score: 200
},
enemyTank_3: {
sprite: spr.tank.enemyTank_3,
defaultSprite: spr.tank.enemyTank_3,
sprite2: spr.tank.enemyTank_3_red,
stepInter: 1,
score: 300,
bulletClass: 'fastBullet'
},
enemyTank_4: {
sprite: spr.tank.enemyTank_4a,
defaultSprite: spr.tank.enemyTank_4a,
sprite2: spr.tank.enemyTank_4_red,
stepInter: 1,
health: 4,
score: 400,
flashFlag: 0,
bulletClass: 'slowBullet',
initialize: function(t) {
t.defaultSprite = {};
},
beginTick: function(t) {
if( ! t.hasBonus) {
switch(t.health) {
case 4:
t.sprite = t.flashFlag == 0 ?
spr.tank.enemyTank_4a : spr.tank.enemyTank_4c;
t.updateFlashFlag(t);
break;
case 3:
t.sprite = t.flashFlag == 0 ?
spr.tank.enemyTank_4a : spr.tank.enemyTank_4b;
t.updateFlashFlag(t);
break;
case 2:
t.sprite = t.flashFlag == 0 ?
spr.tank.enemyTank_4b : spr.tank.enemyTank_4c;
t.updateFlashFlag(t);
break;
case 1:
t.sprite = spr.tank.enemyTank_4a;
break;
}
}
},
updateFlashFlag: function(t) {
t.flashFlag = t.flashFlag == 0 ? 1 : 0;
},
beHit: function(t, bullet) {
t.generateBonus(t);
t.health -= 1;
switch(t.health) {
case 3:
t.sprite = spr.tank.enemyTank_4b;
t.defaultSprite = spr.tank.enemyTank_4b;
break;
case 2:
t.sprite = spr.tank.enemyTank_4c;
t.defaultSprite = spr.tank.enemyTank_4c;
break;
case 1:
t.sprite = spr.tank.enemyTank_4a;
t.defaultSprite = spr.tank.enemyTank_4a;
break;
}
if( ! (t.health > 0)) {
t.over(t, true);
}
}
},
// 玩家坦克
playerTank: {
//persistant: true,
group: 1,
level: 0,
sprite: spr.tank.playerTank_1,
divineAlarm: null,
depth: 0,
bulletClass: 'slowBullet',
divine: false,
//divine: true,
divineShield: {},
/* 打滑参数。玩家坦克在雪地地形上会打滑 */
// 打滑步数
slided: 0,
// 是否打滑
sliding: false,
initialize: function(t) {
t.divineShield = {};
t.bullets = [];
t.direction = 'up';
t.becomeDivine(t, 3);
},
tick: function(t) {
if(rm.play.isGameOver) {
t.moving = false;
return ;
}
t.moving = true;
// 移动
if(key.up.down || key.w.pressed) {
t.fixPosX(t);
t.direction = 'up';
} else if(key.down.down || key.s.pressed) {
t.fixPosX(t);
t.direction = 'down';
} else if(key.left.down || key.a.pressed) {
t.fixPosY(t);
t.direction = 'left';
} else if(key.right.down || key.d.pressed) {
t.fixPosY(t);
t.direction = 'right';
} else {
t.moving = false;
}
if(t.sliding == true && t.slided < 2*gridLength && t.isCollidedSnow(t)) {
t.slided++;
if(t.slided == 1) {
snd.snow.play();
}
} else {
t.sliding = false;
}
if(t.moving || t.sliding) {
if(t.moving && ! t.sliding) {
t.slided = 0;
}
t.forward(t);
} else {
// 检测雪地碰撞,是则设置为打滑状态
if(t.isCollidedSnow(t)) {
t.sliding = true;
}
}
// 发射炮弹
if(key.h.down || key.j.pressed) {
t.castBullet(t);
}
// 如果处于无敌状态,光环随坦克移动
if(t.divine && t.divineShield.hasOwnProperty('registered') && t.divineShield.registered) {
t.divineShield.x = t.x;
t.divineShield.y = t.y;
}
},
beHit: function(t, bullet) {
if( ! t.divine) {
t.health -= 1;
}
if( ! (t.health > 0)) {
t.over(t);
}
},
isCollidedSnow: function(t) {
var collisionCount = 0;
walkDown(obj.terrain.snow, function() {
if(Math.abs(this.x-t.x) < gridLength && Math.abs(this.y-t.y) < gridLength
&& collision.masks([[-7, -7], [7, -7], [7, 7], [-7, 7]], t.x, t.y, 0, this.mask, this.x, this.y, 0)) {
collisionCount++;
}
});
return collisionCount >= 4;
},
castBullet: function(t) {
if(t.shuttleIndex >= t.maxBullet && t.bullets.length == 0) {
t.shuttleIndex = 0;
} else if(t.shuttleIndex < t.bullets.length) {
t.shuttleIndex = t.bullets.length;
}
if(t.canCast(t)) {
var bullet = obj.bullet[t.bulletClass];
//if(t.bullets.push(bullet)) {
// 最近一次发弹时间
t.lastCasted = global.time.getNow();
var fixX = 0, fixY = 0;
switch(t.direction) {
case 'up':
fixY = -4;
break;
case 'down':
fixY = 4;
break;
case 'left':
fixX = -4;
break;
case 'right':
fixX = 4;
break;
}
bullet = loop.beget(bullet, t.x+fixX, t.y+fixY);
if(snd.begin.ended) {
snd.fire.play();
}
bullet.direction = t.direction;
bullet.host = t;
t.bullets.push(bullet);
//}
}
},
over: function(t) {
loop.remove(t);
loop.beget(obj.effect.exploding_2, t.x, t.y);
snd.playerDie.play();
global.playerTank = {};
if(global.life['playerTank'] > 0) {
global.life['playerTank'] -= 1;
new Alarm(function() {
generator = loop.beget(obj.tankGenerator.playerTankGenerator, 9, 25);
}).time = loop.rate*0.75;
} else {
rm.play.gameOver();
}