From 55565ea22a40b5d0613a99f2d4529da79baac249 Mon Sep 17 00:00:00 2001 From: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com> Date: Sun, 29 May 2022 11:31:43 +1200 Subject: [PATCH] step trigger fix + optimization (#8503) --- .../StepTrigger/StepTriggerComponent.cs | 12 +++-- .../StepTrigger/StepTriggerSystem.cs | 46 +++++++++---------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/Content.Shared/StepTrigger/StepTriggerComponent.cs b/Content.Shared/StepTrigger/StepTriggerComponent.cs index c54e77e786..76f16b48a7 100644 --- a/Content.Shared/StepTrigger/StepTriggerComponent.cs +++ b/Content.Shared/StepTrigger/StepTriggerComponent.cs @@ -1,4 +1,4 @@ -using Robust.Shared.GameStates; +using Robust.Shared.GameStates; using Robust.Shared.Serialization; namespace Content.Shared.StepTrigger; @@ -11,12 +11,14 @@ public sealed class StepTriggerComponent : Component /// /// List of entities that are currently colliding with the entity. /// + [ViewVariables] public readonly HashSet Colliding = new(); /// /// The list of entities that are standing on this entity, /// which shouldn't be able to trigger it again until stepping off. /// + [ViewVariables] public readonly HashSet CurrentlySteppedOn = new(); /// @@ -50,13 +52,17 @@ public sealed class StepTriggerComponentState : ComponentState { public float IntersectRatio { get; } public float RequiredTriggerSpeed { get; } - public readonly EntityUid[] CurrentlySteppedOn; + public readonly HashSet CurrentlySteppedOn; + public readonly HashSet Colliding; + public readonly bool Active; - public StepTriggerComponentState(float intersectRatio, EntityUid[] currentlySteppedOn, float requiredTriggerSpeed) + public StepTriggerComponentState(float intersectRatio, HashSet currentlySteppedOn, HashSet colliding, float requiredTriggerSpeed, bool active) { IntersectRatio = intersectRatio; CurrentlySteppedOn = currentlySteppedOn; RequiredTriggerSpeed = requiredTriggerSpeed; + Active = active; + Colliding = colliding; } } diff --git a/Content.Shared/StepTrigger/StepTriggerSystem.cs b/Content.Shared/StepTrigger/StepTriggerSystem.cs index 2c242f0a58..33b18585bc 100644 --- a/Content.Shared/StepTrigger/StepTriggerSystem.cs +++ b/Content.Shared/StepTrigger/StepTriggerSystem.cs @@ -1,4 +1,4 @@ -using System.Linq; +using System.Linq; using Robust.Shared.GameStates; using Robust.Shared.Physics.Dynamics; @@ -18,25 +18,25 @@ public sealed class StepTriggerSystem : EntitySystem public override void Update(float frameTime) { - foreach (var (_, trigger, transform) in EntityQuery()) + var query = GetEntityQuery(); + foreach (var (active, trigger, transform) in EntityQuery()) { - if (!Update(trigger, transform)) + if (!Update(trigger, transform, query)) continue; - RemComp(trigger.Owner); + RemComp(trigger.Owner, active); } } - private bool Update(StepTriggerComponent component, TransformComponent transform) + private bool Update(StepTriggerComponent component, TransformComponent transform, EntityQuery query) { if (!component.Active || component.Colliding.Count == 0) return true; - foreach (var otherUid in component.Colliding.ToArray()) { - var shouldRemoveFromColliding = UpdateColliding(component, transform, otherUid); + var shouldRemoveFromColliding = UpdateColliding(component, transform, otherUid, query); if (!shouldRemoveFromColliding) continue; component.Colliding.Remove(otherUid); @@ -46,16 +46,11 @@ public sealed class StepTriggerSystem : EntitySystem return false; } - private bool UpdateColliding(StepTriggerComponent component, TransformComponent ownerTransform, EntityUid otherUid) + private bool UpdateColliding(StepTriggerComponent component, TransformComponent ownerTransform, EntityUid otherUid, EntityQuery query) { - if (!TryComp(otherUid, out PhysicsComponent? otherPhysics)) + if (!query.TryGetComponent(otherUid, out var otherPhysics)) return true; - if (component.CurrentlySteppedOn.Contains(otherUid) || - !CanTrigger(component.Owner, otherUid, component) || - otherPhysics.LinearVelocity.Length < component.RequiredTriggerSpeed) - return false; - // TODO: This shouldn't be calculating based on world AABBs. var ourAabb = _entityLookup.GetWorldAABB(component.Owner, ownerTransform); var otherAabb = _entityLookup.GetWorldAABB(otherUid); @@ -63,8 +58,10 @@ public sealed class StepTriggerSystem : EntitySystem if (!ourAabb.Intersects(otherAabb)) return true; - var percentage = otherAabb.IntersectPercentage(ourAabb); - if (percentage < component.IntersectRatio) + if (otherPhysics.LinearVelocity.Length < component.RequiredTriggerSpeed + || component.CurrentlySteppedOn.Contains(otherUid) + || otherAabb.IntersectPercentage(ourAabb) < component.IntersectRatio + || !CanTrigger(component.Owner, otherUid, component)) return false; var ev = new StepTriggeredEvent { Source = component.Owner, Tripper = otherUid }; @@ -106,20 +103,23 @@ public sealed class StepTriggerSystem : EntitySystem component.RequiredTriggerSpeed = state.RequiredTriggerSpeed; component.IntersectRatio = state.IntersectRatio; - component.CurrentlySteppedOn.Clear(); + component.Active = state.Active; - foreach (var slipped in state.CurrentlySteppedOn) - { - component.CurrentlySteppedOn.Add(slipped); - } + component.CurrentlySteppedOn.Clear(); + component.Colliding.Clear(); + + component.CurrentlySteppedOn.UnionWith(state.CurrentlySteppedOn); + component.Colliding.UnionWith(state.Colliding); } private static void TriggerGetState(EntityUid uid, StepTriggerComponent component, ref ComponentGetState args) { args.State = new StepTriggerComponentState( component.IntersectRatio, - component.CurrentlySteppedOn.ToArray(), - component.RequiredTriggerSpeed); + component.CurrentlySteppedOn, + component.Colliding, + component.RequiredTriggerSpeed, + component.Active); } public void SetIntersectRatio(EntityUid uid, float ratio, StepTriggerComponent? component = null)