WiP movement prediction.

This commit is contained in:
Pieter-Jan Briers
2020-06-24 02:21:20 +02:00
parent 822436bb81
commit da45a52325
48 changed files with 1101 additions and 540 deletions

View File

@@ -1,6 +1,4 @@
using Content.Server.AI.Utility;
using Content.Server.AI.Utility.AiLogic;
using Content.Server.Interfaces.GameObjects.Components.Movement;
using Content.Shared.GameObjects.Components.Movement;
using Robust.Server.AI;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
@@ -111,7 +109,7 @@ namespace Content.Server.GameObjects.Components.Movement
/// Is the entity Sprinting (running)?
/// </summary>
[ViewVariables]
public bool Sprinting { get; set; } = true;
public bool Sprinting { get; } = true;
/// <summary>
/// Calculated linear velocity direction of the entity.
@@ -119,11 +117,14 @@ namespace Content.Server.GameObjects.Components.Movement
[ViewVariables]
public Vector2 VelocityDir { get; set; }
(Vector2 walking, Vector2 sprinting) IMoverComponent.VelocityDir =>
Sprinting ? (Vector2.Zero, VelocityDir) : (VelocityDir, Vector2.Zero);
public GridCoordinates LastPosition { get; set; }
[ViewVariables(VVAccess.ReadWrite)]
public float StepSoundDistance { get; set; }
[ViewVariables(VVAccess.ReadWrite)] public float StepSoundDistance { get; set; }
public void SetVelocityDirection(Direction direction, bool enabled) { }
public void SetVelocityDirection(Direction direction, ushort subTick, bool enabled) { }
public void SetSprinting(ushort subTick, bool enabled) { }
}
}

View File

@@ -1,93 +0,0 @@
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Movement
{
[RegisterComponent]
public class MovementSpeedModifierComponent : Component
{
public const float DefaultBaseWalkSpeed = 4.0f;
public const float DefaultBaseSprintSpeed = 7.0f;
public override string Name => "MovementSpeedModifier";
private float _cachedWalkSpeedModifier = 1.0f;
[ViewVariables]
public float WalkSpeedModifier
{
get
{
RecalculateMovementSpeedModifiers();
return _cachedWalkSpeedModifier;
}
}
private float _cachedSprintSpeedModifier;
[ViewVariables]
public float SprintSpeedModifier
{
get
{
RecalculateMovementSpeedModifiers();
return _cachedSprintSpeedModifier;
}
}
[ViewVariables(VVAccess.ReadWrite)]
public float BaseWalkSpeed { get; set; }
[ViewVariables(VVAccess.ReadWrite)]
public float BaseSprintSpeed { get; set; }
[ViewVariables]
public float CurrentWalkSpeed => WalkSpeedModifier * BaseWalkSpeed;
[ViewVariables]
public float CurrentSprintSpeed => SprintSpeedModifier * BaseSprintSpeed;
/// <summary>
/// set to warn us that a component's movespeed modifier has changed
/// </summary>
private bool _movespeedModifiersNeedRefresh = true;
public void RefreshMovementSpeedModifiers()
{
_movespeedModifiersNeedRefresh = true;
}
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(this, p => p.BaseWalkSpeed, "baseWalkSpeed", 4);
serializer.DataField(this, p => p.BaseSprintSpeed, "baseSprintSpeed", 7);
}
/// <summary>
/// Recalculate movement speed with current modifiers, or return early if no change
/// </summary>
private void RecalculateMovementSpeedModifiers()
{
{
if (!_movespeedModifiersNeedRefresh)
return;
var movespeedModifiers = Owner.GetAllComponents<IMoveSpeedModifier>();
float walkSpeedModifier = 1.0f;
float sprintSpeedModifier = 1.0f;
foreach (var component in movespeedModifiers)
{
walkSpeedModifier *= component.WalkSpeedModifier;
sprintSpeedModifier *= component.SprintSpeedModifier;
}
_cachedWalkSpeedModifier = walkSpeedModifier;
_cachedSprintSpeedModifier = sprintSpeedModifier;
}
_movespeedModifiersNeedRefresh = false;
}
}
interface IMoveSpeedModifier
{
float WalkSpeedModifier { get; }
float SprintSpeedModifier { get; }
}
}

View File

@@ -1,17 +1,11 @@

