/
mario.js
1619 lines (1536 loc) · 45.5 KB
/
mario.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
function Mario(posX,posY){
// 定数
this.NORMAL_JUMP_POWER = 10;
this.DASH_JUMP_POWER = 13;
this.addPosX = 0;
this.addPosY = 0;
this.posX = posX;
this.posY = posY;
// どのタイミングでアニメーションを切り替えるか
this.animCnt = 0;
// 切り出す始点のX座標
this.animX = 0;
this.animOffsetX = 0;
// 方向を切り替えるための変数
this.direction = RIGHT_DIR;
// ダッシュフラグ
this.isDash = false;
// ジャンプ
this.isJump = false;
this.jumpCnt = 0;
this.jumpPower = 0;
// マップチップ座標
this.rightMapX = 0;
this.leftMapX = 0;
this.upMapY = 0;
this.downMapY = 0;
// chapter26
this.centerMapY = 0;
// chapter22スクロール処理
this.maxDrawMapX = DRAW_MAX_MAP_X; // 最大の描画範囲X
this.minDrawMapX = 0; // 最小の描画範囲X
this.mapScrollX = 0; // スクロール量X
this.moveNumX = posX; // 総移動量X
this.scrollEndX = (215 - 10) * MAP_SIZE - HALF_MAP_SIZE; // スクロールの終わりとなる終点X
// chapter27
this.state = NORMAL_STATE;
this.height = 32;
// chapter30
this.coinNum = 0;
this.playerNum = PLAYER_NUM;
// chapter33
this.isBlockCoinAnim = [false,false];
this.blockCoinFrame = [0,0];
this.blockCoinX = [0,0];
this.blockCoinY = [0,0];
// chapter34
this.kinoko = new Kinoko(0,0,LEFT_DIR);
// chapter37
this.blockAttackX = [[0,0,0,0],[0,0,0,0]]; // Block破壊時の座標X
this.blockAttackY = [[0,0,0,0],[0,0,0,0]]; // Block破壊時の座標Y
this.blockAttackCnt = [0,0]; // animation cnt
this.blockAttackIndex = 0; // blockのindex
this.isBlockUp = [false,false]; // ブロック上昇フラグ
this.isBlockAttack = [false,false]; // 破壊フラグ
this.blockAttackAddY = [0,0]; // ブロックの移動量Y
this.blockUpX = [0,0]; // 上昇ブロック用座標X
this.blockUpY = [0,0]; // 上昇ブロック用座標Y
this.blockAttackIndexX = [0,0]; // ブロックを移動させる対象のブロックマップチップ番号X
this.blockAttackIndexY = [0,0]; // ブロックを移動させる対象のブロックマップチップ番号Y
// chapter38
this.fireKinoko = new FireKinoko(0,0);
this.textureOffsetY = 0;
// chapter39
this.MAX_FIRE_NUM = 3;
this.fire = [];
for(var i = 0;i < this.MAX_FIRE_NUM;++i){
this.fire[i] = new Fire(0,0);
}
// chapter41
this.star = new Star(0,0,LEFT_DIR);
this.isStar = false;
this.starOffsetX = 0;
this.starTimer = 0; // star状態の時間
// chapter42
this.docanMoveCnt = 0; // 土管移動用カウンター
this.mapMoveStage = MAP_ONE; // 遷移先マップ
this.docanfirstMoveDirection = DOCAN_UP; // 移動方向
this.docanEndMoveDirection = DOCAN_UP; // 移動方向
this.isMapMove = false; // map移動中フラグ
this.isSecondMapMove = false; // 土管から出てくる時のフラグ
this.docanPosX1 = 0; // 最初に遷移するX
this.docanPosY1 = 0; // 最初に遷移するY
this.docanPosX2 = 0; // 遷移後のx
this.docanPosY2 = 0; // 遷移後のy
this.newMapSizeX = 0; // 新規マップのマップ描画量
this.keyDisable = false; // キー入力を受け付けないフラグ
// chapter43
this.isSit = false; // offsetに利用したいので、0 1で管理する
this.textureHeight = MAP_SIZE; // テクスチャを切り取る範囲Y
// chapter44
this.continueCnt = 0; // マリオが死んでからコンテニュー画面に遷移するまでのタイマー時間
// chapter45
this.oneUpKinoko = new OneUpKinoko(0,0,LEFT_DIR);
// one upを表示するタイマー
this.oneUpCnt = 0;
this.isDrawOneUp = false;
// chapter47
// 連続踏みつけ数
this.sequenceJumpCnt = 0;
// chapter48
// 無敵フラグ
this.isInvincible = false;
this.invincibleCnt = 0;
// 小さくなるアニメーションフラグ
this.onSmallAnimation = false;
this.smallAnimationCnt = 0;
// chapter49
// 隠しブロックの出現セーブ用
this.hideBlockX = -1;
this.hideBlockY = -1;
// chapter50
// ゴールアニメーション状態を管理する
this.goalAnimationState = INACTIVE;
// 重力など行動を止めるフラグ
this.isMarioStop = false;
// ゴールの位置
this.goalPosX = 205 * MAP_SIZE;
// chapter51
// ゴールアニメーションのためのタイマー
this.goalCnt = 0
// chapter53
// 連続コインの時間
this.coinTimer = 0;
this.onCoinTimer = false;
}
/**
* マリオの初期化
* @param {*} posX
* @param {*} posY
*/
Mario.prototype.init = function(posX,posY){
this.addPosX = 0;
this.addPosY = 0;
this.posX = posX;
this.posY = posY;
// どのタイミングでアニメーションを切り替えるか
this.animCnt = 0;
// 切り出す始点のX座標
this.animX = 0;
this.animOffsetX = 0;
// 方向を切り替えるための変数
this.direction = RIGHT_DIR;
// ダッシュフラグ
this.isDash = false;
// ジャンプ
this.isJump = false;
this.jumpCnt = 0;
this.jumpPower = 0;
// マップチップ座標
this.rightMapX = 0;
this.leftMapX = 0;
this.upMapY = 0;
this.downMapY = 0;
// chapter26
this.centerMapY = 0;
// chapter22スクロール処理
this.maxDrawMapX = DRAW_MAX_MAP_X; // 最大の描画範囲X
this.minDrawMapX = 0; // 最小の描画範囲X
this.mapScrollX = 0; // スクロール量X
this.moveNumX = 0; // 総移動量X
this.scrollEndX = (215 - 10) * MAP_SIZE - HALF_MAP_SIZE; // スクロールの終わりとなる終点X
// chapter27
this.state = NORMAL_STATE;
this.height = 32;
// chapter33
this.isBlockCoinAnim = [false,false];
this.blockCoinFrame = [0,0];
this.blockCoinX = [0,0];
this.blockCoinY = [0,0];
// chapter37
this.blockAttackX = [[0,0,0,0],[0,0,0,0]]; // Block破壊時の座標X
this.blockAttackY = [[0,0,0,0],[0,0,0,0]]; // Block破壊時の座標Y
this.blockAttackCnt = [0,0]; // animation cnt
this.blockAttackIndex = 0; // blockのindex
this.isBlockUp = [false,false]; // ブロック上昇フラグ
this.isBlockAttack = [false,false]; // 破壊フラグ
this.blockAttackAddY = [0,0]; // ブロックの移動量Y
this.blockUpX = [0,0]; // 上昇ブロック用座標X
this.blockUpY = [0,0]; // 上昇ブロック用座標Y
this.blockAttackIndexX = [0,0]; // ブロックを移動させる対象のブロックマップチップ番号X
this.blockAttackIndexY = [0,0]; // ブロックを移動させる対象のブロックマップチップ番号Y
this.textureOffsetY = 0;
// chapter39
this.isStar = false;
this.starOffsetX = 0;
this.starTimer = 0; // star状態の時間
// chapter42
this.docanMoveCnt = 0; // 土管移動用カウンター
this.mapMoveStage = MAP_ONE; // 遷移先マップ
this.docanfirstMoveDirection = DOCAN_UP; // 移動方向
this.docanEndMoveDirection = DOCAN_UP; // 移動方向
this.isMapMove = false; // map移動中フラグ
this.isSecondMapMove = false; // 土管から出てくる時のフラグ
this.docanPosX1 = 0; // 最初に遷移するX
this.docanPosY1 = 0; // 最初に遷移するY
this.docanPosX2 = 0; // 遷移後のx
this.docanPosY2 = 0; // 遷移後のy
this.newMapSizeX = 0; // 新規マップのマップ描画量
this.keyDisable = false; // キー入力を受け付けないフラグ
// chapter43
this.isSit = false; // offsetに利用したいので、0 1で管理する
this.textureHeight = MAP_SIZE; // テクスチャを切り取る範囲Y
// chapter44
this.continueCnt = 0; // マリオが死んでからコンテニュー画面に遷移するのタイマー時間
// 連続踏みつけ数
this.sequenceJumpCnt = 0;
// 無敵フラグ
this.isInvincible = false;
this.invincibleCnt = 0;
// 小さくなるアニメーションフラグ
this.onSmallAnimation = false;
this.smallAnimationCnt = 0;
// chapter50
// ゴールアニメーション状態を管理する
this.goalAnimationState = INACTIVE;
// 重力など行動を止めるフラグ
this.isMarioStop = false;
this.goalPosX = 205 * MAP_SIZE;
// chapter51
this.goalCnt = 0
// chapter53
// 連続コインの時間
this.coinTimer = 0;
this.onCoinTimer = false;
}
/*
描画関数
ctx:context
texture:img class
*/
Mario.prototype.draw = function(ctx,texture){
if(this.isDraw()){
let offsetY = this.isSit ? (29 + 64) : 0;
ctx.drawImage(texture,this.starOffsetX + (this.animX * 32) + this.animOffsetX,((this.direction * 2) * this.textureHeight) + this.textureOffsetY + offsetY,32,this.height,this.posX,this.posY,32,this.height);
}
}
Mario.prototype.moveX = function(mapChip,moveX){
// 加算量を代入する
this.addPosX = moveX;
// 移動後の加算量を渡すマップチップの状況に応じてx方向の加算量を決める
this.collisionX(mapChip,this.moveNumX + this.addPosX);
// 移動方向変える
if(moveX > 0){
this.direction = RIGHT_DIR;
}
else{
this.direction = LEFT_DIR;
}
this.posX += this.addPosX;
this.moveNumX += this.addPosX;
this.updateMapPositionX(this.moveNumX);
// ダッシュ時のアニメーションは早くする
var cnt = this.isDash ? 2 : 1;
this.increseAnimCnt(cnt);
}
Mario.prototype.setIsDash = function(isDash){
this.isDash = isDash;
}
/**
ジャンプ動作ボタンが押された時にジャンプフラグを立てる
*/
Mario.prototype.setJumpSettings = function(isDash){
if(!this.isJump){
this.isJump = true;
this.animOffsetX = 128;
var jumpNum = isDash ? this.DASH_JUMP_POWER : this.NORMAL_JUMP_POWER;
this.jumpPower = jumpNum;
}
}
/**
ジャンプ動作
isPush : 対象のキーが押されているか
*/
Mario.prototype.jumpAction = function(isPush,mapChip){
// 移動量を2分割する
for(var i = 2;i > 0;--i){
this.addPosY = parseInt(this.jumpPower / i);
// 当たり判定があった場合は抜ける
if(this.collisionY(mapChip,this.posY - this.addPosY)){
break;
}
}
this.posY -= this.addPosY;
// 落下中はジャンプさせないようにする
if(this.addPosY < 0){
this.isJump = true;
}
// 落下量調整
if(this.jumpPower > -MAX_GRAVITY){
// 上昇中かつキーが押されている場合は下降量を減らす
if(isPush && this.jumpPower > 0){
this.jumpPower -= (GRAVITY_POWER - (GRAVITY_POWER / 2));
}else{
this.jumpPower -= GRAVITY_POWER;
}
}
}
/**
x軸方向のマップチップ座標の更新
posX : マップチップ更新対象となるx座標
*/
Mario.prototype.updateMapPositionX = function(posX){
// x座標
this.leftMapX = Math.floor(posX / MAP_SIZE);
this.rightMapX = Math.floor((posX + MAP_SIZE - 1) / MAP_SIZE);
// 配列外チェック
if(this.leftMapX >= MAX_MAP_CHIP_X){
this.leftMapX = MAX_MAP_CHIP_X - 1;
}
if(this.leftMapX < 0){
this.leftMapX = 0;
}
if(this.rightMapX >= MAX_MAP_CHIP_X){
this.rightMapX = MAX_MAP_CHIP_X - 1;
}
if(this.rightMapX < 0){
this.rightMapX = 0;
}
}
/**
chapter20
Y軸方向のマップチップの更新
*/
Mario.prototype.updateMapPositionY = function(posY){
// キノコ状態の時と分ける
if(this.isBig()){
// キノコ状態の時のマップチップ座標
this.upMapY = Math.floor(posY / MAP_SIZE);
// 通常よりもマップチップ一つ分多い
this.downMapY = Math.floor((posY + this.height - 1) / MAP_SIZE);
// 中央座標
this.centerMapY = Math.floor((posY + MAP_SIZE - 1) / MAP_SIZE);
}
else{
// 小さい状態
this.upMapY = Math.floor(posY / MAP_SIZE);
this.downMapY = Math.floor((posY + MAP_SIZE - 1) / MAP_SIZE);
// 中央座標
this.centerMapY = Math.floor((posY + (MAP_SIZE / 2) - 1) / MAP_SIZE);
}
// 配列外チェック
if(this.upMapY >= MAX_MAP_Y - 1){
this.upMapY = MAX_MAP_Y - 1;
}
if(this.upMapY < 0){
this.upMapY = 0;
}
if(this.downMapY >= MAX_MAP_Y - 1){
this.downMapY = MAX_MAP_Y - 1;
}
if(this.downMapY < 0){
this.downMapY = 0;
}
// 中央座標の配列外チェック
if(this.centerMapY >= MAX_MAP_Y - 1){
this.centerMapY = MAX_MAP_Y - 1;
}
if(this.centerMapY < 0){
this.centerMapY = 0;
}
}
/**
chapter17
マップチップ座標を更新する
*/
Mario.prototype.updateMapPosition = function(){
this.updateMapPositionX(this.moveNumX);
this.updateMapPositionY(this.posY);
// log
// console.log("rightMapX = " + this.rightMapX + ", leftMapX = " + this.leftMapX + ",upMapY = " + this.upMapY + ",this.downMapY = "
// + this.downMapY);
//console.log("mario posX = " + this.posX + ",mario posY = " + this.posY);
}
/**
chapter19
オブジェクトとの当たり判定X
*/
Mario.prototype.collisionX = function(map,posX){
// マップ移動中は当たり判定を起こさない
if(!this.isMapMove){
this.updateMapPositionX(posX);
// マリオの右側
if(isObjectMap(map[this.downMapY][this.rightMapX]) || isObjectMap(map[this.upMapY][this.rightMapX]) || isObjectMap(map[this.centerMapY][this.rightMapX])){
// (加算される前の)中心点からの距離を取る
var vecX = Math.abs((this.moveNumX + HALF_MAP_SIZE) - ((this.rightMapX * MAP_SIZE) + HALF_MAP_SIZE));
this.addPosX = Math.abs(MAP_SIZE - vecX);
}
// マリオの左側
else if(isObjectMap(map[this.downMapY][this.leftMapX]) || isObjectMap(map[this.upMapY][this.leftMapX]) || isObjectMap(map[this.centerMapY][this.leftMapX])){
// (加算される前の)中心点からの距離を取る
var vecX = Math.abs((this.moveNumX + HALF_MAP_SIZE) - ((this.leftMapX * MAP_SIZE) + HALF_MAP_SIZE));
this.addPosX = -Math.abs(MAP_SIZE - vecX);
}
}
}
/**
* Y軸方向の当たり判定
*
* @param {*} map
* @param {*} posY
*
* return : 当たり判定の有無
*/
Mario.prototype.collisionY = function(map,posY){
if(!this.isMapMove){
this.updateMapPositionY(posY);
// マップ座標xを配列で保管する
var mapsX = [this.rightMapX,this.leftMapX];
for(var i = 0;i < 2;++i){
// マリオの上側に当たった場合
if(isObjectMap(map[this.upMapY][mapsX[i]])){
// コインブロックだった場合
if(isCoinBlock(map[this.upMapY][mapsX[i]])){
// コインブロック用のアニメーションをセットする
var coinX = mapsX[i] * MAP_SIZE;
// 一つ上にセットする
var coinY = (this.upMapY - 1) * MAP_SIZE;
this.setBlockCoinMapAnim(i,coinX,coinY);
// ボックスを空にする
replaceEmptyBoxMap(map,mapsX[i],this.upMapY);
// coinの取得
this.getCoin();
}
// 連続コインブロックだった場合
if(isSequenceCoinBlock(map[this.upMapY][mapsX[i]])){
// 初回チェック
if(!this.onCoinTimer){
this.setBlockCoinTimer(true);
}
else{
// タイマーが切れているか
if(this.isOverCoinTimer()){
// フラグを解除する
this.setBlockCoinTimer(false);
// ボックスを空にする
replaceEmptyBoxMap(map,mapsX[i],this.upMapY);
}
}
// コインブロック用のアニメーションをセットする
var coinX = mapsX[i] * MAP_SIZE;
// 一つ上にセットする
var coinY = (this.upMapY - 1) * MAP_SIZE - 16;
this.setBlockCoinMapAnim(i,coinX,coinY);
// ブロックも動かす
this.blockMoveAction(mapsX[i],this.upMapY,map);
// coinの取得
this.getCoin();
}
// キノコブロックだった場合(chapter34&38)
if(isKinokoBlock(map[this.upMapY][mapsX[i]])){
// chapter40 キノコを有効化する処理を関数にした
var kinokoPosX = mapsX[i] * MAP_SIZE;
var kinokoPosY = this.upMapY * MAP_SIZE;
let dir = getItemDir(map[this.upMapY][mapsX[i]]);
this.activateKinoko(kinokoPosX,kinokoPosY,dir);
// ボックスを空にする
replaceEmptyBoxMap(map,mapsX[i],this.upMapY);
}
// starブロックだった場合(chapter41)
if(isStarBlock(map[this.upMapY][mapsX[i]])){
// chapter40 キノコを有効化する処理を関数にした
let starPosX = mapsX[i] * MAP_SIZE;
let starPosY = this.upMapY * MAP_SIZE;
let dir = getItemDir(map[this.upMapY][mapsX[i]]);
this.star.activate(starPosX,starPosY,dir);
// ボックスを空にする
replaceEmptyBoxMap(map,mapsX[i],this.upMapY);
}
// ブロックのアニメーション
if(isBlockMap(map[this.upMapY][mapsX[i]])){
var posX = mapsX[i] * MAP_SIZE;
var posY = this.upMapY * MAP_SIZE;
this.blockAction(mapsX[i],this.upMapY,false,map);
}
// one up blockだった場合
if(isOneUpBlock(map[this.upMapY][mapsX[i]])){
let posX = mapsX[i] * MAP_SIZE;
let posY = this.upMapY * MAP_SIZE;
this.oneUpKinoko.activate(posX,posY,LEFT_DIR);
// ボックスを空にする
replaceEmptyBoxMap(map,mapsX[i],this.upMapY);
}
// (加算される前の)中心点からの距離をみる
var vecY = Math.abs((this.posY + HALF_MAP_SIZE) - ((this.upMapY * MAP_SIZE) + HALF_MAP_SIZE));
// Yの加算量調整
this.addPosY = Math.abs(MAP_SIZE - vecY);
// 落下させる
this.jumpPower = 0;
return true;
}
// 隠しブロックだった場合
else if(isHideBlock(map[this.upMapY][mapsX[i]])){
// ブロックの下半分かつマリオが上昇中のみ隠しブロックを出現させる
if(this.addPosY > 0){
// 半分以下なら隠しブロックを出現させる
let blockPosY = (this.upMapY * MAP_SIZE) - HALF_MAP_SIZE;
// マリオの頭上が隠しブロックブロックの半分の位置以下
if(posY - 32 > blockPosY){
// 隠し1up block
if(isHideOneUpBlock(map[this.upMapY][mapsX[i]])){
// 1up kinokoを出現させる
let posX = mapsX[i] * MAP_SIZE;
let posY = this.upMapY * MAP_SIZE;
let dir = getItemDir(map[this.upMapY][mapsX[i]]);
this.oneUpKinoko.activate(posX,posY,dir);
// ボックスを空にする
replaceEmptyBoxMap(map,mapsX[i],this.upMapY);
// 隠しブロックが出現しないように状態を保存する。
this.hideBlockX = mapsX[i];
this.hideBlockY = this.upMapY;
}
// (加算される前の)中心点からの距離をみる
var vecY = Math.abs((this.posY + HALF_MAP_SIZE) - ((this.upMapY * MAP_SIZE) + HALF_MAP_SIZE));
// Yの加算量調整
this.addPosY = Math.abs(MAP_SIZE - vecY);
// 落下させる
this.jumpPower = 0;
return true;
}
}
}
}
// マリオの下側
if(isObjectMap(map[this.downMapY][this.rightMapX]) || isObjectMap(map[this.downMapY][this.leftMapX])){
// (加算される前の)中心点からの距離を見る
var centerY = this.height == 64 ? this.posY + 32 : this.posY;
var vecY = Math.abs((centerY + HALF_MAP_SIZE) - ((this.downMapY * MAP_SIZE) + HALF_MAP_SIZE));
// Yの加算量調整
this.addPosY = Math.abs(MAP_SIZE - vecY);
// 地面についた
this.posY += this.addPosY;
this.addPosY = 0;
this.jumpPower = 0;
this.isJump = false;
// リセットアニメーション
this.animOffsetX = 0;
// chapter47 地面についたら連続ジャンプのカウントをゼロにする
this.sequenceJumpCnt = 0;
return true;
}
}
return false;
}
/**
アイテムマップチップオブジェクトとの当たり判定
*/
Mario.prototype.collisionWithMapItem = function(map){
var mapsX = [this.rightMapX,this.leftMapX];
var mapsY = [this.upMapY,this.downMapY];
for(var y = 0;y < 2;++y){
for(var x = 0;x < 2;++x){
// コインとの当たり判定
if(isCoinMap(map[mapsY[y]][mapsX[x]])){
// マップチップと空のマップを入れ替える
replaceEmptyMap(map,mapsX[x],mapsY[y]);
// コインを取得した時の処理
this.getCoin();
}
}
}
}
/**
ブロックコインのアニメーション処理のセット
index : coinblockの配列index
*/
Mario.prototype.setBlockCoinMapAnim = function(index,posX,posY){
this.isBlockCoinAnim[index] = true;
this.blockCoinFrame[index] = 0;
this.blockCoinX[index] = posX;
this.blockCoinY[index] = posY;
}
/**
ブロックコインのアニメーション処理
*/
Mario.prototype.animateBlockCoin = function(){
for(var i = 0;i < MAX_MAP_BLOCK;++i){
if(this.isBlockCoinAnim[i]){
// コインを上昇させる
this.blockCoinY[i] -= 1;
if(this.blockCoinFrame[i]++ >= MAX_BLOCK_COIN_FRAME){
this.isBlockCoinAnim[i] = false;
}
}
}
}
/**
コインを取得した時の処理
*/
Mario.prototype.getCoin = function(){
// コインを100枚とったら1upさせる
// スコア上昇
gScore += COIN_SCORE;
if(++this.coinNum >= 100){
this.coinNum = 0;
this.playerNum++;
}
}
/**
chapter22 スクロール処理
*/
Mario.prototype.doMapScrollX = function(){
// スクロール基準点を越えたら
if(this.moveNumX >= SCROLL_POINT_X && this.moveNumX < this.scrollEndX)
{
this.mapScrollX = this.moveNumX - SCROLL_POINT_X; // マップスクロール量
this.posX = SCROLL_POINT_X; // 固定
// マップを描画する範囲をずらす
this.maxDrawMapX = DRAW_MAX_MAP_X + Math.floor(this.mapScrollX / MAP_SIZE); // 最大の描画範囲X
this.minDrawMapX = this.maxDrawMapX - DRAW_MAX_MAP_X; // 最小の描画範囲X
}
// スクロールの終点まで来たらスクロールを止める
else if(this.moveNumX >= this.scrollEndX)
{
this.mapScrollX = this.scrollEndX - SCROLL_POINT_X; // マップスクロール量
this.maxDrawMapX = MAX_MAP_X + Math.floor((this.mapScrollX + HALF_MAP_SIZE) / MAP_SIZE); // 最大の描画範囲X
if(this.maxDrawMapX > MAX_MAP_CHIP_X) this.maxDrawMapX = MAX_MAP_CHIP_X;
this.minDrawMapX = this.maxDrawMapX - DRAW_MAX_MAP_X; // 最小の描画範囲X
// 中央固定を止める
this.posX = this.moveNumX - this.mapScrollX;
}
}
/*
敵と当たった時のアクション
*/
Mario.prototype.collisionWithEnemy = function(){
// 無敵状態の時は実行しない
if(!this.isInvincible){
// でかい状態なら一回もつ
if(this.isBig()){
// 小さくする
this.height = MAP_SIZE;
this.textureHeight = MAP_SIZE;
this.textureOffsetY = 0;
this.posY += MAP_SIZE;
this.updateMapPositionY(this.posY);
this.state = NORMAL_STATE;
this.isInvincible = true;
this.onSmallAnimation = true;
this.keyDisable = true;
// 全体の動きを止めるフラグ
gActionStop = true;
}
else{
this.setDeadParam();
}
}
}
/**
死亡演出
*/
Mario.prototype.deadAction = function(){
if(this.state == DEAD_ACTION){
this.posY -= this.addPosY; // 上昇と下降
if(this.addPosY >= -MAX_GRAVITY)
{
this.addPosY -= 1;
}
if(this.posY > 480)
{
this.state = DEAD; // 死亡
}
}
else if(this.state == DEAD){
// 一定時間たったらコンテニュー画面へ
if(this.continueCnt++ >= CONTINUE_CNT){
this.playerNum--;
if(this.playerNum <= 0){
gState = GAME_OVER;
}
else{
gState = PRE_STAGE;
}
}
}
}
/**
マリオの死亡判定を返す
*/
Mario.prototype.isDead = function(){
if(this.state >= DEAD_ACTION){
return true;
}
return false;
}
/**
* マリオがキノコを取得した時の処理
*/
Mario.prototype.getKinoko = function(){
this.state = KINOKO_STATE;
this.height = MAP_SIZE * 2;
this.posY -= MAP_SIZE;
this.textureOffsetY = 128;
this.textureHeight = MAP_SIZE * 2;
gScore += KINOKO_SCORE;
}
/**
* get fire kinoko action
*/
Mario.prototype.getFireKinoko = function(){
this.state = FIRE_STATE;
this.textureOffsetY = 384;
this.textureHeight = MAP_SIZE * 2;
gScore += FIRE_SCORE;
}
/**
マリオの更新関数
kuribos:マリオのブロックアクション時に連動させるクリボの配列
nokos:マリオのブロックアクション時に連動させるノコノコの配列
docans:土管オブジェクト
*/
Mario.prototype.update = function(mapChip,kuribos,nokos,docans){
if(!this.isDead()){
// マップ座標の更新
this.updateMapPosition();
// 左キーが押されている状態
if(!this.keyDisable && !this.isSit){
if(gLeftPush){
if(docans){
for(var i = 0;i < docans.length;++i){
this.docanXEnter(docans[i],LEFT_DIR);
}
}
if(gSpacePush){
this.setIsDash(true);
this.moveX(mapChip,-DASH_SPEED);
}
else{
this.setIsDash(false);
this.moveX(mapChip,-NORMAL_SPPED);
}
}
}
// →キーが押されている状態
if(!this.keyDisable && !this.isSit){
if(gRightPush){
if(docans){
for(var i = 0;i < docans.length;++i){
this.docanXEnter(docans[i],RIGHT_DIR);
}
}
if(gSpacePush){
this.setIsDash(true);
this.moveX(mapChip,DASH_SPEED);
}
else{
this.setIsDash(false);
this.moveX(mapChip,NORMAL_SPPED);
}
}
}
// 下キーが押された
if(gDownPush){
if(!this.keyDisable){
this.sit();
if(docans){
for(var i = 0;i < docans.length;++i){
this.docanDownEnter(docans[i]);
}
}
}
}
else{
this.sitRelease();
}
this.docanMove();
// ジャンプ動作
if(!this.keyDisable){
if(gUpPush){
// ジャンプ設定をオンにする
this.setJumpSettings(gSpacePush);
}
}
// ジャンプ処理
if(!this.isMapMove && !this.stop()){
this.jumpAction(gUpPush,mapChip);
}
// マップチップアイテムオブジェクトとの当たり判定
this.collisionWithMapItem(mapChip);
// blockが動いたことによる当たり判定
this.blockCollisionAction(kuribos,nokos);
// fireShot処理
if(!this.keyDisable){
if(gADown){
// key状態を解除
gADown = false;
this.shotFire();
}
}
// scroll処理
this.doMapScrollX();
// star処理
this.starAction();
// 無敵処理
this.invincibleAction();
// 小さくする処理
this.smallAnimationAction();
// one up動作
this.oneUpAction();
// ゴールアニメーション
this.goalAnimationAction(mapChip);
// 連続ブロックアニメーション更新
this.updateCoinTimer();
// 落下判定
this.isFall();
}
// update fire
for(var i = 0;i < this.MAX_FIRE_NUM;++i){
this.fire[i].update(mapChip,this.moveNumX,this.mapScrollX);
}
// 死亡後処理
this.deadAction();
}
/**
* chapter37
* ブロックの上昇処理か破壊処理のアニメーション設定を仕込む
*
* mapIndexX : map index x座標
* mapIndexY : map index y座標
* map:マップチップ配列
* isUp:破壊できないマップの場合は上昇させる
*/
Mario.prototype.blockAction = function(mapIndexX,mapIndexY,isUp,map){
// 対象のマップチップ座標を代入
this.blockAttackIndexX[this.blockAttackIndex] = mapIndexX;
this.blockAttackIndexY[this.blockAttackIndex] = mapIndexY;
// 小さいかつ上昇フラグが立っている場合は破壊できないので、上昇させる
if(this.state == NORMAL_STATE && !isUp)
{
this.isBlockUp[this.blockAttackIndex] = true; // 上昇フラグon
this.blockUpX[this.blockAttackIndex] = mapIndexX * MAP_SIZE;
this.blockUpY[this.blockAttackIndex] = mapIndexY * MAP_SIZE;
this.blockAttackAddY[this.blockAttackIndex] = 8;
}
// 大きい場合は破壊可能
else if(this.isBig()){
// 空マップにする
replaceEmptyMap(map,mapIndexX,mapIndexY);
// アニメーションを仕込む
this.isBlockAttack[this.blockAttackIndex] = true; // block attack flag
// 右と左で分けているんだ
this.blockAttackX[this.blockAttackIndex][0] = this.blockAttackX[this.blockAttackIndex][1] = mapIndexX * MAP_SIZE;
this.blockAttackX[this.blockAttackIndex][2] = this.blockAttackX[this.blockAttackIndex][3] = mapIndexX * MAP_SIZE + HALF_MAP_SIZE;
this.blockAttackY[this.blockAttackIndex][0] = this.blockAttackY[this.blockAttackIndex][2] = mapIndexY * MAP_SIZE + HALF_MAP_SIZE;
this.blockAttackY[this.blockAttackIndex][1] = this.blockAttackY[this.blockAttackIndex][3] = mapIndexY * MAP_SIZE - HALF_MAP_SIZE;
this.blockAttackAddY[this.blockAttackIndex] = 10;
}
// animationフラグとして利用
this.blockAttackCnt[this.blockAttackIndex]++;
// 対象のブロック
if(++this.blockAttackIndex >= MAX_MAP_BLOCK)this.blockAttackIndex = 0;
}
/**
* チビマリオがブロックに頭突きした際にブロックを上下に動かす
* @param {*} mapIndexX
* @param {*} mapIndexY
* @param {*} map
*/
Mario.prototype.blockMoveAction = function(mapIndexX,mapIndexY,map){
// 対象のマップチップ座標を代入
this.blockAttackIndexX[this.blockAttackIndex] = mapIndexX;
this.blockAttackIndexY[this.blockAttackIndex] = mapIndexY;
this.isBlockUp[this.blockAttackIndex] = true; // 上昇フラグon
this.blockUpX[this.blockAttackIndex] = mapIndexX * MAP_SIZE;
this.blockUpY[this.blockAttackIndex] = mapIndexY * MAP_SIZE;
this.blockAttackAddY[this.blockAttackIndex] = 8;
// animationフラグとして利用
this.blockAttackCnt[this.blockAttackIndex]++;
// 対象のブロック
if(++this.blockAttackIndex >= MAX_MAP_BLOCK)this.blockAttackIndex = 0;
}
/**
* chapter37
* ブロックのアニメーション処理
*/
Mario.prototype.animateBlock = function(map){
// ブロックの数分
for(var i = 0;i < MAX_MAP_BLOCK;++i){
// ブロック破壊フラグ
if(this.isBlockAttack[i]){
// 上昇させる
for(var j = 0;j < 4;++j){
this.blockAttackY[i][j] -= this.blockAttackAddY[i];
}
this.blockAttackAddY[i] -= 1;
// 4つのブロックのアニメーション
this.blockAttackX[i][0] -= 4;
this.blockAttackX[i][1] = this.blockAttackX[i][0];
this.blockAttackX[i][2] += 4;
this.blockAttackX[i][3] = this.blockAttackX[i][2];
// ブロックが画面外に出たらアニメーションを停止する
if(this.blockAttackY[i][3] >= 512){
this.isBlockAttack[i] = false;
}
}
// ブロック上昇処理
else if(this.isBlockUp[i]){
this.blockAttackAddY[i] -= 1;
// 上下運動が終わった場合
if(this.blockAttackAddY[i] == 0){
this.isBlockUp[i] = false;
}
}
}
}
/**
* chapter37
* blockが移動したときのcollision eventを発生させる
* collisionのところで呼ぶ
*
* kuribos:マップチップアクションの対象となるクリボの配列
* nokos:マップチップアクションの対象となるクリボの配列
*/
Mario.prototype.blockCollisionAction = function(kuribos,nokos){
for(var i = 0;i < MAX_MAP_BLOCK;++i){
// ブロック破壊フラグ
if(this.isBlockAttack[i]){
// きのこの上昇処理(ずらした分を考慮)
this.kinoko.blockUpAction(this.blockAttackX[i][0],this.blockAttackY[i][0] - HALF_MAP_SIZE);
// one up kinokoの上昇処理
this.oneUpKinoko.blockUpAction(this.blockAttackX[i][0],this.blockAttackY[i][0] - HALF_MAP_SIZE);
// クリボの当たり判定
if(kuribos != null){
for(var j = 0;j < kuribos.length;++j){
kuribos[j].blockUpAction(this.blockAttackX[i][0],this.blockAttackY[i][0] - HALF_MAP_SIZE);
}
}
// ノコノコの当たり判定
if(nokos != null){
for(var j = 0;j < nokos.length;++j){
nokos[j].blockUpAction(this.blockAttackX[i][0],this.blockAttackY[i][0] - HALF_MAP_SIZE);
}
}
}
// ブロック上昇処理
else if(this.isBlockUp[i]){
// きのこの上昇処理
this.kinoko.blockUpAction(this.blockUpX[i],this.blockUpY[i]);
this.oneUpKinoko.blockUpAction(this.blockUpX[i],this.blockUpY[i]);
// クリボ
if(kuribos != null){
for(var j = 0;j < kuribos.length;++j){
kuribos[j].blockUpAction(this.blockUpX[i],this.blockUpY[i]);
}
}
// ノコノコ
if(nokos != null){
for(var j = 0;j < nokos.length;++j){
nokos[j].blockUpAction(this.blockUpX[i],this.blockUpY[i]);
}
}
}
}
}
/**