MovementSpeedModifierComponent (#721)

* movement modifier system

* update default exposedata values

* changing base speed marks as dirty

* formatting

* movement speed modifier component

* Moves slowdown component to the prototype

AIController respects slowdown

* reset dirty flag
This commit is contained in:
4dplanner
2020-02-22 23:37:56 +00:00
committed by GitHub
parent ffe55648b6
commit b0e3ab3393
9 changed files with 231 additions and 49 deletions

View File

@@ -57,16 +57,49 @@ namespace Content.Server.GameObjects.Components.Movement
}
/// <summary>
/// Movement speed (m/s) that the entity walks.
/// Movement speed (m/s) that the entity walks, before modifiers
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public float WalkMoveSpeed { get; set; } = 4.0f;
public float BaseWalkSpeed { get; set; } = PlayerInputMoverComponent.DefaultBaseWalkSpeed;
/// <summary>
/// Movement speed (m/s) that the entity sprints.
/// Movement speed (m/s) that the entity sprints, before modifiers
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public float SprintMoveSpeed { get; set; } = 10.0f;
public float BaseSprintSpeed { get; set; } = PlayerInputMoverComponent.DefaultBaseSprintSpeed;
/// <summary>
/// Movement speed (m/s) that the entity walks, after modifiers
/// </summary>
[ViewVariables]
public float CurrentWalkSpeed
{
get
{
float speed = BaseWalkSpeed;
if (Owner.TryGetComponent<MovementSpeedModifierComponent>(out MovementSpeedModifierComponent component))
{
speed *= component.WalkSpeedModifier;
}
return speed;
}
}
/// <summary>
/// Movement speed (m/s) that the entity walks, after modifiers
/// </summary>
[ViewVariables]
public float CurrentSprintSpeed
{
get
{
float speed = BaseSprintSpeed;
if (Owner.TryGetComponent<MovementSpeedModifierComponent>(out MovementSpeedModifierComponent component))
{
speed *= component.SprintSpeedModifier;
}
return speed;
}
}
/// <summary>
/// Is the entity Sprinting (running)?

View File

@@ -0,0 +1,68 @@
using Robust.Shared.GameObjects;
namespace Content.Server.GameObjects.Components.Movement
{
[RegisterComponent]
public class MovementSpeedModifierComponent : Component
{
public override string Name => "MovementSpeedModifier";
private float _cachedWalkSpeedModifier = 1.0f;
public float WalkSpeedModifier
{
get
{
RecalculateMovementSpeedModifiers();
return _cachedWalkSpeedModifier;
}
}
private float _cachedSprintSpeedModifier;
public float SprintSpeedModifier
{
get
{
RecalculateMovementSpeedModifiers();
return _cachedSprintSpeedModifier;
}
}
/// <summary>
/// set to warn us that a component's movespeed modifier has changed
/// </summary>
private bool _movespeedModifiersNeedRefresh = true;
public void RefreshMovementSpeedModifiers()
{
_movespeedModifiersNeedRefresh = true;
}
/// <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

@@ -22,6 +22,8 @@ namespace Content.Server.GameObjects.Components.Movement
[ComponentReference(typeof(IMoverComponent))]
public class PlayerInputMoverComponent : Component, IMoverComponent, ICollideSpecial
{
public const float DefaultBaseWalkSpeed = 4.0f;
public const float DefaultBaseSprintSpeed = 7.0f;
#pragma warning disable 649
[Dependency] private readonly IConfigurationManager _configurationManager;
@@ -35,17 +37,51 @@ namespace Content.Server.GameObjects.Components.Movement
/// <inheritdoc />
public override string Name => "PlayerInputMover";
/// <summary>
/// Movement speed (m/s) that the entity walks.
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public float WalkMoveSpeed { get; set; } = 4.0f;
/// <summary>
/// Movement speed (m/s) that the entity sprints.
/// Movement speed (m/s) that the entity walks, before modifiers
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public float SprintMoveSpeed { get; set; } = 7.0f;
public float BaseWalkSpeed { get; set; } = DefaultBaseWalkSpeed;
/// <summary>
/// Movement speed (m/s) that the entity sprints, before modifiers
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public float BaseSprintSpeed { get; set; } = DefaultBaseSprintSpeed;
/// <summary>
/// Movement speed (m/s) that the entity walks, after modifiers
/// </summary>
[ViewVariables]
public float CurrentWalkSpeed
{
get
{
float speed = BaseWalkSpeed;
if (Owner.TryGetComponent<MovementSpeedModifierComponent>(out MovementSpeedModifierComponent component))
{
speed *= component.WalkSpeedModifier;
}
return speed;
}
}
/// <summary>
/// Movement speed (m/s) that the entity walks, after modifiers
/// </summary>
[ViewVariables]
public float CurrentSprintSpeed
{
get
{
float speed = BaseSprintSpeed;
if (Owner.TryGetComponent<MovementSpeedModifierComponent>(out MovementSpeedModifierComponent component))
{
speed *= component.SprintSpeedModifier;
}
return speed;
}
}
/// <summary>
/// Is the entity Sprinting (running)?
@@ -89,13 +125,15 @@ namespace Content.Server.GameObjects.Components.Movement
{
base.ExposeData(serializer);
serializer.DataReadWriteFunction("wspd", 4.0f, value => WalkMoveSpeed = value, () => WalkMoveSpeed);
serializer.DataReadWriteFunction("rspd", 10.0f, value => SprintMoveSpeed = value, () => SprintMoveSpeed);
//only save the base speeds - the current speeds are transient.
serializer.DataReadWriteFunction("wspd", DefaultBaseWalkSpeed, value => BaseWalkSpeed = value, () => BaseWalkSpeed);
serializer.DataReadWriteFunction("rspd", DefaultBaseSprintSpeed, value => BaseSprintSpeed = value, () => BaseSprintSpeed);
// The velocity and moving directions is usually set from player or AI input,
// so we don't want to save/load these derived fields.
}
/// <summary>
/// Toggles one of the four cardinal directions. Each of the four directions are
/// composed into a single direction vector, <see cref="VelocityDir"/>. Enabling
@@ -150,7 +188,7 @@ namespace Content.Server.GameObjects.Components.Movement
/// <returns></returns>
bool ICollideSpecial.PreventCollide(IPhysBody collidedwith)
{
// Don't collid with other mobs
// Don't collide with other mobs
if (collidedwith.Owner.TryGetComponent<SpeciesComponent>(out var collidedSpeciesComponent))
{
return true;

View File

@@ -32,8 +32,8 @@ namespace Content.Server.GameObjects.Components.Movement
public override string Name => "ShuttleController";
[ViewVariables(VVAccess.ReadWrite)]
public float WalkMoveSpeed { get; set; } = 8;
public float SprintMoveSpeed { get; set; }
public float CurrentWalkSpeed { get; set; } = 8;
public float CurrentSprintSpeed { get; set; }
public bool Sprinting { get; set; }
public Vector2 VelocityDir { get; } = Vector2.Zero;
public GridCoordinates LastPosition { get; set; }
@@ -61,7 +61,7 @@ namespace Content.Server.GameObjects.Components.Movement
collideComp.PhysicsShapes.Add(new PhysShapeGrid(grid));
}
physComp.LinearVelocity = CalcNewVelocity(direction, enabled) * WalkMoveSpeed;
physComp.LinearVelocity = CalcNewVelocity(direction, enabled) * CurrentWalkSpeed;
}
}