Use more entity queries in physics systems & entity manager (#5082)

This commit is contained in:
Leon Friedrich
2024-04-29 15:46:10 +12:00
committed by GitHub
parent 4504731588
commit 7d1915096a
4 changed files with 57 additions and 45 deletions

View File

@@ -114,7 +114,7 @@ namespace Robust.Shared.GameObjects
public void InitializeComponents(EntityUid uid, MetaDataComponent? metadata = null)
{
DebugTools.AssertOwner(uid, metadata);
metadata ??= GetComponent<MetaDataComponent>(uid);
metadata ??= MetaQuery.GetComponent(uid);
DebugTools.Assert(metadata.EntityLifeStage == EntityLifeStage.PreInit);
SetLifeStage(metadata, EntityLifeStage.Initializing);
@@ -158,13 +158,12 @@ namespace Robust.Shared.GameObjects
// TODO: please for the love of god remove these initialization order hacks.
// Init transform first, we always have it.
var transform = GetComponent<TransformComponent>(uid);
var transform = TransformQuery.GetComponent(uid);
if (transform.LifeStage == ComponentLifeStage.Initialized)
LifeStartup(transform);
// Init physics second if it exists.
if (TryGetComponent<PhysicsComponent>(uid, out var phys)
&& phys.LifeStage == ComponentLifeStage.Initialized)
if (_physicsQuery.TryComp(uid, out var phys) && phys.LifeStage == ComponentLifeStage.Initialized)
{
LifeStartup(phys);
}
@@ -294,7 +293,7 @@ namespace Robust.Shared.GameObjects
if (!uid.IsValid() || !EntityExists(uid))
throw new ArgumentException($"Entity {uid} is not valid.", nameof(uid));
AddComponentInternal(uid, newComponent, false, true);
AddComponentInternal(uid, newComponent, false, true, null);
return new CompInitializeHandle<T>(this, uid, newComponent, reg.Idx);
}
@@ -302,10 +301,11 @@ namespace Robust.Shared.GameObjects
/// <inheritdoc />
public void AddComponent<T>(EntityUid uid, T component, bool overwrite = false, MetaDataComponent? metadata = null) where T : IComponent
{
if (!uid.IsValid() || !EntityExists(uid))
if (!MetaQuery.Resolve(uid, ref metadata, false))
throw new ArgumentException($"Entity {uid} is not valid.", nameof(uid));
if (component == null) throw new ArgumentNullException(nameof(component));
if (component == null)
throw new ArgumentNullException(nameof(component));
#pragma warning disable CS0618 // Type or member is obsolete
if (component.Owner == default)
@@ -321,14 +321,17 @@ namespace Robust.Shared.GameObjects
AddComponentInternal(uid, component, overwrite, false, metadata);
}
private void AddComponentInternal<T>(EntityUid uid, T component, bool overwrite, bool skipInit, MetaDataComponent? metadata = null) where T : IComponent
private void AddComponentInternal<T>(EntityUid uid, T component, bool overwrite, bool skipInit, MetaDataComponent? metadata) where T : IComponent
{
if (!MetaQuery.ResolveInternal(uid, ref metadata, false))
throw new ArgumentException($"Entity {uid} is not valid.", nameof(uid));
// get interface aliases for mapping
var reg = _componentFactory.GetRegistration(component);
AddComponentInternal(uid, component, reg, overwrite, skipInit, metadata);
}
private void AddComponentInternal<T>(EntityUid uid, T component, ComponentRegistration reg, bool overwrite, bool skipInit, MetaDataComponent? metadata = null) where T : IComponent
private void AddComponentInternal<T>(EntityUid uid, T component, ComponentRegistration reg, bool overwrite, bool skipInit, MetaDataComponent metadata) where T : IComponent
{
// We can't use typeof(T) here in case T is just Component
DebugTools.Assert(component is MetaDataComponent ||
@@ -642,13 +645,14 @@ namespace Robust.Shared.GameObjects
_runtimeLog.LogException(e, nameof(CullRemovedComponents));
}
#endif
DeleteComponent(uid, component, false);
var meta = MetaQuery.GetComponent(uid);
DeleteComponent(uid, component, false, meta);
}
_deleteSet.Clear();
}
private void DeleteComponent(EntityUid entityUid, IComponent component, bool terminating, MetaDataComponent? metadata = null)
private void DeleteComponent(EntityUid entityUid, IComponent component, bool terminating, MetaDataComponent? metadata)
{
if (!MetaQuery.ResolveInternal(entityUid, ref metadata))
return;

View File

@@ -11,6 +11,7 @@ using Robust.Shared.Map;
using Robust.Shared.Map.Components;
using Robust.Shared.Maths;
using Robust.Shared.Network;
using Robust.Shared.Physics.Components;
using Robust.Shared.Player;
using Robust.Shared.Profiling;
using Robust.Shared.Prototypes;
@@ -47,6 +48,7 @@ namespace Robust.Shared.GameObjects
public EntityQuery<MetaDataComponent> MetaQuery;
public EntityQuery<TransformComponent> TransformQuery;
private EntityQuery<PhysicsComponent> _physicsQuery;
private EntityQuery<ActorComponent> _actorQuery;
#endregion Dependencies
@@ -210,6 +212,7 @@ namespace Robust.Shared.GameObjects
_containers = System<SharedContainerSystem>();
MetaQuery = GetEntityQuery<MetaDataComponent>();
TransformQuery = GetEntityQuery<TransformComponent>();
_physicsQuery = GetEntityQuery<PhysicsComponent>();
_actorQuery = GetEntityQuery<ActorComponent>();
}

