CatStation/Content.Shared/NightVision/SharedNightVisionSystem.cs
KeTuFaisPiKiNut a087deac1f
Changeling Night Vision (#44577)
* cling

* eqwewe

* resprite 2

* another resprite

* Revert "another resprite"

This reverts commit fe61cf99835b59620c94720c1ca9e9b16e2e52c6.

* cleanup

- Made overlay more red
- Made action part of the NightVisionComponent
- Default UseDelay
- Less flavorful store listing (bc its the store)
- Removed unintended changes

* Update changeling.yml

* fix test

---------

Co-authored-by: nomdéraisonnablementlong <you@example.com>
Co-authored-by: ScarKy0 <scarky0@onet.eu>
2026-07-10 02:09:14 +00:00

109 lines
3.3 KiB
C#

using Content.Shared.Actions;
using Content.Shared.Inventory;
using Content.Shared.Inventory.Events;
using Content.Shared.Overlays;
namespace Content.Shared.NightVision;
/// <summary>
/// Shows/hides the <see cref="NightVisionOverlay"/> based on whether the observed
/// entity has a <see cref="NightVisionComponent"/> equipped.
/// </summary>
public abstract partial class SharedNightVisionSystem : EntitySystem
{
[Dependency] private SharedActionsSystem _actions = default!;
[SubscribeLocalEvent]
private void OnStartup(Entity<NightVisionComponent> ent, ref MapInitEvent args)
{
if (ent.Comp.RelayOverlay)
return;
RefreshOverlay(ent);
_actions.AddAction(ent, ref ent.Comp.ActionEntity, ent.Comp.Action);
}
[SubscribeLocalEvent]
private void OnRemove(Entity<NightVisionComponent> ent, ref ComponentShutdown args)
{
if (ent.Comp.RelayOverlay)
return;
RefreshOverlay(ent);
_actions.RemoveAction(ent.Owner, ent.Comp.ActionEntity);
}
[SubscribeLocalEvent]
private void OnCompEquip(Entity<NightVisionComponent> ent, ref GotEquippedEvent args)
{
if (!ent.Comp.RelayOverlay)
return;
RefreshOverlay(args.EquipTarget);
_actions.AddAction(args.EquipTarget, ref ent.Comp.ActionEntity, ent.Comp.Action, ent);
}
[SubscribeLocalEvent]
private void OnCompUnequip(Entity<NightVisionComponent> ent, ref GotUnequippedEvent args)
{
if (!ent.Comp.RelayOverlay)
return;
RefreshOverlay(args.EquipTarget);
}
[SubscribeLocalEvent]
protected virtual void OnRefreshEquipmentHud(Entity<NightVisionComponent> ent, ref InventoryRelayedEvent<RefreshNightVisionEvent> args)
{
OnRefreshComponentHud(ent, ref args.Args);
}
[SubscribeLocalEvent]
protected virtual void OnRefreshComponentHud(Entity<NightVisionComponent> ent, ref RefreshNightVisionEvent args)
{
if (!ent.Comp.Enabled)
return;
args.Entities.Add(ent);
}
[SubscribeLocalEvent]
private void OnToggleNightVisionEvent(ToggleNightVisionEvent args)
{
var ent = args.Action.Comp.Container;
if (!TryComp<NightVisionComponent>(ent, out var nightVisionComp))
return;
SetEnabled(ent.Value, !nightVisionComp.Enabled, args.Performer);
args.Handled = true;
}
/// <summary>
/// Enables or disables the component.
/// </summary>
/// <param name="ent">The night vision to toggle.</param>
/// <param name="enabled">Whether to enable or disable.</param>
/// <param name="viewer">Viewer of the night vision, used to refresh their overlay. If null, assumes the night vision entity is the viewer.</param>
public void SetEnabled(Entity<NightVisionComponent?> ent, bool enabled, EntityUid? viewer = null)
{
if (!Resolve(ent, ref ent.Comp, false))
return;
if (ent.Comp.Enabled == enabled)
return;
ent.Comp.Enabled = enabled;
Dirty(ent);
RefreshOverlay(viewer ?? ent);
}
protected virtual void RefreshOverlay(EntityUid entity) { }
}
[ByRefEvent]
public record struct RefreshNightVisionEvent() : IInventoryRelayEvent
{
public SlotFlags TargetSlots => SlotFlags.WITHOUT_POCKET;
public List<Entity<NightVisionComponent>> Entities = new();
}