Skip to content

Commit

Permalink
Photocopier: improved stability of shape collision tracker system
Browse files Browse the repository at this point in the history
  • Loading branch information
TheArturZh committed Jun 27, 2023
1 parent dbf82f7 commit 52f3883
Showing 1 changed file with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public sealed class ShapeCollisionTrackerSystem : EntitySystem
[Dependency] private readonly SharedBroadphaseSystem _broadphase = default!;
[Dependency] private readonly FixtureSystem _fixtures = default!;

private readonly HashSet<(EntityUid, ShapeCollisionTrackerComponent)> _trackerDebounce = new();

/// <inheritdoc/>
public override void Initialize()
{
Expand All @@ -32,6 +34,7 @@ private void OnAnchor(

if (!component.Enabled)
{
_trackerDebounce.Remove((uid, component));
component.Colliding.Clear();
}
// Re-check for contacts as we cleared them.
Expand All @@ -50,7 +53,7 @@ private void OnStartCollide(
return;

component.Colliding.Add(args.OtherEntity);
RaiseLocalEvent(uid, new ShapeCollisionTrackerUpdatedEvent(component.Colliding.ToImmutableHashSet()));
_trackerDebounce.Add((uid, component));
}

private void OnEndCollide(
Expand All @@ -62,7 +65,7 @@ private void OnEndCollide(
return;

component.Colliding.Remove(args.OtherEntity);
RaiseLocalEvent(uid, new ShapeCollisionTrackerUpdatedEvent(component.Colliding.ToImmutableHashSet()));
_trackerDebounce.Add((uid, component));
}

private void OnMapInit(
Expand All @@ -84,11 +87,36 @@ private void OnMapInit(
collisionLayer: (int) (CollisionGroup.MidImpassable | CollisionGroup.LowImpassable | CollisionGroup.HighImpassable));
}

private static void OnShutdown(
private void OnShutdown(
EntityUid uid,
ShapeCollisionTrackerComponent component,
ComponentShutdown args)
{
component.Colliding.Clear();
_trackerDebounce.Remove((uid, component));
}

public override void Update(float frameTime)
{
base.Update(frameTime);

foreach (var trackerPair in _trackerDebounce)
{
RaiseLocalEvent(trackerPair.Item1, new ShapeCollisionTrackerUpdatedEvent(trackerPair.Item2.Colliding.ToImmutableHashSet()));
}

_trackerDebounce.Clear();
}

public override void Shutdown()
{
_trackerDebounce.Clear();
var trackerQuery = EntityQuery<ShapeCollisionTrackerComponent>(true);
foreach (var trackerEntity in trackerQuery)
{
trackerEntity.Colliding.Clear();
}

base.Shutdown();
}
}

0 comments on commit 52f3883

Please sign in to comment.