minor stun refactor (#18752)

* file scope

* stun autogen state

---------

Co-authored-by: deltanedas <@deltanedas:kde.org>
This commit is contained in:
deltanedas
2023-08-06 09:19:47 +01:00
committed by GitHub
parent 999fa62538
commit 61bc0a3d98
4 changed files with 255 additions and 323 deletions

View File

@@ -2,33 +2,17 @@ using Robust.Shared.Audio;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
namespace Content.Shared.Stunnable
namespace Content.Shared.Stunnable;
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState, Access(typeof(SharedStunSystem))]
public sealed partial class KnockedDownComponent : Component
{
[RegisterComponent]
[NetworkedComponent]
[Access(typeof(SharedStunSystem))]
public sealed class KnockedDownComponent : Component
{
[DataField("helpInterval")]
public float HelpInterval { get; set; } = 1f;
[DataField("helpInterval"), AutoNetworkedField]
public float HelpInterval = 1f;
[DataField("helpAttemptSound")]
public SoundSpecifier StunAttemptSound = new SoundPathSpecifier("/Audio/Effects/thudswoosh.ogg");
[ViewVariables]
public float HelpTimer { get; set; } = 0f;
}
[Serializable, NetSerializable]
public sealed class KnockedDownComponentState : ComponentState
{
public float HelpInterval { get; set; }
public float HelpTimer { get; set; }
public KnockedDownComponentState(float helpInterval, float helpTimer)
{
HelpInterval = helpInterval;
HelpTimer = helpTimer;
}
}
[ViewVariables, AutoNetworkedField]
public float HelpTimer = 0f;
}

View File

@@ -17,22 +17,20 @@ using Content.Shared.Movement.Systems;
using Content.Shared.Standing;
using Content.Shared.StatusEffect;
using Content.Shared.Throwing;
using JetBrains.Annotations;
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
using Robust.Shared.Player;
namespace Content.Shared.Stunnable
namespace Content.Shared.Stunnable;
public abstract class SharedStunSystem : EntitySystem
{
[UsedImplicitly]
public abstract class SharedStunSystem : EntitySystem
{
[Dependency] private readonly ActionBlockerSystem _blocker = default!;
[Dependency] private readonly StandingStateSystem _standingStateSystem = default!;
[Dependency] private readonly StatusEffectsSystem _statusEffectSystem = default!;
[Dependency] private readonly MovementSpeedModifierSystem _movementSpeedModifierSystem = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
[Dependency] private readonly MovementSpeedModifierSystem _movementSpeedModifier = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly StandingStateSystem _standingState = default!;
[Dependency] private readonly StatusEffectsSystem _statusEffect = default!;
/// <summary>
/// Friction modifier for knocked down players.
@@ -52,12 +50,6 @@ namespace Content.Shared.Stunnable
SubscribeLocalEvent<StunnedComponent, ComponentStartup>(UpdateCanMove);
SubscribeLocalEvent<StunnedComponent, ComponentShutdown>(UpdateCanMove);
SubscribeLocalEvent<SlowedDownComponent, ComponentGetState>(OnSlowGetState);
SubscribeLocalEvent<SlowedDownComponent, ComponentHandleState>(OnSlowHandleState);
SubscribeLocalEvent<KnockedDownComponent, ComponentGetState>(OnKnockGetState);
SubscribeLocalEvent<KnockedDownComponent, ComponentHandleState>(OnKnockHandleState);
// helping people up if they're knocked down
SubscribeLocalEvent<KnockedDownComponent, InteractHandEvent>(OnInteractHand);
SubscribeLocalEvent<SlowedDownComponent, RefreshMovementSpeedModifiersEvent>(OnRefreshMovespeed);
@@ -94,12 +86,12 @@ namespace Content.Shared.Stunnable
}
case MobState.Critical:
{
_statusEffectSystem.TryRemoveStatusEffect(uid, "Stun");
_statusEffect.TryRemoveStatusEffect(uid, "Stun");
break;
}
case MobState.Dead:
{
_statusEffectSystem.TryRemoveStatusEffect(uid, "Stun");
_statusEffect.TryRemoveStatusEffect(uid, "Stun");
break;
}
case MobState.Invalid:
@@ -114,42 +106,14 @@ namespace Content.Shared.Stunnable
_blocker.UpdateCanMove(uid);
}
private void OnSlowGetState(EntityUid uid, SlowedDownComponent component, ref ComponentGetState args)
{
args.State = new SlowedDownComponentState(component.SprintSpeedModifier, component.WalkSpeedModifier);
}
private void OnSlowHandleState(EntityUid uid, SlowedDownComponent component, ref ComponentHandleState args)
{
if (args.Current is SlowedDownComponentState state)
{
component.SprintSpeedModifier = state.SprintSpeedModifier;
component.WalkSpeedModifier = state.WalkSpeedModifier;
}
}
private void OnKnockGetState(EntityUid uid, KnockedDownComponent component, ref ComponentGetState args)
{
args.State = new KnockedDownComponentState(component.HelpInterval, component.HelpTimer);
}
private void OnKnockHandleState(EntityUid uid, KnockedDownComponent component, ref ComponentHandleState args)
{
if (args.Current is KnockedDownComponentState state)
{
component.HelpInterval = state.HelpInterval;
component.HelpTimer = state.HelpTimer;
}
}
private void OnKnockInit(EntityUid uid, KnockedDownComponent component, ComponentInit args)
{
_standingStateSystem.Down(uid);
_standingState.Down(uid);
}
private void OnKnockShutdown(EntityUid uid, KnockedDownComponent component, ComponentShutdown args)
{
_standingStateSystem.Stand(uid);
_standingState.Stand(uid);
}
private void OnStandAttempt(EntityUid uid, KnockedDownComponent component, StandAttemptEvent args)
@@ -160,14 +124,14 @@ namespace Content.Shared.Stunnable
private void OnSlowInit(EntityUid uid, SlowedDownComponent component, ComponentInit args)
{
_movementSpeedModifierSystem.RefreshMovementSpeedModifiers(uid);
_movementSpeedModifier.RefreshMovementSpeedModifiers(uid);
}
private void OnSlowRemove(EntityUid uid, SlowedDownComponent component, ComponentShutdown args)
{
component.SprintSpeedModifier = 1f;
component.WalkSpeedModifier = 1f;
_movementSpeedModifierSystem.RefreshMovementSpeedModifiers(uid);
_movementSpeedModifier.RefreshMovementSpeedModifiers(uid);
}
private void OnRefreshMovespeed(EntityUid uid, SlowedDownComponent component, RefreshMovementSpeedModifiersEvent args)
@@ -189,7 +153,7 @@ namespace Content.Shared.Stunnable
if (!Resolve(uid, ref status, false))
return false;
if (!_statusEffectSystem.TryAddStatusEffect<StunnedComponent>(uid, "Stun", time, refresh))
if (!_statusEffect.TryAddStatusEffect<StunnedComponent>(uid, "Stun", time, refresh))
return false;
_adminLogger.Add(LogType.Stamina, LogImpact.Medium, $"{ToPrettyString(uid):user} stunned for {time.Seconds} seconds");
return true;
@@ -207,7 +171,7 @@ namespace Content.Shared.Stunnable
if (!Resolve(uid, ref status, false))
return false;
return _statusEffectSystem.TryAddStatusEffect<KnockedDownComponent>(uid, "KnockedDown", time, refresh);
return _statusEffect.TryAddStatusEffect<KnockedDownComponent>(uid, "KnockedDown", time, refresh);
}
/// <summary>
@@ -235,9 +199,9 @@ namespace Content.Shared.Stunnable
if (time <= TimeSpan.Zero)
return false;
if (_statusEffectSystem.TryAddStatusEffect<SlowedDownComponent>(uid, "SlowedDown", time, refresh, status))
if (_statusEffect.TryAddStatusEffect<SlowedDownComponent>(uid, "SlowedDown", time, refresh, status))
{
var slowed = EntityManager.GetComponent<SlowedDownComponent>(uid);
var slowed = Comp<SlowedDownComponent>(uid);
// Doesn't make much sense to have the "TrySlowdown" method speed up entities now does it?
walkSpeedMultiplier = Math.Clamp(walkSpeedMultiplier, 0f, 1f);
runSpeedMultiplier = Math.Clamp(runSpeedMultiplier, 0f, 1f);
@@ -245,7 +209,7 @@ namespace Content.Shared.Stunnable
slowed.WalkSpeedModifier *= walkSpeedMultiplier;
slowed.SprintSpeedModifier *= runSpeedMultiplier;
_movementSpeedModifierSystem.RefreshMovementSpeedModifiers(uid);
_movementSpeedModifier.RefreshMovementSpeedModifiers(uid);
return true;
}
@@ -265,7 +229,7 @@ namespace Content.Shared.Stunnable
// Set it to half the help interval so helping is actually useful...
knocked.HelpTimer = knocked.HelpInterval/2f;
_statusEffectSystem.TryRemoveTime(uid, "KnockedDown", TimeSpan.FromSeconds(knocked.HelpInterval));
_statusEffect.TryRemoveTime(uid, "KnockedDown", TimeSpan.FromSeconds(knocked.HelpInterval));
_audio.PlayPredicted(knocked.StunAttemptSound, uid, args.User);
Dirty(knocked);
@@ -308,5 +272,4 @@ namespace Content.Shared.Stunnable
#endregion
}
}

