Put base movement in control of MovementSpeedModifierComponent.

PlayerInputMoverComponent was unsuitable because it gets added/removed dynamically and is specific to players (AIs don't use it).
This commit is contained in:
Pieter-Jan Briers
2020-04-18 13:46:35 +02:00
parent 028ca7a732
commit 1578a32fa1
3 changed files with 42 additions and 58 deletions

View File

@@ -56,18 +56,6 @@ namespace Content.Server.GameObjects.Components.Movement
serializer.DataField(ref _visionRadius, "vision", 8.0f); serializer.DataField(ref _visionRadius, "vision", 8.0f);
} }
/// <summary>
/// Movement speed (m/s) that the entity walks, before modifiers
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public float BaseWalkSpeed { get; set; } = PlayerInputMoverComponent.DefaultBaseWalkSpeed;
/// <summary>
/// Movement speed (m/s) that the entity sprints, before modifiers
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
public float BaseSprintSpeed { get; set; } = PlayerInputMoverComponent.DefaultBaseSprintSpeed;
/// <summary> /// <summary>
/// Movement speed (m/s) that the entity walks, after modifiers /// Movement speed (m/s) that the entity walks, after modifiers
/// </summary> /// </summary>
@@ -76,14 +64,15 @@ namespace Content.Server.GameObjects.Components.Movement
{ {
get get
{ {
float speed = BaseWalkSpeed; if (Owner.TryGetComponent(out MovementSpeedModifierComponent component))
if (Owner.TryGetComponent<MovementSpeedModifierComponent>(out MovementSpeedModifierComponent component))
{ {
speed *= component.WalkSpeedModifier; return component.CurrentWalkSpeed;
} }
return speed;
return MovementSpeedModifierComponent.DefaultBaseWalkSpeed;
} }
} }
/// <summary> /// <summary>
/// Movement speed (m/s) that the entity walks, after modifiers /// Movement speed (m/s) that the entity walks, after modifiers
/// </summary> /// </summary>
@@ -92,12 +81,12 @@ namespace Content.Server.GameObjects.Components.Movement
{ {
get get
{ {
float speed = BaseSprintSpeed; if (Owner.TryGetComponent(out MovementSpeedModifierComponent component))
if (Owner.TryGetComponent<MovementSpeedModifierComponent>(out MovementSpeedModifierComponent component))
{ {
speed *= component.SprintSpeedModifier; return component.CurrentSprintSpeed;
} }
return speed;
return MovementSpeedModifierComponent.DefaultBaseSprintSpeed;
} }
} }

View File

@@ -1,14 +1,20 @@
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Movement namespace Content.Server.GameObjects.Components.Movement
{ {
[RegisterComponent] [RegisterComponent]
public class MovementSpeedModifierComponent : Component public class MovementSpeedModifierComponent : Component
{ {
public const float DefaultBaseWalkSpeed = 4.0f;
public const float DefaultBaseSprintSpeed = 7.0f;
public override string Name => "MovementSpeedModifier"; public override string Name => "MovementSpeedModifier";
private float _cachedWalkSpeedModifier = 1.0f; private float _cachedWalkSpeedModifier = 1.0f;
[ViewVariables]
public float WalkSpeedModifier public float WalkSpeedModifier
{ {
get get
@@ -18,6 +24,7 @@ namespace Content.Server.GameObjects.Components.Movement
} }
} }
private float _cachedSprintSpeedModifier; private float _cachedSprintSpeedModifier;
[ViewVariables]
public float SprintSpeedModifier public float SprintSpeedModifier
{ {
get get
@@ -27,6 +34,16 @@ namespace Content.Server.GameObjects.Components.Movement
} }
} }
[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> /// <summary>
/// set to warn us that a component's movespeed modifier has changed /// set to warn us that a component's movespeed modifier has changed
/// </summary> /// </summary>
@@ -37,6 +54,14 @@ namespace Content.Server.GameObjects.Components.Movement
_movespeedModifiersNeedRefresh = true; _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> /// <summary>
/// Recalculate movement speed with current modifiers, or return early if no change /// Recalculate movement speed with current modifiers, or return early if no change
/// </summary> /// </summary>

View File

@@ -22,9 +22,6 @@ 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;
#pragma warning restore 649 #pragma warning restore 649
@@ -37,19 +34,6 @@ 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, before modifiers
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
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> /// <summary>
/// Movement speed (m/s) that the entity walks, after modifiers /// Movement speed (m/s) that the entity walks, after modifiers
/// </summary> /// </summary>
@@ -58,12 +42,12 @@ namespace Content.Server.GameObjects.Components.Movement
{ {
get get
{ {
float speed = BaseWalkSpeed; if (Owner.TryGetComponent(out MovementSpeedModifierComponent component))
if (Owner.TryGetComponent<MovementSpeedModifierComponent>(out MovementSpeedModifierComponent component))
{ {
speed *= component.WalkSpeedModifier; return component.CurrentWalkSpeed;
} }
return speed;
return MovementSpeedModifierComponent.DefaultBaseWalkSpeed;
} }
} }
/// <summary> /// <summary>
@@ -74,12 +58,12 @@ namespace Content.Server.GameObjects.Components.Movement
{ {
get get
{ {
float speed = BaseSprintSpeed; if (Owner.TryGetComponent(out MovementSpeedModifierComponent component))
if (Owner.TryGetComponent<MovementSpeedModifierComponent>(out MovementSpeedModifierComponent component))
{ {
speed *= component.SprintSpeedModifier; return component.CurrentSprintSpeed;
} }
return speed;
return MovementSpeedModifierComponent.DefaultBaseSprintSpeed;
} }
} }
@@ -114,20 +98,6 @@ namespace Content.Server.GameObjects.Components.Movement
base.OnAdd(); base.OnAdd();
} }
/// <inheritdoc />
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
//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> /// <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