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

@@ -8,6 +8,7 @@ using Content.Server.DoAfter;
using Content.Server.Hands.Components; using Content.Server.Hands.Components;
using Content.Server.Popups; using Content.Server.Popups;
using Content.Server.Power.Components; using Content.Server.Power.Components;
using Content.Server.Power.EntitySystems;
using Content.Shared.ActionBlocker; using Content.Shared.ActionBlocker;
using Content.Shared.Atmos; using Content.Shared.Atmos;
using Content.Shared.Construction.Components; using Content.Shared.Construction.Components;
@@ -25,13 +26,11 @@ using Content.Shared.Popups;
using Content.Shared.Throwing; using Content.Shared.Throwing;
using Content.Shared.Verbs; using Content.Shared.Verbs;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.Containers; using Robust.Shared.Containers;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;
using Robust.Shared.Physics.Components; using Robust.Shared.Physics.Components;
using Robust.Shared.Player; using Robust.Shared.Player;
using Robust.Shared.Random; using Robust.Shared.Random;
using Robust.Shared.Map.Components;
namespace Content.Server.Disposal.Unit.EntitySystems namespace Content.Server.Disposal.Unit.EntitySystems
{ {
@@ -50,6 +49,7 @@ namespace Content.Server.Disposal.Unit.EntitySystems
[Dependency] private readonly SharedHandsSystem _handsSystem = default!; [Dependency] private readonly SharedHandsSystem _handsSystem = default!;
[Dependency] private readonly TransformSystem _transformSystem = default!; [Dependency] private readonly TransformSystem _transformSystem = default!;
[Dependency] private readonly UserInterfaceSystem _ui = default!; [Dependency] private readonly UserInterfaceSystem _ui = default!;
[Dependency] private readonly PowerReceiverSystem _power = default!;
public override void Initialize() public override void Initialize()
{ {
@@ -223,13 +223,7 @@ namespace Content.Server.Disposal.Unit.EntitySystems
_adminLogger.Add(LogType.Action, LogImpact.Low, $"{ToPrettyString(player):player} hit flush button on {ToPrettyString(uid)}, it's now {(component.Engaged ? "on" : "off")}"); _adminLogger.Add(LogType.Action, LogImpact.Low, $"{ToPrettyString(player):player} hit flush button on {ToPrettyString(uid)}, it's now {(component.Engaged ? "on" : "off")}");
break; break;
case SharedDisposalUnitComponent.UiButton.Power: case SharedDisposalUnitComponent.UiButton.Power:
TogglePower(component); _power.TogglePower(uid, user: args.Session.AttachedEntity);
_audio.PlayPvs(new SoundPathSpecifier("/Audio/Machines/machine_switch.ogg"), component.Owner,
AudioParams.Default.WithVolume(-2f));
if (EntityManager.TryGetComponent(component.Owner, out ApcPowerReceiverComponent? receiver))
{
_adminLogger.Add(LogType.Action, LogImpact.Low, $"{ToPrettyString(player):player} hit power button on {ToPrettyString(uid)}, it's now {(!receiver.PowerDisabled ? "on" : "off")}");
}
break; break;
default: default:
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
@@ -249,17 +243,6 @@ namespace Content.Server.Disposal.Unit.EntitySystems
Disengage(component); Disengage(component);
} }
} }
public void TogglePower(DisposalUnitComponent component)
{
if (!EntityManager.TryGetComponent(component.Owner, out ApcPowerReceiverComponent? receiver))
{
return;
}
receiver.PowerDisabled = !receiver.PowerDisabled;
UpdateInterface(component, receiver.Powered);
}
#endregion #endregion
#region Eventbus Handlers #region Eventbus Handlers

View File

@@ -0,0 +1,9 @@
namespace Content.Server.Power.Components
{
/// <summary>
/// Provides an alt verb to toggle power.
/// </summary>
[RegisterComponent]
public sealed class PowerSwitchComponent : Component
{}
}

View File

