Add client pulling prediction (#2041)

* WIP changes

* Merge conflict fixes

* Bring pull controlelr to current year

* Sync and predict PullController on the client

* Clean imports

* Slow down pullers and make pulling tighter

* Stop pulls on pullable or puller component removals

* Make pulling not occur when moving towards the pulled entity
This commit is contained in:
DrSmugleaf
2020-10-16 20:35:09 +02:00
committed by GitHub
parent 0345fbbd22
commit b1fe4bad01
24 changed files with 734 additions and 341 deletions

View File

@@ -1,22 +1,28 @@
#nullable enable
using System;
using Content.Shared.GameObjects.EntitySystems;
using System.Linq;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Physics;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Physics;
using Robust.Shared.Utility;
using static Content.Shared.GameObjects.EntitySystems.SharedInteractionSystem;
namespace Content.Shared.Physics.Pull
{
public class PullController : VirtualController
{
private const float DistBeforePull = 1.0f;
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly IPhysicsManager _physicsManager = default!;
private const float DistBeforeStopPull = SharedInteractionSystem.InteractionRange;
private const float DistBeforeStopPull = InteractionRange;
private const float StopMoveThreshold = 0.25f;
private IPhysicsComponent? _puller;
@@ -26,81 +32,167 @@ namespace Content.Shared.Physics.Pull
public IPhysicsComponent? Puller => _puller;
public void StartPull(IPhysicsComponent puller)
public EntityCoordinates? MovingTo
{
get => _movingTo;
set
{
if (_movingTo == value || ControlledComponent == null)
{
return;
}
_movingTo = value;
ControlledComponent.WakeBody();
}
}
private float DistanceBeforeStopPull()
{
if (_puller == null)
{
return 0;
}
var aabbSize = _puller.AABB.Size;
return (aabbSize.X > aabbSize.Y ? aabbSize.X : aabbSize.Y) + 0.15f;
}
private bool PullerMovingTowardsPulled()
{
if (_puller == null)
{
return false;
}
if (ControlledComponent == null)
{
return false;
}
if (_puller.LinearVelocity.EqualsApprox(Vector2.Zero))
{
return false;
}
var pullerTransform = _puller.Owner.Transform;
var origin = pullerTransform.Coordinates.Position;
var velocity = _puller.LinearVelocity.Normalized;
var mapId = pullerTransform.MapPosition.MapId;
var ray = new CollisionRay(origin, velocity, (int) CollisionGroup.AllMask);
bool Predicate(IEntity e) => e != ControlledComponent.Owner;
var rayResults =
_physicsManager.IntersectRayWithPredicate(mapId, ray, DistanceBeforeStopPull() * 2, Predicate);
return rayResults.Any();
}
public bool StartPull(IEntity entity)
{
DebugTools.AssertNotNull(entity);
if (!entity.TryGetComponent(out IPhysicsComponent? physics))
{
return false;
}
return StartPull(physics);
}
public bool StartPull(IPhysicsComponent puller)
{
DebugTools.AssertNotNull(puller);
if (_puller == puller)
{
return;
return false;
}
_puller = puller;
if (ControlledComponent == null)
{
return;
return false;
}
ControlledComponent.WakeBody();
_puller = puller;
var message = new PullStartedMessage(this, _puller, ControlledComponent);
_puller.Owner.SendMessage(null, message);
ControlledComponent.Owner.SendMessage(null, message);
_puller.Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, message);
ControlledComponent.WakeBody();
return true;
}
public void StopPull()
public bool StopPull()
{
var oldPuller = _puller;
if (oldPuller == null)
{
return;
return false;
}
_puller = null;
if (ControlledComponent == null)
{
return;
return false;
}
ControlledComponent.WakeBody();
var message = new PullStoppedMessage(this, oldPuller, ControlledComponent);
var message = new PullStoppedMessage(oldPuller, ControlledComponent);
oldPuller.Owner.SendMessage(null, message);
ControlledComponent.Owner.SendMessage(null, message);
oldPuller.Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, message);
ControlledComponent.WakeBody();
ControlledComponent.TryRemoveController<PullController>();
return true;
}
public void TryMoveTo(EntityCoordinates from, EntityCoordinates to)
public bool TryMoveTo(EntityCoordinates from, EntityCoordinates to)
{
if (_puller == null || ControlledComponent == null)
{
return;
return false;
}
var entityManager = IoCManager.Resolve<IEntityManager>();
if (!from.InRange(entityManager, to, SharedInteractionSystem.InteractionRange))
if (!_puller.Owner.Transform.Coordinates.InRange(_entityManager, from, InteractionRange))
{
return;
return false;
}
ControlledComponent.WakeBody();
var dist = _puller.Owner.Transform.Coordinates.Position - to.Position;
if (Math.Sqrt(dist.LengthSquared) > DistBeforeStopPull ||
Math.Sqrt(dist.LengthSquared) < 0.25f)
if (!_puller.Owner.Transform.Coordinates.InRange(_entityManager, to, InteractionRange))
{
return;
return false;
}
_movingTo = to;
if (!from.InRange(_entityManager, to, InteractionRange))
{
return false;
}
if (from.Position.EqualsApprox(to.Position))
{
return false;
}
if (!_puller.Owner.Transform.Coordinates.TryDistance(_entityManager, to, out var distance) ||
Math.Sqrt(distance) > DistBeforeStopPull ||
Math.Sqrt(distance) < StopMoveThreshold)
{
return false;
}
MovingTo = to;
return true;
}
public override void UpdateBeforeProcessing()
@@ -116,21 +208,20 @@ namespace Content.Shared.Physics.Pull
return;
}
// Are we outside of pulling range?
var dist = _puller.Owner.Transform.WorldPosition - ControlledComponent.Owner.Transform.WorldPosition;
var distance = _puller.Owner.Transform.WorldPosition - ControlledComponent.Owner.Transform.WorldPosition;
if (dist.Length > DistBeforeStopPull)
if (distance.Length > DistBeforeStopPull)
{
StopPull();
}
else if (_movingTo.HasValue)
else if (MovingTo.HasValue)
{
var diff = _movingTo.Value.Position - ControlledComponent.Owner.Transform.Coordinates.Position;
var diff = MovingTo.Value.Position - ControlledComponent.Owner.Transform.Coordinates.Position;
LinearVelocity = diff.Normalized * 5;
}
else if (dist.Length > DistBeforePull)
else if (distance.Length > DistanceBeforeStopPull() && !PullerMovingTowardsPulled())
{
LinearVelocity = dist.Normalized * _puller.LinearVelocity.Length * 1.1f;
LinearVelocity = distance.Normalized * _puller.LinearVelocity.Length * 1.5f;
}
else
{
@@ -144,18 +235,14 @@ namespace Content.Shared.Physics.Pull
if (ControlledComponent == null)
{
_movingTo = null;
MovingTo = null;
return;
}
if (_movingTo == null)
if (MovingTo != null &&
ControlledComponent.Owner.Transform.Coordinates.Position.EqualsApprox(MovingTo.Value.Position, 0.01))
{
return;
}
if (ControlledComponent.Owner.Transform.Coordinates.Position.EqualsApprox(_movingTo.Value.Position, 0.01))
{
_movingTo = null;
MovingTo = null;
}
}
}

View File

@@ -5,13 +5,11 @@ namespace Content.Shared.Physics.Pull
{
public class PullMessage : ComponentMessage
{
public readonly PullController Controller;
public readonly IPhysicsComponent Puller;
public readonly IPhysicsComponent Pulled;
protected PullMessage(PullController controller, IPhysicsComponent puller, IPhysicsComponent pulled)
protected PullMessage(IPhysicsComponent puller, IPhysicsComponent pulled)
{
Controller = controller;
Puller = puller;
Pulled = pulled;
}

View File

@@ -5,7 +5,7 @@ namespace Content.Shared.Physics.Pull
public class PullStartedMessage : PullMessage
{
public PullStartedMessage(PullController controller, IPhysicsComponent puller, IPhysicsComponent pulled) :
base(controller, puller, pulled)
base(puller, pulled)
{
}
}

View File

@@ -4,8 +4,7 @@ namespace Content.Shared.Physics.Pull
{
public class PullStoppedMessage : PullMessage
{
public PullStoppedMessage(PullController controller, IPhysicsComponent puller, IPhysicsComponent pulled) :
base(controller, puller, pulled)
public PullStoppedMessage(IPhysicsComponent puller, IPhysicsComponent pulled) : base(puller, pulled)
{
}
}