Remove IMoveSpeedModifier in favor of events (#5212)

* Remove IMoveSpeedModifier

* fucking magboots

* yope

* rabiews
This commit is contained in:
mirrorcult
2021-11-07 22:17:35 -07:00
committed by GitHub
parent 3612d25539
commit 2d3077f560
26 changed files with 236 additions and 277 deletions

View File

@@ -4,6 +4,7 @@ using Robust.Shared.Players;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
using System;
using Robust.Shared.Analyzers;
using Robust.Shared.GameStates;
using Robust.Shared.Timing;
using Robust.Shared.IoC;
@@ -13,7 +14,7 @@ namespace Content.Shared.Chemistry.Components
//TODO: refactor movement modifier component because this is a pretty poor solution
[RegisterComponent]
[NetworkedComponent]
public sealed class MovespeedModifierMetabolismComponent : Component, IMoveSpeedModifier
public sealed class MovespeedModifierMetabolismComponent : Component
{
[ViewVariables]
public override string Name => "MovespeedModifierMetabolism";

View File

@@ -6,13 +6,16 @@ using Robust.Shared.Timing;
using System.Collections.Generic;
using System.Linq;
using Content.Shared.Movement.Components;
using Content.Shared.Movement.EntitySystems;
using static Content.Shared.Chemistry.Components.MovespeedModifierMetabolismComponent;
namespace Content.Shared.Chemistry
{
// TODO CONVERT THIS TO A STATUS EFFECT!!!!!!!!!!!!!!!!!!!!!!!!
public class MetabolismMovespeedModifierSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly MovementSpeedModifierSystem _movespeed = default!;
private readonly List<MovespeedModifierMetabolismComponent> _components = new();
@@ -22,6 +25,7 @@ namespace Content.Shared.Chemistry
SubscribeLocalEvent<MovespeedModifierMetabolismComponent, ComponentHandleState>(OnMovespeedHandleState);
SubscribeLocalEvent<MovespeedModifierMetabolismComponent, ComponentStartup>(AddComponent);
SubscribeLocalEvent<MovespeedModifierMetabolismComponent, RefreshMovementSpeedModifiersEvent>(OnRefreshMovespeed);
}
private void OnMovespeedHandleState(EntityUid uid, MovespeedModifierMetabolismComponent component, ref ComponentHandleState args)
@@ -33,7 +37,7 @@ namespace Content.Shared.Chemistry
(!component.WalkSpeedModifier.Equals(cast.WalkSpeedModifier) ||
!component.SprintSpeedModifier.Equals(cast.SprintSpeedModifier)))
{
modifier.RefreshMovementSpeedModifiers();
_movespeed.RefreshMovementSpeedModifiers(uid);
}
component.WalkSpeedModifier = cast.WalkSpeedModifier;
@@ -41,6 +45,12 @@ namespace Content.Shared.Chemistry
component.ModifierTimer = cast.ModifierTimer;
}
private void OnRefreshMovespeed(EntityUid uid, MovespeedModifierMetabolismComponent component, RefreshMovementSpeedModifiersEvent args)
{
args.ModifySpeed(component.WalkSpeedModifier, component.SprintSpeedModifier);
}
private void AddComponent(EntityUid uid, MovespeedModifierMetabolismComponent component, ComponentStartup args)
{
_components.Add(component);
@@ -65,12 +75,9 @@ namespace Content.Shared.Chemistry
if (component.ModifierTimer > currentTime) continue;
_components.RemoveAt(i);
EntityManager.RemoveComponent<MovespeedModifierMetabolismComponent>(component.Owner.Uid);
EntityManager.RemoveComponent<MovespeedModifierMetabolismComponent>(component.OwnerUid);
if (component.Owner.TryGetComponent(out MovementSpeedModifierComponent? modifier))
{
modifier.RefreshMovementSpeedModifiers();
}
_movespeed.RefreshMovementSpeedModifiers(component.OwnerUid);
}
}
}

View File

