BlockGame.Pieces.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using Content.Shared.Arcade;
  2. using System.Linq;
  3. namespace Content.Server.Arcade.BlockGame;
  4. public sealed partial class BlockGame
  5. {
  6. /// <summary>
  7. /// The set of types of game pieces that exist.
  8. /// Used as templates when creating pieces for the game.
  9. /// </summary>
  10. private readonly BlockGamePieceType[] _allBlockGamePieces;
  11. /// <summary>
  12. /// The set of types of game pieces that exist.
  13. /// Used to generate the templates used when creating pieces for the game.
  14. /// </summary>
  15. private enum BlockGamePieceType
  16. {
  17. I,
  18. L,
  19. LInverted,
  20. S,
  21. SInverted,
  22. T,
  23. O
  24. }
  25. /// <summary>
  26. /// The set of possible rotations for the game pieces.
  27. /// </summary>
  28. private enum BlockGamePieceRotation
  29. {
  30. North,
  31. East,
  32. South,
  33. West
  34. }
  35. /// <summary>
  36. /// A static extension for the rotations that allows rotating through the possible rotations.
  37. /// </summary>
  38. private static BlockGamePieceRotation Next(BlockGamePieceRotation rotation, bool inverted)
  39. {
  40. return rotation switch
  41. {
  42. BlockGamePieceRotation.North => inverted ? BlockGamePieceRotation.West : BlockGamePieceRotation.East,
  43. BlockGamePieceRotation.East => inverted ? BlockGamePieceRotation.North : BlockGamePieceRotation.South,
  44. BlockGamePieceRotation.South => inverted ? BlockGamePieceRotation.East : BlockGamePieceRotation.West,
  45. BlockGamePieceRotation.West => inverted ? BlockGamePieceRotation.South : BlockGamePieceRotation.North,
  46. _ => throw new ArgumentOutOfRangeException(nameof(rotation), rotation, null)
  47. };
  48. }
  49. /// <summary>
  50. /// A static extension for the rotations that allows rotating through the possible rotations.
  51. /// </summary>
  52. private struct BlockGamePiece
  53. {
  54. /// <summary>
  55. /// Where all of the blocks that make up this piece are located relative to the origin of the piece.
  56. /// </summary>
  57. public Vector2i[] Offsets;
  58. /// <summary>
  59. /// The color of all of the blocks that make up this piece.
  60. /// </summary>
  61. private BlockGameBlock.BlockGameBlockColor _gameBlockColor;
  62. /// <summary>
  63. /// Whether or not the block should be able to rotate about its origin.
  64. /// </summary>
  65. public bool CanSpin;
  66. /// <summary>
  67. /// Generates a list of the positions of each block comprising this game piece in worldspace.
  68. /// </summary>
  69. /// <param name="center">The position of the game piece in worldspace.</param>
  70. /// <param name="rotation">The rotation of the game piece in worldspace.</param>
  71. public readonly Vector2i[] Positions(Vector2i center, BlockGamePieceRotation rotation)
  72. {
  73. return RotatedOffsets(rotation).Select(v => center + v).ToArray();
  74. }
  75. /// <summary>
  76. /// Gets the relative position of each block comprising this piece given a rotation.
  77. /// </summary>
  78. /// <param name="rotation">The rotation to be applied to the local position of the blocks in this piece.</param>
  79. private readonly Vector2i[] RotatedOffsets(BlockGamePieceRotation rotation)
  80. {
  81. var rotatedOffsets = (Vector2i[]) Offsets.Clone();
  82. //until i find a better algo
  83. var amount = rotation switch
  84. {
  85. BlockGamePieceRotation.North => 0,
  86. BlockGamePieceRotation.East => 1,
  87. BlockGamePieceRotation.South => 2,
  88. BlockGamePieceRotation.West => 3,
  89. _ => 0
  90. };
  91. for (var i = 0; i < amount; i++)
  92. {
  93. for (var j = 0; j < rotatedOffsets.Length; j++)
  94. {
  95. rotatedOffsets[j] = rotatedOffsets[j].Rotate90DegreesAsOffset();
  96. }
  97. }
  98. return rotatedOffsets;
  99. }
  100. /// <summary>
  101. /// Gets a list of all of the blocks comprising this piece in worldspace.
  102. /// </summary>
  103. /// <param name="center">The position of the game piece in worldspace.</param>
  104. /// <param name="rotation">The rotation of the game piece in worldspace.</param>
  105. public readonly BlockGameBlock[] Blocks(Vector2i center, BlockGamePieceRotation rotation)
  106. {
  107. var positions = Positions(center, rotation);
  108. var result = new BlockGameBlock[positions.Length];
  109. var i = 0;
  110. foreach (var position in positions)
  111. {
  112. result[i++] = position.ToBlockGameBlock(_gameBlockColor);
  113. }
  114. return result;
  115. }
  116. /// <summary>
  117. /// Gets a list of all of the blocks comprising this piece in worldspace.
  118. /// Used to generate the held piece/next piece preview images.
  119. /// </summary>
  120. public readonly BlockGameBlock[] BlocksForPreview()
  121. {
  122. var xOffset = 0;
  123. var yOffset = 0;
  124. foreach (var offset in Offsets)
  125. {
  126. if (offset.X < xOffset)
  127. xOffset = offset.X;
  128. if (offset.Y < yOffset)
  129. yOffset = offset.Y;
  130. }
  131. return Blocks(new Vector2i(-xOffset, -yOffset), BlockGamePieceRotation.North);
  132. }
  133. /// <summary>
  134. /// Generates a game piece for a given type of game piece.
  135. /// See <see cref="BlockGamePieceType"/> for the available options.
  136. /// </summary>
  137. /// <param name="type">The type of game piece to generate.</param>
  138. public static BlockGamePiece GetPiece(BlockGamePieceType type)
  139. {
  140. //switch statement, hardcoded offsets
  141. return type switch
  142. {
  143. BlockGamePieceType.I => new BlockGamePiece
  144. {
  145. Offsets = new[]
  146. {
  147. new Vector2i(0, -1), new Vector2i(0, 0), new Vector2i(0, 1), new Vector2i(0, 2),
  148. },
  149. _gameBlockColor = BlockGameBlock.BlockGameBlockColor.LightBlue,
  150. CanSpin = true
  151. },
  152. BlockGamePieceType.L => new BlockGamePiece
  153. {
  154. Offsets = new[]
  155. {
  156. new Vector2i(0, -1), new Vector2i(0, 0), new Vector2i(0, 1), new Vector2i(1, 1),
  157. },
  158. _gameBlockColor = BlockGameBlock.BlockGameBlockColor.Orange,
  159. CanSpin = true
  160. },
  161. BlockGamePieceType.LInverted => new BlockGamePiece
  162. {
  163. Offsets = new[]
  164. {
  165. new Vector2i(0, -1), new Vector2i(0, 0), new Vector2i(-1, 1),
  166. new Vector2i(0, 1),
  167. },
  168. _gameBlockColor = BlockGameBlock.BlockGameBlockColor.Blue,
  169. CanSpin = true
  170. },
  171. BlockGamePieceType.S => new BlockGamePiece
  172. {
  173. Offsets = new[]
  174. {
  175. new Vector2i(0, -1), new Vector2i(1, -1), new Vector2i(-1, 0),
  176. new Vector2i(0, 0),
  177. },
  178. _gameBlockColor = BlockGameBlock.BlockGameBlockColor.Green,
  179. CanSpin = true
  180. },
  181. BlockGamePieceType.SInverted => new BlockGamePiece
  182. {
  183. Offsets = new[]
  184. {
  185. new Vector2i(-1, -1), new Vector2i(0, -1), new Vector2i(0, 0),
  186. new Vector2i(1, 0),
  187. },
  188. _gameBlockColor = BlockGameBlock.BlockGameBlockColor.Red,
  189. CanSpin = true
  190. },
  191. BlockGamePieceType.T => new BlockGamePiece
  192. {
  193. Offsets = new[]
  194. {
  195. new Vector2i(0, -1),
  196. new Vector2i(-1, 0), new Vector2i(0, 0), new Vector2i(1, 0),
  197. },
  198. _gameBlockColor = BlockGameBlock.BlockGameBlockColor.Purple,
  199. CanSpin = true
  200. },
  201. BlockGamePieceType.O => new BlockGamePiece
  202. {
  203. Offsets = new[]
  204. {
  205. new Vector2i(0, -1), new Vector2i(1, -1), new Vector2i(0, 0),
  206. new Vector2i(1, 0),
  207. },
  208. _gameBlockColor = BlockGameBlock.BlockGameBlockColor.Yellow,
  209. CanSpin = false
  210. },
  211. _ => new BlockGamePiece
  212. {
  213. Offsets = new[]
  214. {
  215. new Vector2i(0, 0)
  216. }
  217. },
  218. };
  219. }
  220. }
  221. }