Add EntityUid fields to some physics events (#4103)

This commit is contained in:
Leon Friedrich
2023-06-03 00:16:37 +12:00
committed by GitHub
parent 46ae16718e
commit d95c1aef05
5 changed files with 73 additions and 38 deletions

View File

@@ -79,7 +79,7 @@ namespace Robust.Shared.GameObjects
private void OnJointRemove(EntityUid uid, CollisionWakeComponent component, JointRemovedEvent args)
{
UpdateCanCollide(uid, component, (PhysicsComponent) args.OurBody);
UpdateCanCollide(uid, component, args.OurBody);
}
private void OnJointAdd(EntityUid uid, CollisionWakeComponent component, JointAddedEvent args)

View File

@@ -11,35 +11,52 @@ namespace Robust.Shared.Physics.Events;
public struct PreventCollideEvent
{
/// <summary>
/// One of the bodies to prevent the collision of.
/// The entity that this event was directed at. Owner of <see cref="OurBody"/>
/// </summary>
public PhysicsComponent BodyA;
public readonly EntityUid OurEntity;
/// <summary>
/// The other body to prevent the collision of.
/// The other colliding entity. Owner of <see cref="OtherBody"/>
/// </summary>
public PhysicsComponent BodyB;
public readonly EntityUid OtherEntity;
/// <summary>
/// The body of the entity that this event was directed at.
/// </summary>
public readonly PhysicsComponent OurBody;
/// <summary>
/// The other body..
/// </summary>
public readonly PhysicsComponent OtherBody;
/// <summary>
/// The fixture on the first body to prevent the collision of if specified.
/// </summary>
public Fixture FixtureA;
public readonly Fixture OurFixture;
/// <summary>
/// The fixture on the other body to prevent the collision of if specified.
/// </summary>
public Fixture FixtureB;
public readonly Fixture OtherFixture;
/// <summary>
/// Whether or not to prevent the collision between the physics bodies.
/// </summary>
public bool Cancelled = false;
public PreventCollideEvent(PhysicsComponent ourBody, PhysicsComponent otherBody, Fixture ourFixture, Fixture otherFixture)
public PreventCollideEvent(
EntityUid ourEntity,
EntityUid otherEntity,
PhysicsComponent ourBody,
PhysicsComponent otherBody,
Fixture ourFixture,
Fixture otherFixture)
{
BodyA = ourBody;
BodyB = otherBody;
FixtureA = ourFixture;
FixtureB = otherFixture;
OurEntity = ourEntity;
OtherEntity = otherEntity;
OurBody = ourBody;
OtherBody = otherBody;
OurFixture = ourFixture;
OtherFixture = otherFixture;
}
}

View File

@@ -6,15 +6,26 @@ namespace Robust.Shared.Physics;
public sealed class JointAddedEvent : EntityEventArgs
{
public readonly EntityUid OurEntity;
public readonly EntityUid OtherEntity;
public PhysicsComponent OurBody { get; }
public PhysicsComponent OtherBody { get; }
public Joint Joint { get; }
public JointAddedEvent(Joint joint, PhysicsComponent ourBody, PhysicsComponent otherBody)
public JointAddedEvent(
Joint joint,
EntityUid ourEntity,
EntityUid otherEntity,
PhysicsComponent ourBody,
PhysicsComponent otherBody)
{
Joint = joint;
OurEntity = ourEntity;
OtherEntity = otherEntity;
OurBody = ourBody;
OtherBody = otherBody;
}
@@ -22,15 +33,26 @@ public sealed class JointAddedEvent : EntityEventArgs
public sealed class JointRemovedEvent : EntityEventArgs
{
public readonly EntityUid OurEntity;
public readonly EntityUid OtherEntity;
public PhysicsComponent OurBody { get; }
public PhysicsComponent OtherBody { get; }
public Joint Joint { get; }
public JointRemovedEvent(Joint joint, PhysicsComponent ourBody, PhysicsComponent otherBody)
public JointRemovedEvent(
Joint joint,
EntityUid ourEntity,
EntityUid otherEntity,
PhysicsComponent ourBody,
PhysicsComponent otherBody)
{
Joint = joint;
OurEntity = ourEntity;
OtherEntity = otherEntity;
OurBody = ourBody;
OtherBody = otherBody;
}

View File

@@ -71,10 +71,10 @@ public abstract partial class SharedJointSystem : EntitySystem
_physics.WakeBody(joint.BodyBUid, body: bodyB);
// Raise broadcast last so we can do both sides of directed first.
var vera = new JointAddedEvent(joint, bodyA, bodyB);
RaiseLocalEvent(bodyA.Owner, vera);
var smug = new JointAddedEvent(joint, bodyB, bodyA);
RaiseLocalEvent(bodyB.Owner, smug);
var vera = new JointAddedEvent(joint, joint.BodyAUid, joint.BodyBUid, bodyA, bodyB);
RaiseLocalEvent(joint.BodyAUid, vera);
var smug = new JointAddedEvent(joint, joint.BodyBUid, joint.BodyAUid, bodyB, bodyA);
RaiseLocalEvent(joint.BodyBUid, smug);
EntityManager.EventBus.RaiseEvent(EventSource.Local, vera);
}
@@ -198,9 +198,9 @@ public abstract partial class SharedJointSystem : EntitySystem
// Note: creating a joint doesn't wake the bodies.
// Raise broadcast last so we can do both sides of directed first.
var vera = new JointAddedEvent(joint, bodyA, bodyB);
var vera = new JointAddedEvent(joint, aUid, bUid, bodyA, bodyB);
EntityManager.EventBus.RaiseLocalEvent(aUid, vera);
var smug = new JointAddedEvent(joint, bodyB, bodyA);
var smug = new JointAddedEvent(joint, bUid, aUid, bodyB, bodyA);
EntityManager.EventBus.RaiseLocalEvent(bUid, smug);
EntityManager.EventBus.RaiseEvent(EventSource.Local, vera);
}
@@ -548,10 +548,10 @@ public abstract partial class SharedJointSystem : EntitySystem
}
else
{
var vera = new JointRemovedEvent(joint, bodyA, bodyB);
EntityManager.EventBus.RaiseLocalEvent(bodyA.Owner, vera, false);
var smug = new JointRemovedEvent(joint, bodyB, bodyA);
EntityManager.EventBus.RaiseLocalEvent(bodyB.Owner, smug, false);
var vera = new JointRemovedEvent(joint, bodyAUid, bodyBUid, bodyA, bodyB);
EntityManager.EventBus.RaiseLocalEvent(bodyAUid, vera, false);
var smug = new JointRemovedEvent(joint, bodyBUid, bodyAUid, bodyB, bodyA);
EntityManager.EventBus.RaiseLocalEvent(bodyBUid, smug, false);
EntityManager.EventBus.RaiseEvent(EventSource.Local, vera);
if (_gameTiming.IsFirstTimePredicted)

View File

@@ -609,6 +609,9 @@ public abstract partial class SharedPhysicsSystem
/// </summary>
protected bool ShouldCollide(PhysicsComponent body, PhysicsComponent other, Fixture fixture, Fixture otherFixture)
{
var aUid = body.Owner;
var bUid = other.Owner;
if (((body.BodyType & (BodyType.Kinematic | BodyType.Static)) != 0 &&
(other.BodyType & (BodyType.Kinematic | BodyType.Static)) != 0) ||
// Kinematic controllers can't collide.
@@ -621,30 +624,23 @@ public abstract partial class SharedPhysicsSystem
// Does a joint prevent collision?
// if one of them doesn't have jointcomp then they can't share a common joint.
// otherwise, only need to iterate over the joints of one component as they both store the same joint.
if (TryComp(body.Owner, out JointComponent? jointComponentA) &&
TryComp(other.Owner, out JointComponent? jointComponentB))
if (TryComp(aUid, out JointComponent? jointComponentA) && HasComp<JointComponent>(bUid))
{
var aUid = jointComponentA.Owner;
var bUid = jointComponentB.Owner;
foreach (var joint in jointComponentA.Joints.Values)
{
// Check if either: the joint even allows collisions OR the other body on the joint is actually the other body we're checking.
if (!joint.CollideConnected &&
((aUid == joint.BodyAUid &&
bUid == joint.BodyBUid) ||
(bUid == joint.BodyAUid &&
aUid == joint.BodyBUid))) return false;
if (!joint.CollideConnected && (bUid == joint.BodyAUid || bUid == joint.BodyBUid))
return false;
}
}
var preventCollideMessage = new PreventCollideEvent(body, other, fixture, otherFixture);
RaiseLocalEvent(body.Owner, ref preventCollideMessage);
var preventCollideMessage = new PreventCollideEvent(aUid, bUid, body, other, fixture, otherFixture);
RaiseLocalEvent(aUid, ref preventCollideMessage);
if (preventCollideMessage.Cancelled) return false;
preventCollideMessage = new PreventCollideEvent(other, body, otherFixture, fixture);
RaiseLocalEvent(other.Owner, ref preventCollideMessage);
preventCollideMessage = new PreventCollideEvent(bUid, aUid, other, body, otherFixture, fixture);
RaiseLocalEvent(bUid, ref preventCollideMessage);
if (preventCollideMessage.Cancelled) return false;