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:
117
Content.Shared/GameObjects/EntitySystems/SharedPullingSystem.cs
Normal file
117
Content.Shared/GameObjects/EntitySystems/SharedPullingSystem.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Content.Shared.GameObjects.Components.Pulling;
|
||||
using Content.Shared.Input;
|
||||
using Content.Shared.Physics.Pull;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameObjects.Systems;
|
||||
using Robust.Shared.Input.Binding;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Players;
|
||||
|
||||
namespace Content.Shared.GameObjects.EntitySystems
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public class SharedPullingSystem : EntitySystem
|
||||
{
|
||||
private readonly Dictionary<IEntity, IEntity> _pullers =
|
||||
new Dictionary<IEntity, IEntity>();
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<PullStartedMessage>(OnPullStarted);
|
||||
SubscribeLocalEvent<PullStoppedMessage>(OnPullStopped);
|
||||
|
||||
CommandBinds.Builder
|
||||
.Bind(ContentKeyFunctions.MovePulledObject, new PointerInputCmdHandler(HandleMovePulledObject))
|
||||
.Bind(ContentKeyFunctions.ReleasePulledObject, InputCmdHandler.FromDelegate(HandleReleasePulledObject))
|
||||
.Register<SharedPullingSystem>();
|
||||
}
|
||||
|
||||
private void OnPullStarted(PullStartedMessage message)
|
||||
{
|
||||
SetPuller(message.Puller.Owner, message.Pulled.Owner);
|
||||
}
|
||||
|
||||
private void OnPullStopped(PullStoppedMessage message)
|
||||
{
|
||||
RemovePuller(message.Puller.Owner);
|
||||
}
|
||||
|
||||
private bool HandleMovePulledObject(ICommonSession? session, EntityCoordinates coords, EntityUid uid)
|
||||
{
|
||||
var player = session?.AttachedEntity;
|
||||
|
||||
if (player == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!TryGetPulled(player, out var pulled))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!pulled.TryGetComponent(out SharedPullableComponent? pullable))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
pullable.TryMoveTo(coords);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void HandleReleasePulledObject(ICommonSession? session)
|
||||
{
|
||||
var player = session?.AttachedEntity;
|
||||
|
||||
if (player == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!TryGetPulled(player, out var pulled))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!pulled.TryGetComponent(out SharedPullableComponent? pullable))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
pullable.TryStopPull();
|
||||
}
|
||||
|
||||
private void SetPuller(IEntity puller, IEntity pulled)
|
||||
{
|
||||
_pullers[puller] = pulled;
|
||||
}
|
||||
|
||||
private bool RemovePuller(IEntity puller)
|
||||
{
|
||||
return _pullers.Remove(puller);
|
||||
}
|
||||
|
||||
public IEntity? GetPulled(IEntity by)
|
||||
{
|
||||
return _pullers.GetValueOrDefault(by);
|
||||
}
|
||||
|
||||
public bool TryGetPulled(IEntity by, [NotNullWhen(true)] out IEntity? pulled)
|
||||
{
|
||||
return (pulled = GetPulled(by)) != null;
|
||||
}
|
||||
|
||||
public bool IsPulling(IEntity puller)
|
||||
{
|
||||
return _pullers.ContainsKey(puller);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user