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

85 lines
2.4 KiB
C#
Raw Normal View History

2023-04-25 20:23:14 -04:00
using Content.Server.GameTicking.Rules.Components;
2021-12-21 21:23:29 +01:00
namespace Content.Server.GameTicking.Rules;
2023-04-25 20:23:14 -04:00
public abstract class GameRuleSystem<T> : EntitySystem where T : Component
2021-12-21 21:23:29 +01:00
{
2023-04-28 23:14:15 -04:00
[Dependency] protected readonly GameTicker GameTicker = 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);
}
/// <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
}