Files
OldThink/Content.Server/GameTicking/Rules/GameRuleSystem.cs

98 lines
3.0 KiB
C#
Raw Normal View History

using Content.Server.Atmos.EntitySystems;
using Content.Server.Chat.Managers;
2023-04-25 20:23:14 -04:00
using Content.Server.GameTicking.Rules.Components;
using Robust.Server.GameObjects;
using Robust.Shared.Random;
using Robust.Shared.Timing;
2021-12-21 21:23:29 +01:00
namespace Content.Server.GameTicking.Rules;
public abstract partial class GameRuleSystem<T> : EntitySystem where T : IComponent
2021-12-21 21:23:29 +01:00
{
[Dependency] protected readonly IRobustRandom RobustRandom = default!;
[Dependency] protected readonly IChatManager ChatManager = default!;
2023-04-28 23:14:15 -04:00
[Dependency] protected readonly GameTicker GameTicker = default!;
[Dependency] protected readonly IGameTiming Timing = default!;
2021-12-21 21:23:29 +01:00
// Not protected, just to be used in utility methods
[Dependency] private readonly AtmosphereSystem _atmosphere = default!;
[Dependency] private readonly MapSystem _map = default!;
2021-12-21 21:23:29 +01:00
public override void Initialize()
{
base.Initialize();
2023-04-25 20:23:14 -04:00
SubscribeLocalEvent<T, GameRuleAddedEvent>(OnGameRuleAdded);
SubscribeLocalEvent<T, GameRuleStartedEvent>(OnGameRuleStarted);
SubscribeLocalEvent<T, GameRuleEndedEvent>(OnGameRuleEnded);
2021-12-21 21:23:29 +01:00
}
2023-04-25 20:23:14 -04:00
private void OnGameRuleAdded(EntityUid uid, T component, ref GameRuleAddedEvent args)
{
2023-04-25 20:23:14 -04:00
if (!TryComp<GameRuleComponent>(uid, out var ruleData))
return;
2023-04-25 20:23:14 -04:00
Added(uid, component, ruleData, args);
}
2023-04-25 20:23:14 -04:00
private void OnGameRuleStarted(EntityUid uid, T component, ref GameRuleStartedEvent args)
2021-12-21 21:23:29 +01:00
{
2023-04-25 20:23:14 -04:00
if (!TryComp<GameRuleComponent>(uid, out var ruleData))
2021-12-21 21:23:29 +01:00
return;
2023-04-25 20:23:14 -04:00
Started(uid, component, ruleData, args);
2021-12-21 21:23:29 +01:00
}
2023-04-25 20:23:14 -04:00
private void OnGameRuleEnded(EntityUid uid, T component, ref GameRuleEndedEvent args)
2023-04-24 01:20:51 -04:00
{
2023-04-25 20:23:14 -04:00
if (!TryComp<GameRuleComponent>(uid, out var ruleData))
2023-04-24 16:21:05 +10:00
return;
2023-04-25 20:23:14 -04:00
Ended(uid, component, ruleData, args);
}
2023-04-25 20:23:14 -04:00
/// <summary>
/// Called when the gamerule is added
/// </summary>
protected virtual void Added(EntityUid uid, T component, GameRuleComponent gameRule, GameRuleAddedEvent args)
{
2023-04-24 01:20:51 -04:00
}
/// <summary>
2023-04-25 20:23:14 -04:00
/// Called when the gamerule begins
2021-12-21 21:23:29 +01:00
/// </summary>
2023-04-25 20:23:14 -04:00
protected virtual void Started(EntityUid uid, T component, GameRuleComponent gameRule, GameRuleStartedEvent args)
{
}
2021-12-21 21:23:29 +01:00
/// <summary>
2023-04-25 20:23:14 -04:00
/// Called when the gamerule ends
2021-12-21 21:23:29 +01:00
/// </summary>
2023-04-25 20:23:14 -04:00
protected virtual void Ended(EntityUid uid, T component, GameRuleComponent gameRule, GameRuleEndedEvent args)
{
}
2023-04-24 01:20:51 -04:00
2023-04-24 16:21:05 +10:00
/// <summary>
2023-04-25 20:23:14 -04:00
/// Called on an active gamerule entity in the Update function
2023-04-24 16:21:05 +10:00
/// </summary>
2023-04-25 20:23:14 -04:00
protected virtual void ActiveTick(EntityUid uid, T component, GameRuleComponent gameRule, float frameTime)
{
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<T, GameRuleComponent>();
while (query.MoveNext(out var uid, out var comp1, out var comp2))
{
if (!GameTicker.IsGameRuleActive(uid, comp2))
continue;
ActiveTick(uid, comp1, comp2, frameTime);
}
}
2021-12-21 21:23:29 +01:00
}