StunnableComponent cleanup, proper prediction for stuns and slips. (#3552)

This commit is contained in:
Vera Aguilera Puerto
2021-03-08 05:00:50 +01:00
committed by GitHub
parent 0ad70d62ec
commit 6c77801d90
13 changed files with 243 additions and 226 deletions

View File

@@ -1,12 +1,15 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Threading;
using Content.Shared.Alert;
using Content.Shared.GameObjects.Components.Mobs.State;
using Content.Shared.GameObjects.Components.Movement;
using Content.Shared.GameObjects.EntitySystems.ActionBlocker;
using Content.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Players;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Timing;
@@ -22,17 +25,21 @@ namespace Content.Shared.GameObjects.Components.Mobs
public sealed override string Name => "Stunnable";
public override uint? NetID => ContentNetIDs.STUNNABLE;
protected TimeSpan? LastStun;
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] protected TimeSpan? StunStart => LastStun;
[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]
protected TimeSpan? StunEnd => LastStun == null
? (TimeSpan?) null
: _gameTiming.CurTime +
(TimeSpan.FromSeconds(Math.Max(StunnedTimer, Math.Max(KnockdownTimer, SlowdownTimer))));
private bool _canHelp = true;
[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;
@@ -48,21 +55,15 @@ namespace Content.Shared.GameObjects.Components.Mobs
[DataField("helpInterval")]
private float _helpInterval = 1f;
protected float StunnedTimer;
protected float KnockdownTimer;
protected float SlowdownTimer;
[DataField("stunAlertId")] private string _stunAlertId = "stun";
private bool _canHelp = true;
protected CancellationTokenSource StatusRemoveCancellation = new();
[ViewVariables] protected float WalkModifierOverride = 0f;
[ViewVariables] protected float RunModifierOverride = 0f;
[ViewVariables] public bool Stunned => StunnedTimer > 0f;
[ViewVariables] public bool KnockedDown => KnockdownTimer > 0f;
[ViewVariables] public bool SlowedDown => SlowdownTimer > 0f;
private float StunTimeModifier
{
get
@@ -118,15 +119,14 @@ namespace Content.Shared.GameObjects.Components.Mobs
/// <returns>Whether or not the owner was stunned.</returns>
public bool Stun(float seconds)
{
seconds = MathF.Min(StunnedTimer + (seconds * StunTimeModifier), _stunCap);
seconds = MathF.Min(StunnedSeconds + (seconds * StunTimeModifier), _stunCap);
if (seconds <= 0f)
{
return false;
}
StunnedTimer = seconds;
LastStun = _gameTiming.CurTime;
StunnedTimer = (_gameTiming.CurTime, _gameTiming.CurTime.Add(TimeSpan.FromSeconds(seconds)));
SetAlert();
OnStun();
@@ -145,15 +145,14 @@ namespace Content.Shared.GameObjects.Components.Mobs
/// <returns>Whether or not the owner was knocked down.</returns>
public bool Knockdown(float seconds)
{
seconds = MathF.Min(KnockdownTimer + (seconds * KnockdownTimeModifier), _knockdownCap);
seconds = MathF.Min(KnockdownSeconds + (seconds * KnockdownTimeModifier), _knockdownCap);
if (seconds <= 0f)
{
return false;
}
KnockdownTimer = seconds;
LastStun = _gameTiming.CurTime;
KnockdownTimer = (_gameTiming.CurTime, _gameTiming.CurTime.Add(TimeSpan.FromSeconds(seconds)));;
SetAlert();
OnKnockdown();
@@ -183,7 +182,7 @@ namespace Content.Shared.GameObjects.Components.Mobs
/// <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(SlowdownTimer + (seconds * SlowdownTimeModifier), _slowdownCap);
seconds = MathF.Min(SlowdownSeconds + (seconds * SlowdownTimeModifier), _slowdownCap);
if (seconds <= 0f)
return;
@@ -191,8 +190,7 @@ namespace Content.Shared.GameObjects.Components.Mobs
WalkModifierOverride = walkModifierOverride;
RunModifierOverride = runModifierOverride;
SlowdownTimer = seconds;
LastStun = _gameTiming.CurTime;
SlowdownTimer = (_gameTiming.CurTime, _gameTiming.CurTime.Add(TimeSpan.FromSeconds(seconds)));
if (Owner.TryGetComponent(out MovementSpeedModifierComponent? movement))
movement.RefreshMovementSpeedModifiers();
@@ -201,6 +199,44 @@ namespace Content.Shared.GameObjects.Components.Mobs
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))
@@ -208,8 +244,12 @@ namespace Content.Shared.GameObjects.Components.Mobs
return;
}
status.ShowAlert(AlertType.Stun, cooldown:
(StunStart == null || StunEnd == null) ? default : (StunStart.Value, StunEnd.Value));
var timers = GetTimers();
if (timers == null)
return;
status.ShowAlert(AlertType.Stun, cooldown:timers);
StatusRemoveCancellation.Cancel();
StatusRemoveCancellation = new CancellationTokenSource();
}
@@ -226,7 +266,7 @@ namespace Content.Shared.GameObjects.Components.Mobs
_canHelp = false;
Owner.SpawnTimer((int) _helpInterval * 1000, () => _canHelp = true);
KnockdownTimer -= _helpKnockdownRemove;
KnockdownTimer = (KnockdownTimer!.Value.Start, KnockdownTimer.Value.End.Subtract(TimeSpan.FromSeconds(_helpInterval)));
OnInteractHand();
@@ -236,6 +276,59 @@ namespace Content.Shared.GameObjects.Components.Mobs
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 CanMove() => (!Stunned);
@@ -273,13 +366,15 @@ namespace Content.Shared.GameObjects.Components.Mobs
[Serializable, NetSerializable]
protected sealed class StunnableComponentState : ComponentState
{
public float StunnedTimer { get; }
public float KnockdownTimer { get; }
public float SlowdownTimer { get; }
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(float stunnedTimer, float knockdownTimer, float slowdownTimer, float walkModifierOverride, float runModifierOverride) : base(ContentNetIDs.STUNNABLE)
public StunnableComponentState(
(TimeSpan Start, TimeSpan End)? stunnedTimer, (TimeSpan Start, TimeSpan End)? knockdownTimer,
(TimeSpan Start, TimeSpan End)? slowdownTimer, float walkModifierOverride, float runModifierOverride) : base(ContentNetIDs.STUNNABLE)
{
StunnedTimer = stunnedTimer;
KnockdownTimer = knockdownTimer;