using Content.Shared.Examine;
using Content.Shared.Xenoarchaeology.Artifact.Components;
using Content.Shared.Xenoarchaeology.Artifact.XAT.Components;
using Robust.Shared.Random;
namespace Content.Shared.Xenoarchaeology.Artifact.XAT;
///
/// System for xeno artifact trigger that activates from time to time on schedule.
///
public sealed class XATTimerSystem : BaseQueryUpdateXATSystem
{
[Dependency] private readonly IRobustRandom _robustRandom = default!;
///
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent(OnMapInit);
XATSubscribeDirectEvent(OnExamine);
}
// We handle the timer resetting here because we need to keep it updated even if the node isn't able to unlock.
public override void Update(float frameTime)
{
base.Update(frameTime);
var timerQuery = EntityQueryEnumerator();
while (timerQuery.MoveNext(out var uid, out var timer))
{
if (Timing.CurTime < timer.NextActivation)
continue;
timer.NextActivation += GetNextDelay(timer);
Dirty(uid, timer);
}
}
///
protected override void UpdateXAT(Entity artifact, Entity node, float frameTime)
{
if (Timing.CurTime > node.Comp1.NextActivation)
Trigger(artifact, node);
}
private void OnMapInit(Entity ent, ref MapInitEvent args)
{
var delay = GetNextDelay(ent);
ent.Comp.NextActivation = Timing.CurTime + delay;
Dirty(ent);
}
private void OnExamine(Entity artifact, Entity node, ref ExaminedEvent args)
{
if (!args.IsInDetailsRange)
return;
args.PushMarkup(
Loc.GetString("xenoarch-trigger-examine-timer",
("time", MathF.Ceiling((float) (node.Comp1.NextActivation - Timing.CurTime).TotalSeconds)))
);
}
private TimeSpan GetNextDelay(XATTimerComponent comp)
{
return TimeSpan.FromSeconds(comp.PossibleDelayInSeconds.Next(_robustRandom));
}
}