Files
wwdpublic/Content.Shared/Overlays/Switchable/SwitchableOverlaySystem.cs
Eris 19834bbb5e Port Changelings From Goobstation (Funky PR 387 Included) (#1855)
<!--
This is a semi-strict format, you can add/remove sections as needed but
the order/format should be kept the same
Remove these comments before submitting
-->

# Description

<!--
Explain this PR in as much detail as applicable

Some example prompts to consider:
How might this affect the game? The codebase?
What might be some alternatives to this?
How/Who does this benefit/hurt [the game/codebase]?
-->

Finally gets around to porting over Changelings from Goobstation, as
well as a *certain evil PR* from FunkyStation (with the fixes it comes
with).
---

# TODO

<!--
A list of everything you have to do before this PR is "complete"
You probably won't have to complete everything before merging but it's
good to leave future references
-->

- [x] Wait for #1856 to be merged
- [x] Wait for #1860 to be merged
- [x] Fix broken code to make it actually compile (right now is just
porting prototypes, code, and locale)
- [X] Port That Funky PR I Mentioned Earlier
- [] Throw bricks at the codebase until it stops failing tests
- [X] Maybe do some local testing

---

<!--
This is default collapsed, readers click to expand it and see all your
media
The PR media section can get very large at times, so this is a good way
to keep it clean
The title is written using HTML tags
The title must be within the <summary> tags or you won't see it
-->

<details><summary><h1>Media</h1></summary>
<p>

![image](https://github.com/user-attachments/assets/04473aef-d076-4dd0-a700-0c656428b88a)

</p>
</details>

---

# Changelog

<!--
You can add an author after the `🆑` to change the name that appears
in the changelog (ex: `🆑 Death`)
Leaving it blank will default to your GitHub display name
This includes all available types for the changelog
-->

🆑
- add: Changelings have been ported!

---------

Signed-off-by: Eris <erisfiregamer1@gmail.com>
Co-authored-by: VMSolidus <evilexecutive@gmail.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
(cherry picked from commit ee4f7aa7097f260d07af3ab0583a2a5c14a38c74)
2025-03-21 17:06:45 +03:00

197 lines
6.8 KiB
C#

using Content.Shared._Goobstation.Flashbang;
using Content.Shared.Actions;
using Content.Shared.Inventory;
using Robust.Shared.Audio.Systems;
using Robust.Shared.GameStates;
using Robust.Shared.Network;
using Robust.Shared.Player;
using Robust.Shared.Timing;
namespace Content.Shared.Overlays.Switchable;
public abstract class SwitchableOverlaySystem<TComp, TEvent> : EntitySystem
where TComp : SwitchableOverlayComponent
where TEvent : InstantActionEvent
{
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedActionsSystem _actions = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly INetManager _net = default!;
public override void Initialize()
{
SubscribeLocalEvent<TComp, TEvent>(OnToggle);
SubscribeLocalEvent<TComp, ComponentInit>(OnInit);
SubscribeLocalEvent<TComp, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<TComp, ComponentShutdown>(OnShutdown);
SubscribeLocalEvent<TComp, GetItemActionsEvent>(OnGetItemActions);
SubscribeLocalEvent<TComp, ComponentGetState>(OnGetState);
SubscribeLocalEvent<TComp, ComponentHandleState>(OnHandleState);
SubscribeLocalEvent<TComp, FlashDurationMultiplierEvent>(OnGetFlashMultiplier);
SubscribeLocalEvent<TComp, InventoryRelayedEvent<FlashDurationMultiplierEvent>>(OnGetInventoryFlashMultiplier);
}
private void OnGetFlashMultiplier(Entity<TComp> ent, ref FlashDurationMultiplierEvent args)
{
if (!ent.Comp.IsEquipment)
args.Multiplier *= GetFlashMultiplier(ent);
}
private void OnGetInventoryFlashMultiplier(Entity<TComp> ent,
ref InventoryRelayedEvent<FlashDurationMultiplierEvent> args)
{
if (ent.Comp.IsEquipment)
args.Args.Multiplier *= GetFlashMultiplier(ent);
}
private float GetFlashMultiplier(TComp comp)
{
if (!comp.IsActive && (comp.PulseTime <= 0f || comp.PulseAccumulator >= comp.PulseTime))
return 1f;
return comp.FlashDurationMultiplier;
}
public override void FrameUpdate(float frameTime)
{
base.FrameUpdate(frameTime);
if (_net.IsClient)
ActiveTick(frameTime);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
if (_net.IsServer)
ActiveTick(frameTime);
}
private void ActiveTick(float frameTime)
{
var query = EntityQueryEnumerator<TComp>();
while (query.MoveNext(out var uid, out var comp))
{
if (comp.PulseTime <= 0f || comp.PulseAccumulator >= comp.PulseTime)
continue;
// The accumulator is for visually rendering the pulse strength decaying.
comp.PulseAccumulator += frameTime;
// This line is for the actual check that shuts off the pulse when its time is up.
if (comp.PulseAccumulator < comp.PulseTime)
continue;
Toggle(uid, comp, false, false);
RaiseSwitchableOverlayToggledEvent(uid, uid, comp.IsActive);
RaiseSwitchableOverlayToggledEvent(uid, Transform(uid).ParentUid, comp.IsActive);
}
}
private void OnGetState(EntityUid uid, TComp component, ref ComponentGetState args)
{
args.State = new SwitchableVisionOverlayComponentState
{
Color = component.Color,
IsActive = component.IsActive,
ActivateSound = component.ActivateSound,
DeactivateSound = component.DeactivateSound,
ToggleAction = component.ToggleAction,
LightRadius = component is ThermalVisionComponent thermal ? thermal.LightRadius : 0f,
};
}
private void OnHandleState(EntityUid uid, TComp component, ref ComponentHandleState args)
{
if (args.Current is not SwitchableVisionOverlayComponentState state)
return;
component.Color = state.Color;
component.ActivateSound = state.ActivateSound;
component.DeactivateSound = state.DeactivateSound;
if (component.ToggleAction != state.ToggleAction)
{
_actions.RemoveAction(uid, component.ToggleActionEntity);
component.ToggleAction = state.ToggleAction;
if (component.ToggleAction != null)
_actions.AddAction(uid, ref component.ToggleActionEntity, component.ToggleAction);
}
if (component is ThermalVisionComponent thermal)
thermal.LightRadius = state.LightRadius;
if (component.IsActive == state.IsActive)
return;
component.IsActive = state.IsActive;
RaiseSwitchableOverlayToggledEvent(uid,
component.IsEquipment ? Transform(uid).ParentUid : uid,
component.IsActive);
}
private void OnGetItemActions(Entity<TComp> ent, ref GetItemActionsEvent args)
{
if (ent.Comp.IsEquipment && ent.Comp.ToggleAction != null && args.SlotFlags is not SlotFlags.POCKET and not null)
args.AddAction(ref ent.Comp.ToggleActionEntity, ent.Comp.ToggleAction);
}
private void OnShutdown(EntityUid uid, TComp component, ComponentShutdown args)
{
if (component.IsEquipment)
return;
_actions.RemoveAction(uid, component.ToggleActionEntity);
}
private void OnInit(EntityUid uid, TComp component, ComponentInit args)
{
component.PulseAccumulator = component.PulseTime;
}
private void OnMapInit(EntityUid uid, TComp component, MapInitEvent args)
{
if (component is { IsEquipment: false, ToggleActionEntity: null, ToggleAction: not null })
_actions.AddAction(uid, ref component.ToggleActionEntity, component.ToggleAction);
}
private void OnToggle(EntityUid uid, TComp component, TEvent args)
{
Toggle(uid, component, !component.IsActive);
RaiseSwitchableOverlayToggledEvent(uid, args.Performer, component.IsActive);
args.Handled = true;
}
private void Toggle(EntityUid uid, TComp component, bool activate, bool playSound = true)
{
if (playSound && _net.IsClient && _timing.IsFirstTimePredicted)
{
_audio.PlayEntity(activate ? component.ActivateSound : component.DeactivateSound,
Filter.Local(),
uid,
false);
}
if (component.PulseTime > 0f)
{
component.PulseAccumulator = activate ? 0f : component.PulseTime;
return;
}
component.IsActive = activate;
Dirty(uid, component);
}
private void RaiseSwitchableOverlayToggledEvent(EntityUid uid, EntityUid user, bool activated)
{
var ev = new SwitchableOverlayToggledEvent(user, activated);
RaiseLocalEvent(uid, ref ev);
}
}
[ByRefEvent]
public record struct SwitchableOverlayToggledEvent(EntityUid User, bool Activated);