@@ -1,14 +1,20 @@
using Content.Server.Power.Components; using Content.Server.Power.Components;
using Content.Server.Hands.Components;
using Content.Server.Administration.Logs;
using Content.Shared.Examine; using Content.Shared.Examine;
using Content.Shared.Power; using Content.Shared.Power;
using Content.Shared.Verbs;
using Content.Shared.Database;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Shared.Audio;
namespace Content.Server.Power.EntitySystems namespace Content.Server.Power.EntitySystems
{ {
public sealed class PowerReceiverSystem : EntitySystem public sealed class PowerReceiverSystem : EntitySystem
{ {
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
[Dependency] private readonly AppearanceSystem _appearance = default!; [Dependency] private readonly AppearanceSystem _appearance = default!;
[Dependency] private readonly AudioSystem _audio = default!;
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
@@ -20,6 +26,8 @@ namespace Content.Server.Power.EntitySystems
SubscribeLocalEvent<ApcPowerProviderComponent, ComponentShutdown>(OnProviderShutdown); SubscribeLocalEvent<ApcPowerProviderComponent, ComponentShutdown>(OnProviderShutdown);
SubscribeLocalEvent<ApcPowerProviderComponent, ExtensionCableSystem.ReceiverConnectedEvent>(OnReceiverConnected); SubscribeLocalEvent<ApcPowerProviderComponent, ExtensionCableSystem.ReceiverConnectedEvent>(OnReceiverConnected);
SubscribeLocalEvent<ApcPowerProviderComponent, ExtensionCableSystem.ReceiverDisconnectedEvent>(OnReceiverDisconnected); SubscribeLocalEvent<ApcPowerProviderComponent, ExtensionCableSystem.ReceiverDisconnectedEvent>(OnReceiverDisconnected);
SubscribeLocalEvent<PowerSwitchComponent, GetVerbsEvent<AlternativeVerb>>(AddSwitchPowerVerb);
} }
///<summary> ///<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) private void ProviderChanged(ApcPowerReceiverComponent receiver)
{ {
receiver.NetworkLoad.LinkedNetwork = default; receiver.NetworkLoad.LinkedNetwork = default;
@@ -99,5 +134,35 @@ namespace Content.Server.Power.EntitySystems
return receiver.Powered; 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
}
} }
} }

View File

@@ -0,0 +1 @@
power-switch-component-toggle-verb = Toggle power

View File

@@ -76,6 +76,7 @@
DisposalUnit: !type:Container DisposalUnit: !type:Container
- type: StaticPrice - type: StaticPrice
price: 100 price: 100
- type: PowerSwitch
- type: entity - type: entity
id: DisposalUnit id: DisposalUnit
@@ -92,7 +93,6 @@
- key: enum.DisposalUnitUiKey.Key - key: enum.DisposalUnitUiKey.Key
type: DisposalUnitBoundUserInterface type: DisposalUnitBoundUserInterface
- type: entity - type: entity
id: MailingUnit id: MailingUnit
parent: DisposalUnitBase parent: DisposalUnitBase

View File

