Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor contact QOL #5560

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Robust.Shared/Physics/Dynamics/Contacts/Contact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ internal Contact(IManifoldManager manifoldManager)
/// </summary>
public float TangentSpeed { get; set; }

[ViewVariables]
public bool Deleting => (Flags & ContactFlags.Deleting) == ContactFlags.Deleting;

/// <summary>
/// If either fixture is hard then it's a hard contact.
/// </summary>
public bool Hard => FixtureA != null && FixtureB != null && (FixtureA.Hard && FixtureB.Hard);

public void ResetRestitution()
{
Restitution = MathF.Max(FixtureA?.Restitution ?? 0.0f, FixtureB?.Restitution ?? 0.0f);
Expand Down Expand Up @@ -353,6 +361,16 @@ public override int GetHashCode()
return HashCode.Combine(EntityA, EntityB);
}

public EntityUid OurEnt(EntityUid uid)
{
if (uid == EntityA)
return EntityA;
else if (uid == EntityB)
return EntityB;

throw new InvalidOperationException();
}

/// <summary>
/// Gets the other ent for this contact.
/// </summary>
Expand All @@ -366,6 +384,16 @@ public EntityUid OtherEnt(EntityUid uid)
throw new InvalidOperationException();
}

public (string Id, Fixture) OurFixture(EntityUid uid)
{
if (uid == EntityA)
return (FixtureAId, FixtureA!);
else if (uid == EntityB)
return (FixtureBId, FixtureB!);

throw new InvalidOperationException();
}

public (string Id, Fixture) OtherFixture(EntityUid uid)
{
if (uid == EntityA)
Expand Down
18 changes: 15 additions & 3 deletions Robust.Shared/Physics/Systems/SharedPhysicsSystem.Contacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -790,10 +790,10 @@ public int GetTouchingContacts(Entity<FixturesComponent?> entity, string? ignore
/// Returns all of this entity's contacts.
/// </summary>
[Pure]
public ContactEnumerator GetContacts(Entity<FixturesComponent?> entity)
public ContactEnumerator GetContacts(Entity<FixturesComponent?> entity, bool includeDeleting = false)
{
_fixturesQuery.Resolve(entity.Owner, ref entity.Comp);
return new ContactEnumerator(entity.Comp);
return new ContactEnumerator(entity.Comp, includeDeleting);
}
}

Expand All @@ -804,8 +804,16 @@ public record struct ContactEnumerator
private Dictionary<string, Fixture>.ValueCollection.Enumerator _fixtureEnumerator;
private Dictionary<Fixture, Contact>.ValueCollection.Enumerator _contactEnumerator;

public ContactEnumerator(FixturesComponent? fixtures)
/// <summary>
/// Also include deleting contacts.
/// This typically includes the current contact if you're invoking this in the eventbus for an EndCollideEvent.
/// </summary>
public bool IncludeDeleting;

public ContactEnumerator(FixturesComponent? fixtures, bool includeDeleting = false)
{
IncludeDeleting = includeDeleting;

if (fixtures == null || fixtures.Fixtures.Count == 0)
{
this = Empty;
Expand All @@ -832,6 +840,10 @@ public bool MoveNext([NotNullWhen(true)] out Contact? contact)
}

contact = _contactEnumerator.Current;

if (!IncludeDeleting && contact.Deleting)
return MoveNext(out contact);

return true;
}
}
Expand Down
Loading