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

@@ -4,6 +4,5 @@ namespace Content.Shared.GameObjects.Components.Items
{
public interface ISharedHandsComponent : IComponent
{
void StopPull();
}
}

View File

@@ -1,6 +1,8 @@
#nullable enable
using System;
using Content.Shared.GameObjects.Components.Pulling;
using Content.Shared.Physics.Pull;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components;
using Robust.Shared.Interfaces.GameObjects;
@@ -13,42 +15,6 @@ namespace Content.Shared.GameObjects.Components.Items
{
public sealed override string Name => "Hands";
public sealed override uint? NetID => ContentNetIDs.HANDS;
[ViewVariables]
public IPhysicsComponent? PulledObject { get; protected set; }
[ViewVariables]
protected bool IsPulling => PulledObject != null;
public virtual void StopPull()
{
if (PulledObject != null &&
PulledObject.TryGetController(out PullController controller))
{
controller.StopPull();
}
}
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
if (!(message is PullMessage pullMessage) ||
pullMessage.Puller.Owner != Owner)
{
return;
}
switch (message)
{
case PullStartedMessage msg:
PulledObject = msg.Pulled;
break;
case PullStoppedMessage _:
PulledObject = null;
break;
}
}
}
[Serializable, NetSerializable]

View File

@@ -14,7 +14,11 @@ namespace Content.Shared.GameObjects.Components.Mobs
public override string Name => "StatusEffectsUI";
public override uint? NetID => ContentNetIDs.STATUSEFFECTS;
public abstract void ChangeStatusEffectIcon(StatusEffect effect, string icon);
public abstract void ChangeStatusEffect(StatusEffect effect, string icon, ValueTuple<TimeSpan, TimeSpan>? cooldown);
public abstract void RemoveStatusEffect(StatusEffect effect);
}
[Serializable, NetSerializable]

View File