View File

@@ -1,27 +1,14 @@
using Robust.Shared.GameStates;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
namespace Content.Shared.Stunnable
namespace Content.Shared.Stunnable;
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState, Access(typeof(SharedStunSystem))]
public sealed partial class SlowedDownComponent : Component
{
[RegisterComponent]
[NetworkedComponent]
[Access(typeof(SharedStunSystem))]
public sealed class SlowedDownComponent : Component
{
public float SprintSpeedModifier { get; set; } = 0.5f;
public float WalkSpeedModifier { get; set; } = 0.5f;
}
[ViewVariables, DataField("sprintSpeedModifier"), AutoNetworkedField]
public float SprintSpeedModifier = 0.5f;
[Serializable, NetSerializable]
public sealed class SlowedDownComponentState : ComponentState
{
public float SprintSpeedModifier { get; set; }
public float WalkSpeedModifier { get; set; }
public SlowedDownComponentState(float sprintSpeedModifier, float walkSpeedModifier)
{
SprintSpeedModifier = sprintSpeedModifier;
WalkSpeedModifier = walkSpeedModifier;
}
}
[ViewVariables, DataField("walkSpeedModifier"), AutoNetworkedField]
public float WalkSpeedModifier = 0.5f;
}

View File

@@ -1,10 +1,8 @@
using Robust.Shared.GameStates;
namespace Content.Shared.Stunnable
namespace Content.Shared.Stunnable;
[RegisterComponent, NetworkedComponent, Access(typeof(SharedStunSystem))]
public sealed class StunnedComponent : Component
{
[Access(typeof(SharedStunSystem))]
[RegisterComponent, NetworkedComponent]
public sealed class StunnedComponent : Component
{
}
}