-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameBoard.cs
1688 lines (1540 loc) · 75.5 KB
/
GameBoard.cs
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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MusicPlayer;
using System;
using System.Collections.Generic;
namespace PuzzleBattle
{
public class GameBoard : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
BlendState alphaBlend = new BlendState()
{
AlphaBlendFunction = BlendFunction.Add,
AlphaSourceBlend = Blend.One,
AlphaDestinationBlend = Blend.InverseSourceAlpha,
ColorBlendFunction = BlendFunction.Add,
ColorSourceBlend = Blend.SourceAlpha,
ColorDestinationBlend = Blend.InverseSourceAlpha
};
Random random;
GameState CurrentGameState = GameState.Match;
MatchState CurrentMatchState;
// 全域遊戲狀態紀錄區
enum GameState
{
StartScreen,
Match,
ResultScreen
}
enum MatchPhase
{
NotStarted,
TurnStart,
Placement,
AITurnStart,
AIPlacement,
AfterPlacement,
Ended
}
class MatchState
{
// TODO:PVP模式
public bool VSMode = false;
public int Turns = 1;
// public TimeSpan LastLoopStartTime;
public MatchPhase Phase = MatchPhase.NotStarted;
public double AIActionTimer = 0;
public MovementState PlacementAnimation = new MovementState(0, 0, 0);
public bool IsPointingTile = false;
public short PointedTileX, PointedTileY;
public class PlayerState
{
// 存放Reserves與Next Piece的陣列
public PieceShape[] Reserves = new PieceShape[] { new PieceShape(), new PieceShape(), new PieceShape() };
public PieceShape NextPiece;
// 集氣條
public short BombGauge = 0, DisplayedBombGauge = 0;
// 玩家動畫
public enum AnimationState
{
Idle, // 一般狀態
// TODO:Panic(可下的步數很少時)
Fall, // 確定落敗時(單次動畫)
Dead, // 落敗後(靜止)
Victory // 勝利時(循環動畫)
}
AnimationState _aniState;
short _aFrame = 0;
public AnimationState AvatarState
{
get
{
return _aniState;
}
set
{
// 每次改變State都從頭開始播
_aniState = value;
_aFrame = 0;
}
}
public short AvatarCurrentFrame
{
get
{
return (short)(Math.Floor(_aFrame / (60f / AVATAR_FRAMERATE)) + 1);
}
set
{
_aFrame = (short)(value * (int)(60f / AVATAR_FRAMERATE) - 1);
}
}
/// <summary>
/// 推進Avatar動畫
/// </summary>
public void AvatarFrameForward()
{
_aFrame++;
switch (AvatarState)
{
case AnimationState.Idle:
if (AvatarCurrentFrame > AVATAR_IDLE_FRAMES) _aFrame = 0;
break;
case AnimationState.Fall:
if (AvatarCurrentFrame > AVATAR_FALL_FRAMES)
{
_aFrame = 0;
AvatarState = AnimationState.Dead;
}
break;
case AnimationState.Dead:
if (AvatarCurrentFrame > AVATAR_DEAD_FRAMES) _aFrame = 0;
break;
case AnimationState.Victory:
if (AvatarCurrentFrame > AVATAR_VICTORY_FRAMES) _aFrame = 0;
break;
}
}
}
public List<PlayerState> Player = new List<PlayerState>();
// 目前行動中的玩家編號
// 0 = 1P(畫面下方) 1 = 2P(畫面上方)
short _cPlayer;
public short CurrentPlayer
{
get
{
return _cPlayer;
}
set
{
_cPlayer = value;
}
}
public void SwitchPlayer()
{
if (_cPlayer == 0) _cPlayer = 1; else _cPlayer = 0;
_chosenPieceNo = 0;
}
short _chosenPieceNo;
/// <summary>
/// 目前選取的Piece是Reserves中的第幾個
/// </summary>
public short ChosenPiece
{
get
{
return _chosenPieceNo;
}
set
{
_chosenPieceNo = value;
UpdateCurrentPieceShape();
}
}
public PieceShape CurrentPieceShape;
/// <summary>
/// 旋轉目前選定的Piece,預設為順時針
/// </summary>
/// <param name="clockwise">是否為順時針旋轉</param>
public void Rotate(bool clockwise = true)
{
if (clockwise)
{
Player[_cPlayer].Reserves[_chosenPieceNo] = GetRotatedPieceShape(Player[_cPlayer].Reserves[_chosenPieceNo], 1);
}
else
{
Player[_cPlayer].Reserves[_chosenPieceNo] = GetRotatedPieceShape(Player[_cPlayer].Reserves[_chosenPieceNo], -1);
}
UpdateCurrentPieceShape();
}
public void UpdateCurrentPieceShape()
{
CurrentPieceShape = Player[_cPlayer].Reserves[_chosenPieceNo];
}
/// <summary>
/// 根據輸入的Piece種類與旋轉角度,傳回最終的Piece形狀
/// </summary>
/// <param name="piece">原始Piece</param>
/// <param name="rotation">旋轉方式,支援 1-2-3(順時針90-180-270度)和 1 / -1(順時針90 / 逆時針90度)兩種格式</param>
/// <returns></returns>
public PieceShape GetRotatedPieceShape(PieceShape piece, short rotation)
{
PieceShape newPiece;
switch (rotation)
{
case 1:
// 順時針90度:876543210 => 258147036
newPiece = new PieceShape(piece.Tile6, piece.Tile3, piece.Tile0, piece.Tile7, piece.Tile4, piece.Tile1, piece.Tile8, piece.Tile5, piece.Tile2);
return newPiece;
case 2:
// 順時針180度:876543210 => 012345678
newPiece = new PieceShape(piece.Tile8, piece.Tile7, piece.Tile6, piece.Tile5, piece.Tile4, piece.Tile3, piece.Tile2, piece.Tile1, piece.Tile0);
return newPiece;
case -1:
case 3:
// 逆時針90度:876543210 => 630741852
newPiece = new PieceShape(piece.Tile2, piece.Tile5, piece.Tile8, piece.Tile1, piece.Tile4, piece.Tile7, piece.Tile0, piece.Tile3, piece.Tile6);
return newPiece;
default:
return piece;
}
}
}
/// <summary>
/// 動畫狀態
/// </summary>
class MovementState
{
public MovementState(short tile_x, short tile_y, double time)
{
TileX = tile_x;
TileY = tile_y;
TotalDuration = time;
ElapsedDuration = 0;
}
public bool IsAnimating = false;
public short TileX;
public short TileY;
public double TotalDuration;
public double ElapsedDuration;
/// <summary>
/// 累積經過時間移動位置,如果時間到則傳回True並將IsAnimating狀態關掉
/// </summary>
/// <param name="ms">累積的經過時間</param>
/// <returns></returns>
public bool TimerUpdate(double ms)
{
bool t = false;
ElapsedDuration += ms;
if (ElapsedDuration >= TotalDuration)
{
IsAnimating = false;
t = true;
}
return t;
}
}
// 資源存放區
Texture2D TEX_TILE, TEX_NEXT, TEX_RESERVES;
Texture2D TEX_KEYS, TEX_CONTROLS, TEX_EXITRETRY;
Texture2D TEX_BORDER;
Texture2D TEX_AVATAR_IDLE, TEX_AVATAR_FALL, TEX_AVATAR_DEAD, TEX_AVATAR_VICTORY;
Texture2D TEX_MOB_HOP, TEX_MOB_TURN, TEX_MOB_IDLE;
Texture2D TEX_BOMB, TEX_PODIUM, TEX_BACKDROP, TEX_GAUGE_BORDER, TEX_GAUGE;
// 音效
SoundPlayer soundPlayer;
/// <summary>
/// 音效編號
/// </summary>
public enum SFX
{
Intro = 0,
MainLoop = 1,
MatchEnd = 2,
CursorMove = 3,
Rotate = 4,
ReserveSelect = 5,
Place = 6,
Bomb = 7
}
// 盤面layout設定區
const short TILE_WIDTH = 32, TILE_HEIGHT = 32, BORDER_THICKNESS = 16;
const short KEY_WIDTH = 36, KEY_HEIGHT = 36;
const short BOMB_WIDTH = 128, BOMB_HEIGHT = 128, BOMB_FRAMES = 6, BOMB_INTERVAL = 5;
static short VIEW_WIDTH = 1024, VIEW_HEIGHT = 720;
short BOARD_MAX_Y = 10, BOARD_MAX_X = 10;
short BOMB_COST = 20, BOMBGAUGE_MAX = 40;
short RESERVES_WIDTH, RESERVES_HEIGHT, NEXT_WIDTH, NEXT_HEIGHT;
Vector2 BOARD_TOPLEFT, RESERVE_TOPLEFT_1P, NEXT_PIECE_TOPLEFT_1P, RESERVE_TOPLEFT_2P, NEXT_PIECE_TOPLEFT_2P;
const short CONTROLS_WIDTH = 184, CONTROLS_HEIGHT = 150, EXITRETRY_WIDTH = 152, EXITRETRY_HEIGHT = 54;
Vector2 CONTROLS_XY;
// 動畫layout設定區
const short AVATAR_WIDTH = 160, AVATAR_HEIGHT = 200;
const short MOB_WIDTH = 80, MOB_HEIGHT = 100;
const short AVATAR_FRAMERATE = 12, EFFECT_FRAMERATE = 24, AVATAR_IDLE_FRAMES = 24, AVATAR_FALL_FRAMES = 10, AVATAR_DEAD_FRAMES = 12, AVATAR_VICTORY_FRAMES = 18;
const short MOB_HOP_FRAMES = 8, MOB_TURN_FRAMES = 4, MOB_IDLE_FRAMES = 8;
const float MOB_MIN_Y = 230;
const int MOB_MAX = 200, MOB_SPAWN_INTERVAL = 10; // 圍觀球兔上限人數、出現頻率
static Vector2 AVATAR_TOPLEFT_1P, AVATAR_TOPLEFT_2P, PODIUM_TOPLEFT_1P, PODIUM_TOPLEFT_2P, BOMBGAUGE_TOPLEFT_1P, BOMBGAUGE_TOPLEFT_2P;
static Rectangle BOMBDOT_1P, BOMBDOT_2P;
// Piece移動動畫總毫秒數
static short PIECE_MOVE_TIME = 100;
// 色彩
Color COLOR_UNPLACEABLE = new Color(255, 255, 0, 255);
Color COLOR_PLACEABLE = new Color(255, 255, 255, 128);
Color COLOR_GAUGE_FILLED = new Color(0, 255, 0, 255);
Color COLOR_GAUGE_EMPTY = new Color(0, 0, 0, 255);
// 存放盤面的陣列,Content Load時再決定實際大小
// 盤面中0代表空格,1代表1P佔領,2代表2P佔領
// 所以將Piece填入盤面時要將Player編號+1
short[,] Board;
// 存放Piece類別的資料陣列
// 每種類別都是3x3共9格的Tile,所以需要9位元來儲存
List<PieceShape> PieceTypes;
public struct PieceShape
{
public bool Tile0, Tile1, Tile2, Tile3, Tile4, Tile5, Tile6, Tile7, Tile8;
/// <summary>
/// 以字串建構Piece形狀
/// </summary>
/// <param name="tiles">9個字元的字串,1代表有方塊,其他字元(建議用0)代表沒有方塊</param>
public PieceShape(string tiles)
{
Tile0 = tiles[0] == '1';
Tile1 = tiles[1] == '1';
Tile2 = tiles[2] == '1';
Tile3 = tiles[3] == '1';
Tile4 = tiles[4] == '1';
Tile5 = tiles[5] == '1';
Tile6 = tiles[6] == '1';
Tile7 = tiles[7] == '1';
Tile8 = tiles[8] == '1';
}
/// <summary>
/// 以九個bool值建構Piece形狀
/// </summary>
public PieceShape(bool t0, bool t1, bool t2, bool t3, bool t4, bool t5, bool t6, bool t7, bool t8)
{
Tile0 = t0;
Tile1 = t1;
Tile2 = t2;
Tile3 = t3;
Tile4 = t4;
Tile5 = t5;
Tile6 = t6;
Tile7 = t7;
Tile8 = t8;
}
public bool GetTile(int tileNo)
{
switch (tileNo)
{
case 0: return Tile0;
case 1: return Tile1;
case 2: return Tile2;
case 3: return Tile3;
case 4: return Tile4;
case 5: return Tile5;
case 6: return Tile6;
case 7: return Tile7;
default: return Tile8;
}
}
}
// 圖層專用Enum(由前至後)
public enum Layer
{
Message,
FrontEffect,
PlacingPiece,
PiecePreview,
PlacedPieces,
PoolSelection,
PoolBorder,
AvatarAnimation,
BoardBackground,
GaugeBorder,
Gauge,
Podium,
Mobs,
Background,
MAX
}
/// <summary>
/// 特效動畫
/// </summary>
public class EffectAnimation
{
public short X, Y;
public bool Looping = false;
public Texture2D AnimationTexture;
public Vector2 AnimationPosition;
public short FrameRate;
short _cFrame = 0;
public short CurrentFrame
{
get
{
return (short)(_cFrame / (60 / FrameRate) + 1);
}
set
{
_cFrame = (short)(value * (int)(60 / FrameRate) - 1);
}
}
public short FramesToDelay, FramesPerLoop;
public AnimationState State;
public enum AnimationState
{
Delaying, Playing, Ended
}
public EffectAnimation(Texture2D texture, Vector2 position, short totalFrames, short delayFrames = 0, short tileX = 0, short tileY = 0, short fps = EFFECT_FRAMERATE, bool loops = false)
{
X = tileX;
Y = tileY;
AnimationTexture = texture;
AnimationPosition = position;
FramesToDelay = delayFrames;
FramesPerLoop = totalFrames;
FrameRate = fps;
Looping = loops;
if (delayFrames > 0) State = AnimationState.Delaying; else State = AnimationState.Playing;
}
public void FrameForward()
{
switch (State)
{
case AnimationState.Delaying:
FramesToDelay--;
if (FramesToDelay == 0) State = AnimationState.Playing;
break;
case AnimationState.Playing:
_cFrame++;
if (CurrentFrame > FramesPerLoop)
{
if (Looping) _cFrame = 0; else State = AnimationState.Ended;
}
break;
}
}
}
// 動畫排程
List<EffectAnimation> BombAnimations; // 炸彈動畫
EffectAnimation ControlsAnimation; // 操作說明動畫
/// <summary>
/// 圍觀球兔
/// </summary>
public class Mob
{
public enum MobStates { Hopping, Turning, Idle }
public MobStates MobState = MobStates.Hopping;
public float CurrentX, TargetX, TargetY, Scale;
public bool LeftEntry = true;
short _cFrame;
public short CurrentFrame
{
get
{
return (short)(_cFrame / (60 / AVATAR_FRAMERATE) + 1);
}
set
{
_cFrame = (short)(value * (int)(60 / AVATAR_FRAMERATE) - 1);
}
}
public Mob(float x, float y)
{
TargetX = x;
TargetY = y;
Scale = (y - MOB_MIN_Y) / (PODIUM_TOPLEFT_1P.Y - MOB_HEIGHT - MOB_MIN_Y) * .5f + .5f;
// 決定圍觀球兔的進場延遲
if (x > VIEW_WIDTH / 2)
{
LeftEntry = false;
CurrentX = VIEW_WIDTH + (TargetX % AVATAR_FRAMERATE);
}
else
{
CurrentX = -MOB_WIDTH - (TargetX % AVATAR_FRAMERATE);
}
}
/// <summary>
/// 推進圍觀球兔動畫
/// </summary>
public void MobFrameForward()
{
_cFrame++;
switch (MobState)
{
case MobStates.Hopping:
// Hopping:還在蹦跳移動中
if (LeftEntry) CurrentX++; else CurrentX--;
// 落地後如果已超過目標位置就接Turn
if (CurrentFrame > MOB_HOP_FRAMES)
{
if ((LeftEntry && CurrentX >= TargetX) || (!LeftEntry && CurrentX <= TargetX))
{
MobState = MobStates.Turning;
}
_cFrame = 0;
}
break;
case MobStates.Turning:
// Turn:播完接Idle
if (CurrentFrame > MOB_TURN_FRAMES)
{
_cFrame = 0;
MobState = MobStates.Idle;
}
break;
case MobStates.Idle:
// Idle:迴圈
if (CurrentFrame > MOB_IDLE_FRAMES) _cFrame = 0;
break;
}
}
}
List<Mob> MobAnimations;
// 輸入
MouseState mouPrevious, mouCurrent;
KeyboardState keyPrevious, keyCurrent;
// 鍵盤定義
static Keys KEY_EXIT = Keys.Escape;
static Keys KEY_REMATCH = Keys.Space;
static Keys KEY_CHOICE_1 = Keys.A, KEY_CHOICE_2 = Keys.S, KEY_CHOICE_3 = Keys.D;
static Keys KEY_BOMB = Keys.B;
static Keys KEY_ROTATE_COUNTERCLOCKWISE = Keys.Q, KEY_ROTATE_CLOCKWISE = Keys.W;
// 音量
static float VOLUME_SFX = .5f, VOLUME_BGM = 1f;
public GameBoard()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
graphics.PreferMultiSampling = false;
IsMouseVisible = true;
// 設定畫面
graphics.IsFullScreen = false;
graphics.PreferredBackBufferWidth = VIEW_WIDTH;
graphics.PreferredBackBufferHeight = VIEW_HEIGHT;
graphics.ApplyChanges();
random = new Random();
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
TEX_TILE = Content.Load<Texture2D>("Sprites\\tiles");
TEX_BORDER = Content.Load<Texture2D>("Sprites\\border");
TEX_NEXT = Content.Load<Texture2D>("Sprites\\next");
TEX_KEYS = Content.Load<Texture2D>("Sprites\\keys");
TEX_CONTROLS = Content.Load<Texture2D>("Sprites\\controls");
TEX_EXITRETRY = Content.Load<Texture2D>("Sprites\\inout");
TEX_AVATAR_IDLE = Content.Load<Texture2D>("Sprites\\br-idle");
TEX_AVATAR_FALL = Content.Load<Texture2D>("Sprites\\br-fall");
TEX_AVATAR_DEAD = Content.Load<Texture2D>("Sprites\\br-dead");
TEX_AVATAR_VICTORY = Content.Load<Texture2D>("Sprites\\br-victory");
TEX_MOB_HOP = Content.Load<Texture2D>("Sprites\\mob-hop");
TEX_MOB_TURN = Content.Load<Texture2D>("Sprites\\mob-turn");
TEX_MOB_IDLE = Content.Load<Texture2D>("Sprites\\mob-idle");
TEX_BOMB = Content.Load<Texture2D>("Sprites\\bomb");
TEX_PODIUM = Content.Load<Texture2D>("Sprites\\podium");
TEX_GAUGE_BORDER = Content.Load<Texture2D>("Sprites\\gauge");
TEX_GAUGE = Content.Load<Texture2D>("Sprites\\gaugedot");
// 音效
soundPlayer = new SoundPlayer();
soundPlayer.Initialize(Content.RootDirectory + "\\" + "Audio");
soundPlayer.Clear();
soundPlayer.AddSound("intro.ogg", VOLUME_BGM, false, true);
soundPlayer.AddSound("loop.ogg", VOLUME_BGM, true);
soundPlayer.AddSound("matchend.ogg", VOLUME_BGM);
soundPlayer.AddSound("cursor_move.ogg", VOLUME_SFX);
soundPlayer.AddSound("rotate.ogg", VOLUME_SFX);
soundPlayer.AddSound("select.ogg", VOLUME_SFX);
soundPlayer.AddSound("place.ogg", VOLUME_SFX);
soundPlayer.AddSound("bomb.ogg", VOLUME_SFX);
// 設定各項layout位置
// 設定Avatar位置
// Avatar位置是Pool列以外高度置中,與盤面邊緣距離固定
AVATAR_TOPLEFT_1P = new Vector2(VIEW_WIDTH - AVATAR_WIDTH - 64, (VIEW_HEIGHT - RESERVES_HEIGHT - AVATAR_HEIGHT + RESERVES_HEIGHT) / 2);
AVATAR_TOPLEFT_2P = new Vector2(64, AVATAR_TOPLEFT_1P.Y);
PODIUM_TOPLEFT_1P = new Vector2(VIEW_WIDTH - TEX_PODIUM.Width - 64 + (TEX_PODIUM.Width - AVATAR_WIDTH) / 2, AVATAR_TOPLEFT_1P.Y + (AVATAR_HEIGHT - TEX_PODIUM.Height) * 1.4f);
PODIUM_TOPLEFT_2P = new Vector2(64 - (TEX_PODIUM.Width - AVATAR_WIDTH) / 2, PODIUM_TOPLEFT_1P.Y);
BOMBGAUGE_TOPLEFT_1P = new Vector2(VIEW_WIDTH - TEX_GAUGE_BORDER.Width - 64 + (TEX_GAUGE_BORDER.Width - AVATAR_WIDTH) / 2, PODIUM_TOPLEFT_1P.Y + TEX_PODIUM.Height - TEX_GAUGE_BORDER.Height * 1.4f);
BOMBGAUGE_TOPLEFT_2P = new Vector2(64 - (TEX_GAUGE_BORDER.Width - AVATAR_WIDTH) / 2, BOMBGAUGE_TOPLEFT_1P.Y);
BOMBDOT_1P = new Rectangle((int)(BOMBGAUGE_TOPLEFT_1P.X + 7), (int)(BOMBGAUGE_TOPLEFT_1P.Y + 4), TEX_GAUGE_BORDER.Width - 14, TEX_GAUGE_BORDER.Height - 8);
BOMBDOT_2P = new Rectangle((int)(BOMBGAUGE_TOPLEFT_2P.X + 7), (int)(BOMBGAUGE_TOPLEFT_2P.Y + 4), TEX_GAUGE_BORDER.Width - 14, TEX_GAUGE_BORDER.Height - 8);
// 設定Pool起點位置
// Next Piece最大寬度是3格
// Reserves的寬度是(Piece最大寬度3格*3)=9格 + Piece之間各1格緩衝共2格 + 左右各1格緩衝共2格 + 邊框共2格緩衝,與盤面一樣置中
// 因此第一個Piece的左端是Reserves左端 + 左邊框1格 + 左端0.5格緩衝,共1.5格
// Reserves的高度是Piece最大高度3格 + 上下各1格緩衝共2格 + 邊框共2格緩衝
// 因此第一個Piece的頂端是Reserves頂端 + 上邊框1格 + 上端1格,共2格
RESERVES_WIDTH = TILE_WIDTH * 15;
RESERVES_HEIGHT = TILE_HEIGHT * 7;
// 1P側Reserves位於盤面下方
RESERVE_TOPLEFT_1P = new Vector2((VIEW_WIDTH - RESERVES_WIDTH) / 2, VIEW_HEIGHT - RESERVES_HEIGHT + 48);
// 2P側Reserves位於盤面上方
RESERVE_TOPLEFT_2P = new Vector2((VIEW_WIDTH - RESERVES_WIDTH) / 2, -48);
// Next Piece Pool的寬度是Piece最大寬度3格 + 左右各0.5格緩衝共1格 + 邊框共2格緩衝,從畫面右端逆算位置
// Next Piece Pool的高度和Reserves一樣
NEXT_WIDTH = TILE_WIDTH * 6;
NEXT_HEIGHT = RESERVES_HEIGHT;
// 1P側Next Piece位於左下方,與邊緣固定距離
NEXT_PIECE_TOPLEFT_1P = new Vector2(64, VIEW_HEIGHT - NEXT_HEIGHT + 48);
// 2P側Next Piece位於右上方,與邊緣固定距離
NEXT_PIECE_TOPLEFT_2P = new Vector2(VIEW_WIDTH - NEXT_WIDTH - 64, -48);
// 操作說明位於右下方,與邊緣固定距離
CONTROLS_XY = new Vector2(VIEW_WIDTH - CONTROLS_WIDTH - KEY_WIDTH, VIEW_HEIGHT - CONTROLS_HEIGHT - KEY_HEIGHT * 0.5f);
// 設定盤面起點位置
// 盤面置中
BOARD_TOPLEFT = new Vector2((VIEW_WIDTH - TILE_WIDTH * BOARD_MAX_X) / 2, (VIEW_HEIGHT - TILE_HEIGHT * BOARD_MAX_Y) / 2);
// 生成Piece種類
// == Piece 種類 ==
// 0:方塊 1:S形 2:L形 3:I形 4:小L形 5:T形 6:大S形 7:大L形 8:短I形 9: 逆S 10: 逆大S 11: W形
// ■■□ □□■ ■■□ □■□ □■□ □■□ □□■ ■■■ □■□ ■□□ ■□□ □□■
// ■■□ □■■ □■□ □■□ ■■□ ■■■ ■■■ □□■ □■□ ■■□ ■■■ □■■
// □□□ □■□ □■□ □■□ □□□ □□□ ■□□ □□■ □□□ □■□ □□■ ■■□
PieceTypes = new List<PieceShape>();
PieceTypes.Add(new PieceShape("110110000"));
PieceTypes.Add(new PieceShape("001011010"));
PieceTypes.Add(new PieceShape("110010010"));
PieceTypes.Add(new PieceShape("010010010"));
PieceTypes.Add(new PieceShape("010110000"));
PieceTypes.Add(new PieceShape("010111000"));
PieceTypes.Add(new PieceShape("001111100"));
PieceTypes.Add(new PieceShape("111001001"));
PieceTypes.Add(new PieceShape("010010000"));
PieceTypes.Add(new PieceShape("100110010"));
PieceTypes.Add(new PieceShape("100111001"));
PieceTypes.Add(new PieceShape("001011110"));
// 準備玩家
CurrentMatchState = new MatchState();
CurrentMatchState.Player.Add(new MatchState.PlayerState());
CurrentMatchState.Player.Add(new MatchState.PlayerState());
// 讀取滑鼠鍵盤狀態
mouCurrent = Mouse.GetState();
mouPrevious = mouCurrent;
keyCurrent = Keyboard.GetState();
keyPrevious = keyCurrent;
// 準備動畫
BombAnimations = new List<EffectAnimation>();
MobAnimations = new List<Mob>();
ControlsAnimation = new EffectAnimation(TEX_CONTROLS, CONTROLS_XY, 20, 0, 0, 0, 12, true);
TEX_RESERVES = Content.Load<Texture2D>("Sprites\\reserves");
TEX_BACKDROP = Content.Load<Texture2D>("Sprites\\backdrop");
// 生成盤面、雙方的Reserves和Next Piece等
InitializeMatch();
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific content.
/// </summary>
protected override void UnloadContent()
{
soundPlayer.Dispose();
Dispose();
}
protected override void Update(GameTime gameTime)
{
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// Intro銜接Loop的工作交給SoundPlayer做
soundPlayer.Update();
// BGM在一方敗北時銜接到結束曲
if (CurrentMatchState.Phase == MatchPhase.Ended && soundPlayer.IsPlaying((int)SFX.MainLoop))
{
uint approach = soundPlayer.GetPosition((int)SFX.MainLoop) % 441;
if (approach < 50 || approach > 391)
{
// 如果時機夠接近了就換歌
soundPlayer.StopSound((int)SFX.MainLoop);
soundPlayer.StartSound((int)SFX.MatchEnd);
}
else
{
// 不夠的話就fade out
soundPlayer.SetVolume((int)SFX.MainLoop, soundPlayer.GetVolume((int)SFX.MainLoop) * .9f);
}
}
// 更新輸入
UpdateMouseInput();
UpdateKeyInput();
// 更新操作說明動畫
ControlsAnimation.FrameForward();
CurrentMatchState.Player[0].AvatarFrameForward();
CurrentMatchState.Player[1].AvatarFrameForward();
// 更新集氣條顯示值
if (CurrentMatchState.Player[0].DisplayedBombGauge < CurrentMatchState.Player[0].BombGauge) CurrentMatchState.Player[0].DisplayedBombGauge++;
if (CurrentMatchState.Player[0].DisplayedBombGauge > CurrentMatchState.Player[0].BombGauge) CurrentMatchState.Player[0].DisplayedBombGauge -= 2;
if (CurrentMatchState.Player[1].DisplayedBombGauge < CurrentMatchState.Player[1].BombGauge) CurrentMatchState.Player[1].DisplayedBombGauge++;
if (CurrentMatchState.Player[1].DisplayedBombGauge > CurrentMatchState.Player[1].BombGauge) CurrentMatchState.Player[1].DisplayedBombGauge -= 2;
// 更新炸彈動畫
foreach (EffectAnimation obj in BombAnimations)
{
obj.FrameForward();
if (obj.State == EffectAnimation.AnimationState.Ended) BombClear(obj.X, obj.Y);
}
BombAnimations.RemoveAll(obj => obj.State == EffectAnimation.AnimationState.Ended);
// 更新圍觀球兔動畫
MobAnimations.ForEach(obj => obj.MobFrameForward());
switch (CurrentMatchState.Phase)
{
case MatchPhase.TurnStart:
Update_TurnStart();
break;
case MatchPhase.AITurnStart:
// AI回合開始時稍作停頓
CurrentMatchState.AIActionTimer += gameTime.ElapsedGameTime.TotalMilliseconds;
if (CurrentMatchState.AIActionTimer >= .5)
{
CurrentMatchState.Phase = MatchPhase.AIPlacement;
CurrentMatchState.AIActionTimer = 0;
}
break;
case MatchPhase.AIPlacement:
Update_AIPlacement(gameTime);
break;
case MatchPhase.AfterPlacement:
Update_Afterplacement();
break;
}
base.Update(gameTime);
}
private void Update_Afterplacement()
{
// 判斷放置Piece動畫是否播完
if (!CurrentMatchState.PlacementAnimation.IsAnimating)
{
// 動畫已播完
// 實際將Piece存入盤面
PlacePiece(CurrentMatchState.PlacementAnimation.TileX, CurrentMatchState.PlacementAnimation.TileY, CurrentMatchState.CurrentPieceShape);
// 遞補Piece
CurrentMatchState.Player[CurrentMatchState.CurrentPlayer].Reserves[CurrentMatchState.ChosenPiece] = CurrentMatchState.Player[CurrentMatchState.CurrentPlayer].NextPiece;
CurrentMatchState.Player[CurrentMatchState.CurrentPlayer].NextPiece = PieceTypes[random.Next(0, PieceTypes.Count)];
// 替換玩家
CurrentMatchState.SwitchPlayer();
CurrentMatchState.UpdateCurrentPieceShape();
CurrentMatchState.AIActionTimer = 0;
// 回合數增加
CurrentMatchState.Turns++;
// 在場上增加圍觀球兔(有上限)
// TODO:想辦法讓球兔不會撞位置?
if (CurrentMatchState.Turns % MOB_SPAWN_INTERVAL == 0 && MobAnimations.Count < MOB_MAX)
{
float dist = (float)random.NextDouble() * BOARD_TOPLEFT.X - MOB_WIDTH;
// 有一半機率從右邊入場
if (random.NextDouble() > .5) dist = VIEW_WIDTH - dist - MOB_WIDTH;
MobAnimations.Add(new Mob(dist, (float)random.NextDouble() * (PODIUM_TOPLEFT_1P.Y - MOB_HEIGHT - MOB_MIN_Y) + MOB_MIN_Y));
}
CurrentMatchState.Phase = MatchPhase.TurnStart;
}
}
private void Update_AIPlacement(GameTime gameTime)
{
if (CurrentMatchState.AIActionTimer == 0)
{
// 進入AIPlacement階段的瞬間,AI開始思考
short xPick, yPick;
short reservePick, rotationPick;
GetPlaceable(out xPick, out yPick, out reservePick, out rotationPick);
// 思考完畢,確定放置
soundPlayer.StartSound((int)SFX.Place);
// 先旋轉reserve
CurrentMatchState.Player[CurrentMatchState.CurrentPlayer].Reserves[reservePick] = CurrentMatchState.GetRotatedPieceShape(CurrentMatchState.Player[CurrentMatchState.CurrentPlayer].Reserves[reservePick], rotationPick);
// 再選擇reserve,才會在ChosenPiece過程中正確更新形狀
CurrentMatchState.ChosenPiece = reservePick;
CurrentMatchState.PointedTileX = xPick;
CurrentMatchState.PointedTileY = yPick;
CurrentMatchState.Phase = MatchPhase.AfterPlacement;
CurrentMatchState.PlacementAnimation = new MovementState(CurrentMatchState.PointedTileX, CurrentMatchState.PointedTileY, PIECE_MOVE_TIME);
CurrentMatchState.PlacementAnimation.IsAnimating = true;
}
CurrentMatchState.AIActionTimer += gameTime.ElapsedGameTime.TotalMilliseconds;
}
private void Update_TurnStart()
{
// 如果有炸彈動畫正在播放就先停頓
// (炸彈播放時Phase一定會是TurnStart)
if (BombAnimations.Count > 0) return;
// 判定是否有步可下,有則移至Placement階段,否則移至Ended階段
// 針對三個Reserves判定盤面上是否有空間可放,只要有就停止判定
PieceShape shapeToFind;
bool isAlive = false;
for (int i = 0; i < 3; i++)
{
shapeToFind = CurrentMatchState.Player[CurrentMatchState.CurrentPlayer].Reserves[i];
// 偵測各種旋轉後的形狀
for (int r = 0; r < 4; r++)
{
for (int x = 0; x <= BOARD_MAX_X; x++)
{
for (int y = 0; y <= BOARD_MAX_Y; y++)
{
if (IsPlaceable(shapeToFind, x, y))
{
isAlive = true;
// 可以進行回合
if (CurrentMatchState.VSMode || CurrentMatchState.CurrentPlayer == 0)
{
// 如果是玩家的回合就進入Placement階段
CurrentMatchState.Phase = MatchPhase.Placement;
}
else
{
// 如果是電腦的回合就進入AIPlacement階段並直接呼叫AI思考程序
// AI思考程序內會自動決定行動並推進到AfterPlacement階段
CurrentMatchState.Phase = MatchPhase.AITurnStart;
}
break;
}
}
}
// 轉一次
shapeToFind = CurrentMatchState.GetRotatedPieceShape(shapeToFind, 1);
}
}
// 如果有炸彈就無條件可以行動
if (CurrentMatchState.Player[CurrentMatchState.CurrentPlayer].BombGauge >= BOMB_COST)
{
if (!CurrentMatchState.VSMode && CurrentMatchState.CurrentPlayer == 1)
{
// 如果輪到AI行動且不用炸彈不行就會自動用炸彈
if (!isAlive)
{
CurrentMatchState.Player[CurrentMatchState.CurrentPlayer].BombGauge -= BOMB_COST;
BombBoard();
// 不改變階段,仍在TurnStart
}
}
else
{
// 如果是玩家的回合就進入Placement階段,由玩家操作
CurrentMatchState.Phase = MatchPhase.Placement;
}
// 不管哪種情形都不播放敗北動畫
isAlive = true;
}
if (!isAlive)
{
// 播放敗北玩家的Fall動畫、勝利玩家的Victory動畫
short oppo;
if (CurrentMatchState.CurrentPlayer == 0) oppo = 1; else oppo = 0;
CurrentMatchState.Player[CurrentMatchState.CurrentPlayer].AvatarState = MatchState.PlayerState.AnimationState.Fall;
CurrentMatchState.Player[oppo].AvatarState = MatchState.PlayerState.AnimationState.Victory;
CurrentMatchState.Phase = MatchPhase.Ended;
}
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
switch (CurrentGameState)
{
case GameState.Match:
GraphicsDevice.Clear(Color.Black);
RestartSpriteBatch();
DrawMatch(gameTime);
break;
}
spriteBatch.End();
base.Draw(gameTime);
}
void InitializeMatch()
{
// 生成盤面
Board = new short[BOARD_MAX_X + 1, BOARD_MAX_Y + 1];
for (int i = 0; i < CurrentMatchState.Player.Count; i++)
{
for (int j = 0; j < 3; j++)
{
CurrentMatchState.Player[i].Reserves[j] = PieceTypes[random.Next(0, PieceTypes.Count)];
}
CurrentMatchState.Player[i].NextPiece = PieceTypes[random.Next(0, PieceTypes.Count)];
CurrentMatchState.Player[i].AvatarState = MatchState.PlayerState.AnimationState.Idle;
CurrentMatchState.Player[i].BombGauge = 0;
CurrentMatchState.Player[i].DisplayedBombGauge = 0;
}
CurrentMatchState.CurrentPlayer = 0;
CurrentMatchState.ChosenPiece = 0;
// 清場
MobAnimations.Clear();
CurrentMatchState.Turns = 1;
CurrentMatchState.Phase = MatchPhase.TurnStart;
// 音效重置
soundPlayer.AllStop();
soundPlayer.SetVolume((int)SFX.Intro, VOLUME_BGM);
soundPlayer.SetVolume((int)SFX.MainLoop, VOLUME_BGM);
soundPlayer.SetVolume((int)SFX.MatchEnd, VOLUME_BGM);
soundPlayer.SetVolume((int)SFX.CursorMove, VOLUME_SFX);
soundPlayer.SetVolume((int)SFX.Bomb, VOLUME_SFX);
soundPlayer.SetVolume((int)SFX.Place, VOLUME_SFX);
soundPlayer.SetVolume((int)SFX.ReserveSelect, VOLUME_SFX);
soundPlayer.SetVolume((int)SFX.Rotate, VOLUME_SFX);
// 背景音樂從頭開始播
soundPlayer.StartSound((int)SFX.Intro);
}
void UpdateMouseInput()
{
if (IsActive)
{
mouPrevious = mouCurrent;
mouCurrent = Mouse.GetState();
switch (CurrentGameState)
{
case GameState.Match:
UpdateMouseInput_Match();
break;
}
}