diff --git a/Content.Server/GameObjects/Components/Interactable/HandheldLightComponent.cs b/Content.Server/GameObjects/Components/Interactable/HandheldLightComponent.cs index 430d0e785d..7c3862ccd5 100644 --- a/Content.Server/GameObjects/Components/Interactable/HandheldLightComponent.cs +++ b/Content.Server/GameObjects/Components/Interactable/HandheldLightComponent.cs @@ -80,6 +80,12 @@ namespace Content.Server.GameObjects.Components.Interactable Dirty(); } + public override void OnRemove() + { + base.OnRemove(); + Owner.EntityManager.EventBus.QueueEvent(EventSource.Local, new DeactivateHandheldLightMessage(this)); + } + async Task IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs) { if (!ActionBlockerSystem.CanInteract(eventArgs.User)) return false; @@ -125,6 +131,7 @@ namespace Content.Server.GameObjects.Components.Interactable SetState(false); Activated = false; UpdateLightAction(); + Owner.EntityManager.EventBus.QueueEvent(EventSource.Local, new DeactivateHandheldLightMessage(this)); if (makeNoise) { @@ -163,6 +170,7 @@ namespace Content.Server.GameObjects.Components.Interactable Activated = true; UpdateLightAction(); SetState(true); + Owner.EntityManager.EventBus.QueueEvent(EventSource.Local, new ActivateHandheldLightMessage(this)); if (TurnOnSound != null) EntitySystem.Get().PlayFromEntity(TurnOnSound, Owner); return true; @@ -283,4 +291,24 @@ namespace Content.Server.GameObjects.Components.Interactable return lightComponent.ToggleStatus(args.Performer); } } + + internal sealed class ActivateHandheldLightMessage : EntitySystemMessage + { + public HandheldLightComponent Component { get; } + + public ActivateHandheldLightMessage(HandheldLightComponent component) + { + Component = component; + } + } + + internal sealed class DeactivateHandheldLightMessage : EntitySystemMessage + { + public HandheldLightComponent Component { get; } + + public DeactivateHandheldLightMessage(HandheldLightComponent component) + { + Component = component; + } + } } diff --git a/Content.Server/GameObjects/EntitySystems/HandHeldLightSystem.cs b/Content.Server/GameObjects/EntitySystems/HandHeldLightSystem.cs index 3947f882a7..6ccfa9d180 100644 --- a/Content.Server/GameObjects/EntitySystems/HandHeldLightSystem.cs +++ b/Content.Server/GameObjects/EntitySystems/HandHeldLightSystem.cs @@ -1,3 +1,5 @@ +using System.Collections.Generic; +using System.Linq; using Content.Server.GameObjects.Components.Interactable; using JetBrains.Annotations; using Robust.Shared.GameObjects.Systems; @@ -7,11 +9,41 @@ namespace Content.Server.GameObjects.EntitySystems [UsedImplicitly] internal sealed class HandHeldLightSystem : EntitySystem { + // TODO: Ideally you'd be able to subscribe to power stuff to get events at certain percentages.. or something? + // But for now this will be better anyway. + private HashSet _activeLights = new(); + + public override void Initialize() + { + base.Initialize(); + SubscribeLocalEvent(HandleActivate); + SubscribeLocalEvent(HandleDeactivate); + } + + public override void Shutdown() + { + base.Shutdown(); + _activeLights.Clear(); + UnsubscribeLocalEvent(); + UnsubscribeLocalEvent(); + } + + private void HandleActivate(ActivateHandheldLightMessage message) + { + _activeLights.Add(message.Component); + } + + private void HandleDeactivate(DeactivateHandheldLightMessage message) + { + _activeLights.Remove(message.Component); + } + public override void Update(float frameTime) { - foreach (var comp in ComponentManager.EntityQuery(false)) + foreach (var handheld in _activeLights.ToArray()) { - comp.OnUpdate(frameTime); + if (handheld.Deleted || handheld.Paused) continue; + handheld.OnUpdate(frameTime); } } }