Files
tbd-station-14/Content.Server/Anomaly/Effects/AnomalyCoreSystem.cs
Ed 96ff529c82 Anomaly Cores (#21306)
* add first anomaly core

* meme

* 5 min to 10 min
fix yml

* fix

* Update doc

* no static price
2023-10-31 19:20:52 -04:00

57 lines
1.9 KiB
C#

using Content.Server.Anomaly.Components;
using Content.Server.Cargo.Systems;
using Content.Shared.Anomaly;
using Robust.Shared.Timing;
namespace Content.Server.Anomaly.Effects;
/// <summary>
/// This component reduces the value of the entity during decay
/// </summary>
public sealed class AnomalyCoreSystem : EntitySystem
{
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
public override void Initialize()
{
SubscribeLocalEvent<AnomalyCoreComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<AnomalyCoreComponent, PriceCalculationEvent>(OnGetPrice);
}
private void OnMapInit(Entity<AnomalyCoreComponent> core, ref MapInitEvent args)
{
core.Comp.DecayMoment = _gameTiming.CurTime + TimeSpan.FromSeconds(core.Comp.TimeToDecay);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<AnomalyCoreComponent>();
while (query.MoveNext(out var uid, out var component))
{
if (component.IsDecayed)
continue;
//When time runs out, we completely decompose
if (component.DecayMoment < _gameTiming.CurTime)
Decay(uid, component);
}
}
private void OnGetPrice(Entity<AnomalyCoreComponent> core, ref PriceCalculationEvent args)
{
var timeLeft = core.Comp.DecayMoment - _gameTiming.CurTime;
var lerp = (double) (timeLeft.TotalSeconds / core.Comp.TimeToDecay);
lerp = Math.Clamp(lerp, 0, 1);
args.Price = MathHelper.Lerp(core.Comp.EndPrice, core.Comp.StartPrice, lerp);
}
private void Decay(EntityUid uid, AnomalyCoreComponent component)
{
_appearance.SetData(uid, AnomalyCoreVisuals.Decaying, false);
component.IsDecayed = true;
}
}