Files
OldThink/Content.Client/_White/Overlays/NightVisionSystem.cs

119 lines
3.9 KiB
C#
Raw Normal View History

using Content.Shared._Miracle.Systems;
2024-01-26 17:36:10 +07:00
using Content.Shared.GameTicking;
using Content.Shared._White.Overlays;
2024-01-26 17:36:10 +07:00
using Robust.Client.Graphics;
using Robust.Client.Player;
using Robust.Shared.Player;
namespace Content.Client._White.Overlays;
2024-01-26 17:36:10 +07:00
public sealed class NightVisionSystem : SharedEnhancedVisionSystem<NightVisionComponent, TemporaryNightVisionComponent,
ToggleNightVisionEvent>
2024-01-26 17:36:10 +07:00
{
[Dependency] private readonly IPlayerManager _player = default!;
[Dependency] private readonly IOverlayManager _overlayMan = default!;
[Dependency] private readonly ILightManager _lightManager = default!;
private NightVisionOverlay _overlay = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<NightVisionComponent, PlayerAttachedEvent>(OnPlayerAttached);
SubscribeLocalEvent<NightVisionComponent, PlayerDetachedEvent>(OnPlayerDetached);
SubscribeLocalEvent<RoundRestartCleanupEvent>(OnRestart);
SubscribeLocalEvent<TemporaryNightVisionComponent, ComponentInit>(OnTempInit);
SubscribeLocalEvent<TemporaryNightVisionComponent, ComponentRemove>(OnTempRemove);
SubscribeLocalEvent<TemporaryNightVisionComponent, PlayerAttachedEvent>(OnTempPlayerAttached);
SubscribeLocalEvent<TemporaryNightVisionComponent, PlayerDetachedEvent>(OnTempPlayerDetached);
2024-01-26 17:36:10 +07:00
_overlay = new NightVisionOverlay();
}
private void OnTempPlayerAttached(Entity<TemporaryNightVisionComponent> ent, ref PlayerAttachedEvent args)
{
UpdateNightVision(args.Player, true);
}
private void OnTempPlayerDetached(Entity<TemporaryNightVisionComponent> ent, ref PlayerDetachedEvent args)
{
UpdateNightVision(args.Player, false);
}
private void OnTempRemove(Entity<TemporaryNightVisionComponent> ent, ref ComponentRemove args)
{
if (TryComp(ent, out NightVisionComponent? nightVision) && nightVision.IsActive)
return;
UpdateEnhancedVision(ent, false);
}
private void OnTempInit(Entity<TemporaryNightVisionComponent> ent, ref ComponentInit args)
{
UpdateEnhancedVision(ent, true);
}
2024-01-26 17:36:10 +07:00
private void OnPlayerAttached(EntityUid uid, NightVisionComponent component, PlayerAttachedEvent args)
{
if (!component.IsActive && HasComp<TemporaryNightVisionComponent>(args.Entity))
2024-02-07 19:30:49 +09:00
return;
UpdateNightVision(args.Player, component.IsActive);
2024-01-26 17:36:10 +07:00
}
private void OnPlayerDetached(EntityUid uid, NightVisionComponent component, PlayerDetachedEvent args)
{
UpdateNightVision(args.Player, false);
}
private void UpdateNightVision(ICommonSession player, bool active)
{
if (_player.LocalSession != player)
2024-02-07 19:30:49 +09:00
return;
UpdateOverlay(active);
UpdateNightVision(active);
2024-01-26 17:36:10 +07:00
}
protected override void UpdateEnhancedVision(EntityUid uid, bool active)
2024-01-26 17:36:10 +07:00
{
if (_player.LocalSession?.AttachedEntity != uid)
return;
UpdateOverlay(active);
2024-02-07 19:30:49 +09:00
UpdateNightVision(active);
}
public void UpdateOverlay(bool active)
2024-02-07 19:30:49 +09:00
{
if (_player.LocalEntity == null)
{
_overlayMan.RemoveOverlay(_overlay);
return;
}
var uid = _player.LocalEntity.Value;
active |= TryComp<NightVisionComponent>(uid, out var nv) && nv.IsActive ||
TryComp<ThermalVisionComponent>(uid, out var thermal) && thermal.IsActive ||
HasComp<TemporaryNightVisionComponent>(uid) ||
HasComp<TemporaryThermalVisionComponent>(uid);
if (active)
_overlayMan.AddOverlay(_overlay);
else
_overlayMan.RemoveOverlay(_overlay);
}
2024-01-26 17:36:10 +07:00
private void UpdateNightVision(bool active)
{
_lightManager.DrawLighting = !active;
2024-01-26 17:36:10 +07:00
}
private void OnRestart(RoundRestartCleanupEvent ev)
{
_overlayMan.RemoveOverlay(_overlay);
_lightManager.DrawLighting = true;
}
}