From 8e2c75c7b6c9fd0b6bc2656062d86a14fd87cc8f Mon Sep 17 00:00:00 2001 From: metalgearsloth Date: Sat, 24 Aug 2024 12:31:34 +1000 Subject: [PATCH] Fix contacts --- .../Systems/SharedPhysicsSystem.Contacts.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Contacts.cs b/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Contacts.cs index 810388a039d..f7cee57fa47 100644 --- a/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Contacts.cs +++ b/Robust.Shared/Physics/Systems/SharedPhysicsSystem.Contacts.cs @@ -31,6 +31,7 @@ using System.Buffers; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Diagnostics.CodeAnalysis; using System.Numerics; using JetBrains.Annotations; using Microsoft.Extensions.ObjectPool; @@ -791,27 +792,32 @@ public int GetTouchingContacts(Entity entity, string? ignore [Pure] public ContactEnumerator GetContacts(Entity entity) { - if (!_fixturesQuery.Resolve(entity.Owner, ref entity.Comp)) - return ContactEnumerator.Empty; - + _fixturesQuery.Resolve(entity.Owner, ref entity.Comp); return new ContactEnumerator(entity.Comp); } } public record struct ContactEnumerator { - public static readonly ContactEnumerator Empty = new(); + public static readonly ContactEnumerator Empty = new(null); private Dictionary.ValueCollection.Enumerator _fixtureEnumerator; private Dictionary.ValueCollection.Enumerator _contactEnumerator; - public ContactEnumerator(FixturesComponent fixtures) + public ContactEnumerator(FixturesComponent? fixtures) { + if (fixtures == null || fixtures.Fixtures.Count == 0) + { + this = Empty; + return; + } + _fixtureEnumerator = fixtures.Fixtures.Values.GetEnumerator(); + _fixtureEnumerator.MoveNext(); _contactEnumerator = _fixtureEnumerator.Current.Contacts.Values.GetEnumerator(); } - public bool MoveNext(out Contact? contact) + public bool MoveNext([NotNullWhen(true)] out Contact? contact) { if (!_contactEnumerator.MoveNext()) { @@ -822,6 +828,7 @@ public bool MoveNext(out Contact? contact) } _contactEnumerator = _fixtureEnumerator.Current.Contacts.Values.GetEnumerator(); + return MoveNext(out contact); } contact = _contactEnumerator.Current;