* Add nullable to some Content.Shared files. * Use [NotNullWhen(true)] * Undo adding now redundant !'s * Forgot one * Add a ton more nullable * You can guess * Fix some issues * It actually compiles now * Auto stash before merge of "null2" and "origin/master" * I lied * enable annotations -> enable * Revert ActionBlockerSystem.cs to original * Fix ActionBlockerSystem.cs * More nullable * Undo some added exclamation marks * Fix issues * Update Content.Shared/Maps/ContentTileDefinition.cs Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> * Resolve some issues * Remove unused method * Fix more issues * Fix more issues * Fix more issues * Fix more issues * Fix issue, rollback SharedGhostComponent.cs * Update submodule * Fix issue, invert some if-statements to reduce nesting * Revert RobustToolbox * FIx things broken by merge * Some fixes - Replaced with string.Empty - Remove some exclamation marks - Revert file * Some fixes * Trivial #nullable enable * Fix null ables Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
105 lines
3.4 KiB
C#
105 lines
3.4 KiB
C#
#nullable enable
|
|
using Robust.Shared.Containers;
|
|
using Robust.Shared.GameObjects;
|
|
using Robust.Shared.Serialization;
|
|
using Robust.Shared.ViewVariables;
|
|
|
|
namespace Content.Shared.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 static void RefreshItemModifiers(IEntity item)
|
|
{
|
|
if (item.TryGetContainer(out var container) &&
|
|
container.Owner.TryGetComponent(out MovementSpeedModifierComponent? mod))
|
|
{
|
|
mod.RefreshMovementSpeedModifiers();
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
public interface IMoveSpeedModifier
|
|
{
|
|
float WalkSpeedModifier { get; }
|
|
float SprintSpeedModifier { get; }
|
|
}
|
|
}
|