Make doors event-based rather than EntityQuery based (#2195)

* Event-based door updates

Slight perf increase

* Optimise CheckCrush too

Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
This commit is contained in:
metalgearsloth
2020-10-10 22:33:56 +11:00
committed by GitHub
parent fe39b5fb71
commit 7bf28ff3d5
2 changed files with 64 additions and 30 deletions

View File

@@ -1,6 +1,9 @@
using Content.Server.GameObjects.Components.Doors;
using System;
using System.Collections.Generic;
using Content.Server.GameObjects.Components.Doors;
using JetBrains.Annotations;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Server.GameObjects.EntitySystems
{
@@ -31,19 +34,44 @@ namespace Content.Server.GameObjects.EntitySystems
/// <summary> Allows everyone to open all doors. </summary>
AllowAll
}
private List<ServerDoorComponent> _activeDoors = new List<ServerDoorComponent>();
public override void Initialize()
{
base.Initialize();
AccessType = AccessTypes.Id;
SubscribeLocalEvent<DoorStateMessage>(HandleDoorState);
}
private void HandleDoorState(DoorStateMessage message)
{
switch (message.State)
{
case ServerDoorComponent.DoorState.Closed:
_activeDoors.Remove(message.Component);
break;
case ServerDoorComponent.DoorState.Open:
_activeDoors.Add(message.Component);
break;
case ServerDoorComponent.DoorState.Closing:
case ServerDoorComponent.DoorState.Opening:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
/// <inheritdoc />
public override void Update(float frameTime)
{
foreach (var comp in ComponentManager.EntityQuery<ServerDoorComponent>())
for (var i = _activeDoors.Count - 1; i >= 0; i--)
{
var comp = _activeDoors[i];
if (comp.Deleted)
_activeDoors.RemoveAt(i);
comp.OnUpdate(frameTime);
}
}