Pulling fasto (#17696)
* faster pulling + pulling gravity tweaks * merciful
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Content.Shared.Pulling;
|
||||
using Content.Shared.Gravity;
|
||||
using Content.Shared.Pulling;
|
||||
using Content.Shared.Pulling.Components;
|
||||
using Content.Shared.Rotatable;
|
||||
using Robust.Shared.Physics;
|
||||
@@ -36,6 +37,8 @@ namespace Content.Server.Physics.Controllers
|
||||
private const float MinimumMovementDistance = 0.005f;
|
||||
|
||||
[Dependency] private readonly SharedPullingSystem _pullableSystem = default!;
|
||||
[Dependency] private readonly SharedGravitySystem _gravity = default!;
|
||||
[Dependency] private readonly SharedTransformSystem _transform = default!;
|
||||
|
||||
// TODO: Move this stuff to pullingsystem
|
||||
/// <summary>
|
||||
@@ -63,19 +66,19 @@ namespace Content.Server.Physics.Controllers
|
||||
|
||||
private void OnPullerMove(EntityUid uid, SharedPullerComponent component, ref MoveEvent args)
|
||||
{
|
||||
if (component.Pulling == null ||
|
||||
!TryComp<SharedPullableComponent>(component.Pulling.Value, out var pullable)) return;
|
||||
if (component.Pulling is not { } pullable || !TryComp<SharedPullableComponent>(pullable, out var pullableComponent))
|
||||
return;
|
||||
|
||||
UpdatePulledRotation(uid, pullable.Owner);
|
||||
UpdatePulledRotation(uid, pullable);
|
||||
|
||||
if (args.NewPosition.EntityId == args.OldPosition.EntityId &&
|
||||
(args.NewPosition.Position - args.OldPosition.Position).LengthSquared < MinimumMovementDistance * MinimumMovementDistance)
|
||||
return;
|
||||
|
||||
if (TryComp<PhysicsComponent>(pullable.Owner, out var physics))
|
||||
PhysicsSystem.WakeBody(pullable.Owner, body: physics);
|
||||
if (TryComp<PhysicsComponent>(pullable, out var physics))
|
||||
PhysicsSystem.WakeBody(pullable, body: physics);
|
||||
|
||||
_pullableSystem.StopMoveTo(pullable);
|
||||
_pullableSystem.StopMoveTo(pullableComponent);
|
||||
}
|
||||
|
||||
private void UpdatePulledRotation(EntityUid puller, EntityUid pulled)
|
||||
@@ -101,7 +104,7 @@ namespace Content.Server.Physics.Controllers
|
||||
var newAngle = Angle.FromWorldVec(dir);
|
||||
|
||||
var diff = newAngle - oldAngle;
|
||||
if (Math.Abs(diff.Degrees) > (ThresholdRotAngle / 2f))
|
||||
if (Math.Abs(diff.Degrees) > ThresholdRotAngle / 2f)
|
||||
{
|
||||
// Ok, so this bit is difficult because ideally it would look like it's snapping to sane angles.
|
||||
// Otherwise PIANO DOOR STUCK! happens.
|
||||
@@ -126,47 +129,45 @@ namespace Content.Server.Physics.Controllers
|
||||
// or due to being deleted.
|
||||
|
||||
if (pullable.Deleted)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (pullable.MovingTo == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (pullable.Puller is not {Valid: true} puller)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var pullableEnt = pullable.Owner;
|
||||
var pullableXform = Transform(pullableEnt);
|
||||
var pullerXform = Transform(puller);
|
||||
|
||||
// Now that's over with...
|
||||
|
||||
var pullerPosition = EntityManager.GetComponent<TransformComponent>(puller).MapPosition;
|
||||
var movingTo = pullable.MovingTo.Value.ToMap(EntityManager);
|
||||
var pullerPosition = pullerXform.MapPosition;
|
||||
var movingTo = pullable.MovingTo.Value.ToMap(EntityManager, _transform);
|
||||
if (movingTo.MapId != pullerPosition.MapId)
|
||||
{
|
||||
_pullableSystem.StopMoveTo(pullable);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!EntityManager.TryGetComponent<PhysicsComponent?>(pullable.Owner, out var physics) ||
|
||||
if (!TryComp<PhysicsComponent?>(pullableEnt, out var physics) ||
|
||||
physics.BodyType == BodyType.Static ||
|
||||
movingTo.MapId != EntityManager.GetComponent<TransformComponent>(pullable.Owner).MapID)
|
||||
movingTo.MapId != pullableXform.MapID)
|
||||
{
|
||||
_pullableSystem.StopMoveTo(pullable);
|
||||
continue;
|
||||
}
|
||||
|
||||
var movingPosition = movingTo.Position;
|
||||
var ownerPosition = EntityManager.GetComponent<TransformComponent>(pullable.Owner).MapPosition.Position;
|
||||
var ownerPosition = pullableXform.MapPosition.Position;
|
||||
|
||||
var diff = movingPosition - ownerPosition;
|
||||
var diffLength = diff.Length;
|
||||
|
||||
if (diffLength < MaximumSettleDistance && (physics.LinearVelocity.Length < MaximumSettleVelocity))
|
||||
if (diffLength < MaximumSettleDistance && physics.LinearVelocity.Length < MaximumSettleVelocity)
|
||||
{
|
||||
PhysicsSystem.SetLinearVelocity(pullable.Owner, Vector2.Zero, body: physics);
|
||||
PhysicsSystem.SetLinearVelocity(pullableEnt, Vector2.Zero, body: physics);
|
||||
_pullableSystem.StopMoveTo(pullable);
|
||||
continue;
|
||||
}
|
||||
@@ -177,17 +178,25 @@ namespace Content.Server.Physics.Controllers
|
||||
// Note the implication that the real rules of physics don't apply to pulling control.
|
||||
var accel = diff.Normalized * multiplier;
|
||||
// Now for the part where velocity gets shutdown...
|
||||
if ((diffLength < SettleShutdownDistance) && (physics.LinearVelocity.Length >= SettleMinimumShutdownVelocity))
|
||||
if (diffLength < SettleShutdownDistance && physics.LinearVelocity.Length >= SettleMinimumShutdownVelocity)
|
||||
{
|
||||
// Shutdown velocity increases as we get closer to centre
|
||||
var scaling = (SettleShutdownDistance - diffLength) / SettleShutdownDistance;
|
||||
accel -= physics.LinearVelocity * SettleShutdownMultiplier * scaling;
|
||||
}
|
||||
|
||||
PhysicsSystem.WakeBody(pullable.Owner, body: physics);
|
||||
PhysicsSystem.WakeBody(pullableEnt, body: physics);
|
||||
|
||||
var impulse = accel * physics.Mass * frameTime;
|
||||
PhysicsSystem.ApplyLinearImpulse(pullable.Owner, impulse, body: physics);
|
||||
PhysicsSystem.ApplyLinearImpulse(pullableEnt, impulse, body: physics);
|
||||
|
||||
// if the puller is weightless, then we apply the inverse impulse.
|
||||
// doing it under gravity produces an unsatisfying wiggling when pulling.
|
||||
if (_gravity.IsWeightless(puller) && pullerXform.GridUid == null)
|
||||
{
|
||||
PhysicsSystem.WakeBody(puller);
|
||||
PhysicsSystem.ApplyLinearImpulse(puller, -impulse);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user