2
0

GameBoard.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. namespace Flood_Control
  7. {
  8. class GameBoard
  9. {
  10. Random rand = new Random();
  11. public const int GameBoardWidth = 8;
  12. public const int GameBoardHeight = 10;
  13. private GamePiece[,] boardSquares =
  14. new GamePiece[GameBoardWidth, GameBoardHeight];
  15. private List<Vector2> WaterTracker = new List<Vector2>();
  16. public Dictionary<string, FallingPiece> fallingPieces =
  17. new Dictionary<string, FallingPiece>();
  18. public Dictionary<string, RotatingPiece> rotatingPieces =
  19. new Dictionary<string, RotatingPiece>();
  20. public Dictionary<string, FadingPiece> fadingPieces =
  21. new Dictionary<string, FadingPiece>();
  22. public GameBoard()
  23. {
  24. ClearBoard();
  25. }
  26. public void ClearBoard()
  27. {
  28. for (int x = 0; x < GameBoardWidth; x++)
  29. for (int y = 0; y < GameBoardHeight; y++)
  30. boardSquares[x, y] = new GamePiece("Empty");
  31. }
  32. public void RotatePiece(int x, int y, bool clockwise)
  33. {
  34. boardSquares[x, y].RotatePiece(clockwise);
  35. }
  36. public Rectangle GetSourceRect(int x, int y)
  37. {
  38. return boardSquares[x, y].GetSourceRect();
  39. }
  40. public string GetSquare(int x, int y)
  41. {
  42. return boardSquares[x, y].PieceType;
  43. }
  44. public void SetSquare(int x, int y, string pieceName)
  45. {
  46. boardSquares[x, y].SetPiece(pieceName);
  47. }
  48. public bool HasConnector(int x, int y, string direction)
  49. {
  50. return boardSquares[x, y].HasConnector(direction);
  51. }
  52. public void RandomPiece(int x, int y)
  53. {
  54. boardSquares[x, y].SetPiece(GamePiece.PieceTypes[rand.Next(0,
  55. GamePiece.MaxPlayablePieceIndex + 1)]);
  56. }
  57. public void FillFromAbove(int x, int y)
  58. {
  59. int rowLookup = y - 1;
  60. while (rowLookup >= 0)
  61. {
  62. if (GetSquare(x, rowLookup) != "Empty")
  63. {
  64. SetSquare(x, y,
  65. GetSquare(x, rowLookup));
  66. SetSquare(x, rowLookup, "Empty");
  67. AddFallingPiece(x, y, GetSquare(x, y),
  68. GamePiece.PieceHeight * (y - rowLookup));
  69. rowLookup = -1;
  70. }
  71. rowLookup--;
  72. }
  73. }
  74. public void GenerateNewPieces(bool dropSquares)
  75. {
  76. if (dropSquares)
  77. {
  78. for (int x = 0; x < GameBoard.GameBoardWidth; x++)
  79. {
  80. for (int y = GameBoard.GameBoardHeight - 1; y >= 0; y--)
  81. {
  82. if (GetSquare(x, y) == "Empty")
  83. {
  84. FillFromAbove(x, y);
  85. }
  86. }
  87. }
  88. }
  89. for (int y = 0; y < GameBoard.GameBoardHeight; y++)
  90. for (int x = 0; x < GameBoard.GameBoardWidth; x++)
  91. {
  92. if (GetSquare(x, y) == "Empty")
  93. {
  94. RandomPiece(x, y);
  95. AddFallingPiece(x, y, GetSquare(x, y),
  96. GamePiece.PieceHeight * GameBoardHeight);
  97. }
  98. }
  99. }
  100. public void ResetWater()
  101. {
  102. for (int y = 0; y < GameBoardHeight; y++)
  103. for (int x = 0; x < GameBoardWidth; x++)
  104. boardSquares[x, y].RemoveSuffix("W");
  105. }
  106. public void FillPiece(int X, int Y)
  107. {
  108. boardSquares[X, Y].AddSuffix("W");
  109. }
  110. public void PropagateWater(int x, int y, string fromDirection)
  111. {
  112. if ((y >= 0) && (y < GameBoardHeight) &&
  113. (x >= 0) && (x < GameBoardWidth))
  114. {
  115. if (boardSquares[x, y].HasConnector(fromDirection) &&
  116. !boardSquares[x, y].Suffix.Contains("W"))
  117. {
  118. FillPiece(x, y);
  119. WaterTracker.Add(new Vector2(x, y));
  120. foreach (string end in
  121. boardSquares[x, y].GetOtherEnds(fromDirection))
  122. switch (end)
  123. {
  124. case "Left": PropagateWater(x - 1, y, "Right");
  125. break;
  126. case "Right": PropagateWater(x + 1, y, "Left");
  127. break;
  128. case "Top": PropagateWater(x, y - 1, "Bottom");
  129. break;
  130. case "Bottom": PropagateWater(x, y + 1, "Top");
  131. break;
  132. }
  133. }
  134. }
  135. }
  136. public List<Vector2> GetWaterChain(int y)
  137. {
  138. WaterTracker.Clear();
  139. PropagateWater(0, y, "Left");
  140. return WaterTracker;
  141. }
  142. public void AddFallingPiece(int X, int Y,
  143. string PieceName, int VerticalOffset)
  144. {
  145. fallingPieces[X.ToString() + "_" + Y.ToString()] = new
  146. FallingPiece(PieceName, VerticalOffset);
  147. }
  148. public void AddRotatingPiece(int X, int Y,
  149. string PieceName, bool Clockwise)
  150. {
  151. rotatingPieces[X.ToString() + "_" + Y.ToString()] = new
  152. RotatingPiece(PieceName, Clockwise);
  153. }
  154. public void AddFadingPiece(int X, int Y, string PieceName)
  155. {
  156. fadingPieces[X.ToString() + "_" + Y.ToString()] = new
  157. FadingPiece(PieceName, "W");
  158. }
  159. public bool ArePiecesAnimating()
  160. {
  161. if ((fallingPieces.Count == 0) &&
  162. (rotatingPieces.Count == 0) &&
  163. (fadingPieces.Count == 0))
  164. {
  165. return false;
  166. }
  167. else
  168. {
  169. return true;
  170. }
  171. }
  172. private void UpdateFadingPieces()
  173. {
  174. Queue<string> RemoveKeys = new Queue<string>();
  175. foreach (string thisKey in fadingPieces.Keys)
  176. {
  177. fadingPieces[thisKey].UpdatePiece();
  178. if (fadingPieces[thisKey].alphaLevel == 0.0f)
  179. RemoveKeys.Enqueue(thisKey.ToString());
  180. }
  181. while (RemoveKeys.Count > 0)
  182. fadingPieces.Remove(RemoveKeys.Dequeue());
  183. }
  184. private void UpdateFallingPieces()
  185. {
  186. Queue<string> RemoveKeys = new Queue<string>();
  187. foreach (string thisKey in fallingPieces.Keys)
  188. {
  189. fallingPieces[thisKey].UpdatePiece();
  190. if (fallingPieces[thisKey].VerticalOffset == 0)
  191. RemoveKeys.Enqueue(thisKey.ToString());
  192. }
  193. while (RemoveKeys.Count > 0)
  194. fallingPieces.Remove(RemoveKeys.Dequeue());
  195. }
  196. private void UpdateRotatingPieces()
  197. {
  198. Queue<string> RemoveKeys = new Queue<string>();
  199. foreach (string thisKey in rotatingPieces.Keys)
  200. {
  201. rotatingPieces[thisKey].UpdatePiece();
  202. if (rotatingPieces[thisKey].rotationTicksRemaining == 0)
  203. RemoveKeys.Enqueue(thisKey.ToString());
  204. }
  205. while (RemoveKeys.Count > 0)
  206. rotatingPieces.Remove(RemoveKeys.Dequeue());
  207. }
  208. public void UpdateAnimatedPieces()
  209. {
  210. if (fadingPieces.Count == 0)
  211. {
  212. UpdateFallingPieces();
  213. UpdateRotatingPieces();
  214. }
  215. else
  216. {
  217. UpdateFadingPieces();
  218. }
  219. }
  220. }
  221. }