Files
tbd-station-14/Content.Client/Flash/FlashSystem.cs
Vera Aguilera Puerto 77fe21eb0d Refactors flashes to ECS, sunglasses protect you from being flashed. (#4579)
* Refactors flashes to ECS, sunglasses protect you from being flashed.

* VV ReadWrite for FlashImmunity Enabled.

* Use cached IEntityLookup.

* Consistent formatting.

* Fix flashbang duration.
Flash duration is a mess.

* Small area flash code cleanup.

* Flashable state is only sent to attached player.
2021-09-10 00:20:41 +10:00

65 lines
2.1 KiB
C#

using Content.Shared.Flash;
using Robust.Client.Graphics;
using Robust.Client.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using Robust.Shared.Timing;
namespace Content.Client.Flash
{
public class FlashSystem : SharedFlashSystem
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IOverlayManager _overlayManager = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<FlashableComponent, ComponentHandleState>(OnFlashableHandleState);
}
private void OnFlashableHandleState(EntityUid uid, FlashableComponent component, ref ComponentHandleState args)
{
if (args.Current is not FlashableComponentState state)
return;
// Yes, this code is awful. I'm just porting it to an entity system so don't blame me.
if (_playerManager.LocalPlayer != null && _playerManager.LocalPlayer.Session.AttachedEntityUid != uid)
{
return;
}
if (state.Time == default)
{
return;
}
// Few things here:
// 1. If a shorter duration flash is applied then don't do anything
// 2. If the client-side time is later than when the flash should've ended don't do anything
var currentTime = _gameTiming.CurTime.TotalSeconds;
var newEndTime = state.Time.TotalSeconds + state.Duration;
var currentEndTime = component.LastFlash.TotalSeconds + component.Duration;
if (currentEndTime > newEndTime)
{
return;
}
if (currentTime > newEndTime)
{
return;
}
component.LastFlash = state.Time;
component.Duration = state.Duration;
var overlay = _overlayManager.GetOverlay<FlashOverlay>();
overlay.ReceiveFlash(component.Duration);
}
}
}