BlockGameArcadeSystem.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Content.Shared.UserInterface;
  2. using Content.Server.Advertise.EntitySystems;
  3. using Content.Shared.Advertise.Components;
  4. using Content.Shared.Arcade;
  5. using Content.Shared.Power;
  6. using Robust.Server.GameObjects;
  7. namespace Content.Server.Arcade.BlockGame;
  8. public sealed class BlockGameArcadeSystem : EntitySystem
  9. {
  10. [Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
  11. [Dependency] private readonly SpeakOnUIClosedSystem _speakOnUIClosed = default!;
  12. public override void Initialize()
  13. {
  14. base.Initialize();
  15. SubscribeLocalEvent<BlockGameArcadeComponent, ComponentInit>(OnComponentInit);
  16. SubscribeLocalEvent<BlockGameArcadeComponent, AfterActivatableUIOpenEvent>(OnAfterUIOpen);
  17. SubscribeLocalEvent<BlockGameArcadeComponent, PowerChangedEvent>(OnBlockPowerChanged);
  18. Subs.BuiEvents<BlockGameArcadeComponent>(BlockGameUiKey.Key, subs =>
  19. {
  20. subs.Event<BoundUIClosedEvent>(OnAfterUiClose);
  21. subs.Event<BlockGameMessages.BlockGamePlayerActionMessage>(OnPlayerAction);
  22. });
  23. }
  24. public override void Update(float frameTime)
  25. {
  26. var query = EntityManager.EntityQueryEnumerator<BlockGameArcadeComponent>();
  27. while (query.MoveNext(out var _, out var blockGame))
  28. {
  29. blockGame.Game?.GameTick(frameTime);
  30. }
  31. }
  32. private void UpdatePlayerStatus(EntityUid uid, EntityUid actor, BlockGameArcadeComponent? blockGame = null)
  33. {
  34. if (!Resolve(uid, ref blockGame))
  35. return;
  36. _uiSystem.ServerSendUiMessage(uid, BlockGameUiKey.Key, new BlockGameMessages.BlockGameUserStatusMessage(blockGame.Player == actor), actor);
  37. }
  38. private void OnComponentInit(EntityUid uid, BlockGameArcadeComponent component, ComponentInit args)
  39. {
  40. component.Game = new(uid);
  41. }
  42. private void OnAfterUIOpen(EntityUid uid, BlockGameArcadeComponent component, AfterActivatableUIOpenEvent args)
  43. {
  44. if (component.Player == null)
  45. component.Player = args.Actor;
  46. else
  47. component.Spectators.Add(args.Actor);
  48. UpdatePlayerStatus(uid, args.Actor, component);
  49. component.Game?.UpdateNewPlayerUI(args.Actor);
  50. }
  51. private void OnAfterUiClose(EntityUid uid, BlockGameArcadeComponent component, BoundUIClosedEvent args)
  52. {
  53. if (component.Player != args.Actor)
  54. {
  55. component.Spectators.Remove(args.Actor);
  56. UpdatePlayerStatus(uid, args.Actor, blockGame: component);
  57. return;
  58. }
  59. var temp = component.Player;
  60. if (component.Spectators.Count > 0)
  61. {
  62. component.Player = component.Spectators[0];
  63. component.Spectators.Remove(component.Player.Value);
  64. UpdatePlayerStatus(uid, component.Player.Value, blockGame: component);
  65. }
  66. UpdatePlayerStatus(uid, temp.Value, blockGame: component);
  67. }
  68. private void OnBlockPowerChanged(EntityUid uid, BlockGameArcadeComponent component, ref PowerChangedEvent args)
  69. {
  70. if (args.Powered)
  71. return;
  72. _uiSystem.CloseUi(uid, BlockGameUiKey.Key);
  73. component.Player = null;
  74. component.Spectators.Clear();
  75. }
  76. private void OnPlayerAction(EntityUid uid, BlockGameArcadeComponent component, BlockGameMessages.BlockGamePlayerActionMessage msg)
  77. {
  78. if (component.Game == null)
  79. return;
  80. if (!BlockGameUiKey.Key.Equals(msg.UiKey))
  81. return;
  82. if (msg.Actor != component.Player)
  83. return;
  84. if (msg.PlayerAction == BlockGamePlayerAction.NewGame)
  85. {
  86. if (component.Game.Started == true)
  87. component.Game = new(uid);
  88. component.Game.StartGame();
  89. return;
  90. }
  91. if (TryComp<SpeakOnUIClosedComponent>(uid, out var speakComponent))
  92. _speakOnUIClosed.TrySetFlag((uid, speakComponent));
  93. component.Game.ProcessInput(msg.PlayerAction);
  94. }
  95. }