Network MovementSpeedModifierComponent (#5100)

This commit is contained in:
metalgearsloth
2021-11-01 23:24:00 +11:00
committed by GitHub
parent 6fbb0d195b
commit 012be96877
2 changed files with 67 additions and 5 deletions

View File

@@ -1,17 +1,18 @@
using Robust.Shared.Containers; using Robust.Shared.Containers;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization.Manager.Attributes; using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables; using Robust.Shared.ViewVariables;
namespace Content.Shared.Movement.Components namespace Content.Shared.Movement.Components
{ {
[RegisterComponent] [RegisterComponent]
public class MovementSpeedModifierComponent : Component [NetworkedComponent]
public sealed class MovementSpeedModifierComponent : Component
{ {
public const float DefaultBaseWalkSpeed = 4.0f; public const float DefaultBaseWalkSpeed = 4.0f;
public const float DefaultBaseSprintSpeed = 7.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;
@@ -36,12 +37,32 @@ namespace Content.Shared.Movement.Components
} }
[ViewVariables(VVAccess.ReadWrite)] [ViewVariables(VVAccess.ReadWrite)]
[DataField("baseWalkSpeed")] public float BaseWalkSpeedVV
public float BaseWalkSpeed { get; set; } = 4; {
get => BaseWalkSpeed;
set
{
BaseWalkSpeed = value;
Dirty();
}
}
[ViewVariables(VVAccess.ReadWrite)] [ViewVariables(VVAccess.ReadWrite)]
public float BaseSprintSpeedVV
{
get => BaseSprintSpeed;
set
{
BaseSprintSpeed = value;
Dirty();
}
}
[DataField("baseWalkSpeed")]
public float BaseWalkSpeed { get; set; } = DefaultBaseWalkSpeed;
[DataField("baseSprintSpeed")] [DataField("baseSprintSpeed")]
public float BaseSprintSpeed { get; set; } = 7; public float BaseSprintSpeed { get; set; } = DefaultBaseSprintSpeed;
[ViewVariables] [ViewVariables]
public float CurrentWalkSpeed => WalkSpeedModifier * BaseWalkSpeed; public float CurrentWalkSpeed => WalkSpeedModifier * BaseWalkSpeed;

View File

@@ -0,0 +1,41 @@
using System;
using Content.Shared.Movement.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
namespace Content.Shared.Movement.EntitySystems
{
public sealed class MovementSpeedModifierSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<MovementSpeedModifierComponent, ComponentGetState>(OnGetState);
SubscribeLocalEvent<MovementSpeedModifierComponent, ComponentHandleState>(OnHandleState);
}
private void OnGetState(EntityUid uid, MovementSpeedModifierComponent component, ref ComponentGetState args)
{
args.State = new MovementSpeedModifierComponentState
{
BaseWalkSpeed = component.BaseWalkSpeed,
BaseSprintSpeed = component.BaseSprintSpeed,
};
}
private void OnHandleState(EntityUid uid, MovementSpeedModifierComponent component, ref ComponentHandleState args)
{
if (args.Current is not MovementSpeedModifierComponentState state) return;
component.BaseWalkSpeed = state.BaseWalkSpeed;
component.BaseSprintSpeed = state.BaseSprintSpeed;
}
[Serializable, NetSerializable]
private sealed class MovementSpeedModifierComponentState : ComponentState
{
public float BaseWalkSpeed;
public float BaseSprintSpeed;
}
}
}