@@ -1,5 +1,7 @@
using System;
using Content.Shared.Movement.Components;
using Content.Shared.Movement.EntitySystems;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
@@ -7,16 +9,19 @@ using Robust.Shared.Serialization;
namespace Content.Shared.Clothing
{
[NetworkedComponent()]
public abstract class SharedMagbootsComponent : Component, IMoveSpeedModifier
public abstract class SharedMagbootsComponent : Component
{
public sealed override string Name => "Magboots";
public abstract bool On { get; set; }
protected void OnChanged()
{
MovementSpeedModifierComponent.RefreshItemModifiers(Owner);
// inventory system will automatically hook into the event raised by this and update accordingly
if (Owner.TryGetContainer(out var container))
{
EntitySystem.Get<MovementSpeedModifierSystem>().RefreshMovementSpeedModifiers(container.Owner.Uid);
}
}
public float WalkSpeedModifier => On ? 0.85f : 1;

View File

@@ -14,7 +14,7 @@ using static Content.Shared.Inventory.EquipmentSlotDefines;
namespace Content.Shared.Inventory
{
[NetworkedComponent()]
public abstract class SharedInventoryComponent : Component, IMoveSpeedModifier
public abstract class SharedInventoryComponent : Component
{
[Dependency] protected readonly IReflectionManager ReflectionManager = default!;
[Dependency] protected readonly IDynamicTypeFactory DynamicTypeFactory = default!;
@@ -98,8 +98,5 @@ namespace Content.Shared.Inventory
Slot = slot;
}
}
public abstract float WalkSpeedModifier { get; }
public abstract float SprintSpeedModifier { get; }
}
}

View File

@@ -1,3 +1,5 @@
using Content.Shared.Movement.EntitySystems;
using Robust.Shared.Analyzers;
using Robust.Shared.Containers;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
@@ -7,7 +9,7 @@ using Robust.Shared.ViewVariables;
namespace Content.Shared.Movement.Components
{
[RegisterComponent]
[NetworkedComponent]
[NetworkedComponent, Friend(typeof(MovementSpeedModifierSystem))]
public sealed class MovementSpeedModifierComponent : Component
{
public const float DefaultBaseWalkSpeed = 4.0f;
@@ -15,26 +17,11 @@ namespace Content.Shared.Movement.Components
public override string Name => "MovementSpeedModifier";
private float _cachedWalkSpeedModifier = 1.0f;
[ViewVariables]
public float WalkSpeedModifier
{
get
{
RecalculateMovementSpeedModifiers();
return _cachedWalkSpeedModifier;
}
}
private float _cachedSprintSpeedModifier;
public float WalkSpeedModifier = 1.0f;
[ViewVariables]
public float SprintSpeedModifier
{
get
{
RecalculateMovementSpeedModifiers();
return _cachedSprintSpeedModifier;
}
}
public float SprintSpeedModifier = 1.0f;
[ViewVariables(VVAccess.ReadWrite)]
public float BaseWalkSpeedVV
@@ -68,52 +55,5 @@ namespace Content.Shared.Movement.Components
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();
}
}
/// <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; }
}
}

View File

@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Content.Shared.Movement.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
@@ -8,6 +10,18 @@ namespace Content.Shared.Movement.EntitySystems
{
public sealed class MovementSpeedModifierSystem : EntitySystem
{
private readonly HashSet<EntityUid> _needsRefresh = new();
public override void Update(float frameTime)
{
foreach (var uid in _needsRefresh)
{
RecalculateMovementSpeedModifiers(uid);
}
_needsRefresh.Clear();
}
public override void Initialize()
{
base.Initialize();
@@ -31,6 +45,25 @@ namespace Content.Shared.Movement.EntitySystems
component.BaseSprintSpeed = state.BaseSprintSpeed;
}
public void RefreshMovementSpeedModifiers(EntityUid uid)
{
_needsRefresh.Add(uid);
}
private void RecalculateMovementSpeedModifiers(EntityUid uid, MovementSpeedModifierComponent? move = null)
{
if (!Resolve(uid, ref move, false))
return;
var ev = new RefreshMovementSpeedModifiersEvent();
RaiseLocalEvent(uid, ev, false);
move.WalkSpeedModifier = ev.WalkSpeedModifier;
move.SprintSpeedModifier = ev.SprintSpeedModifier;
move.Dirty();
}
[Serializable, NetSerializable]
private sealed class MovementSpeedModifierComponentState : ComponentState
{
@@ -38,4 +71,21 @@ namespace Content.Shared.Movement.EntitySystems
public float BaseSprintSpeed;
}
}
/// <summary>
/// Raised on an entity to determine its new movement speed. Any system that wishes to change movement speed
/// should hook into this event and set it then. If you want this event to be raised,
/// call <see cref="MovementSpeedModifierSystem.RefreshMovementSpeedModifiers"/>.
/// </summary>
public class RefreshMovementSpeedModifiersEvent : EntityEventArgs
{
public float WalkSpeedModifier { get; private set; } = 1.0f;
public float SprintSpeedModifier { get; private set; } = 1.0f;
public void ModifySpeed(float walk, float sprint)
{
WalkSpeedModifier *= walk;
SprintSpeedModifier *= sprint;
}
}
}

