SpaceVillainArcadeSystem.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using Content.Server.Power.Components;
  2. using Content.Shared.UserInterface;
  3. using Content.Server.Advertise.EntitySystems;
  4. using Content.Shared.Advertise.Components;
  5. using Content.Shared.Arcade;
  6. using Content.Shared.Power;
  7. using Robust.Server.GameObjects;
  8. using Robust.Shared.Audio;
  9. using Robust.Shared.Audio.Systems;
  10. using Robust.Shared.Random;
  11. namespace Content.Server.Arcade.SpaceVillain;
  12. public sealed partial class SpaceVillainArcadeSystem : EntitySystem
  13. {
  14. [Dependency] private readonly IRobustRandom _random = default!;
  15. [Dependency] private readonly SharedAudioSystem _audioSystem = default!;
  16. [Dependency] private readonly UserInterfaceSystem _uiSystem = default!;
  17. [Dependency] private readonly SpeakOnUIClosedSystem _speakOnUIClosed = default!;
  18. public override void Initialize()
  19. {
  20. base.Initialize();
  21. SubscribeLocalEvent<SpaceVillainArcadeComponent, ComponentInit>(OnComponentInit);
  22. SubscribeLocalEvent<SpaceVillainArcadeComponent, AfterActivatableUIOpenEvent>(OnAfterUIOpenSV);
  23. SubscribeLocalEvent<SpaceVillainArcadeComponent, SharedSpaceVillainArcadeComponent.SpaceVillainArcadePlayerActionMessage>(OnSVPlayerAction);
  24. SubscribeLocalEvent<SpaceVillainArcadeComponent, PowerChangedEvent>(OnSVillainPower);
  25. }
  26. /// <summary>
  27. /// Called when the user wins the game.
  28. /// Dispenses a prize if the arcade machine has any left.
  29. /// </summary>
  30. /// <param name="uid"></param>
  31. /// <param name="arcade"></param>
  32. /// <param name="xform"></param>
  33. public void ProcessWin(EntityUid uid, SpaceVillainArcadeComponent? arcade = null, TransformComponent? xform = null)
  34. {
  35. if (!Resolve(uid, ref arcade, ref xform))
  36. return;
  37. if (arcade.RewardAmount <= 0)
  38. return;
  39. EntityManager.SpawnEntity(_random.Pick(arcade.PossibleRewards), xform.Coordinates);
  40. arcade.RewardAmount--;
  41. }
  42. /// <summary>
  43. /// Picks a fight-verb from the list of possible Verbs.
  44. /// </summary>
  45. /// <returns>A fight-verb.</returns>
  46. public string GenerateFightVerb(SpaceVillainArcadeComponent arcade)
  47. {
  48. return _random.Pick(arcade.PossibleFightVerbs);
  49. }
  50. /// <summary>
  51. /// Generates an enemy-name comprised of a first- and last-name.
  52. /// </summary>
  53. /// <returns>An enemy-name.</returns>
  54. public string GenerateEnemyName(SpaceVillainArcadeComponent arcade)
  55. {
  56. return $"{_random.Pick(arcade.PossibleFirstEnemyNames)} {_random.Pick(arcade.PossibleLastEnemyNames)}";
  57. }
  58. private void OnComponentInit(EntityUid uid, SpaceVillainArcadeComponent component, ComponentInit args)
  59. {
  60. // Random amount of prizes
  61. component.RewardAmount = new Random().Next(component.RewardMinAmount, component.RewardMaxAmount + 1);
  62. }
  63. private void OnSVPlayerAction(EntityUid uid, SpaceVillainArcadeComponent component, SharedSpaceVillainArcadeComponent.SpaceVillainArcadePlayerActionMessage msg)
  64. {
  65. if (component.Game == null)
  66. return;
  67. if (!TryComp<ApcPowerReceiverComponent>(uid, out var power) || !power.Powered)
  68. return;
  69. switch (msg.PlayerAction)
  70. {
  71. case SharedSpaceVillainArcadeComponent.PlayerAction.Attack:
  72. case SharedSpaceVillainArcadeComponent.PlayerAction.Heal:
  73. case SharedSpaceVillainArcadeComponent.PlayerAction.Recharge:
  74. component.Game.ExecutePlayerAction(uid, msg.PlayerAction, component);
  75. // Any sort of gameplay action counts
  76. if (TryComp<SpeakOnUIClosedComponent>(uid, out var speakComponent))
  77. _speakOnUIClosed.TrySetFlag((uid, speakComponent));
  78. break;
  79. case SharedSpaceVillainArcadeComponent.PlayerAction.NewGame:
  80. _audioSystem.PlayPvs(component.NewGameSound, uid, AudioParams.Default.WithVolume(-4f));
  81. component.Game = new SpaceVillainGame(uid, component, this);
  82. _uiSystem.ServerSendUiMessage(uid, SharedSpaceVillainArcadeComponent.SpaceVillainArcadeUiKey.Key, component.Game.GenerateMetaDataMessage());
  83. break;
  84. case SharedSpaceVillainArcadeComponent.PlayerAction.RequestData:
  85. _uiSystem.ServerSendUiMessage(uid, SharedSpaceVillainArcadeComponent.SpaceVillainArcadeUiKey.Key, component.Game.GenerateMetaDataMessage());
  86. break;
  87. }
  88. }
  89. private void OnAfterUIOpenSV(EntityUid uid, SpaceVillainArcadeComponent component, AfterActivatableUIOpenEvent args)
  90. {
  91. component.Game ??= new(uid, component, this);
  92. }
  93. private void OnSVillainPower(EntityUid uid, SpaceVillainArcadeComponent component, ref PowerChangedEvent args)
  94. {
  95. if (TryComp<ApcPowerReceiverComponent>(uid, out var power) && power.Powered)
  96. return;
  97. _uiSystem.CloseUi(uid, SharedSpaceVillainArcadeComponent.SpaceVillainArcadeUiKey.Key);
  98. }
  99. }