Skip to content

Commit

Permalink
electrification hud
Browse files Browse the repository at this point in the history
  • Loading branch information
slarticodefast authored and sleepyyapril committed Jan 18, 2025
1 parent fbf0bf7 commit eb4ce6c
Show file tree
Hide file tree
Showing 14 changed files with 192 additions and 32 deletions.
101 changes: 101 additions & 0 deletions Content.Client/Electrocution/ElectrocutionOverlaySystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using Content.Shared.Electrocution;
using Robust.Client.GameObjects;
using Robust.Client.Player;
using Robust.Shared.Player;

namespace Content.Client.Electrocution;

/// <summary>
/// Shows the ElectrocutionOverlay to entities with the ElectrocutionOverlayComponent.
/// </summary>
public sealed class ElectrocutionOverlaySystem : EntitySystem
{

[Dependency] private readonly AppearanceSystem _appearance = default!;
[Dependency] private readonly IPlayerManager _playerMan = default!;

/// <inheritdoc/>
public override void Initialize()
{
SubscribeLocalEvent<ElectrocutionOverlayComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<ElectrocutionOverlayComponent, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<ElectrocutionOverlayComponent, LocalPlayerAttachedEvent>(OnPlayerAttached);
SubscribeLocalEvent<ElectrocutionOverlayComponent, LocalPlayerDetachedEvent>(OnPlayerDetached);

SubscribeLocalEvent<ElectrifiedComponent, AppearanceChangeEvent>(OnAppearanceChange);
}

private void OnPlayerAttached(Entity<ElectrocutionOverlayComponent> ent, ref LocalPlayerAttachedEvent args)
{
ShowOverlay();
}

private void OnPlayerDetached(Entity<ElectrocutionOverlayComponent> ent, ref LocalPlayerDetachedEvent args)
{
RemoveOverlay();
}

private void OnInit(Entity<ElectrocutionOverlayComponent> ent, ref ComponentInit args)
{
if (_playerMan.LocalEntity == ent)
{
ShowOverlay();
}
}

private void OnShutdown(Entity<ElectrocutionOverlayComponent> ent, ref ComponentShutdown args)
{
if (_playerMan.LocalEntity == ent)
{
RemoveOverlay();
}
}

private void ShowOverlay()
{
var electrifiedQuery = AllEntityQuery<ElectrifiedComponent, AppearanceComponent, SpriteComponent>();
while (electrifiedQuery.MoveNext(out var uid, out var _, out var appearanceComp, out var spriteComp))
{
if (!_appearance.TryGetData<bool>(uid, ElectrifiedVisuals.IsElectrified, out var electrified, appearanceComp))
continue;

if (!spriteComp.LayerMapTryGet(ElectrifiedLayers.Overlay, out var layer))
continue;

if (electrified)
spriteComp.LayerSetVisible(ElectrifiedLayers.Overlay, true);
else
spriteComp.LayerSetVisible(ElectrifiedLayers.Overlay, false);
}
}

private void RemoveOverlay()
{
var electrifiedQuery = AllEntityQuery<ElectrifiedComponent, AppearanceComponent, SpriteComponent>();
while (electrifiedQuery.MoveNext(out var uid, out var _, out var appearanceComp, out var spriteComp))
{
if (!spriteComp.LayerMapTryGet(ElectrifiedLayers.Overlay, out var layer))
continue;

spriteComp.LayerSetVisible(layer, false);
}
}

private void OnAppearanceChange(Entity<ElectrifiedComponent> ent, ref AppearanceChangeEvent args)
{
if (args.Sprite == null)
return;

if (!_appearance.TryGetData<bool>(ent.Owner, ElectrifiedVisuals.IsElectrified, out var electrified, args.Component))
return;

if (!args.Sprite.LayerMapTryGet(ElectrifiedLayers.Overlay, out var layer))
return;

var player = _playerMan.LocalEntity;
if (electrified && HasComp<ElectrocutionOverlayComponent>(player))
args.Sprite.LayerSetVisible(layer, true);
else
args.Sprite.LayerSetVisible(layer, false);
}
}
15 changes: 2 additions & 13 deletions Content.Server/Electrocution/ElectrocutionSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private void UpdateState(float frameTime)
activated.TimeLeft -= frameTime;
if (activated.TimeLeft <= 0 || !IsPowered(uid, electrified, transform))
{
_appearance.SetData(uid, ElectrifiedVisuals.IsPowered, false);
_appearance.SetData(uid, ElectrifiedVisuals.ShowSparks, false);
RemComp<ActivatedElectrifiedComponent>(uid);
}
}
Expand Down Expand Up @@ -219,7 +219,7 @@ public bool TryDoElectrifiedAct(EntityUid uid, EntityUid targetUid,
return false;

EnsureComp<ActivatedElectrifiedComponent>(uid);
_appearance.SetData(uid, ElectrifiedVisuals.IsPowered, true);
_appearance.SetData(uid, ElectrifiedVisuals.ShowSparks, true);

siemens *= electrified.SiemensCoefficient;
if (!DoCommonElectrocutionAttempt(targetUid, uid, ref siemens) || siemens <= 0)
Expand Down Expand Up @@ -490,15 +490,4 @@ private void PlayElectrocutionSound(EntityUid targetUid, EntityUid sourceUid, El
}
_audio.PlayPvs(electrified.ShockNoises, targetUid, AudioParams.Default.WithVolume(electrified.ShockVolume));
}

