Refactors stunnable to be ECS. (#4819)
Also cleans up StandingStatesystem.
This commit is contained in:
committed by
GitHub
parent
19a588a70a
commit
6eee256b11
402
Content.Shared/Stunnable/SharedStunSystem.cs
Normal file
402
Content.Shared/Stunnable/SharedStunSystem.cs
Normal file
@@ -0,0 +1,402 @@
|
||||
using System;
|
||||
using Content.Shared.Alert;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.DragDrop;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Interaction.Events;
|
||||
using Content.Shared.Inventory.Events;
|
||||
using Content.Shared.Item;
|
||||
using Content.Shared.Movement;
|
||||
using Content.Shared.Movement.Components;
|
||||
using Content.Shared.Speech;
|
||||
using Content.Shared.Standing;
|
||||
using Content.Shared.Throwing;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Shared.Stunnable
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public abstract class SharedStunSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly StandingStateSystem _standingStateSystem = default!;
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
SubscribeLocalEvent<StunnableComponent, ComponentGetState>(OnGetState);
|
||||
SubscribeLocalEvent<StunnableComponent, ComponentHandleState>(OnHandleState);
|
||||
SubscribeLocalEvent<StunnableComponent, InteractHandEvent>(OnInteractHand);
|
||||
|
||||
// Attempt event subscriptions.
|
||||
SubscribeLocalEvent<StunnableComponent, MovementAttemptEvent>(OnMoveAttempt);
|
||||
SubscribeLocalEvent<StunnableComponent, InteractionAttemptEvent>(OnInteractAttempt);
|
||||
SubscribeLocalEvent<StunnableComponent, UseAttemptEvent>(OnUseAttempt);
|
||||
SubscribeLocalEvent<StunnableComponent, ThrowAttemptEvent>(OnThrowAttempt);
|
||||
SubscribeLocalEvent<StunnableComponent, DropAttemptEvent>(OnDropAttempt);
|
||||
SubscribeLocalEvent<StunnableComponent, PickupAttemptEvent>(OnPickupAttempt);
|
||||
SubscribeLocalEvent<StunnableComponent, AttackAttemptEvent>(OnAttackAttempt);
|
||||
SubscribeLocalEvent<StunnableComponent, EquipAttemptEvent>(OnEquipAttempt);
|
||||
SubscribeLocalEvent<StunnableComponent, UnequipAttemptEvent>(OnUnequipAttempt);
|
||||
SubscribeLocalEvent<StunnableComponent, StandAttemptEvent>(OnStandAttempt);
|
||||
}
|
||||
|
||||
private void OnGetState(EntityUid uid, StunnableComponent stunnable, ref ComponentGetState args)
|
||||
{
|
||||
args.State = new StunnableComponentState(stunnable.StunnedTimer, stunnable.KnockdownTimer, stunnable.SlowdownTimer, stunnable.WalkSpeedMultiplier, stunnable.RunSpeedMultiplier);
|
||||
}
|
||||
|
||||
private void OnHandleState(EntityUid uid, StunnableComponent stunnable, ref ComponentHandleState args)
|
||||
{
|
||||
if (args.Current is not StunnableComponentState state)
|
||||
return;
|
||||
|
||||
stunnable.StunnedTimer = state.StunnedTimer;
|
||||
stunnable.KnockdownTimer = state.KnockdownTimer;
|
||||
stunnable.SlowdownTimer = state.SlowdownTimer;
|
||||
|
||||
stunnable.WalkSpeedMultiplier = state.WalkSpeedMultiplier;
|
||||
stunnable.RunSpeedMultiplier = state.RunSpeedMultiplier;
|
||||
|
||||
if (EntityManager.TryGetComponent(uid, out MovementSpeedModifierComponent? movement))
|
||||
movement.RefreshMovementSpeedModifiers();
|
||||
}
|
||||
|
||||
private TimeSpan AdjustTime(TimeSpan time, (TimeSpan Start, TimeSpan End)? timer, float cap)
|
||||
{
|
||||
if (timer != null)
|
||||
{
|
||||
time = timer.Value.End - timer.Value.Start + time;
|
||||
}
|
||||
|
||||
if (time.TotalSeconds > cap)
|
||||
time = TimeSpan.FromSeconds(cap);
|
||||
|
||||
return time;
|
||||
}
|
||||
|
||||
// TODO STUN: Make events for different things. (Getting modifiers, attempt events, informative events...)
|
||||
|
||||
/// <summary>
|
||||
/// Stuns the entity, disallowing it from doing many interactions temporarily.
|
||||
/// </summary>
|
||||
public void Stun(EntityUid uid, TimeSpan time,
|
||||
StunnableComponent? stunnable = null,
|
||||
SharedAlertsComponent? alerts = null)
|
||||
{
|
||||
if (!Resolve(uid, ref stunnable))
|
||||
return;
|
||||
|
||||
time = AdjustTime(time, stunnable.StunnedTimer, stunnable.StunCap);
|
||||
|
||||
if (time <= TimeSpan.Zero)
|
||||
return;
|
||||
|
||||
stunnable.StunnedTimer = (_gameTiming.CurTime, _gameTiming.CurTime + time);
|
||||
|
||||
SetAlert(uid, stunnable, alerts);
|
||||
|
||||
stunnable.Dirty();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Knocks down the entity, making it fall to the ground.
|
||||
/// </summary>
|
||||
public void Knockdown(EntityUid uid, TimeSpan time,
|
||||
StunnableComponent? stunnable = null,
|
||||
SharedAlertsComponent? alerts = null,
|
||||
StandingStateComponent? standingState = null,
|
||||
SharedAppearanceComponent? appearance = null)
|
||||
{
|
||||
if (!Resolve(uid, ref stunnable))
|
||||
return;
|
||||
|
||||
time = AdjustTime(time, stunnable.KnockdownTimer, stunnable.KnockdownCap);
|
||||
|
||||
if (time <= TimeSpan.Zero)
|
||||
return;
|
||||
|
||||
// Check if we can actually knock down the mob.
|
||||
if (!_standingStateSystem.Down(uid, standingState:standingState, appearance:appearance))
|
||||
return;
|
||||
|
||||
stunnable.KnockdownTimer = (_gameTiming.CurTime, _gameTiming.CurTime + time);;
|
||||
|
||||
SetAlert(uid, stunnable, alerts);
|
||||
|
||||
stunnable.Dirty();
|
||||
}
|
||||
/// <summary>
|
||||
/// Applies knockdown and stun to the entity temporarily.
|
||||
/// </summary>
|
||||
public void Paralyze(EntityUid uid, TimeSpan time,
|
||||
StunnableComponent? stunnable = null,
|
||||
SharedAlertsComponent? alerts = null)
|
||||
{
|
||||
if (!Resolve(uid, ref stunnable))
|
||||
return;
|
||||
|
||||
// Optional component.
|
||||
Resolve(uid, ref alerts, false);
|
||||
|
||||
Stun(uid, time, stunnable, alerts);
|
||||
Knockdown(uid, time, stunnable, alerts);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Slows down the mob's walking/running speed temporarily
|
||||
/// </summary>
|
||||
public void Slowdown(EntityUid uid, TimeSpan time, float walkSpeedMultiplier = 1f, float runSpeedMultiplier = 1f,
|
||||
StunnableComponent? stunnable = null,
|
||||
MovementSpeedModifierComponent? speedModifier = null,
|
||||
SharedAlertsComponent? alerts = null)
|
||||
{
|
||||
if (!Resolve(uid, ref stunnable))
|
||||
return;
|
||||
|
||||
// "Optional" component.
|
||||
Resolve(uid, ref speedModifier, false);
|
||||
|
||||
time = AdjustTime(time, stunnable.SlowdownTimer, stunnable.SlowdownCap);
|
||||
|
||||
if (time <= TimeSpan.Zero)
|
||||
return;
|
||||
|
||||
// Doesn't make much sense to have the "Slowdown" method speed up entities now does it?
|
||||
walkSpeedMultiplier = Math.Clamp(walkSpeedMultiplier, 0f, 1f);
|
||||
runSpeedMultiplier = Math.Clamp(runSpeedMultiplier, 0f, 1f);
|
||||
|
||||
stunnable.WalkSpeedMultiplier *= walkSpeedMultiplier;
|
||||
stunnable.RunSpeedMultiplier *= runSpeedMultiplier;
|
||||
|
||||
stunnable.SlowdownTimer = (_gameTiming.CurTime, _gameTiming.CurTime + time);
|
||||
|
||||
speedModifier?.RefreshMovementSpeedModifiers();
|
||||
|
||||
SetAlert(uid, stunnable, alerts);
|
||||
stunnable.Dirty();
|
||||
}
|
||||
|
||||
public void Reset(EntityUid uid,
|
||||
StunnableComponent? stunnable = null,
|
||||
MovementSpeedModifierComponent? speedModifier = null,
|
||||
StandingStateComponent? standingState = null,
|
||||
SharedAppearanceComponent? appearance = null)
|
||||
{
|
||||
if (!Resolve(uid, ref stunnable))
|
||||
return;
|
||||
|
||||
// Optional component.
|
||||
Resolve(uid, ref speedModifier, false);
|
||||
|
||||
stunnable.StunnedTimer = null;
|
||||
stunnable.SlowdownTimer = null;
|
||||
stunnable.KnockdownTimer = null;
|
||||
|
||||
speedModifier?.RefreshMovementSpeedModifiers();
|
||||
_standingStateSystem.Stand(uid, standingState, appearance);
|
||||
|
||||
stunnable.Dirty();
|
||||
}
|
||||
|
||||
private void SetAlert(EntityUid uid,
|
||||
StunnableComponent? stunnable = null,
|
||||
SharedAlertsComponent? alerts = null)
|
||||
{
|
||||
// This method is really just optional, doesn't matter if the entity doesn't support alerts.
|
||||
if (!Resolve(uid, ref stunnable, ref alerts, false))
|
||||
return;
|
||||
|
||||
if (GetTimers(uid, stunnable) is not {} timers)
|
||||
return;
|
||||
|
||||
alerts.ShowAlert(AlertType.Stun, cooldown:timers);
|
||||
}
|
||||
|
||||
private (TimeSpan, TimeSpan)? GetTimers(EntityUid uid, StunnableComponent? stunnable = null)
|
||||
{
|
||||
if (!Resolve(uid, ref stunnable))
|
||||
return null;
|
||||
|
||||
// Don't do anything if no stuns are applied.
|
||||
if (!stunnable.AnyStunActive)
|
||||
return null;
|
||||
|
||||
TimeSpan start = TimeSpan.MaxValue, end = TimeSpan.MinValue;
|
||||
|
||||
if (stunnable.StunnedTimer != null)
|
||||
{
|
||||
if (stunnable.StunnedTimer.Value.Start < start)
|
||||
start = stunnable.StunnedTimer.Value.Start;
|
||||
|
||||
if (stunnable.StunnedTimer.Value.End > end)
|
||||
end = stunnable.StunnedTimer.Value.End;
|
||||
}
|
||||
|
||||
if (stunnable.KnockdownTimer != null)
|
||||
{
|
||||
if (stunnable.KnockdownTimer.Value.Start < start)
|
||||
start = stunnable.KnockdownTimer.Value.Start;
|
||||
|
||||
if (stunnable.KnockdownTimer.Value.End > end)
|
||||
end = stunnable.KnockdownTimer.Value.End;
|
||||
}
|
||||
|
||||
if (stunnable.SlowdownTimer != null)
|
||||
{
|
||||
if (stunnable.SlowdownTimer.Value.Start < start)
|
||||
start = stunnable.SlowdownTimer.Value.Start;
|
||||
|
||||
if (stunnable.SlowdownTimer.Value.End > end)
|
||||
end = stunnable.SlowdownTimer.Value.End;
|
||||
}
|
||||
|
||||
return (start, end);
|
||||
}
|
||||
|
||||
private void OnInteractHand(EntityUid uid, StunnableComponent stunnable, InteractHandEvent args)
|
||||
{
|
||||
if (args.Handled || stunnable.HelpTimer > 0f || !stunnable.KnockedDown)
|
||||
return;
|
||||
|
||||
// Set it to half the help interval so helping is actually useful...
|
||||
stunnable.HelpTimer = stunnable.HelpInterval/2f;
|
||||
|
||||
stunnable.KnockdownTimer = (stunnable.KnockdownTimer!.Value.Start, stunnable.KnockdownTimer.Value.End - TimeSpan.FromSeconds(stunnable.HelpInterval));
|
||||
|
||||
SoundSystem.Play(Filter.Pvs(uid), stunnable.StunAttemptSound.GetSound(), uid, AudioHelpers.WithVariation(0.05f));
|
||||
|
||||
SetAlert(uid, stunnable);
|
||||
stunnable.Dirty();
|
||||
|
||||
args.Handled = true;
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
var curTime = _gameTiming.CurTime;
|
||||
|
||||
foreach (var stunnable in EntityManager.EntityQuery<StunnableComponent>())
|
||||
{
|
||||
var uid = stunnable.Owner.Uid;
|
||||
|
||||
if(stunnable.HelpTimer > 0f)
|
||||
// If it goes negative, that's okay.
|
||||
stunnable.HelpTimer -= frameTime;
|
||||
|
||||
if (stunnable.StunnedTimer != null)
|
||||
{
|
||||
if (stunnable.StunnedTimer.Value.End <= curTime)
|
||||
{
|
||||
stunnable.StunnedTimer = null;
|
||||
stunnable.Dirty();
|
||||
}
|
||||
}
|
||||
|
||||
if (stunnable.KnockdownTimer != null)
|
||||
{
|
||||
if (stunnable.KnockdownTimer.Value.End <= curTime)
|
||||
{
|
||||
stunnable.KnockdownTimer = null;
|
||||
|
||||
// Try to stand up the mob...
|
||||
_standingStateSystem.Stand(uid);
|
||||
|
||||
stunnable.Dirty();
|
||||
}
|
||||
}
|
||||
|
||||
if (stunnable.SlowdownTimer != null)
|
||||
{
|
||||
if (stunnable.SlowdownTimer.Value.End <= curTime)
|
||||
{
|
||||
if (EntityManager.TryGetComponent(uid, out MovementSpeedModifierComponent? movement))
|
||||
movement.RefreshMovementSpeedModifiers();
|
||||
|
||||
|
||||
stunnable.SlowdownTimer = null;
|
||||
stunnable.Dirty();
|
||||
}
|
||||
}
|
||||
|
||||
if (stunnable.AnyStunActive || !EntityManager.TryGetComponent(uid, out SharedAlertsComponent? status)
|
||||
|| !status.IsShowingAlert(AlertType.Stun))
|
||||
continue;
|
||||
|
||||
status.ClearAlert(AlertType.Stun);
|
||||
}
|
||||
}
|
||||
|
||||
#region Attempt Event Handling
|
||||
|
||||
private void OnMoveAttempt(EntityUid uid, StunnableComponent stunnable, MovementAttemptEvent args)
|
||||
{
|
||||
if (stunnable.Stunned)
|
||||
args.Cancel();
|
||||
}
|
||||
|
||||
private void OnInteractAttempt(EntityUid uid, StunnableComponent stunnable, InteractionAttemptEvent args)
|
||||
{
|
||||
if(stunnable.Stunned)
|
||||
args.Cancel();
|
||||
}
|
||||
|
||||
private void OnUseAttempt(EntityUid uid, StunnableComponent stunnable, UseAttemptEvent args)
|
||||
{
|
||||
if(stunnable.Stunned)
|
||||
args.Cancel();
|
||||
}
|
||||
|
||||
private void OnThrowAttempt(EntityUid uid, StunnableComponent stunnable, ThrowAttemptEvent args)
|
||||
{
|
||||
if (stunnable.Stunned)
|
||||
args.Cancel();
|
||||
}
|
||||
|
||||
private void OnDropAttempt(EntityUid uid, StunnableComponent stunnable, DropAttemptEvent args)
|
||||
{
|
||||
if(stunnable.Stunned)
|
||||
args.Cancel();
|
||||
}
|
||||
|
||||
private void OnPickupAttempt(EntityUid uid, StunnableComponent stunnable, PickupAttemptEvent args)
|
||||
{
|
||||
if(stunnable.Stunned)
|
||||
args.Cancel();
|
||||
}
|
||||
|
||||
private void OnAttackAttempt(EntityUid uid, StunnableComponent stunnable, AttackAttemptEvent args)
|
||||
{
|
||||
if(stunnable.Stunned)
|
||||
args.Cancel();
|
||||
}
|
||||
|
||||
private void OnEquipAttempt(EntityUid uid, StunnableComponent stunnable, EquipAttemptEvent args)
|
||||
{
|
||||
if(stunnable.Stunned)
|
||||
args.Cancel();
|
||||
}
|
||||
|
||||
private void OnUnequipAttempt(EntityUid uid, StunnableComponent stunnable, UnequipAttemptEvent args)
|
||||
{
|
||||
if(stunnable.Stunned)
|
||||
args.Cancel();
|
||||
}
|
||||
|
||||
private void OnStandAttempt(EntityUid uid, StunnableComponent stunnable, StandAttemptEvent args)
|
||||
{
|
||||
if(stunnable.KnockedDown)
|
||||
args.Cancel();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,388 +0,0 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Content.Shared.ActionBlocker;
|
||||
using Content.Shared.Alert;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Movement.Components;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Players;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Shared.Stunnable
|
||||
{
|
||||
[NetworkedComponent()]
|
||||
public abstract class SharedStunnableComponent : Component, IMoveSpeedModifier, IActionBlocker, IInteractHand
|
||||
{
|
||||
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
||||
|
||||
public sealed override string Name => "Stunnable";
|
||||
|
||||
public (TimeSpan Start, TimeSpan End)? StunnedTimer { get; protected set; }
|
||||
public (TimeSpan Start, TimeSpan End)? KnockdownTimer { get; protected set; }
|
||||
public (TimeSpan Start, TimeSpan End)? SlowdownTimer { get; protected set; }
|
||||
|
||||
[ViewVariables] public float StunnedSeconds =>
|
||||
StunnedTimer == null ? 0f : (float)(StunnedTimer.Value.End - StunnedTimer.Value.Start).TotalSeconds;
|
||||
[ViewVariables] public float KnockdownSeconds =>
|
||||
KnockdownTimer == null ? 0f : (float)(KnockdownTimer.Value.End - KnockdownTimer.Value.Start).TotalSeconds;
|
||||
[ViewVariables] public float SlowdownSeconds =>
|
||||
SlowdownTimer == null ? 0f : (float)(SlowdownTimer.Value.End - SlowdownTimer.Value.Start).TotalSeconds;
|
||||
|
||||
[ViewVariables] public bool AnyStunActive => Stunned || KnockedDown || SlowedDown;
|
||||
[ViewVariables] public bool Stunned => StunnedTimer != null;
|
||||
[ViewVariables] public bool KnockedDown => KnockdownTimer != null;
|
||||
[ViewVariables] public bool SlowedDown => SlowdownTimer != null;
|
||||
|
||||
[DataField("stunCap")]
|
||||
protected float _stunCap = 20f;
|
||||
|
||||
[DataField("knockdownCap")]
|
||||
protected float _knockdownCap = 20f;
|
||||
|
||||
[DataField("slowdownCap")]
|
||||
protected float _slowdownCap = 20f;
|
||||
|
||||
[DataField("helpInterval")]
|
||||
private float _helpInterval = 1f;
|
||||
|
||||
private bool _canHelp = true;
|
||||
|
||||
protected CancellationTokenSource StatusRemoveCancellation = new();
|
||||
|
||||
[ViewVariables] protected float WalkModifierOverride = 0f;
|
||||
[ViewVariables] protected float RunModifierOverride = 0f;
|
||||
|
||||
private float StunTimeModifier
|
||||
{
|
||||
get
|
||||
{
|
||||
var modifier = 1.0f;
|
||||
var components = Owner.GetAllComponents<IStunModifier>();
|
||||
|
||||
foreach (var component in components)
|
||||
{
|
||||
modifier *= component.StunTimeModifier;
|
||||
}
|
||||
|
||||
return modifier;
|
||||
}
|
||||
}
|
||||
|
||||
private float KnockdownTimeModifier
|
||||
{
|
||||
get
|
||||
{
|
||||
var modifier = 1.0f;
|
||||
var components = Owner.GetAllComponents<IStunModifier>();
|
||||
|
||||
foreach (var component in components)
|
||||
{
|
||||
modifier *= component.KnockdownTimeModifier;
|
||||
}
|
||||
|
||||
return modifier;
|
||||
}
|
||||
}
|
||||
|
||||
private float SlowdownTimeModifier
|
||||
{
|
||||
get
|
||||
{
|
||||
var modifier = 1.0f;
|
||||
var components = Owner.GetAllComponents<IStunModifier>();
|
||||
|
||||
foreach (var component in components)
|
||||
{
|
||||
modifier *= component.SlowdownTimeModifier;
|
||||
}
|
||||
|
||||
return modifier;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stuns the entity, disallowing it from doing many interactions temporarily.
|
||||
/// </summary>
|
||||
/// <param name="seconds">How many seconds the mob will stay stunned.</param>
|
||||
/// <returns>Whether or not the owner was stunned.</returns>
|
||||
public bool Stun(float seconds)
|
||||
{
|
||||
seconds = MathF.Min(StunnedSeconds + (seconds * StunTimeModifier), _stunCap);
|
||||
|
||||
if (seconds <= 0f)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
StunnedTimer = (_gameTiming.CurTime, _gameTiming.CurTime.Add(TimeSpan.FromSeconds(seconds)));
|
||||
|
||||
SetAlert();
|
||||
OnStun();
|
||||
|
||||
Dirty();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected virtual void OnStun() { }
|
||||
|
||||
/// <summary>
|
||||
/// Knocks down the mob, making it fall to the ground.
|
||||
/// </summary>
|
||||
/// <param name="seconds">How many seconds the mob will stay on the ground.</param>
|
||||
/// <returns>Whether or not the owner was knocked down.</returns>
|
||||
public bool Knockdown(float seconds)
|
||||
{
|
||||
seconds = MathF.Min(KnockdownSeconds + (seconds * KnockdownTimeModifier), _knockdownCap);
|
||||
|
||||
if (seconds <= 0f)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
KnockdownTimer = (_gameTiming.CurTime, _gameTiming.CurTime.Add(TimeSpan.FromSeconds(seconds)));;
|
||||
|
||||
SetAlert();
|
||||
OnKnockdown();
|
||||
|
||||
Dirty();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected virtual void OnKnockdown() { }
|
||||
|
||||
/// <summary>
|
||||
/// Applies knockdown and stun to the mob temporarily.
|
||||
/// </summary>
|
||||
/// <param name="seconds">How many seconds the mob will be paralyzed-</param>
|
||||
/// <returns>Whether or not the owner of this component was paralyzed-</returns>
|
||||
public bool Paralyze(float seconds)
|
||||
{
|
||||
return Stun(seconds) && Knockdown(seconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Slows down the mob's walking/running speed temporarily
|
||||
/// </summary>
|
||||
/// <param name="seconds">How many seconds the mob will be slowed down</param>
|
||||
/// <param name="walkModifierOverride">Walk speed modifier. Set to 0 or negative for default value. (0.5f)</param>
|
||||
/// <param name="runModifierOverride">Run speed modifier. Set to 0 or negative for default value. (0.5f)</param>
|
||||
public void Slowdown(float seconds, float walkModifierOverride = 0f, float runModifierOverride = 0f)
|
||||
{
|
||||
seconds = MathF.Min(SlowdownSeconds + (seconds * SlowdownTimeModifier), _slowdownCap);
|
||||
|
||||
if (seconds <= 0f)
|
||||
return;
|
||||
|
||||
WalkModifierOverride = walkModifierOverride;
|
||||
RunModifierOverride = runModifierOverride;
|
||||
|
||||
SlowdownTimer = (_gameTiming.CurTime, _gameTiming.CurTime.Add(TimeSpan.FromSeconds(seconds)));
|
||||
|
||||
if (Owner.TryGetComponent(out MovementSpeedModifierComponent? movement))
|
||||
movement.RefreshMovementSpeedModifiers();
|
||||
|
||||
SetAlert();
|
||||
Dirty();
|
||||
}
|
||||
|
||||
private (TimeSpan, TimeSpan)? GetTimers()
|
||||
{
|
||||
// Don't do anything if no stuns are applied.
|
||||
if (!AnyStunActive)
|
||||
return null;
|
||||
|
||||
TimeSpan start = TimeSpan.MaxValue, end = TimeSpan.MinValue;
|
||||
|
||||
if (StunnedTimer != null)
|
||||
{
|
||||
if (StunnedTimer.Value.Start < start)
|
||||
start = StunnedTimer.Value.Start;
|
||||
|
||||
if (StunnedTimer.Value.End > end)
|
||||
end = StunnedTimer.Value.End;
|
||||
}
|
||||
|
||||
if (KnockdownTimer != null)
|
||||
{
|
||||
if (KnockdownTimer.Value.Start < start)
|
||||
start = KnockdownTimer.Value.Start;
|
||||
|
||||
if (KnockdownTimer.Value.End > end)
|
||||
end = KnockdownTimer.Value.End;
|
||||
}
|
||||
|
||||
if (SlowdownTimer != null)
|
||||
{
|
||||
if (SlowdownTimer.Value.Start < start)
|
||||
start = SlowdownTimer.Value.Start;
|
||||
|
||||
if (SlowdownTimer.Value.End > end)
|
||||
end = SlowdownTimer.Value.End;
|
||||
}
|
||||
|
||||
return (start, end);
|
||||
}
|
||||
|
||||
private void SetAlert()
|
||||
{
|
||||
if (!Owner.TryGetComponent(out SharedAlertsComponent? status))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var timers = GetTimers();
|
||||
|
||||
if (timers == null)
|
||||
return;
|
||||
|
||||
status.ShowAlert(AlertType.Stun, cooldown:timers);
|
||||
StatusRemoveCancellation.Cancel();
|
||||
StatusRemoveCancellation = new CancellationTokenSource();
|
||||
}
|
||||
|
||||
protected virtual void OnInteractHand() { }
|
||||
|
||||
bool IInteractHand.InteractHand(InteractHandEventArgs eventArgs)
|
||||
{
|
||||
if (!_canHelp || !KnockedDown)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_canHelp = false;
|
||||
Owner.SpawnTimer((int) _helpInterval * 1000, () => _canHelp = true);
|
||||
|
||||
KnockdownTimer = (KnockdownTimer!.Value.Start, KnockdownTimer.Value.End.Subtract(TimeSpan.FromSeconds(_helpInterval)));
|
||||
|
||||
OnInteractHand();
|
||||
|
||||
SetAlert();
|
||||
Dirty();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public override ComponentState GetComponentState(ICommonSession player)
|
||||
{
|
||||
return new StunnableComponentState(StunnedTimer, KnockdownTimer, SlowdownTimer, WalkModifierOverride, RunModifierOverride);
|
||||
}
|
||||
|
||||
protected virtual void OnKnockdownEnd()
|
||||
{
|
||||
}
|
||||
|
||||
public void Update(float delta)
|
||||
{
|
||||
var curTime = _gameTiming.CurTime;
|
||||
|
||||
if (StunnedTimer != null)
|
||||
{
|
||||
if (StunnedTimer.Value.End <= curTime)
|
||||
{
|
||||
StunnedTimer = null;
|
||||
Dirty();
|
||||
}
|
||||
}
|
||||
|
||||
if (KnockdownTimer != null)
|
||||
{
|
||||
if (KnockdownTimer.Value.End <= curTime)
|
||||
{
|
||||
OnKnockdownEnd();
|
||||
|
||||
KnockdownTimer = null;
|
||||
Dirty();
|
||||
}
|
||||
}
|
||||
|
||||
if (SlowdownTimer != null)
|
||||
{
|
||||
if (SlowdownTimer.Value.End <= curTime)
|
||||
{
|
||||
if (Owner.TryGetComponent(out MovementSpeedModifierComponent? movement))
|
||||
{
|
||||
movement.RefreshMovementSpeedModifiers();
|
||||
}
|
||||
|
||||
SlowdownTimer = null;
|
||||
Dirty();
|
||||
}
|
||||
}
|
||||
|
||||
if (AnyStunActive || !Owner.TryGetComponent(out SharedAlertsComponent? status) || !status.IsShowingAlert(AlertType.Stun))
|
||||
return;
|
||||
|
||||
status.ClearAlert(AlertType.Stun);
|
||||
}
|
||||
|
||||
#region ActionBlockers
|
||||
public bool CanInteract() => (!Stunned);
|
||||
|
||||
public bool CanUse() => (!Stunned);
|
||||
|
||||
public bool CanThrow() => (!Stunned);
|
||||
|
||||
public bool CanSpeak() => true;
|
||||
|
||||
public bool CanDrop() => (!Stunned);
|
||||
|
||||
public bool CanPickup() => (!Stunned);
|
||||
|
||||
public bool CanEmote() => true;
|
||||
|
||||
public bool CanAttack() => (!Stunned);
|
||||
|
||||
public bool CanEquip() => (!Stunned);
|
||||
|
||||
public bool CanUnequip() => (!Stunned);
|
||||
public bool CanChangeDirection() => true;
|
||||
|
||||
public bool CanShiver() => !Stunned;
|
||||
public bool CanSweat() => true;
|
||||
|
||||
#endregion
|
||||
|
||||
[ViewVariables]
|
||||
public float WalkSpeedModifier => (SlowedDown ? (WalkModifierOverride <= 0f ? 0.5f : WalkModifierOverride) : 1f);
|
||||
[ViewVariables]
|
||||
public float SprintSpeedModifier => (SlowedDown ? (RunModifierOverride <= 0f ? 0.5f : RunModifierOverride) : 1f);
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
protected sealed class StunnableComponentState : ComponentState
|
||||
{
|
||||
public (TimeSpan Start, TimeSpan End)? StunnedTimer { get; }
|
||||
public (TimeSpan Start, TimeSpan End)? KnockdownTimer { get; }
|
||||
public (TimeSpan Start, TimeSpan End)? SlowdownTimer { get; }
|
||||
public float WalkModifierOverride { get; }
|
||||
public float RunModifierOverride { get; }
|
||||
|
||||
public StunnableComponentState(
|
||||
(TimeSpan Start, TimeSpan End)? stunnedTimer, (TimeSpan Start, TimeSpan End)? knockdownTimer,
|
||||
(TimeSpan Start, TimeSpan End)? slowdownTimer, float walkModifierOverride, float runModifierOverride)
|
||||
{
|
||||
StunnedTimer = stunnedTimer;
|
||||
KnockdownTimer = knockdownTimer;
|
||||
SlowdownTimer = slowdownTimer;
|
||||
WalkModifierOverride = walkModifierOverride;
|
||||
RunModifierOverride = runModifierOverride;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This interface allows components to multiply the time in seconds of various stuns by a number.
|
||||
/// </summary>
|
||||
public interface IStunModifier
|
||||
{
|
||||
float StunTimeModifier => 1.0f;
|
||||
float KnockdownTimeModifier => 1.0f;
|
||||
float SlowdownTimeModifier => 1.0f;
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
using Content.Shared.Movement;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Shared.Stunnable
|
||||
{
|
||||
[UsedImplicitly]
|
||||
internal sealed class StunSystem : EntitySystem
|
||||
{
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<SharedStunnableComponent, MovementAttemptEvent>(HandleMoveAttempt);
|
||||
}
|
||||
|
||||
private void HandleMoveAttempt(EntityUid uid, SharedStunnableComponent component, MovementAttemptEvent args)
|
||||
{
|
||||
if (component.Stunned)
|
||||
args.Cancel();
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
foreach (var component in EntityManager.EntityQuery<SharedStunnableComponent>(true))
|
||||
{
|
||||
component.Update(frameTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
96
Content.Shared/Stunnable/StunnableComponent.cs
Normal file
96
Content.Shared/Stunnable/StunnableComponent.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using Content.Shared.Movement.Components;
|
||||
using Content.Shared.Sound;
|
||||
using Robust.Shared.Analyzers;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Shared.Stunnable
|
||||
{
|
||||
[Friend(typeof(SharedStunSystem))]
|
||||
[RegisterComponent, NetworkedComponent]
|
||||
public sealed class StunnableComponent : Component, IMoveSpeedModifier
|
||||
{
|
||||
public sealed override string Name => "Stunnable";
|
||||
|
||||
public (TimeSpan Start, TimeSpan End)? StunnedTimer { get; set; }
|
||||
public (TimeSpan Start, TimeSpan End)? KnockdownTimer { get; set; }
|
||||
public (TimeSpan Start, TimeSpan End)? SlowdownTimer { get; set; }
|
||||
|
||||
[ViewVariables]
|
||||
public float StunnedSeconds =>
|
||||
StunnedTimer == null ? 0f : (float)(StunnedTimer.Value.End - StunnedTimer.Value.Start).TotalSeconds;
|
||||
|
||||
[ViewVariables]
|
||||
public float KnockdownSeconds =>
|
||||
KnockdownTimer == null ? 0f : (float)(KnockdownTimer.Value.End - KnockdownTimer.Value.Start).TotalSeconds;
|
||||
|
||||
[ViewVariables]
|
||||
public float SlowdownSeconds =>
|
||||
SlowdownTimer == null ? 0f : (float)(SlowdownTimer.Value.End - SlowdownTimer.Value.Start).TotalSeconds;
|
||||
|
||||
[ViewVariables]
|
||||
public bool AnyStunActive => Stunned || KnockedDown || SlowedDown;
|
||||
|
||||
[ViewVariables]
|
||||
public bool Stunned => StunnedTimer != null;
|
||||
|
||||
[ViewVariables]
|
||||
public bool KnockedDown => KnockdownTimer != null;
|
||||
|
||||
[ViewVariables]
|
||||
public bool SlowedDown => SlowdownTimer != null;
|
||||
|
||||
[DataField("stunCap")]
|
||||
public float StunCap { get; set; } = 20f;
|
||||
|
||||
[DataField("knockdownCap")]
|
||||
public float KnockdownCap { get; set; } = 20f;
|
||||
|
||||
[DataField("slowdownCap")]
|
||||
public float SlowdownCap { get; set; } = 20f;
|
||||
|
||||
[DataField("helpInterval")]
|
||||
public float HelpInterval { get; set; } = 1f;
|
||||
|
||||
[DataField("stunAttemptSound")]
|
||||
public SoundSpecifier StunAttemptSound = new SoundPathSpecifier("/Audio/Effects/thudswoosh.ogg");
|
||||
|
||||
[ViewVariables]
|
||||
public float HelpTimer { get; set; } = 0f;
|
||||
|
||||
[ViewVariables]
|
||||
public float WalkSpeedMultiplier { get; set; } = 0f;
|
||||
[ViewVariables]
|
||||
public float RunSpeedMultiplier { get; set; } = 0f;
|
||||
|
||||
[ViewVariables]
|
||||
public float WalkSpeedModifier => (SlowedDown ? (WalkSpeedMultiplier <= 0f ? 0.5f : WalkSpeedMultiplier) : 1f);
|
||||
[ViewVariables]
|
||||
public float SprintSpeedModifier => (SlowedDown ? (RunSpeedMultiplier <= 0f ? 0.5f : RunSpeedMultiplier) : 1f);
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public sealed class StunnableComponentState : ComponentState
|
||||
{
|
||||
public (TimeSpan Start, TimeSpan End)? StunnedTimer { get; }
|
||||
public (TimeSpan Start, TimeSpan End)? KnockdownTimer { get; }
|
||||
public (TimeSpan Start, TimeSpan End)? SlowdownTimer { get; }
|
||||
public float WalkSpeedMultiplier { get; }
|
||||
public float RunSpeedMultiplier { get; }
|
||||
|
||||
public StunnableComponentState(
|
||||
(TimeSpan Start, TimeSpan End)? stunnedTimer, (TimeSpan Start, TimeSpan End)? knockdownTimer,
|
||||
(TimeSpan Start, TimeSpan End)? slowdownTimer, float walkSpeedMultiplier, float runSpeedMultiplier)
|
||||
{
|
||||
StunnedTimer = stunnedTimer;
|
||||
KnockdownTimer = knockdownTimer;
|
||||
SlowdownTimer = slowdownTimer;
|
||||
WalkSpeedMultiplier = walkSpeedMultiplier;
|
||||
RunSpeedMultiplier = runSpeedMultiplier;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user