View File

@@ -8,37 +8,13 @@ using Robust.Shared.ViewVariables;
namespace Content.Shared.Nutrition.Components
{
[NetworkedComponent()]
public abstract class SharedHungerComponent : Component, IMoveSpeedModifier
public abstract class SharedHungerComponent : Component
{
public sealed override string Name => "Hunger";
[ViewVariables]
public abstract HungerThreshold CurrentHungerThreshold { get; }
float IMoveSpeedModifier.WalkSpeedModifier
{
get
{
if (CurrentHungerThreshold == HungerThreshold.Starving)
{
return 0.75f;
}
return 1.0f;
}
}
float IMoveSpeedModifier.SprintSpeedModifier
{
get
{
if (CurrentHungerThreshold == HungerThreshold.Starving)
{
return 0.75f;
}
return 1.0f;
}
}
[Serializable, NetSerializable]
protected sealed class HungerComponentState : ComponentState
{

View File

@@ -8,36 +8,13 @@ using Robust.Shared.ViewVariables;
namespace Content.Shared.Nutrition.Components
{
[NetworkedComponent()]
public abstract class SharedThirstComponent : Component, IMoveSpeedModifier
public abstract class SharedThirstComponent : Component
{
public sealed override string Name => "Thirst";
[ViewVariables]
public abstract ThirstThreshold CurrentThirstThreshold { get; }
float IMoveSpeedModifier.SprintSpeedModifier
{
get
{
if (CurrentThirstThreshold == ThirstThreshold.Parched)
{
return 0.75f;
}
return 1.0f;
}
}
float IMoveSpeedModifier.WalkSpeedModifier
{
get
{
if (CurrentThirstThreshold == ThirstThreshold.Parched)
{
return 0.75f;
}
return 1.0f;
}
}
[Serializable, NetSerializable]
protected sealed class ThirstComponentState : ComponentState
{

View File

@@ -0,0 +1,22 @@
using Content.Shared.Movement.EntitySystems;
using Content.Shared.Nutrition.Components;
using Robust.Shared.GameObjects;
namespace Content.Shared.Nutrition.EntitySystems
{
public class SharedHungerSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SharedHungerComponent, RefreshMovementSpeedModifiersEvent>(OnRefreshMovespeed);
}
private void OnRefreshMovespeed(EntityUid uid, SharedHungerComponent component, RefreshMovementSpeedModifiersEvent args)
{
float mod = component.CurrentHungerThreshold == HungerThreshold.Starving ? 0.75f : 1.0f;
args.ModifySpeed(mod, mod);
}
}
}

View File

@@ -0,0 +1,22 @@
using Content.Shared.Movement.EntitySystems;
using Content.Shared.Nutrition.Components;
using Robust.Shared.GameObjects;
namespace Content.Shared.Nutrition.EntitySystems
{
public class SharedThirstSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<SharedThirstComponent, RefreshMovementSpeedModifiersEvent>(OnRefreshMovespeed);
}
private void OnRefreshMovespeed(EntityUid uid, SharedThirstComponent component, RefreshMovementSpeedModifiersEvent args)
{
float mod = component.CurrentThirstThreshold == ThirstThreshold.Parched ? 0.75f : 1.0f;
args.ModifySpeed(mod, mod);
}
}
}

View File

@@ -10,7 +10,7 @@ namespace Content.Shared.Pulling.Components
{
[RegisterComponent]
[Friend(typeof(SharedPullingStateManagementSystem))]
public class SharedPullerComponent : Component, IMoveSpeedModifier
public class SharedPullerComponent : Component
{
public override string Name => "Puller";

View File

@@ -1,6 +1,7 @@
using Content.Shared.Alert;
using Content.Shared.Hands;
using Content.Shared.Movement.Components;
using Content.Shared.Movement.EntitySystems;
using Content.Shared.Physics.Pull;
using Content.Shared.Pulling.Components;
using JetBrains.Annotations;
@@ -13,6 +14,7 @@ namespace Content.Shared.Pulling.Systems
public sealed class SharedPullerSystem : EntitySystem
{
[Dependency] private readonly SharedPullingSystem _pullSystem = default!;
[Dependency] private readonly MovementSpeedModifierSystem _movementSpeedModifierSystem = default!;
public override void Initialize()
{
@@ -21,6 +23,7 @@ namespace Content.Shared.Pulling.Systems
SubscribeLocalEvent<SharedPullerComponent, PullStartedMessage>(PullerHandlePullStarted);
SubscribeLocalEvent<SharedPullerComponent, PullStoppedMessage>(PullerHandlePullStopped);
SubscribeLocalEvent<SharedPullerComponent, VirtualItemDeletedEvent>(OnVirtualItemDeleted);
SubscribeLocalEvent<SharedPullerComponent, RefreshMovementSpeedModifiersEvent>(OnRefreshMovespeed);
}
private void OnVirtualItemDeleted(EntityUid uid, SharedPullerComponent component, VirtualItemDeletedEvent args)
@@ -37,7 +40,7 @@ namespace Content.Shared.Pulling.Systems
}
}
private static void PullerHandlePullStarted(
private void PullerHandlePullStarted(
EntityUid uid,
SharedPullerComponent component,
PullStartedMessage args)
@@ -51,7 +54,7 @@ namespace Content.Shared.Pulling.Systems
RefreshMovementSpeed(component);
}
private static void PullerHandlePullStopped(
private void PullerHandlePullStopped(
EntityUid uid,
SharedPullerComponent component,
PullStoppedMessage args)
@@ -65,13 +68,14 @@ namespace Content.Shared.Pulling.Systems
RefreshMovementSpeed(component);
}
private static void RefreshMovementSpeed(SharedPullerComponent component)
private void OnRefreshMovespeed(EntityUid uid, SharedPullerComponent component, RefreshMovementSpeedModifiersEvent args)
{
// Before changing how this is updated, please see SharedPullerComponent
if (component.Owner.TryGetComponent<MovementSpeedModifierComponent>(out var speed))
{
speed.RefreshMovementSpeedModifiers();
}
args.ModifySpeed(component.WalkSpeedModifier, component.SprintSpeedModifier);
}
private void RefreshMovementSpeed(SharedPullerComponent component)
{
_movementSpeedModifierSystem.RefreshMovementSpeedModifiers(component.OwnerUid);
}
}
}

View File

@@ -8,6 +8,7 @@ using Content.Shared.Inventory.Events;
using Content.Shared.Item;
using Content.Shared.Movement;
using Content.Shared.Movement.Components;
using Content.Shared.Movement.EntitySystems;
using Content.Shared.Speech;
using Content.Shared.Standing;
using Content.Shared.StatusEffect;
@@ -27,6 +28,7 @@ namespace Content.Shared.Stunnable
{
[Dependency] private readonly StandingStateSystem _standingStateSystem = default!;
[Dependency] private readonly StatusEffectsSystem _statusEffectSystem = default!;
[Dependency] private readonly MovementSpeedModifierSystem _movementSpeedModifierSystem = default!;
public override void Initialize()
{
@@ -44,6 +46,7 @@ namespace Content.Shared.Stunnable
// helping people up if they're knocked down
SubscribeLocalEvent<KnockedDownComponent, InteractHandEvent>(OnInteractHand);
SubscribeLocalEvent<SlowedDownComponent, RefreshMovementSpeedModifiersEvent>(OnRefreshMovespeed);
// Attempt event subscriptions.
SubscribeLocalEvent<StunnedComponent, MovementAttemptEvent>(OnMoveAttempt);
@@ -97,22 +100,17 @@ namespace Content.Shared.Stunnable
private void OnSlowInit(EntityUid uid, SlowedDownComponent component, ComponentInit args)
{
// needs to be done so the client can also refresh when the addition is replicated,
// if the initial status effect addition wasn't predicted
if (EntityManager.TryGetComponent<MovementSpeedModifierComponent>(uid, out var move))
{
move.RefreshMovementSpeedModifiers();
}
_movementSpeedModifierSystem.RefreshMovementSpeedModifiers(uid);
}
private void OnSlowRemove(EntityUid uid, SlowedDownComponent component, ComponentRemove args)
{
if (EntityManager.TryGetComponent<MovementSpeedModifierComponent>(uid, out var move))
{
component.SprintSpeedModifier = 1.0f;
component.WalkSpeedModifier = 1.0f;
move.RefreshMovementSpeedModifiers();
}
_movementSpeedModifierSystem.RefreshMovementSpeedModifiers(uid);
}
private void OnRefreshMovespeed(EntityUid uid, SlowedDownComponent component, RefreshMovementSpeedModifiersEvent args)
{
args.ModifySpeed(component.WalkSpeedModifier, component.SprintSpeedModifier);
}
// TODO STUN: Make events for different things. (Getting modifiers, attempt events, informative events...)
@@ -127,6 +125,9 @@ namespace Content.Shared.Stunnable
if (time <= TimeSpan.Zero)
return false;
if (!Resolve(uid, ref status, false))
return false;
Resolve(uid, ref alerts, false);
return _statusEffectSystem.TryAddStatusEffect<StunnedComponent>(uid, "Stun", time, alerts: alerts);
@@ -142,6 +143,9 @@ namespace Content.Shared.Stunnable
if (time <= TimeSpan.Zero)
return false;
if (!Resolve(uid, ref status, false))
return false;
Resolve(uid, ref alerts, false);
return _statusEffectSystem.TryAddStatusEffect<KnockedDownComponent>(uid, "KnockedDown", time, alerts: alerts);
@@ -154,6 +158,9 @@ namespace Content.Shared.Stunnable
StatusEffectsComponent? status = null,
SharedAlertsComponent? alerts = null)
{
if (!Resolve(uid, ref status))
return false;
// Optional component.
Resolve(uid, ref alerts, false);
@@ -166,11 +173,13 @@ namespace Content.Shared.Stunnable
public bool TrySlowdown(EntityUid uid, TimeSpan time,
float walkSpeedMultiplier = 1f, float runSpeedMultiplier = 1f,
StatusEffectsComponent? status = null,
MovementSpeedModifierComponent? speedModifier = null,
SharedAlertsComponent? alerts = null)
{
if (!Resolve(uid, ref status))
return false;
// "Optional" component.
Resolve(uid, ref speedModifier, false);
Resolve(uid, ref alerts, false);
if (time <= TimeSpan.Zero)
return false;
@@ -185,7 +194,7 @@ namespace Content.Shared.Stunnable
slowed.WalkSpeedModifier *= walkSpeedMultiplier;
slowed.SprintSpeedModifier *= runSpeedMultiplier;
speedModifier?.RefreshMovementSpeedModifiers();
_movementSpeedModifierSystem.RefreshMovementSpeedModifiers(uid);
return true;
}

View File

@@ -10,7 +10,7 @@ namespace Content.Shared.Stunnable
[RegisterComponent]
[NetworkedComponent]
[Friend(typeof(SharedStunSystem))]
public class SlowedDownComponent : Component, IMoveSpeedModifier
public class SlowedDownComponent : Component
{
public override string Name => "SlowedDown";