@@ -0,0 +1,263 @@
#nullable enable
using System;
using Content.Shared.GameObjects.Components.Mobs;
using Content.Shared.Physics.Pull;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components;
using Robust.Shared.GameObjects.Components.Transform;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Serialization;
namespace Content.Shared.GameObjects.Components.Pulling
{
public abstract class SharedPullableComponent : Component
{
public override string Name => "Pullable";
public override uint? NetID => ContentNetIDs.PULLABLE;
private IEntity? _puller;
public virtual IEntity? Puller
{
get => _puller;
private set
{
if (_puller == value)
{
return;
}
_puller = value;
Dirty();
if (!Owner.TryGetComponent(out IPhysicsComponent? physics))
{
return;
}
PullController controller;
if (value == null)
{
if (physics.TryGetController(out controller))
{
controller.StopPull();
}
return;
}
controller = physics.EnsureController<PullController>();
controller.StartPull(value);
}
}
public bool BeingPulled => Puller != null;
public bool CanStartPull(IEntity puller)
{
if (!puller.HasComponent<SharedPullerComponent>())
{
return false;
}
if (!puller.TryGetComponent(out IPhysicsComponent? physics))
{
return false;
}
if (physics.Anchored)
{
return false;
}
if (puller == Owner)
{
return false;
}
if (!puller.IsInSameOrNoContainer(Owner))
{
return false;
}
return true;
}
public bool TryStartPull(IEntity puller)
{
if (!CanStartPull(puller))
{
return false;
}
TryStopPull();
Puller = puller;
if (Puller != puller)
{
return false;
}
return true;
}
public bool TryStopPull()
{
if (!BeingPulled)
{
return false;
}
Puller = null;
return true;
}
public bool TogglePull(IEntity puller)
{
if (Puller == null)
{
return TryStartPull(puller);
}
return TryStopPull();
}
public bool TryMoveTo(EntityCoordinates to)
{
if (Puller == null)
{
return false;
}
if (!Owner.TryGetComponent(out IPhysicsComponent? physics))
{
return false;
}
if (!physics.TryGetController(out PullController controller))
{
return false;
}
return controller.TryMoveTo(Puller.Transform.Coordinates, to);
}
private void PullerMoved(MoveEvent moveEvent)
{
if (Puller == null)
{
return;
}
if (moveEvent.Sender != Puller)
{
return;
}
if (!Owner.TryGetComponent(out IPhysicsComponent? physics))
{
return;
}
physics.WakeBody();
}
public override ComponentState GetComponentState()
{
return new PullableComponentState(Puller?.Uid);
}
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
base.HandleComponentState(curState, nextState);
if (!(curState is PullableComponentState state))
{
return;
}
if (state.Puller == null)
{
Puller = null;
return;
}
Puller = Owner.EntityManager.GetEntity(state.Puller.Value);
}
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
if (!(message is PullMessage pullMessage) ||
pullMessage.Pulled.Owner != Owner)
{
return;
}
switch (message)
{
case PullStartedMessage msg:
Owner.EntityManager.EventBus.SubscribeEvent<MoveEvent>(EventSource.Local, this, PullerMoved);
AddPullingStatuses(msg.Puller.Owner);
break;
case PullStoppedMessage msg:
Owner.EntityManager.EventBus.UnsubscribeEvent<MoveEvent>(EventSource.Local, this);
RemovePullingStatuses(msg.Puller.Owner);
break;
}
}
private void AddPullingStatuses(IEntity puller)
{
if (Owner.TryGetComponent(out SharedStatusEffectsComponent? pulledStatus))
{
pulledStatus.ChangeStatusEffectIcon(StatusEffect.Pulled,
"/Textures/Interface/StatusEffects/Pull/pulled.png");
}
if (puller.TryGetComponent(out SharedStatusEffectsComponent? ownerStatus))
{
ownerStatus.ChangeStatusEffectIcon(StatusEffect.Pulling,
"/Textures/Interface/StatusEffects/Pull/pulling.png");
}
}
private void RemovePullingStatuses(IEntity puller)
{
if (Owner.TryGetComponent(out SharedStatusEffectsComponent? pulledStatus))
{
pulledStatus.RemoveStatusEffect(StatusEffect.Pulled);
}
if (puller.TryGetComponent(out SharedStatusEffectsComponent? ownerStatus))
{
ownerStatus.RemoveStatusEffect(StatusEffect.Pulling);
}
}
public override void OnRemove()
{
TryStopPull();
base.OnRemove();
}
}
[Serializable, NetSerializable]
public class PullableComponentState : ComponentState
{
public readonly EntityUid? Puller;
public PullableComponentState(EntityUid? puller) : base(ContentNetIDs.PULLABLE)
{
Puller = puller;
}
}
}

View File

@@ -0,0 +1,72 @@
#nullable enable
using Content.Shared.GameObjects.Components.Movement;
using Content.Shared.Physics.Pull;
using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Component = Robust.Shared.GameObjects.Component;
namespace Content.Shared.GameObjects.Components.Pulling
{
[RegisterComponent]
public class SharedPullerComponent : Component, IMoveSpeedModifier
{
public override string Name => "Puller";
private IEntity? _pulling;
public float WalkSpeedModifier => Pulling == null ? 1.0f : 0.75f;
public float SprintSpeedModifier => Pulling == null ? 1.0f : 0.75f;
public IEntity? Pulling
{
get => _pulling;
private set
{
if (_pulling == value)
{
return;
}
_pulling = value;
if (Owner.TryGetComponent(out MovementSpeedModifierComponent? speed))
{
speed.RefreshMovementSpeedModifiers();
}
}
}
public override void OnRemove()
{
if (Pulling != null &&
Pulling.TryGetComponent(out SharedPullableComponent? pullable))
{
pullable.TryStopPull();
}
base.OnRemove();
}
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
if (!(message is PullMessage pullMessage) ||
pullMessage.Puller.Owner != Owner)
{
return;
}
switch (message)
{
case PullStartedMessage msg:
Pulling = msg.Pulled.Owner;
break;
case PullStoppedMessage _:
Pulling = null;
break;
}
}
}
}