public void SetElectrifiedWireCut(Entity<ElectrifiedComponent> ent, bool value)
{
if (ent.Comp.IsWireCut == value)
{
return;
}

ent.Comp.IsWireCut = value;
Dirty(ent);
}
}
10 changes: 5 additions & 5 deletions Content.Server/Power/PowerWireAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public sealed partial class PowerWireAction : BaseWireAction
[DataField("pulseTimeout")]
private int _pulseTimeout = 30;

private ElectrocutionSystem _electrocutionSystem = default!;
private ElectrocutionSystem _electrocution = default!;

public override object StatusKey { get; } = PowerWireActionKey.Status;

Expand Down Expand Up @@ -109,8 +109,8 @@ private void SetElectrified(EntityUid used, bool setting, ElectrifiedComponent?
&& !EntityManager.TryGetComponent(used, out electrified))
return;

_electrocutionSystem.SetElectrifiedWireCut((used, electrified), setting);
electrified.Enabled = setting;
_electrocution.SetElectrifiedWireCut((used, electrified), setting);
_electrocution.SetElectrified((used, electrified), setting);
}

/// <returns>false if failed, true otherwise, or if the entity cannot be electrified</returns>
Expand All @@ -124,7 +124,7 @@ private bool TrySetElectrocution(EntityUid user, Wire wire, bool timed = false)
// always set this to true
SetElectrified(wire.Owner, true, electrified);

var electrifiedAttempt = _electrocutionSystem.TryDoElectrifiedAct(wire.Owner, user);
var electrifiedAttempt = _electrocution.TryDoElectrifiedAct(wire.Owner, user);

// if we were electrified, then return false
return !electrifiedAttempt;
Expand Down Expand Up @@ -165,7 +165,7 @@ public override void Initialize()
{
base.Initialize();

_electrocutionSystem = EntitySystem.Get<ElectrocutionSystem>();
_electrocution = EntityManager.System<ElectrocutionSystem>();
}

// This should add a wire into the entity's state, whether it be
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Robust.Shared.GameStates;

namespace Content.Shared.Electrocution;

/// <summary>
/// Allow an entity to see the ElectrocutionOverlay showing electrocuted doors.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed partial class ElectrocutionOverlayComponent : Component;
6 changes: 4 additions & 2 deletions Content.Shared/Electrocution/SharedElectrocution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ namespace Content.Shared.Electrocution;
[Serializable, NetSerializable]
public enum ElectrifiedLayers : byte
{
Powered
Sparks,
Overlay,
}