@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
viewBox="0 0 13560 11726.16"
version="1.1"
id="svg8"
sodipodi:docname="poweronoff.svg"
width="13560"
height="11726.16"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<defs
id="defs12" />
<sodipodi:namedview
id="namedview10"
pagecolor="#505050"
bordercolor="#ffffff"
borderopacity="1"
inkscape:showpageshadow="0"
inkscape:pageopacity="0"
inkscape:pagecheckerboard="1"
inkscape:deskcolor="#505050"
showgrid="false" />
<path
d="m -389.36801,-2047.468 c 0,31.204 25.296,56.5 56.5,56.5 31.203,0 56.5,-25.296 56.5,-56.5 0,-16.264 -6.882,-30.91 -17.88,-41.22 l -14.362,15.333 c 6.912,6.482 11.244,15.688 11.244,25.89 0,19.574 -15.926,35.5 -35.5,35.5 -19.575,0 -35.5,-15.926 -35.5,-35.5"
fill="#010101"
id="path2"
style="fill:#ffffff" />
<path
d="m -297.36801,-2047.468 c 0,19.575 -15.925,35.5 -35.5,35.5 -19.574,0 -35.5,-15.926 -35.5,-35.5 0,-10.2 4.332,-19.406 11.245,-25.89 l -14.365,-15.33 c -10.998,10.308 -17.88,24.955 -17.88,41.218 0,31.204 25.297,56.5 56.5,56.5 31.204,0 56.5,-25.297 56.5,-56.5"
fill="#010101"
id="path4"
style="fill:#f9f9f9" />
<path
d="m -342.85501,-2106.24 h 19.975 v 61.576 h -19.975 z"
fill="#010101"
id="path6"
style="fill:#ffffff" />
</svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@@ -1 +1,54 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 120 120"><path d="M3.272 58.772c0 31.204 25.296 56.5 56.5 56.5 31.203 0 56.5-25.296 56.5-56.5 0-16.264-6.882-30.91-17.88-41.22L84.03 32.885c6.912 6.482 11.244 15.688 11.244 25.89 0 19.574-15.926 35.5-35.5 35.5-19.575 0-35.5-15.926-35.5-35.5" fill="#010101"/><path d="M95.272 58.772c0 19.575-15.925 35.5-35.5 35.5-19.574 0-35.5-15.926-35.5-35.5 0-10.2 4.332-19.406 11.245-25.89l-14.365-15.33C10.154 27.86 3.272 42.507 3.272 58.77c0 31.204 25.297 56.5 56.5 56.5 31.204 0 56.5-25.297 56.5-56.5" fill="#010101"/><path d="M49.785 0H69.76v61.576H49.785z" fill="#010101"/></svg> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
viewBox="0 0 120 120"
version="1.1"
id="svg8"
sodipodi:docname="poweronoff.svg"
inkscape:export-filename="poweronoff.svg"
inkscape:export-xdpi="192"
inkscape:export-ydpi="192"
inkscape:version="1.2.1 (9c6d41e410, 2022-07-14)"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<defs
id="defs12" />
<sodipodi:namedview
id="namedview10"
pagecolor="#505050"
bordercolor="#ffffff"
borderopacity="1"
inkscape:showpageshadow="0"
inkscape:pageopacity="0"
inkscape:pagecheckerboard="1"
inkscape:deskcolor="#505050"
showgrid="false"
inkscape:zoom="6.5166667"
inkscape:cx="23.554987"
inkscape:cy="60"
inkscape:window-width="1920"
inkscape:window-height="1011"
inkscape:window-x="0"
inkscape:window-y="0"
inkscape:window-maximized="1"
inkscape:current-layer="svg8" />
<path
d="M3.272 58.772c0 31.204 25.296 56.5 56.5 56.5 31.203 0 56.5-25.296 56.5-56.5 0-16.264-6.882-30.91-17.88-41.22L84.03 32.885c6.912 6.482 11.244 15.688 11.244 25.89 0 19.574-15.926 35.5-35.5 35.5-19.575 0-35.5-15.926-35.5-35.5"
fill="#010101"
id="path2"
style="fill:#ffffff" />
<path
d="M95.272 58.772c0 19.575-15.925 35.5-35.5 35.5-19.574 0-35.5-15.926-35.5-35.5 0-10.2 4.332-19.406 11.245-25.89l-14.365-15.33C10.154 27.86 3.272 42.507 3.272 58.77c0 31.204 25.297 56.5 56.5 56.5 31.204 0 56.5-25.297 56.5-56.5"
fill="#010101"
id="path4"
style="fill:#f9f9f9"
inkscape:export-filename="path4.svg"
inkscape:export-xdpi="192"
inkscape:export-ydpi="192" />
<path
d="M49.785 0H69.76v61.576H49.785z"
fill="#010101"
id="path6"
style="fill:#ffffff" />
</svg>

Before

Width:  |  Height:  |  Size: 624 B

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB