Optimise handheld lights (#2927)

* Optimise handheld lights

* Woops also these

* Might as well do shutdown too

* Also these

Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
This commit is contained in:
metalgearsloth
2021-01-06 22:50:49 +11:00
committed by GitHub
parent 2bd4bf12a4
commit 482106e827
2 changed files with 62 additions and 2 deletions

View File

@@ -80,6 +80,12 @@ namespace Content.Server.GameObjects.Components.Interactable
Dirty(); Dirty();
} }
public override void OnRemove()
{
base.OnRemove();
Owner.EntityManager.EventBus.QueueEvent(EventSource.Local, new DeactivateHandheldLightMessage(this));
}
async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs) async Task<bool> IInteractUsing.InteractUsing(InteractUsingEventArgs eventArgs)
{ {
if (!ActionBlockerSystem.CanInteract(eventArgs.User)) return false; if (!ActionBlockerSystem.CanInteract(eventArgs.User)) return false;
@@ -125,6 +131,7 @@ namespace Content.Server.GameObjects.Components.Interactable
SetState(false); SetState(false);
Activated = false; Activated = false;
UpdateLightAction(); UpdateLightAction();
Owner.EntityManager.EventBus.QueueEvent(EventSource.Local, new DeactivateHandheldLightMessage(this));
if (makeNoise) if (makeNoise)
{ {
@@ -163,6 +170,7 @@ namespace Content.Server.GameObjects.Components.Interactable
Activated = true; Activated = true;
UpdateLightAction(); UpdateLightAction();
SetState(true); SetState(true);
Owner.EntityManager.EventBus.QueueEvent(EventSource.Local, new ActivateHandheldLightMessage(this));
if (TurnOnSound != null) EntitySystem.Get<AudioSystem>().PlayFromEntity(TurnOnSound, Owner); if (TurnOnSound != null) EntitySystem.Get<AudioSystem>().PlayFromEntity(TurnOnSound, Owner);
return true; return true;
@@ -283,4 +291,24 @@ namespace Content.Server.GameObjects.Components.Interactable
return lightComponent.ToggleStatus(args.Performer); 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;
}
}
} }

View File

@@ -1,3 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Content.Server.GameObjects.Components.Interactable; using Content.Server.GameObjects.Components.Interactable;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Shared.GameObjects.Systems; using Robust.Shared.GameObjects.Systems;
@@ -7,11 +9,41 @@ namespace Content.Server.GameObjects.EntitySystems
[UsedImplicitly] [UsedImplicitly]
internal sealed class HandHeldLightSystem : EntitySystem 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<HandheldLightComponent> _activeLights = new();
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ActivateHandheldLightMessage>(HandleActivate);
SubscribeLocalEvent<DeactivateHandheldLightMessage>(HandleDeactivate);
}
public override void Shutdown()
{
base.Shutdown();
_activeLights.Clear();
UnsubscribeLocalEvent<ActivateHandheldLightMessage>();
UnsubscribeLocalEvent<DeactivateHandheldLightMessage>();
}
private void HandleActivate(ActivateHandheldLightMessage message)
{
_activeLights.Add(message.Component);
}
private void HandleDeactivate(DeactivateHandheldLightMessage message)
{
_activeLights.Remove(message.Component);
}
public override void Update(float frameTime) public override void Update(float frameTime)
{ {
foreach (var comp in ComponentManager.EntityQuery<HandheldLightComponent>(false)) foreach (var handheld in _activeLights.ToArray())
{ {
comp.OnUpdate(frameTime); if (handheld.Deleted || handheld.Paused) continue;
handheld.OnUpdate(frameTime);
} }
} }
} }