[Serializable, NetSerializable]
public enum ElectrifiedVisuals : byte
{
IsPowered
ShowSparks, // only shown when zapping someone, deactivated after a short time
IsElectrified, // if the entity is electrified or not, used for the AI HUD
}
15 changes: 15 additions & 0 deletions Content.Shared/Electrocution/SharedElectrocutionSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace Content.Shared.Electrocution
{
public abstract class SharedElectrocutionSystem : EntitySystem
{
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;

public override void Initialize()
{
base.Initialize();
Expand Down Expand Up @@ -35,6 +37,19 @@ public void SetElectrified(Entity<ElectrifiedComponent> ent, bool value)

ent.Comp.Enabled = value;
Dirty(ent, ent.Comp);

_appearance.SetData(ent.Owner, ElectrifiedVisuals.IsElectrified, value);
}

public void SetElectrifiedWireCut(Entity<ElectrifiedComponent> ent, bool value)
{
if (ent.Comp.IsWireCut == value)
{
return;
}

ent.Comp.IsWireCut = value;
Dirty(ent);
}

/// <param name="uid">Entity being electrocuted.</param>
Expand Down
1 change: 1 addition & 0 deletions Resources/Prototypes/Entities/Mobs/Player/observer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
skipChecks: true
- type: Ghost
- type: GhostHearing
- type: ElectrocutionOverlay
- type: IntrinsicRadioReceiver
- type: ActiveRadio
receiveAllChannels: true
Expand Down
1 change: 1 addition & 0 deletions Resources/Prototypes/Entities/Mobs/Player/silicon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- type: IgnoreUIRange
- type: StationAiHeld
- type: StationAiOverlay
- type: ElectrocutionOverlay
- type: ActionGrant
actions:
- ActionJumpToCore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
shader: unshaded
- state: panel_open
map: ["enum.WiresVisualLayers.MaintenancePanel"]
- state: electrified
sprite: Interface/Misc/ai_hud.rsi
shader: unshaded
visible: false
map: ["enum.ElectrifiedLayers.Overlay"]
- type: AnimationPlayer
- type: Physics
- type: Fixtures
Expand Down
14 changes: 7 additions & 7 deletions Resources/Prototypes/Entities/Structures/Walls/fence_metal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
- type: Appearance
- type: GenericVisualizer
visuals:
enum.ElectrifiedVisuals.IsPowered:
enum.ElectrifiedLayers.Powered:
enum.ElectrifiedVisuals.ShowSparks:
enum.ElectrifiedLayers.Sparks:
True: { visible: True }
False: { visible: False }
- type: AnimationPlayer
Expand All @@ -94,7 +94,7 @@
- state: straight_broken
- state: electrified
sprite: Effects/electricity.rsi
map: ["enum.ElectrifiedLayers.Powered"]
map: ["enum.ElectrifiedLayers.Sparks"]
shader: unshaded
visible: false
- type: Physics
Expand Down Expand Up @@ -149,7 +149,7 @@
- state: straight
- state: electrified
sprite: Effects/electricity.rsi
map: ["enum.ElectrifiedLayers.Powered"]
map: ["enum.ElectrifiedLayers.Sparks"]
shader: unshaded
visible: false
- type: Fixtures
Expand Down Expand Up @@ -202,7 +202,7 @@
- state: corner
- state: electrified
sprite: Effects/electricity.rsi
map: ["enum.ElectrifiedLayers.Powered"]
map: ["enum.ElectrifiedLayers.Sparks"]
shader: unshaded
visible: false
- type: Fixtures
Expand Down Expand Up @@ -244,7 +244,7 @@
- state: end
- state: electrified
sprite: Effects/electricity.rsi
map: ["enum.ElectrifiedLayers.Powered"]
map: ["enum.ElectrifiedLayers.Sparks"]
shader: unshaded
visible: false
- type: Fixtures
Expand Down Expand Up @@ -275,7 +275,7 @@
map: ["enum.DoorVisualLayers.Base"]
- state: electrified
sprite: Effects/electricity.rsi
map: [ "enum.ElectrifiedLayers.Powered" ]
map: [ "enum.ElectrifiedLayers.Sparks" ]
shader: unshaded
visible: false
- type: Fixtures
Expand Down
10 changes: 5 additions & 5 deletions Resources/Prototypes/Entities/Structures/Walls/grille.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
- state: grille
- state: electrified
sprite: Effects/electricity.rsi
map: ["enum.ElectrifiedLayers.Powered"]
map: ["enum.ElectrifiedLayers.Sparks"]
shader: unshaded
visible: false
- type: Icon
Expand Down Expand Up @@ -82,8 +82,8 @@
- type: Appearance
- type: GenericVisualizer
visuals:
enum.ElectrifiedVisuals.IsPowered:
enum.ElectrifiedLayers.Powered:
enum.ElectrifiedVisuals.ShowSparks:
enum.ElectrifiedLayers.Sparks:
True: { visible: True }
False: { visible: False }
- type: AnimationPlayer
Expand Down Expand Up @@ -176,7 +176,7 @@
- state: grille_diagonal
- state: electrified_diagonal
sprite: Effects/electricity.rsi
map: ["enum.ElectrifiedLayers.Powered"]
map: ["enum.ElectrifiedLayers.Sparks"]
shader: unshaded
visible: false
- type: Icon
Expand Down Expand Up @@ -211,7 +211,7 @@
- state: ratvargrille_diagonal
- state: electrified_diagonal
sprite: Effects/electricity.rsi
map: ["enum.ElectrifiedLayers.Powered"]
map: ["enum.ElectrifiedLayers.Sparks"]
shader: unshaded
visible: false
- type: Icon
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions Resources/Textures/Interface/Misc/ai_hud.rsi/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"version": 1,
"license": "CC-BY-SA-3.0",
"copyright": "taken from tgstation at commit https://github.com/tgstation/tgstation/commit/d170a410d40eec4fc19fe5eb8d561d58a0902082",
"size": {
"x": 32,
"y": 32
},
"states": [
{
"name": "electrified",
"delays": [
[
0.2,
0.2,
0.2,
0.2,
0.2,
0.2
]
]
},
{
"name": "apc_hacked",
"delays": [
[
0.2,
0.2,
0.2,
0.2,
0.2,
0.2
]
]
}
]
}

0 comments on commit eb4ce6c

Please sign in to comment.