BlockGame.Ui.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. using Content.Shared.Arcade;
  2. using System.Linq;
  3. using Robust.Shared.Player;
  4. namespace Content.Server.Arcade.BlockGame;
  5. public sealed partial class BlockGame
  6. {
  7. /// <summary>
  8. /// How often to check the currently pressed inputs for whether to move the active piece horizontally.
  9. /// </summary>
  10. private const float PressCheckSpeed = 0.08f;
  11. /// <summary>
  12. /// Whether the left button is pressed.
  13. /// Moves the active piece left if true.
  14. /// </summary>
  15. private bool _leftPressed = false;
  16. /// <summary>
  17. /// How long the left button has been pressed.
  18. /// </summary>
  19. private float _accumulatedLeftPressTime = 0f;
  20. /// <summary>
  21. /// Whether the right button is pressed.
  22. /// Moves the active piece right if true.
  23. /// </summary>
  24. private bool _rightPressed = false;
  25. /// <summary>
  26. /// How long the right button has been pressed.
  27. /// </summary>
  28. private float _accumulatedRightPressTime = 0f;
  29. /// <summary>
  30. /// Whether the down button is pressed.
  31. /// Speeds up how quickly the active piece falls if true.
  32. /// </summary>
  33. private bool _softDropPressed = false;
  34. /// <summary>
  35. /// Handles user input.
  36. /// </summary>
  37. /// <param name="action">The action to current player has prompted.</param>
  38. public void ProcessInput(BlockGamePlayerAction action)
  39. {
  40. if (_running)
  41. {
  42. switch (action)
  43. {
  44. case BlockGamePlayerAction.StartLeft:
  45. _leftPressed = true;
  46. break;
  47. case BlockGamePlayerAction.StartRight:
  48. _rightPressed = true;
  49. break;
  50. case BlockGamePlayerAction.Rotate:
  51. TrySetRotation(Next(_currentRotation, false));
  52. break;
  53. case BlockGamePlayerAction.CounterRotate:
  54. TrySetRotation(Next(_currentRotation, true));
  55. break;
  56. case BlockGamePlayerAction.SoftdropStart:
  57. _softDropPressed = true;
  58. if (_accumulatedFieldFrameTime > Speed)
  59. _accumulatedFieldFrameTime = Speed; //to prevent jumps
  60. break;
  61. case BlockGamePlayerAction.Harddrop:
  62. PerformHarddrop();
  63. break;
  64. case BlockGamePlayerAction.Hold:
  65. HoldPiece();
  66. break;
  67. }
  68. }
  69. switch (action)
  70. {
  71. case BlockGamePlayerAction.EndLeft:
  72. _leftPressed = false;
  73. break;
  74. case BlockGamePlayerAction.EndRight:
  75. _rightPressed = false;
  76. break;
  77. case BlockGamePlayerAction.SoftdropEnd:
  78. _softDropPressed = false;
  79. break;
  80. case BlockGamePlayerAction.Pause:
  81. _running = false;
  82. SendMessage(new BlockGameMessages.BlockGameSetScreenMessage(BlockGameMessages.BlockGameScreen.Pause, Started));
  83. break;
  84. case BlockGamePlayerAction.Unpause:
  85. if (!_gameOver && Started)
  86. {
  87. _running = true;
  88. SendMessage(new BlockGameMessages.BlockGameSetScreenMessage(BlockGameMessages.BlockGameScreen.Game));
  89. }
  90. break;
  91. case BlockGamePlayerAction.ShowHighscores:
  92. _running = false;
  93. SendMessage(new BlockGameMessages.BlockGameSetScreenMessage(BlockGameMessages.BlockGameScreen.Highscores, Started));
  94. break;
  95. }
  96. }
  97. /// <summary>
  98. /// Handle moving the active game piece in response to user input.
  99. /// </summary>
  100. /// <param name="frameTime">The amount of time the current game tick covers.</param>
  101. private void InputTick(float frameTime)
  102. {
  103. var anythingChanged = false;
  104. if (_leftPressed)
  105. {
  106. _accumulatedLeftPressTime += frameTime;
  107. while (_accumulatedLeftPressTime >= PressCheckSpeed)
  108. {
  109. if (CurrentPiece.Positions(_currentPiecePosition.AddToX(-1), _currentRotation)
  110. .All(MoveCheck))
  111. {
  112. _currentPiecePosition = _currentPiecePosition.AddToX(-1);
  113. anythingChanged = true;
  114. }
  115. _accumulatedLeftPressTime -= PressCheckSpeed;
  116. }
  117. }
  118. if (_rightPressed)
  119. {
  120. _accumulatedRightPressTime += frameTime;
  121. while (_accumulatedRightPressTime >= PressCheckSpeed)
  122. {
  123. if (CurrentPiece.Positions(_currentPiecePosition.AddToX(1), _currentRotation)
  124. .All(MoveCheck))
  125. {
  126. _currentPiecePosition = _currentPiecePosition.AddToX(1);
  127. anythingChanged = true;
  128. }
  129. _accumulatedRightPressTime -= PressCheckSpeed;
  130. }
  131. }
  132. if (anythingChanged)
  133. UpdateFieldUI();
  134. }
  135. /// <summary>
  136. /// Handles sending a message to all players/spectators.
  137. /// </summary>
  138. /// <param name="message">The message to broadcase to all players/spectators.</param>
  139. private void SendMessage(BoundUserInterfaceMessage message)
  140. {
  141. _uiSystem.ServerSendUiMessage(_owner, BlockGameUiKey.Key, message);
  142. }
  143. /// <summary>
  144. /// Handles sending a message to a specific player/spectator.
  145. /// </summary>
  146. /// <param name="message">The message to send to a specific player/spectator.</param>
  147. /// <param name="actor">The target recipient.</param>
  148. private void SendMessage(BoundUserInterfaceMessage message, EntityUid actor)
  149. {
  150. _uiSystem.ServerSendUiMessage(_owner, BlockGameUiKey.Key, message, actor);
  151. }
  152. /// <summary>
  153. /// Handles sending the current state of the game to a player that has just opened the UI.
  154. /// </summary>
  155. /// <param name="actor">The target recipient.</param>
  156. public void UpdateNewPlayerUI(EntityUid actor)
  157. {
  158. if (_gameOver)
  159. {
  160. SendMessage(new BlockGameMessages.BlockGameGameOverScreenMessage(Points, _highScorePlacement?.LocalPlacement, _highScorePlacement?.GlobalPlacement), actor);
  161. return;
  162. }
  163. if (Paused)
  164. SendMessage(new BlockGameMessages.BlockGameSetScreenMessage(BlockGameMessages.BlockGameScreen.Pause, Started), actor);
  165. else
  166. SendMessage(new BlockGameMessages.BlockGameSetScreenMessage(BlockGameMessages.BlockGameScreen.Game, Started), actor);
  167. FullUpdate(actor);
  168. }
  169. /// <summary>
  170. /// Handles broadcasting the full player-visible game state to everyone who can see the game.
  171. /// </summary>
  172. private void FullUpdate()
  173. {
  174. UpdateFieldUI();
  175. SendHoldPieceUpdate();
  176. SendNextPieceUpdate();
  177. SendLevelUpdate();
  178. SendPointsUpdate();
  179. SendHighscoreUpdate();
  180. }
  181. /// <summary>
  182. /// Handles broadcasting the full player-visible game state to a specific player/spectator.
  183. /// </summary>
  184. /// <param name="session">The target recipient.</param>
  185. private void FullUpdate(EntityUid actor)
  186. {
  187. UpdateFieldUI(actor);
  188. SendNextPieceUpdate(actor);
  189. SendHoldPieceUpdate(actor);
  190. SendLevelUpdate(actor);
  191. SendPointsUpdate(actor);
  192. SendHighscoreUpdate(actor);
  193. }
  194. /// <summary>
  195. /// Handles broadcasting the current location of all of the blocks in the playfield + the active piece to all spectators.
  196. /// </summary>
  197. public void UpdateFieldUI()
  198. {
  199. if (!Started)
  200. return;
  201. var computedField = ComputeField();
  202. SendMessage(new BlockGameMessages.BlockGameVisualUpdateMessage(computedField.ToArray(), BlockGameMessages.BlockGameVisualType.GameField));
  203. }
  204. /// <summary>
  205. /// Handles broadcasting the current location of all of the blocks in the playfield + the active piece to a specific player/spectator.
  206. /// </summary>
  207. public void UpdateFieldUI(EntityUid actor)
  208. {
  209. if (!Started)
  210. return;
  211. var computedField = ComputeField();
  212. SendMessage(new BlockGameMessages.BlockGameVisualUpdateMessage(computedField.ToArray(), BlockGameMessages.BlockGameVisualType.GameField), actor);
  213. }
  214. /// <summary>
  215. /// Generates the set of blocks to send to viewers.
  216. /// </summary>
  217. public List<BlockGameBlock> ComputeField()
  218. {
  219. var result = new List<BlockGameBlock>();
  220. result.AddRange(_field);
  221. result.AddRange(CurrentPiece.Blocks(_currentPiecePosition, _currentRotation));
  222. var dropGhostPosition = _currentPiecePosition;
  223. while (CurrentPiece.Positions(dropGhostPosition.AddToY(1), _currentRotation)
  224. .All(DropCheck))
  225. {
  226. dropGhostPosition = dropGhostPosition.AddToY(1);
  227. }
  228. if (dropGhostPosition != _currentPiecePosition)
  229. {
  230. var blox = CurrentPiece.Blocks(dropGhostPosition, _currentRotation);
  231. for (var i = 0; i < blox.Length; i++)
  232. {
  233. result.Add(new BlockGameBlock(blox[i].Position, BlockGameBlock.ToGhostBlockColor(blox[i].GameBlockColor)));
  234. }
  235. }
  236. return result;
  237. }
  238. /// <summary>
  239. /// Broadcasts the state of the next queued piece to all viewers.
  240. /// </summary>
  241. private void SendNextPieceUpdate()
  242. {
  243. SendMessage(new BlockGameMessages.BlockGameVisualUpdateMessage(NextPiece.BlocksForPreview(), BlockGameMessages.BlockGameVisualType.NextBlock));
  244. }
  245. /// <summary>
  246. /// Broadcasts the state of the next queued piece to a specific viewer.
  247. /// </summary>
  248. private void SendNextPieceUpdate(EntityUid actor)
  249. {
  250. SendMessage(new BlockGameMessages.BlockGameVisualUpdateMessage(NextPiece.BlocksForPreview(), BlockGameMessages.BlockGameVisualType.NextBlock), actor);
  251. }
  252. /// <summary>
  253. /// Broadcasts the state of the currently held piece to all viewers.
  254. /// </summary>
  255. private void SendHoldPieceUpdate()
  256. {
  257. if (HeldPiece.HasValue)
  258. SendMessage(new BlockGameMessages.BlockGameVisualUpdateMessage(HeldPiece.Value.BlocksForPreview(), BlockGameMessages.BlockGameVisualType.HoldBlock));
  259. else
  260. SendMessage(new BlockGameMessages.BlockGameVisualUpdateMessage(Array.Empty<BlockGameBlock>(), BlockGameMessages.BlockGameVisualType.HoldBlock));
  261. }
  262. /// <summary>
  263. /// Broadcasts the state of the currently held piece to a specific viewer.
  264. /// </summary>
  265. private void SendHoldPieceUpdate(EntityUid actor)
  266. {
  267. if (HeldPiece.HasValue)
  268. SendMessage(new BlockGameMessages.BlockGameVisualUpdateMessage(HeldPiece.Value.BlocksForPreview(), BlockGameMessages.BlockGameVisualType.HoldBlock), actor);
  269. else
  270. SendMessage(new BlockGameMessages.BlockGameVisualUpdateMessage(Array.Empty<BlockGameBlock>(), BlockGameMessages.BlockGameVisualType.HoldBlock), actor);
  271. }
  272. /// <summary>
  273. /// Broadcasts the current game level to all viewers.
  274. /// </summary>
  275. private void SendLevelUpdate()
  276. {
  277. SendMessage(new BlockGameMessages.BlockGameLevelUpdateMessage(Level));
  278. }
  279. /// <summary>
  280. /// Broadcasts the current game level to a specific viewer.
  281. /// </summary>
  282. private void SendLevelUpdate(EntityUid actor)
  283. {
  284. SendMessage(new BlockGameMessages.BlockGameLevelUpdateMessage(Level), actor);
  285. }
  286. /// <summary>
  287. /// Broadcasts the current game score to all viewers.
  288. /// </summary>
  289. private void SendPointsUpdate()
  290. {
  291. SendMessage(new BlockGameMessages.BlockGameScoreUpdateMessage(Points));
  292. }
  293. /// <summary>
  294. /// Broadcasts the current game score to a specific viewer.
  295. /// </summary>
  296. private void SendPointsUpdate(EntityUid actor)
  297. {
  298. SendMessage(new BlockGameMessages.BlockGameScoreUpdateMessage(Points), actor);
  299. }
  300. /// <summary>
  301. /// Broadcasts the current game high score positions to all viewers.
  302. /// </summary>
  303. private void SendHighscoreUpdate()
  304. {
  305. SendMessage(new BlockGameMessages.BlockGameHighScoreUpdateMessage(_arcadeSystem.GetLocalHighscores(), _arcadeSystem.GetGlobalHighscores()));
  306. }
  307. /// <summary>
  308. /// Broadcasts the current game high score positions to a specific viewer.
  309. /// </summary>
  310. private void SendHighscoreUpdate(EntityUid actor)
  311. {
  312. SendMessage(new BlockGameMessages.BlockGameHighScoreUpdateMessage(_arcadeSystem.GetLocalHighscores(), _arcadeSystem.GetGlobalHighscores()), actor);
  313. }
  314. }