Pulling state handling changes (#9952)

This commit is contained in:
Leon Friedrich
2022-07-29 14:13:43 +12:00
committed by GitHub
parent 7cf67e1323
commit ebe6093af1
4 changed files with 35 additions and 26 deletions

View File

@@ -71,10 +71,13 @@ namespace Content.Client.Physics.Controllers
if (TryComp(player, out JointComponent? jointComponent)) if (TryComp(player, out JointComponent? jointComponent))
{ {
foreach (var joint in jointComponent.GetJoints) foreach (var joint in jointComponent.GetJoints.Values)
{ {
joint.BodyA.Predict = true; if (TryComp(joint.BodyAUid, out PhysicsComponent? physics))
joint.BodyB.Predict = true; physics.Predict = true;
if (TryComp(joint.BodyBUid, out physics))
physics.Predict = true;
} }
} }

View File

@@ -13,14 +13,13 @@ namespace Content.Shared.Pulling.Components
{ {
/// <summary> /// <summary>
/// The current entity pulling this component. /// The current entity pulling this component.
/// SharedPullingStateManagementSystem should be writing this. This means definitely not you.
/// </summary> /// </summary>
public EntityUid? Puller { get; set; } public EntityUid? Puller { get; set; }
/// <summary> /// <summary>
/// The pull joint. /// The pull joint.
/// SharedPullingStateManagementSystem should be writing this. This means probably not you.
/// </summary> /// </summary>
public DistanceJoint? PullJoint { get; set; } public string? PullJointId { get; set; }
public bool BeingPulled => Puller != null; public bool BeingPulled => Puller != null;

View File

@@ -4,6 +4,7 @@ using Content.Shared.Pulling.Components;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Shared.Map; using Robust.Shared.Map;
using Robust.Shared.Physics; using Robust.Shared.Physics;
using Robust.Shared.Timing;
namespace Content.Shared.Pulling namespace Content.Shared.Pulling
{ {
@@ -16,6 +17,7 @@ namespace Content.Shared.Pulling
{ {
[Dependency] private readonly SharedJointSystem _jointSystem = default!; [Dependency] private readonly SharedJointSystem _jointSystem = default!;
[Dependency] private readonly SharedPhysicsSystem _physics = default!; [Dependency] private readonly SharedPhysicsSystem _physics = default!;
[Dependency] private readonly IGameTiming _timing = default!;
public override void Initialize() public override void Initialize()
{ {
@@ -42,14 +44,14 @@ namespace Content.Shared.Pulling
ForceSetMovingTo(pullable, null); ForceSetMovingTo(pullable, null);
// Joint shutdown // Joint shutdown
if (EntityManager.TryGetComponent<JointComponent?>(puller.Owner, out var jointComp)) if (!_timing.ApplyingState && // During state-handling, joint component will handle its own state.
pullable.PullJointId != null &&
TryComp(puller.Owner, out JointComponent? jointComp))
{ {
if (jointComp.GetJoints.Contains(pullable.PullJoint!)) if (jointComp.GetJoints.TryGetValue(pullable.PullJointId, out var j))
{ _jointSystem.RemoveJoint(j);
_jointSystem.RemoveJoint(pullable.PullJoint!);
}
} }
pullable.PullJoint = null; pullable.PullJointId = null;
// State shutdown // State shutdown
puller.Pulling = null; puller.Pulling = null;
@@ -64,8 +66,8 @@ namespace Content.Shared.Pulling
RaiseLocalEvent(pullable.Owner, message, true); RaiseLocalEvent(pullable.Owner, message, true);
// Networking // Networking
puller.Dirty(); Dirty(puller);
pullable.Dirty(); Dirty(pullable);
} }
public void ForceRelationship(SharedPullerComponent? puller, SharedPullableComponent? pullable) public void ForceRelationship(SharedPullerComponent? puller, SharedPullableComponent? pullable)
@@ -92,26 +94,31 @@ namespace Content.Shared.Pulling
// And now for the actual connection (if any). // And now for the actual connection (if any).
if ((puller != null) && (pullable != null)) if (puller != null && pullable != null)
{ {
var pullerPhysics = EntityManager.GetComponent<PhysicsComponent>(puller.Owner); var pullerPhysics = EntityManager.GetComponent<PhysicsComponent>(puller.Owner);
var pullablePhysics = EntityManager.GetComponent<PhysicsComponent>(pullable.Owner); var pullablePhysics = EntityManager.GetComponent<PhysicsComponent>(pullable.Owner);
pullable.PullJointId = $"pull-joint-{pullable.Owner}";
// State startup // State startup
puller.Pulling = pullable.Owner; puller.Pulling = pullable.Owner;
pullable.Puller = puller.Owner; pullable.Puller = puller.Owner;
// Joint startup // joint state handling will manage its own state
var union = _physics.GetHardAABB(pullerPhysics).Union(_physics.GetHardAABB(pullablePhysics)); if (!_timing.ApplyingState)
var length = Math.Max(union.Size.X, union.Size.Y) * 0.75f; {
// Joint startup
var union = _physics.GetHardAABB(pullerPhysics).Union(_physics.GetHardAABB(pullablePhysics));
var length = Math.Max(union.Size.X, union.Size.Y) * 0.75f;
pullable.PullJoint = _jointSystem.CreateDistanceJoint(pullablePhysics.Owner, pullerPhysics.Owner, id:$"pull-joint-{pullablePhysics.Owner}"); var joint = _jointSystem.CreateDistanceJoint(pullablePhysics.Owner, pullerPhysics.Owner, id: pullable.PullJointId);
pullable.PullJoint.CollideConnected = false; joint.CollideConnected = false;
// This maximum has to be there because if the object is constrained too closely, the clamping goes backwards and asserts. // This maximum has to be there because if the object is constrained too closely, the clamping goes backwards and asserts.
pullable.PullJoint.MaxLength = Math.Max(1.0f, length); joint.MaxLength = Math.Max(1.0f, length);
pullable.PullJoint.Length = length * 0.75f; joint.Length = length * 0.75f;
pullable.PullJoint.MinLength = 0f; joint.MinLength = 0f;
pullable.PullJoint.Stiffness = 1f; joint.Stiffness = 1f;
}
// Messaging // Messaging
var message = new PullStartedMessage(pullerPhysics, pullablePhysics); var message = new PullStartedMessage(pullerPhysics, pullablePhysics);

View File

@@ -65,7 +65,7 @@ namespace Content.Shared.Pulling
if (TryComp(uid, out JointComponent? joints)) if (TryComp(uid, out JointComponent? joints))
{ {
foreach (var jt in joints.GetJoints) foreach (var jt in joints.GetJoints.Values)
{ {
if (jt.BodyAUid == component.Puller || jt.BodyBUid == component.Puller) if (jt.BodyAUid == component.Puller || jt.BodyBUid == component.Puller)
return; return;