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

View File

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

View File

@@ -14,11 +14,11 @@ using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Nutrition namespace Content.Server.GameObjects.Components.Nutrition
{ {
[RegisterComponent] [RegisterComponent]
public sealed class HungerComponent : Component public sealed class HungerComponent : Component, IMoveSpeedModifier
{ {
#pragma warning disable 649 #pragma warning disable 649
[Dependency] private readonly IRobustRandom _random; [Dependency] private readonly IRobustRandom _random;
#pragma warning restore 649 #pragma warning restore 649
public override string Name => "Hunger"; public override string Name => "Hunger";
@@ -33,7 +33,7 @@ namespace Content.Server.GameObjects.Components.Nutrition
private HungerThreshold _currentHungerThreshold; private HungerThreshold _currentHungerThreshold;
private HungerThreshold _lastHungerThreshold; private HungerThreshold _lastHungerThreshold;
public float CurrentHunger => _currentHunger; public float CurrentHunger => _currentHunger;
[ViewVariables] private float _currentHunger; [ViewVariables(VVAccess.ReadWrite)] private float _currentHunger;
public Dictionary<HungerThreshold, float> HungerThresholds => _hungerThresholds; public Dictionary<HungerThreshold, float> HungerThresholds => _hungerThresholds;
private Dictionary<HungerThreshold, float> _hungerThresholds = new Dictionary<HungerThreshold, float> private Dictionary<HungerThreshold, float> _hungerThresholds = new Dictionary<HungerThreshold, float>
@@ -53,16 +53,15 @@ namespace Content.Server.GameObjects.Components.Nutrition
public void HungerThresholdEffect(bool force = false) public void HungerThresholdEffect(bool force = false)
{ {
if (_currentHungerThreshold != _lastHungerThreshold || force) { if (_currentHungerThreshold != _lastHungerThreshold || force)
{
Logger.InfoS("hunger", $"Updating hunger state for {Owner.Name}"); Logger.InfoS("hunger", $"Updating hunger state for {Owner.Name}");
// Revert slow speed if required // Revert slow speed if required
if (_lastHungerThreshold == HungerThreshold.Starving && _currentHungerThreshold != HungerThreshold.Dead && if (_lastHungerThreshold == HungerThreshold.Starving && _currentHungerThreshold != HungerThreshold.Dead &&
Owner.TryGetComponent(out PlayerInputMoverComponent playerSpeedupComponent)) Owner.TryGetComponent(out MovementSpeedModifierComponent movementSlowdownComponent))
{ {
// TODO shitcode: Come up something better movementSlowdownComponent.RefreshMovementSpeedModifiers();
playerSpeedupComponent.WalkMoveSpeed = playerSpeedupComponent.WalkMoveSpeed * 2;
playerSpeedupComponent.SprintMoveSpeed = playerSpeedupComponent.SprintMoveSpeed * 4;
} }
// Update UI // Update UI
@@ -91,9 +90,9 @@ namespace Content.Server.GameObjects.Components.Nutrition
case HungerThreshold.Starving: case HungerThreshold.Starving:
// TODO: If something else bumps this could cause mega-speed. // TODO: If something else bumps this could cause mega-speed.
// If some form of speed update system if multiple things are touching it use that. // If some form of speed update system if multiple things are touching it use that.
if (Owner.TryGetComponent(out PlayerInputMoverComponent playerInputMoverComponent)) { if (Owner.TryGetComponent(out MovementSpeedModifierComponent movementSlowdownComponent1))
playerInputMoverComponent.WalkMoveSpeed = playerInputMoverComponent.WalkMoveSpeed / 2; {
playerInputMoverComponent.SprintMoveSpeed = playerInputMoverComponent.SprintMoveSpeed / 4; movementSlowdownComponent1.RefreshMovementSpeedModifiers();
} }
_lastHungerThreshold = _currentHungerThreshold; _lastHungerThreshold = _currentHungerThreshold;
_actualDecayRate = _baseDecayRate * 0.6f; _actualDecayRate = _baseDecayRate * 0.6f;
@@ -169,6 +168,29 @@ namespace Content.Server.GameObjects.Components.Nutrition
{ {
_currentHunger = HungerThresholds[HungerThreshold.Okay]; _currentHunger = HungerThresholds[HungerThreshold.Okay];
} }
float IMoveSpeedModifier.WalkSpeedModifier
{
get
{
if (_currentHungerThreshold == HungerThreshold.Starving)
{
return 0.5f;
}
return 1.0f;
}
}
float IMoveSpeedModifier.SprintSpeedModifier
{
get
{
if (_currentHungerThreshold == HungerThreshold.Starving)
{
return 0.5f;
}
return 1.0f;
}
}
} }
public enum HungerThreshold public enum HungerThreshold

View File

@@ -14,11 +14,11 @@ using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Nutrition namespace Content.Server.GameObjects.Components.Nutrition
{ {
[RegisterComponent] [RegisterComponent]
public sealed class ThirstComponent : Component public sealed class ThirstComponent : Component, IMoveSpeedModifier
{ {
#pragma warning disable 649 #pragma warning disable 649
[Dependency] private readonly IRobustRandom _random; [Dependency] private readonly IRobustRandom _random;
#pragma warning restore 649 #pragma warning restore 649
public override string Name => "Thirst"; public override string Name => "Thirst";
@@ -53,16 +53,15 @@ namespace Content.Server.GameObjects.Components.Nutrition
public void ThirstThresholdEffect(bool force = false) public void ThirstThresholdEffect(bool force = false)
{ {
if (_currentThirstThreshold != _lastThirstThreshold || force) { if (_currentThirstThreshold != _lastThirstThreshold || force)
{
Logger.InfoS("thirst", $"Updating Thirst state for {Owner.Name}"); Logger.InfoS("thirst", $"Updating Thirst state for {Owner.Name}");
// Revert slow speed if required // Revert slow speed if required
if (_lastThirstThreshold == ThirstThreshold.Parched && _currentThirstThreshold != ThirstThreshold.Dead && if (_lastThirstThreshold == ThirstThreshold.Parched && _currentThirstThreshold != ThirstThreshold.Dead &&
Owner.TryGetComponent(out PlayerInputMoverComponent playerSpeedupComponent)) Owner.TryGetComponent(out MovementSpeedModifierComponent movementSlowdownComponent))
{ {
// TODO shitcode: Come up something better movementSlowdownComponent.RefreshMovementSpeedModifiers();
playerSpeedupComponent.WalkMoveSpeed = playerSpeedupComponent.WalkMoveSpeed * 2;
playerSpeedupComponent.SprintMoveSpeed = playerSpeedupComponent.SprintMoveSpeed * 4;
} }
// Update UI // Update UI
@@ -89,11 +88,9 @@ namespace Content.Server.GameObjects.Components.Nutrition
return; return;
case ThirstThreshold.Parched: case ThirstThreshold.Parched:
// TODO: If something else bumps this could cause mega-speed. if (Owner.TryGetComponent(out MovementSpeedModifierComponent movementSlowdownComponent1))
// If some form of speed update system if multiple things are touching it use that. {
if (Owner.TryGetComponent(out PlayerInputMoverComponent playerInputMoverComponent)) { movementSlowdownComponent1.RefreshMovementSpeedModifiers();
playerInputMoverComponent.WalkMoveSpeed = playerInputMoverComponent.WalkMoveSpeed / 2;
playerInputMoverComponent.SprintMoveSpeed = playerInputMoverComponent.SprintMoveSpeed / 4;
} }
_lastThirstThreshold = _currentThirstThreshold; _lastThirstThreshold = _currentThirstThreshold;
_actualDecayRate = _baseDecayRate * 0.6f; _actualDecayRate = _baseDecayRate * 0.6f;
@@ -166,6 +163,29 @@ namespace Content.Server.GameObjects.Components.Nutrition
} }
} }
float IMoveSpeedModifier.SprintSpeedModifier
{
get
{
if (_currentThirstThreshold == ThirstThreshold.Parched)
{
return 0.25f;
}
return 1.0f;
}
}
float IMoveSpeedModifier.WalkSpeedModifier
{
get
{
if (_currentThirstThreshold == ThirstThreshold.Parched)
{
return 0.5f;
}
return 1.0f;
}
}
public void ResetThirst() public void ResetThirst()
{ {
_currentThirst = ThirstThresholds[ThirstThreshold.Okay]; _currentThirst = ThirstThresholds[ThirstThreshold.Okay];

View File

@@ -90,7 +90,7 @@ namespace Content.Server.GameObjects.EntitySystems
private static void PlayerDetached(PlayerDetachedSystemMessage ev) private static void PlayerDetached(PlayerDetachedSystemMessage ev)
{ {
if(ev.Entity.HasComponent<PlayerInputMoverComponent>()) if (ev.Entity.HasComponent<PlayerInputMoverComponent>())
{ {
ev.Entity.RemoveComponent<PlayerInputMoverComponent>(); ev.Entity.RemoveComponent<PlayerInputMoverComponent>();
} }
@@ -136,7 +136,7 @@ namespace Content.Server.GameObjects.EntitySystems
} }
else else
{ {
physics.LinearVelocity = mover.VelocityDir * (mover.Sprinting ? mover.SprintMoveSpeed : mover.WalkMoveSpeed); physics.LinearVelocity = mover.VelocityDir * (mover.Sprinting ? mover.CurrentSprintSpeed : mover.CurrentWalkSpeed);
transform.LocalRotation = mover.VelocityDir.GetDir().ToAngle(); transform.LocalRotation = mover.VelocityDir.GetDir().ToAngle();
// Handle footsteps. // Handle footsteps.
@@ -177,7 +177,7 @@ namespace Content.Server.GameObjects.EntitySystems
private static void HandleDirChange(ICommonSession session, Direction dir, bool state) private static void HandleDirChange(ICommonSession session, Direction dir, bool state)
{ {
if(!TryGetAttachedComponent(session as IPlayerSession, out IMoverComponent moverComp)) if (!TryGetAttachedComponent(session as IPlayerSession, out IMoverComponent moverComp))
return; return;
moverComp.SetVelocityDirection(dir, state); moverComp.SetVelocityDirection(dir, state);
@@ -185,14 +185,14 @@ namespace Content.Server.GameObjects.EntitySystems
private static void HandleRunChange(ICommonSession session, bool running) private static void HandleRunChange(ICommonSession session, bool running)
{ {
if(!TryGetAttachedComponent(session as IPlayerSession, out PlayerInputMoverComponent moverComp)) if (!TryGetAttachedComponent(session as IPlayerSession, out PlayerInputMoverComponent moverComp))
return; return;
moverComp.Sprinting = running; moverComp.Sprinting = running;
} }
private static bool TryGetAttachedComponent<T>(IPlayerSession session, out T component) private static bool TryGetAttachedComponent<T>(IPlayerSession session, out T component)
where T: IComponent where T : IComponent
{ {
component = default; component = default;

View File

@@ -12,12 +12,12 @@ namespace Content.Server.Interfaces.GameObjects.Components.Movement
/// <summary> /// <summary>
/// Movement speed (m/s) that the entity walks. /// Movement speed (m/s) that the entity walks.
/// </summary> /// </summary>
float WalkMoveSpeed { get; set; } float CurrentWalkSpeed { get; }
/// <summary> /// <summary>
/// Movement speed (m/s) that the entity sprints. /// Movement speed (m/s) that the entity sprints.
/// </summary> /// </summary>
float SprintMoveSpeed { get; set; } float CurrentSprintSpeed { get; }
/// <summary> /// <summary>
/// Is the entity Sprinting (running)? /// Is the entity Sprinting (running)?

View File

@@ -9,6 +9,7 @@
hands: hands:
- left - left
- right - right
- type: MovementSpeedModifier
- type: Hunger - type: Hunger
- type: Thirst - type: Thirst
# Organs # Organs