using Content.Server.Interfaces.GameObjects.Components.Movement;
using Robust.Server.GameObjects;
using Content.Shared.GameObjects.Components.Movement;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components;
using Robust.Shared.Log;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Physics;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
using Robust.Shared.IoC;
using Robust.Shared.Interfaces.Configuration;
using Robust.Shared.Configuration;
#nullable enable
namespace Content.Server.GameObjects.Components.Movement
{
@@ -20,138 +14,11 @@ namespace Content.Server.GameObjects.Components.Movement
/// </summary>
[RegisterComponent]
[ComponentReference(typeof(IMoverComponent))]
public class PlayerInputMoverComponent : Component, IMoverComponent, ICollideSpecial
public class PlayerInputMoverComponent : SharedPlayerInputMoverComponent, IMoverComponent, ICollideSpecial
{
#pragma warning disable 649
[Dependency] private readonly IConfigurationManager _configurationManager;
#pragma warning restore 649
public override GridCoordinates LastPosition { get; set; }
private bool _movingUp;
private bool _movingDown;
private bool _movingLeft;
private bool _movingRight;
/// <inheritdoc />
public override string Name => "PlayerInputMover";
/// <summary>
/// Movement speed (m/s) that the entity walks, after modifiers
/// </summary>
[ViewVariables]
public float CurrentWalkSpeed
{
get
{
if (Owner.TryGetComponent(out MovementSpeedModifierComponent component))
{
return component.CurrentWalkSpeed;
}
return MovementSpeedModifierComponent.DefaultBaseWalkSpeed;
}
}
/// <summary>
/// Movement speed (m/s) that the entity walks, after modifiers
/// </summary>
[ViewVariables]
public float CurrentSprintSpeed
{
get
{
if (Owner.TryGetComponent(out MovementSpeedModifierComponent component))
{
return component.CurrentSprintSpeed;
}
return MovementSpeedModifierComponent.DefaultBaseSprintSpeed;
}
}
/// <inheritdoc />
[ViewVariables]
public float CurrentPushSpeed => 5.0f;
/// <inheritdoc />
[ViewVariables]
public float GrabRange => 0.2f;
/// <summary>
/// Is the entity Sprinting (running)?
/// </summary>
[ViewVariables]
public bool Sprinting { get; set; } = true;
/// <summary>
/// Calculated linear velocity direction of the entity.
/// </summary>
[ViewVariables]
public Vector2 VelocityDir { get; private set; }
public GridCoordinates LastPosition { get; set; }
public float StepSoundDistance { get; set; }
/// <summary>
/// Whether or not the player can move diagonally.
/// </summary>
[ViewVariables] public bool DiagonalMovementEnabled => _configurationManager.GetCVar<bool>("game.diagonalmovement");
/// <inheritdoc />
public override void OnAdd()
{
// This component requires that the entity has a PhysicsComponent.
if (!Owner.HasComponent<PhysicsComponent>())
Logger.Error($"[ECS] {Owner.Prototype.Name} - {nameof(PlayerInputMoverComponent)} requires {nameof(PhysicsComponent)}. ");
base.OnAdd();
}
/// <summary>
/// Toggles one of the four cardinal directions. Each of the four directions are
/// composed into a single direction vector, <see cref="VelocityDir"/>. Enabling
/// opposite directions will cancel each other out, resulting in no direction.
/// </summary>
/// <param name="direction">Direction to toggle.</param>
/// <param name="enabled">If the direction is active.</param>
public void SetVelocityDirection(Direction direction, bool enabled)
{
switch (direction)
{
case Direction.East:
_movingRight = enabled;
break;
case Direction.North:
_movingUp = enabled;
break;
case Direction.West:
_movingLeft = enabled;
break;
case Direction.South:
_movingDown = enabled;
break;
}
// key directions are in screen coordinates
// _moveDir is in world coordinates
// if the camera is moved, this needs to be changed
var x = 0;
x -= _movingLeft ? 1 : 0;
x += _movingRight ? 1 : 0;
var y = 0;
if (DiagonalMovementEnabled || x == 0)
{
y -= _movingDown ? 1 : 0;
y += _movingUp ? 1 : 0;
}
VelocityDir = new Vector2(x, y);
// can't normalize zero length vector
if (VelocityDir.LengthSquared > 1.0e-6)
VelocityDir = VelocityDir.Normalized;
}
public override float StepSoundDistance { get; set; }
/// <summary>
/// Special collision override, can be used to give custom behaviors deciding when to collide
@@ -167,6 +34,5 @@ namespace Content.Server.GameObjects.Components.Movement
}
return false;
}
}
}

View File

@@ -1,5 +1,5 @@
using Content.Server.GameObjects.Components.Mobs;
using Content.Server.Interfaces.GameObjects.Components.Movement;
using Content.Shared.GameObjects.Components.Movement;
using Robust.Server.GameObjects;
using Robust.Server.GameObjects.Components.Container;
using Robust.Shared.GameObjects;
@@ -44,11 +44,11 @@ namespace Content.Server.GameObjects.Components.Movement
public float GrabRange => 0.0f;
public bool Sprinting { get; set; }
public Vector2 VelocityDir { get; } = Vector2.Zero;
public (Vector2 walking, Vector2 sprinting) VelocityDir { get; } = (Vector2.Zero, Vector2.Zero);
public GridCoordinates LastPosition { get; set; }
public float StepSoundDistance { get; set; }
public void SetVelocityDirection(Direction direction, bool enabled)
public void SetVelocityDirection(Direction direction, ushort subTick, bool enabled)
{
var gridId = Owner.Transform.GridID;
@@ -74,6 +74,11 @@ namespace Content.Server.GameObjects.Components.Movement
}
}
public void SetSprinting(ushort subTick, bool enabled)
{
// Shuttles can't sprint.
}
private Vector2 CalcNewVelocity(Direction direction, bool enabled)
{
switch (direction)