Generic power switch component (#11999)

This commit is contained in:
Rane
2023-01-20 11:05:54 -05:00
committed by GitHub
parent d6228d7a4c
commit 80d4dc430d
8 changed files with 175 additions and 24 deletions

View File

@@ -1,14 +1,20 @@
using Content.Server.Power.Components;
using Content.Server.Hands.Components;
using Content.Server.Administration.Logs;
using Content.Shared.Examine;
using Content.Shared.Power;
using Content.Shared.Verbs;
using Content.Shared.Database;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
namespace Content.Server.Power.EntitySystems
{
public sealed class PowerReceiverSystem : EntitySystem
{
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
[Dependency] private readonly AppearanceSystem _appearance = default!;
[Dependency] private readonly AudioSystem _audio = default!;
public override void Initialize()
{
base.Initialize();
@@ -20,6 +26,8 @@ namespace Content.Server.Power.EntitySystems
SubscribeLocalEvent<ApcPowerProviderComponent, ComponentShutdown>(OnProviderShutdown);
SubscribeLocalEvent<ApcPowerProviderComponent, ExtensionCableSystem.ReceiverConnectedEvent>(OnReceiverConnected);
SubscribeLocalEvent<ApcPowerProviderComponent, ExtensionCableSystem.ReceiverDisconnectedEvent>(OnReceiverDisconnected);
SubscribeLocalEvent<PowerSwitchComponent, GetVerbsEvent<AlternativeVerb>>(AddSwitchPowerVerb);
}
///<summary>
@@ -78,6 +86,33 @@ namespace Content.Server.Power.EntitySystems
}
}
private void AddSwitchPowerVerb(EntityUid uid, PowerSwitchComponent component, GetVerbsEvent<AlternativeVerb> args)
{
if(!args.CanAccess || !args.CanInteract)
return;
if (!HasComp<HandsComponent>(args.User))
return;
if (!TryComp<ApcPowerReceiverComponent>(uid, out var receiver))
return;
if (!receiver.NeedsPower)
return;
AlternativeVerb verb = new()
{
Act = () =>
{
TogglePower(uid, user: args.User);
},
IconTexture = "/Textures/Interface/VerbIcons/Spare/poweronoff.svg.192dpi.png",
Text = Loc.GetString("power-switch-component-toggle-verb"),
Priority = -3
};
args.Verbs.Add(verb);
}
private void ProviderChanged(ApcPowerReceiverComponent receiver)
{
receiver.NetworkLoad.LinkedNetwork = default;
@@ -99,5 +134,35 @@ namespace Content.Server.Power.EntitySystems
return receiver.Powered;
}
/// <summary>
/// Turn this machine on or off.
/// Returns true if we turned it on, false if we turned it off.
/// </summary>
public bool TogglePower(EntityUid uid, bool playSwitchSound = true, ApcPowerReceiverComponent? receiver = null, EntityUid? user = null)
{
if (!Resolve(uid, ref receiver, false))
return true;
// it'll save a lot of confusion if 'always powered' means 'always powered'
if (!receiver.NeedsPower)
{
receiver.PowerDisabled = false;
return true;
}
receiver.PowerDisabled = !receiver.PowerDisabled;
if (user != null)
_adminLogger.Add(LogType.Action, LogImpact.Low, $"{ToPrettyString(user.Value):player} hit power button on {ToPrettyString(uid)}, it's now {(!receiver.PowerDisabled ? "on" : "off")}");
if (playSwitchSound)
{
_audio.PlayPvs(new SoundPathSpecifier("/Audio/Machines/machine_switch.ogg"), uid,
AudioParams.Default.WithVolume(-2f));
}
return !receiver.PowerDisabled; // i.e. PowerEnabled
}
}
}