SpaceVillainGame.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using static Content.Shared.Arcade.SharedSpaceVillainArcadeComponent;
  2. using Robust.Server.GameObjects;
  3. using Robust.Shared.Audio;
  4. using Robust.Shared.Audio.Systems;
  5. using Robust.Shared.Random;
  6. namespace Content.Server.Arcade.SpaceVillain;
  7. /// <summary>
  8. /// A Class to handle all the game-logic of the SpaceVillain-game.
  9. /// </summary>
  10. public sealed partial class SpaceVillainGame
  11. {
  12. [Dependency] private readonly IEntityManager _entityManager = default!;
  13. [Dependency] private readonly IRobustRandom _random = default!;
  14. private readonly SharedAudioSystem _audioSystem = default!;
  15. private readonly UserInterfaceSystem _uiSystem = default!;
  16. private readonly SpaceVillainArcadeSystem _svArcade = default!;
  17. [ViewVariables]
  18. private readonly EntityUid _owner = default!;
  19. [ViewVariables]
  20. private bool _running = true;
  21. [ViewVariables]
  22. public string Name => $"{_fightVerb} {_villainName}";
  23. [ViewVariables]
  24. private readonly string _fightVerb;
  25. [ViewVariables]
  26. public readonly Fighter PlayerChar;
  27. [ViewVariables]
  28. private readonly string _villainName;
  29. [ViewVariables]
  30. public readonly Fighter VillainChar;
  31. [ViewVariables]
  32. private int _turtleTracker = 0;
  33. [ViewVariables]
  34. private string _latestPlayerActionMessage = "";
  35. [ViewVariables]
  36. private string _latestEnemyActionMessage = "";
  37. public SpaceVillainGame(EntityUid owner, SpaceVillainArcadeComponent arcade, SpaceVillainArcadeSystem arcadeSystem)
  38. : this(owner, arcade, arcadeSystem, arcadeSystem.GenerateFightVerb(arcade), arcadeSystem.GenerateEnemyName(arcade))
  39. {
  40. }
  41. public SpaceVillainGame(EntityUid owner, SpaceVillainArcadeComponent arcade, SpaceVillainArcadeSystem arcadeSystem, string fightVerb, string enemyName)
  42. {
  43. IoCManager.InjectDependencies(this);
  44. _audioSystem = _entityManager.System<SharedAudioSystem>();
  45. _uiSystem = _entityManager.System<UserInterfaceSystem>();
  46. _svArcade = _entityManager.System<SpaceVillainArcadeSystem>();
  47. _owner = owner;
  48. //todo defeat the curse secret game mode
  49. _fightVerb = fightVerb;
  50. _villainName = enemyName;
  51. PlayerChar = new()
  52. {
  53. HpMax = 30,
  54. Hp = 30,
  55. MpMax = 10,
  56. Mp = 10
  57. };
  58. VillainChar = new()
  59. {
  60. HpMax = 45,
  61. Hp = 45,
  62. MpMax = 20,
  63. Mp = 20
  64. };
  65. }
  66. /// <summary>
  67. /// Called by the SpaceVillainArcadeComponent when Userinput is received.
  68. /// </summary>
  69. /// <param name="uid">The action the user picked.</param>
  70. /// <param name="action">The action the user picked.</param>
  71. /// <param name="arcade">The action the user picked.</param>
  72. public void ExecutePlayerAction(EntityUid uid, PlayerAction action, SpaceVillainArcadeComponent arcade)
  73. {
  74. if (!_running)
  75. return;
  76. switch (action)
  77. {
  78. case PlayerAction.Attack:
  79. var attackAmount = _random.Next(2, 6);
  80. _latestPlayerActionMessage = Loc.GetString(
  81. "space-villain-game-player-attack-message",
  82. ("enemyName", _villainName),
  83. ("attackAmount", attackAmount)
  84. );
  85. _audioSystem.PlayPvs(arcade.PlayerAttackSound, uid, AudioParams.Default.WithVolume(-4f));
  86. if (!VillainChar.Invincible)
  87. VillainChar.Hp -= attackAmount;
  88. _turtleTracker -= _turtleTracker > 0 ? 1 : 0;
  89. break;
  90. case PlayerAction.Heal:
  91. var pointAmount = _random.Next(1, 3);
  92. var healAmount = _random.Next(6, 8);
  93. _latestPlayerActionMessage = Loc.GetString(
  94. "space-villain-game-player-heal-message",
  95. ("magicPointAmount", pointAmount),
  96. ("healAmount", healAmount)
  97. );
  98. _audioSystem.PlayPvs(arcade.PlayerHealSound, uid, AudioParams.Default.WithVolume(-4f));
  99. if (!PlayerChar.Invincible)
  100. PlayerChar.Mp -= pointAmount;
  101. PlayerChar.Hp += healAmount;
  102. _turtleTracker++;
  103. break;
  104. case PlayerAction.Recharge:
  105. var chargeAmount = _random.Next(4, 7);
  106. _latestPlayerActionMessage = Loc.GetString(
  107. "space-villain-game-player-recharge-message",
  108. ("regainedPoints", chargeAmount)
  109. );
  110. _audioSystem.PlayPvs(arcade.PlayerChargeSound, uid, AudioParams.Default.WithVolume(-4f));
  111. PlayerChar.Mp += chargeAmount;
  112. _turtleTracker -= _turtleTracker > 0 ? 1 : 0;
  113. break;
  114. }
  115. if (!CheckGameConditions(uid, arcade))
  116. return;
  117. ExecuteAiAction();
  118. if (!CheckGameConditions(uid, arcade))
  119. return;
  120. UpdateUi(uid);
  121. }
  122. /// <summary>
  123. /// Handles the logic of the AI
  124. /// </summary>
  125. private void ExecuteAiAction()
  126. {
  127. if (_turtleTracker >= 4)
  128. {
  129. var boomAmount = _random.Next(5, 10);
  130. _latestEnemyActionMessage = Loc.GetString(
  131. "space-villain-game-enemy-throws-bomb-message",
  132. ("enemyName", _villainName),
  133. ("damageReceived", boomAmount)
  134. );
  135. if (PlayerChar.Invincible)
  136. return;
  137. PlayerChar.Hp -= boomAmount;
  138. _turtleTracker--;
  139. return;
  140. }
  141. if (VillainChar.Mp <= 5 && _random.Prob(0.7f))
  142. {
  143. var stealAmount = _random.Next(2, 3);
  144. _latestEnemyActionMessage = Loc.GetString(
  145. "space-villain-game-enemy-steals-player-power-message",
  146. ("enemyName", _villainName),
  147. ("stolenAmount", stealAmount)
  148. );
  149. if (PlayerChar.Invincible)
  150. return;
  151. PlayerChar.Mp -= stealAmount;
  152. VillainChar.Mp += stealAmount;
  153. return;
  154. }
  155. if (VillainChar.Hp <= 10 && VillainChar.Mp > 4)
  156. {
  157. VillainChar.Hp += 4;
  158. VillainChar.Mp -= 4;
  159. _latestEnemyActionMessage = Loc.GetString(
  160. "space-villain-game-enemy-heals-message",
  161. ("enemyName", _villainName),
  162. ("healedAmount", 4)
  163. );
  164. return;
  165. }
  166. var attackAmount = _random.Next(3, 6);
  167. _latestEnemyActionMessage =
  168. Loc.GetString(
  169. "space-villain-game-enemy-attacks-message",
  170. ("enemyName", _villainName),
  171. ("damageDealt", attackAmount)
  172. );
  173. if (PlayerChar.Invincible)
  174. return;
  175. PlayerChar.Hp -= attackAmount;
  176. }
  177. /// <summary>
  178. /// Checks the Game conditions and Updates the Ui & Plays a sound accordingly.
  179. /// </summary>
  180. /// <returns>A bool indicating if the game should continue.</returns>
  181. private bool CheckGameConditions(EntityUid uid, SpaceVillainArcadeComponent arcade)
  182. {
  183. switch (
  184. PlayerChar.Hp > 0 && PlayerChar.Mp > 0,
  185. VillainChar.Hp > 0 && VillainChar.Mp > 0
  186. )
  187. {
  188. case (true, true):
  189. return true;
  190. case (true, false):
  191. _running = false;
  192. UpdateUi(
  193. uid,
  194. Loc.GetString("space-villain-game-player-wins-message"),
  195. Loc.GetString("space-villain-game-enemy-dies-message", ("enemyName", _villainName)),
  196. true
  197. );
  198. _audioSystem.PlayPvs(arcade.WinSound, uid, AudioParams.Default.WithVolume(-4f));
  199. _svArcade.ProcessWin(uid, arcade);
  200. return false;
  201. case (false, true):
  202. _running = false;
  203. UpdateUi(
  204. uid,
  205. Loc.GetString("space-villain-game-player-loses-message"),
  206. Loc.GetString("space-villain-game-enemy-cheers-message", ("enemyName", _villainName)),
  207. true
  208. );
  209. _audioSystem.PlayPvs(arcade.GameOverSound, uid, AudioParams.Default.WithVolume(-4f));
  210. return false;
  211. case (false, false):
  212. _running = false;
  213. UpdateUi(
  214. uid,
  215. Loc.GetString("space-villain-game-player-loses-message"),
  216. Loc.GetString("space-villain-game-enemy-dies-with-player-message ", ("enemyName", _villainName)),
  217. true
  218. );
  219. _audioSystem.PlayPvs(arcade.GameOverSound, uid, AudioParams.Default.WithVolume(-4f));
  220. return false;
  221. }
  222. }
  223. }