GameRuleSystem.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using Content.Server.Atmos.EntitySystems;
  2. using Content.Server.Chat.Managers;
  3. using Content.Shared.GameTicking.Components;
  4. using Robust.Server.GameObjects;
  5. using Robust.Shared.Random;
  6. using Robust.Shared.Timing;
  7. namespace Content.Server.GameTicking.Rules;
  8. public abstract partial class GameRuleSystem<T> : EntitySystem where T : IComponent
  9. {
  10. [Dependency] protected readonly IRobustRandom RobustRandom = default!;
  11. [Dependency] protected readonly IChatManager ChatManager = default!;
  12. [Dependency] protected readonly GameTicker GameTicker = default!;
  13. [Dependency] protected readonly IGameTiming Timing = default!;
  14. // Not protected, just to be used in utility methods
  15. [Dependency] private readonly AtmosphereSystem _atmosphere = default!;
  16. [Dependency] private readonly MapSystem _map = default!;
  17. public override void Initialize()
  18. {
  19. base.Initialize();
  20. SubscribeLocalEvent<RoundStartAttemptEvent>(OnStartAttempt);
  21. SubscribeLocalEvent<T, GameRuleAddedEvent>(OnGameRuleAdded);
  22. SubscribeLocalEvent<T, GameRuleStartedEvent>(OnGameRuleStarted);
  23. SubscribeLocalEvent<T, GameRuleEndedEvent>(OnGameRuleEnded);
  24. SubscribeLocalEvent<RoundEndTextAppendEvent>(OnRoundEndTextAppend);
  25. }
  26. private void OnStartAttempt(RoundStartAttemptEvent args)
  27. {
  28. if (args.Forced || args.Cancelled)
  29. return;
  30. var query = QueryAllRules();
  31. while (query.MoveNext(out var uid, out _, out var gameRule))
  32. {
  33. var minPlayers = gameRule.MinPlayers;
  34. if (args.Players.Length >= minPlayers)
  35. continue;
  36. if (gameRule.CancelPresetOnTooFewPlayers)
  37. {
  38. ChatManager.SendAdminAnnouncement(Loc.GetString("preset-not-enough-ready-players",
  39. ("readyPlayersCount", args.Players.Length),
  40. ("minimumPlayers", minPlayers),
  41. ("presetName", ToPrettyString(uid))));
  42. args.Cancel();
  43. }
  44. else
  45. {
  46. ForceEndSelf(uid, gameRule);
  47. }
  48. }
  49. }
  50. private void OnGameRuleAdded(EntityUid uid, T component, ref GameRuleAddedEvent args)
  51. {
  52. if (!TryComp<GameRuleComponent>(uid, out var ruleData))
  53. return;
  54. Added(uid, component, ruleData, args);
  55. }
  56. private void OnGameRuleStarted(EntityUid uid, T component, ref GameRuleStartedEvent args)
  57. {
  58. if (!TryComp<GameRuleComponent>(uid, out var ruleData))
  59. return;
  60. Started(uid, component, ruleData, args);
  61. }
  62. private void OnGameRuleEnded(EntityUid uid, T component, ref GameRuleEndedEvent args)
  63. {
  64. if (!TryComp<GameRuleComponent>(uid, out var ruleData))
  65. return;
  66. Ended(uid, component, ruleData, args);
  67. }
  68. private void OnRoundEndTextAppend(RoundEndTextAppendEvent ev)
  69. {
  70. var query = AllEntityQuery<T>();
  71. while (query.MoveNext(out var uid, out var comp))
  72. {
  73. if (!TryComp<GameRuleComponent>(uid, out var ruleData))
  74. continue;
  75. AppendRoundEndText(uid, comp, ruleData, ref ev);
  76. }
  77. }
  78. /// <summary>
  79. /// Called when the gamerule is added
  80. /// </summary>
  81. protected virtual void Added(EntityUid uid, T component, GameRuleComponent gameRule, GameRuleAddedEvent args)
  82. {
  83. }
  84. /// <summary>
  85. /// Called when the gamerule begins
  86. /// </summary>
  87. protected virtual void Started(EntityUid uid, T component, GameRuleComponent gameRule, GameRuleStartedEvent args)
  88. {
  89. }
  90. /// <summary>
  91. /// Called when the gamerule ends
  92. /// </summary>
  93. protected virtual void Ended(EntityUid uid, T component, GameRuleComponent gameRule, GameRuleEndedEvent args)
  94. {
  95. }
  96. /// <summary>
  97. /// Called at the end of a round when text needs to be added for a game rule.
  98. /// </summary>
  99. protected virtual void AppendRoundEndText(EntityUid uid, T component, GameRuleComponent gameRule, ref RoundEndTextAppendEvent args)
  100. {
  101. }
  102. /// <summary>
  103. /// Called on an active gamerule entity in the Update function
  104. /// </summary>
  105. protected virtual void ActiveTick(EntityUid uid, T component, GameRuleComponent gameRule, float frameTime)
  106. {
  107. }
  108. public override void Update(float frameTime)
  109. {
  110. base.Update(frameTime);
  111. var query = EntityQueryEnumerator<T, GameRuleComponent>();
  112. while (query.MoveNext(out var uid, out var comp1, out var comp2))
  113. {
  114. if (!GameTicker.IsGameRuleActive(uid, comp2))
  115. continue;
  116. ActiveTick(uid, comp1, comp2, frameTime);
  117. }
  118. }
  119. }