Files
wwdpublic/Content.Server/Silicon/DeadStartupButtonSystem/DeadStartupButtonSystem.cs
VMSolidus 4a542df160 IPC Refactor (#771)
# Description

IPCs were ported from a codebase that didn't necessarily follow all of
our repo's coding standards. And while I had done my part to cleanup as
much of the system as was practical within the bounds of a Maintainer
Review, there were a lot of things that I felt were inappropriate to
leave to review, and wished to go over with a fine lense. Thus, here is
my Refactor of IPC code.

Do not merge this without first testing that nothing was broken. Because
I haven't tested it myself yet.
2024-10-19 12:57:44 +07:00

79 lines
3.4 KiB
C#

using Content.Server.Lightning;
using Content.Server.Popups;
using Content.Server.PowerCell;
using Content.Server.Silicon.Charge;
using Content.Shared.Silicon.DeadStartupButton;
using Content.Shared.Audio;
using Content.Shared.Damage;
using Content.Shared.Electrocution;
using Content.Shared.Mobs;
using Content.Shared.Mobs.Components;
using Content.Shared.Mobs.Systems;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Random;
namespace Content.Server.Silicon.DeadStartupButtonSystem;
public sealed class DeadStartupButtonSystem : SharedDeadStartupButtonSystem
{
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly MobStateSystem _mobState = default!;
[Dependency] private readonly MobThresholdSystem _mobThreshold = default!;
[Dependency] private readonly PopupSystem _popup = default!;
[Dependency] private readonly IRobustRandom _robustRandom = default!;
[Dependency] private readonly LightningSystem _lightning = default!;
[Dependency] private readonly SiliconChargeSystem _siliconChargeSystem = default!;
[Dependency] private readonly PowerCellSystem _powerCell = default!;
/// <inheritdoc/>
public override void Initialize()
{
SubscribeLocalEvent<DeadStartupButtonComponent, OnDoAfterButtonPressedEvent>(OnDoAfter);
SubscribeLocalEvent<DeadStartupButtonComponent, ElectrocutedEvent>(OnElectrocuted);
SubscribeLocalEvent<DeadStartupButtonComponent, MobStateChangedEvent>(OnMobStateChanged);
}
private void OnDoAfter(EntityUid uid, DeadStartupButtonComponent comp, OnDoAfterButtonPressedEvent args)
{
if (args.Handled || args.Cancelled
|| !TryComp<MobStateComponent>(uid, out var mobStateComponent)
|| !_mobState.IsDead(uid, mobStateComponent)
|| !TryComp<MobThresholdsComponent>(uid, out var mobThresholdsComponent)
|| !TryComp<DamageableComponent>(uid, out var damageable)
|| !_mobThreshold.TryGetThresholdForState(uid, MobState.Critical, out var criticalThreshold, mobThresholdsComponent))
return;
if (damageable.TotalDamage < criticalThreshold)
_mobState.ChangeMobState(uid, MobState.Alive, mobStateComponent);
else
{
_audio.PlayPvs(comp.BuzzSound, uid, AudioHelpers.WithVariation(0.05f, _robustRandom));
_popup.PopupEntity(Loc.GetString("dead-startup-system-reboot-failed", ("target", MetaData(uid).EntityName)), uid);
Spawn("EffectSparks", Transform(uid).Coordinates);
}
}
private void OnElectrocuted(EntityUid uid, DeadStartupButtonComponent comp, ElectrocutedEvent args)
{
if (!TryComp<MobStateComponent>(uid, out var mobStateComponent)
|| !_mobState.IsDead(uid, mobStateComponent)
|| !_siliconChargeSystem.TryGetSiliconBattery(uid, out var bateria)
|| bateria.CurrentCharge <= 0)
return;
_lightning.ShootRandomLightnings(uid, 2, 4);
_powerCell.TryUseCharge(uid, bateria.CurrentCharge);
}
private void OnMobStateChanged(EntityUid uid, DeadStartupButtonComponent comp, MobStateChangedEvent args)
{
if (args.NewMobState != MobState.Alive)
return;
_popup.PopupEntity(Loc.GetString("dead-startup-system-reboot-success", ("target", MetaData(uid).EntityName)), uid);
_audio.PlayPvs(comp.Sound, uid);
}
}