View File

@@ -21,6 +21,7 @@ public abstract partial class SharedJointSystem : EntitySystem
[Dependency] private readonly IGameTiming _gameTiming = default!;
private EntityQuery<JointComponent> _jointsQuery;
private EntityQuery<PhysicsComponent> _physicsQuery;
private EntityQuery<JointRelayTargetComponent> _relayQuery;
private EntityQuery<TransformComponent> _xformQuery;
@@ -37,6 +38,7 @@ public abstract partial class SharedJointSystem : EntitySystem
_jointsQuery = GetEntityQuery<JointComponent>();
_relayQuery = GetEntityQuery<JointRelayTargetComponent>();
_xformQuery = GetEntityQuery<TransformComponent>();
_physicsQuery = GetEntityQuery<PhysicsComponent>();
UpdatesOutsidePrediction = true;
UpdatesBefore.Add(typeof(SharedPhysicsSystem));
@@ -136,7 +138,7 @@ public abstract partial class SharedJointSystem : EntitySystem
var aUid = joint.BodyAUid;
var bUid = joint.BodyBUid;
if (!Resolve(aUid, ref bodyA, false) || !Resolve(bUid, ref bodyB, false))
if (!_physicsQuery.Resolve(aUid, ref bodyA, false) || !_physicsQuery.Resolve(bUid, ref bodyB, false))
return;
DebugTools.Assert(Transform(aUid).MapID == Transform(bUid).MapID, "Attempted to initialize cross-map joint");
@@ -311,7 +313,7 @@ public abstract partial class SharedJointSystem : EntitySystem
public WeldJoint GetOrCreateWeldJoint(EntityUid bodyA, EntityUid bodyB, string? id = null)
{
if (id != null &&
EntityManager.TryGetComponent(bodyA, out JointComponent? jointComponent) &&
_jointsQuery.TryComp(bodyA, out JointComponent? jointComponent) &&
jointComponent.Joints.TryGetValue(id, out var weldJoint))
{
return (WeldJoint) weldJoint;
@@ -404,17 +406,17 @@ public abstract partial class SharedJointSystem : EntitySystem
protected void AddJoint(Joint joint, PhysicsComponent? bodyA = null, PhysicsComponent? bodyB = null)
{
if (!Resolve(joint.BodyAUid, ref bodyA) || !Resolve(joint.BodyBUid, ref bodyB))
if (!_physicsQuery.Resolve(joint.BodyAUid, ref bodyA) || !_physicsQuery.Resolve(joint.BodyBUid, ref bodyB))
return;
if (!joint.CollideConnected)
FilterContactsForJoint(joint, bodyA, bodyB);
// Maybe make this method AddOrUpdate so we can have an Add one that explicitly throws if present?
var mapidA = EntityManager.GetComponent<TransformComponent>(joint.BodyAUid).MapID;
var mapidA = Transform(joint.BodyAUid).MapID;
if (mapidA == MapId.Nullspace ||
mapidA != EntityManager.GetComponent<TransformComponent>(joint.BodyBUid).MapID)
mapidA != Transform(joint.BodyBUid).MapID)
{
Log.Error($"Tried to add joint to ineligible bodies");
return;
@@ -447,7 +449,8 @@ public abstract partial class SharedJointSystem : EntitySystem
if (!Resolve(uid, ref xform))
return;
Resolve(uid, ref component, ref relay, false);
_jointsQuery.Resolve(uid, ref component, false);
_relayQuery.Resolve(uid, ref relay, false);
if (relay != null)
{
@@ -471,7 +474,7 @@ public abstract partial class SharedJointSystem : EntitySystem
/// </summary>
public void ClearJoints(EntityUid uid, JointComponent? component = null)
{
if (!Resolve(uid, ref component, false))
if (!_jointsQuery.Resolve(uid, ref component, false))
return;
// TODO PERFORMANCE
@@ -497,15 +500,9 @@ public abstract partial class SharedJointSystem : EntitySystem
}
}
[Obsolete("Use the other ClearJoints overload")]
public void ClearJoints(JointComponent joint)
{
ClearJoints(joint.Owner, joint);
}
public void RemoveJoint(EntityUid uid, string id)
{
if (!TryComp<JointComponent>(uid, out var jointComp))
if (!_jointsQuery.TryComp(uid, out var jointComp))
return;
if (!jointComp.Joints.TryGetValue(id, out var joint))
@@ -522,12 +519,12 @@ public abstract partial class SharedJointSystem : EntitySystem
// Originally I logged these but because of prediction the client can just nuke them multiple times in a row
// because each body has its own JointComponent, bleh.
if (!TryComp<JointComponent>(bodyAUid, out var jointComponentA))
if (!_jointsQuery.TryComp(bodyAUid, out var jointComponentA))
{
return;
}
if (!TryComp<JointComponent>(bodyBUid, out var jointComponentB))
if (!_jointsQuery.TryComp(bodyBUid, out var jointComponentB))
{
return;
}
@@ -543,7 +540,7 @@ public abstract partial class SharedJointSystem : EntitySystem
}
// Wake up connected bodies.
if (EntityManager.TryGetComponent<PhysicsComponent>(bodyAUid, out var bodyA) &&
if (_physicsQuery.TryComp(bodyAUid, out var bodyA) &&
MetaData(bodyAUid).EntityLifeStage < EntityLifeStage.Terminating)
{
var uidA = jointComponentA.Relay ?? bodyAUid;
@@ -607,7 +604,7 @@ public abstract partial class SharedJointSystem : EntitySystem
internal void FilterContactsForJoint(Joint joint, PhysicsComponent? bodyA = null, PhysicsComponent? bodyB = null)
{
if (!Resolve(joint.BodyBUid, ref bodyB))
if (!_physicsQuery.Resolve(joint.BodyBUid, ref bodyB))
return;
var node = bodyB.Contacts.First;

View File

@@ -115,7 +115,7 @@ public partial class SharedPhysicsSystem
public void ApplyAngularImpulse(EntityUid uid, float impulse, FixturesComponent? manager = null, PhysicsComponent? body = null)
{
if (!Resolve(uid, ref body) || !IsMoveable(body) || !WakeBody(uid, manager: manager, body: body))
if (!PhysicsQuery.Resolve(uid, ref body) || !IsMoveable(body) || !WakeBody(uid, manager: manager, body: body))
{
return;
}
@@ -125,7 +125,7 @@ public partial class SharedPhysicsSystem
public void ApplyForce(EntityUid uid, Vector2 force, Vector2 point, FixturesComponent? manager = null, PhysicsComponent? body = null)
{
if (!Resolve(uid, ref body) || !IsMoveable(body) || !WakeBody(uid, manager: manager, body: body))
if (!PhysicsQuery.Resolve(uid, ref body) || !IsMoveable(body) || !WakeBody(uid, manager: manager, body: body))
{
return;
}
@@ -137,7 +137,7 @@ public partial class SharedPhysicsSystem
public void ApplyForce(EntityUid uid, Vector2 force, FixturesComponent? manager = null, PhysicsComponent? body = null)
{
if (!Resolve(uid, ref body) || !IsMoveable(body) || !WakeBody(uid, manager: manager, body: body))
if (!PhysicsQuery.Resolve(uid, ref body) || !IsMoveable(body) || !WakeBody(uid, manager: manager, body: body))
{
return;
}
@@ -148,7 +148,7 @@ public partial class SharedPhysicsSystem
public void ApplyTorque(EntityUid uid, float torque, FixturesComponent? manager = null, PhysicsComponent? body = null)
{
if (!Resolve(uid, ref body) || !IsMoveable(body) || !WakeBody(uid, manager: manager, body: body))
if (!PhysicsQuery.Resolve(uid, ref body) || !IsMoveable(body) || !WakeBody(uid, manager: manager, body: body))
{
return;
}
@@ -159,7 +159,7 @@ public partial class SharedPhysicsSystem
public void ApplyLinearImpulse(EntityUid uid, Vector2 impulse, FixturesComponent? manager = null, PhysicsComponent? body = null)
{
if (!Resolve(uid, ref body) || !IsMoveable(body) || !WakeBody(uid, manager: manager, body: body))
if (!PhysicsQuery.Resolve(uid, ref body) || !IsMoveable(body) || !WakeBody(uid, manager: manager, body: body))
{
return;
}
@@ -169,7 +169,7 @@ public partial class SharedPhysicsSystem
public void ApplyLinearImpulse(EntityUid uid, Vector2 impulse, Vector2 point, FixturesComponent? manager = null, PhysicsComponent? body = null)
{
if (!Resolve(uid, ref body) || !IsMoveable(body) || !WakeBody(uid, manager: manager, body: body))
if (!PhysicsQuery.Resolve(uid, ref body) || !IsMoveable(body) || !WakeBody(uid, manager: manager, body: body))
{
return;
}
@@ -250,7 +250,10 @@ public partial class SharedPhysicsSystem
public void ResetMassData(EntityUid uid, FixturesComponent? manager = null, PhysicsComponent? body = null)
{
if (!Resolve(uid, ref manager, ref body))
if (!PhysicsQuery.Resolve(uid, ref body))
return;
if (!_fixturesQuery.Resolve(uid, ref manager))
return;
body._mass = 0.0f;
@@ -315,7 +318,7 @@ public partial class SharedPhysicsSystem
public bool SetAngularVelocity(EntityUid uid, float value, bool dirty = true, FixturesComponent? manager = null, PhysicsComponent? body = null)
{
if (!Resolve(uid, ref body))
if (!PhysicsQuery.Resolve(uid, ref body))
return false;
if (body.BodyType == BodyType.Static)
@@ -346,7 +349,7 @@ public partial class SharedPhysicsSystem
/// </summary>
public bool SetLinearVelocity(EntityUid uid, Vector2 velocity, bool dirty = true, bool wakeBody = true, FixturesComponent? manager = null, PhysicsComponent? body = null)
{
if (!Resolve(uid, ref body))
if (!PhysicsQuery.Resolve(uid, ref body))
return false;
if (body.BodyType == BodyType.Static)
@@ -467,7 +470,7 @@ public partial class SharedPhysicsSystem
public void SetBodyType(EntityUid uid, BodyType value, FixturesComponent? manager = null, PhysicsComponent? body = null, TransformComponent? xform = null)
{
if (!Resolve(uid, ref body))
if (!PhysicsQuery.Resolve(uid, ref body))
return;
if (body.BodyType == value)
@@ -531,7 +534,7 @@ public partial class SharedPhysicsSystem
FixturesComponent? manager = null,
PhysicsComponent? body = null)
{
if (!Resolve(uid, ref body))
if (!PhysicsQuery.Resolve(uid, ref body))
return false;
if (body.CanCollide == value)
@@ -545,7 +548,7 @@ public partial class SharedPhysicsSystem
if (_containerSystem.IsEntityOrParentInContainer(uid))
return false;
if (!Resolve(uid, ref manager) || manager.FixtureCount == 0 && !_mapManager.IsGrid(uid))
if (!_fixturesQuery.Resolve(uid, ref manager) || manager.FixtureCount == 0 && !_mapManager.IsGrid(uid))
return false;
}
else
@@ -575,7 +578,7 @@ public partial class SharedPhysicsSystem
public void SetFixedRotation(EntityUid uid, bool value, bool dirty = true, FixturesComponent? manager = null, PhysicsComponent? body = null)
{
if (!Resolve(uid, ref body) || body.FixedRotation == value)
if (!PhysicsQuery.Resolve(uid, ref body) || body.FixedRotation == value)
return;
body.FixedRotation = value;
@@ -668,10 +671,13 @@ public partial class SharedPhysicsSystem
/// <returns>true if the body is collidable and awake</returns>
public bool WakeBody(EntityUid uid, bool force = false, FixturesComponent? manager = null, PhysicsComponent? body = null)
{
if (!SetCanCollide(uid, true, manager: manager, body: body, force: force) || !Resolve(uid, ref body))
if (!PhysicsQuery.Resolve(uid, ref body))
return false;
SetAwake(uid, body, true);
if (!SetCanCollide(uid, true, manager: manager, body: body, force: force))
return false;
SetAwake((uid, body), true);
return body.Awake;
}
@@ -715,7 +721,9 @@ public partial class SharedPhysicsSystem
public Box2 GetHardAABB(EntityUid uid, FixturesComponent? manager = null, PhysicsComponent? body = null, TransformComponent? xform = null)
{
if (!Resolve(uid, ref body, ref xform, ref manager))
if (!PhysicsQuery.Resolve(uid, ref body)
|| !_fixturesQuery.Resolve(uid, ref manager)
|| !Resolve(uid, ref xform))
{
return Box2.Empty;
}