126 lines
4.2 KiB
C#
126 lines
4.2 KiB
C#
using Content.Server.Power.Components;
|
|
using Content.Shared.Silicon.Systems;
|
|
using Content.Shared.Bed.Sleep;
|
|
using Content.Server.Silicon.Charge;
|
|
using Content.Server.Humanoid;
|
|
using Content.Shared.Humanoid;
|
|
|
|
namespace Content.Server.Silicon.Death;
|
|
|
|
public sealed class SiliconDeathSystem : EntitySystem
|
|
{
|
|
[Dependency] private readonly SleepingSystem _sleep = default!;
|
|
[Dependency] private readonly SiliconChargeSystem _silicon = default!;
|
|
[Dependency] private readonly HumanoidAppearanceSystem _humanoidAppearanceSystem = default!;
|
|
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
|
|
SubscribeLocalEvent<SiliconDownOnDeadComponent, SiliconChargeStateUpdateEvent>(OnSiliconChargeStateUpdate);
|
|
}
|
|
|
|
private void OnSiliconChargeStateUpdate(EntityUid uid, SiliconDownOnDeadComponent siliconDeadComp, SiliconChargeStateUpdateEvent args)
|
|
{
|
|
if (!_silicon.TryGetSiliconBattery(uid, out var batteryComp))
|
|
{
|
|
SiliconDead(uid, siliconDeadComp, null, uid);
|
|
return;
|
|
}
|
|
|
|
if (args.ChargePercent == 0 && siliconDeadComp.Dead)
|
|
return;
|
|
|
|
if (args.ChargePercent == 0 && !siliconDeadComp.Dead)
|
|
SiliconDead(uid, siliconDeadComp, batteryComp, uid);
|
|
else if (args.ChargePercent != 0 && siliconDeadComp.Dead)
|
|
SiliconUnDead(uid, siliconDeadComp, batteryComp, uid);
|
|
}
|
|
|
|
private void SiliconDead(EntityUid uid, SiliconDownOnDeadComponent siliconDeadComp, BatteryComponent? batteryComp, EntityUid batteryUid)
|
|
{
|
|
var deadEvent = new SiliconChargeDyingEvent(uid, batteryComp, batteryUid);
|
|
RaiseLocalEvent(uid, deadEvent);
|
|
|
|
if (deadEvent.Cancelled)
|
|
return;
|
|
|
|
EntityManager.EnsureComponent<SleepingComponent>(uid);
|
|
EntityManager.EnsureComponent<ForcedSleepingComponent>(uid);
|
|
|
|
if (TryComp<HumanoidAppearanceComponent>(uid, out var humanoidAppearanceComponent))
|
|
{
|
|
// Handle humanoid appearance if needed
|
|
}
|
|
|
|
siliconDeadComp.Dead = true;
|
|
|
|
RaiseLocalEvent(uid, new SiliconChargeDeathEvent(uid, batteryComp, batteryUid));
|
|
}
|
|
|
|
private void SiliconUnDead(EntityUid uid, SiliconDownOnDeadComponent siliconDeadComp, BatteryComponent? batteryComp, EntityUid batteryUid)
|
|
{
|
|
RemComp<ForcedSleepingComponent>(uid);
|
|
_sleep.TryWaking(uid, true);
|
|
|
|
siliconDeadComp.Dead = false;
|
|
|
|
RaiseLocalEvent(uid, new SiliconChargeAliveEvent(uid, batteryComp, batteryUid));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// A cancellable event raised when a Silicon is about to go down due to charge.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This probably shouldn't be modified unless you intend to fill the Silicon's battery,
|
|
/// as otherwise it'll just be triggered again next frame.
|
|
/// </remarks>
|
|
public sealed class SiliconChargeDyingEvent : CancellableEntityEventArgs
|
|
{
|
|
public EntityUid SiliconUid { get; }
|
|
public BatteryComponent? BatteryComp { get; }
|
|
public EntityUid BatteryUid { get; }
|
|
|
|
public SiliconChargeDyingEvent(EntityUid siliconUid, BatteryComponent? batteryComp, EntityUid batteryUid)
|
|
{
|
|
SiliconUid = siliconUid;
|
|
BatteryComp = batteryComp;
|
|
BatteryUid = batteryUid;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// An event raised after a Silicon has gone down due to charge.
|
|
/// </summary>
|
|
public sealed class SiliconChargeDeathEvent : EntityEventArgs
|
|
{
|
|
public EntityUid SiliconUid { get; }
|
|
public BatteryComponent? BatteryComp { get; }
|
|
public EntityUid BatteryUid { get; }
|
|
|
|
public SiliconChargeDeathEvent(EntityUid siliconUid, BatteryComponent? batteryComp, EntityUid batteryUid)
|
|
{
|
|
SiliconUid = siliconUid;
|
|
BatteryComp = batteryComp;
|
|
BatteryUid = batteryUid;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// An event raised after a Silicon has reawoken due to an increase in charge.
|
|
/// </summary>
|
|
public sealed class SiliconChargeAliveEvent : EntityEventArgs
|
|
{
|
|
public EntityUid SiliconUid { get; }
|
|
public BatteryComponent? BatteryComp { get; }
|
|
public EntityUid BatteryUid { get; }
|
|
|
|
public SiliconChargeAliveEvent(EntityUid siliconUid, BatteryComponent? batteryComp, EntityUid batteryUid)
|
|
{
|
|
SiliconUid = siliconUid;
|
|
BatteryComp = batteryComp;
|
|
BatteryUid = batteryUid;
|
|
}
|
|
}
|