Files
crystall-punk-14/Content.Server/GameObjects/Components/Mobs/StunnableComponent.cs

332 lines
11 KiB
C#
Raw Normal View History

2020-05-13 16:53:01 +02:00
using System;
using System.Threading;
2020-05-13 22:35:23 +02:00
using Content.Server.GameObjects.Components.Movement;
2020-05-13 16:53:01 +02:00
using Content.Server.GameObjects.EntitySystems;
using Content.Server.Interfaces.GameObjects;
2020-05-13 19:04:50 +02:00
using Content.Server.Mobs;
using Content.Shared.Audio;
2020-05-13 16:53:01 +02:00
using Content.Shared.GameObjects.Components.Mobs;
2020-06-12 16:22:36 +02:00
using Microsoft.Extensions.Logging;
2020-05-13 16:53:01 +02:00
using Robust.Server.GameObjects;
2020-05-13 19:04:50 +02:00
using Robust.Server.GameObjects.EntitySystems;
using Robust.Shared.Audio;
2020-05-13 16:53:01 +02:00
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
2020-05-13 19:04:50 +02:00
using Robust.Shared.Interfaces.GameObjects;
2020-05-13 16:53:01 +02:00
using Robust.Shared.Interfaces.Timers;
2020-05-14 17:26:08 +02:00
using Robust.Shared.Interfaces.Timing;
2020-05-13 16:53:01 +02:00
using Robust.Shared.IoC;
2020-06-12 16:22:36 +02:00
using Robust.Shared.Log;
2020-05-14 17:26:08 +02:00
using Robust.Shared.Maths;
2020-05-13 19:10:29 +02:00
using Robust.Shared.Serialization;
2020-05-13 16:53:01 +02:00
using Robust.Shared.ViewVariables;
using Timer = Robust.Shared.Timers.Timer;
namespace Content.Server.GameObjects.Components.Mobs
{
[RegisterComponent]
2020-05-23 17:23:25 +02:00
public class StunnableComponent : Component, IActionBlocker, IInteractHand, IMoveSpeedModifier
2020-05-13 16:53:01 +02:00
{
2020-05-14 17:26:08 +02:00
public override string Name => "Stunnable";
#pragma warning disable 649
[Dependency] private IGameTiming _gameTiming;
#pragma warning restore 649
2020-05-13 16:53:01 +02:00
2020-05-14 17:26:08 +02:00
private TimeSpan? _lastStun;
[ViewVariables]
public TimeSpan? StunStart => _lastStun;
2020-05-13 16:53:01 +02:00
2020-05-14 17:26:08 +02:00
[ViewVariables]
public TimeSpan? StunEnd => _lastStun == null
? (TimeSpan?) null
2020-06-12 16:22:36 +02:00
: _gameTiming.CurTime + (TimeSpan.FromSeconds(Math.Max(_stunnedTimer, Math.Max(_knockdownTimer, _slowdownTimer))));
2020-05-14 17:26:08 +02:00
private const int StunLevels = 8;
private bool _canHelp = true;
2020-05-13 19:04:50 +02:00
private float _stunCap = 20f;
private float _knockdownCap = 20f;
2020-05-13 22:35:23 +02:00
private float _slowdownCap = 20f;
2020-05-13 19:04:50 +02:00
private float _helpKnockdownRemove = 1f;
2020-05-13 20:21:03 +02:00
private float _helpInterval = 1f;
2020-05-13 16:53:01 +02:00
2020-05-13 19:04:50 +02:00
private float _stunnedTimer = 0f;
private float _knockdownTimer = 0f;
2020-05-13 22:35:23 +02:00
private float _slowdownTimer = 0f;
private float _walkModifierOverride = 0f;
private float _runModifierOverride = 0f;
2020-06-12 16:22:36 +02:00
private string _stunTexture;
private CancellationTokenSource _statusRemoveCancellation = new CancellationTokenSource();
2020-05-13 16:53:01 +02:00
2020-05-13 22:13:22 +02:00
[ViewVariables] public bool Stunned => _stunnedTimer > 0f;
[ViewVariables] public bool KnockedDown => _knockdownTimer > 0f;
2020-05-13 22:35:23 +02:00
[ViewVariables] public bool SlowedDown => _slowdownTimer > 0f;
[ViewVariables] public float StunCap => _stunCap;
[ViewVariables] public float KnockdownCap => _knockdownCap;
[ViewVariables] public float SlowdownCap => _slowdownCap;
2020-05-13 19:10:29 +02:00
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _stunCap, "stunCap", 20f);
serializer.DataField(ref _knockdownCap, "knockdownCap", 20f);
2020-05-13 22:35:23 +02:00
serializer.DataField(ref _slowdownCap, "slowdownCap", 20f);
2020-05-13 20:21:03 +02:00
serializer.DataField(ref _helpInterval, "helpInterval", 1f);
2020-05-13 19:10:29 +02:00
serializer.DataField(ref _helpKnockdownRemove, "helpKnockdownRemove", 1f);
2020-06-12 16:22:36 +02:00
serializer.DataField(ref _stunTexture, "stunTexture", "/Textures/Objects/Melee/stunbaton.rsi/stunbaton_off.png");
2020-05-14 17:26:08 +02:00
}
2020-05-14 17:49:40 +02:00
/// <summary>
/// Stuns the entity, disallowing it from doing many interactions temporarily.
/// </summary>
/// <param name="seconds">How many seconds the mob will stay stunned</param>
2020-05-13 19:04:50 +02:00
public void Stun(float seconds)
2020-05-13 16:53:01 +02:00
{
2020-05-14 18:58:45 +02:00
seconds = MathF.Min(_stunnedTimer + (seconds * StunTimeModifier), _stunCap);
2020-05-14 18:03:08 +02:00
if (seconds <= 0f)
return;
2020-05-13 16:53:01 +02:00
StandingStateHelper.DropAllItemsInHands(Owner, false);
2020-05-13 16:53:01 +02:00
2020-05-13 19:04:50 +02:00
_stunnedTimer = seconds;
2020-05-14 17:26:08 +02:00
_lastStun = _gameTiming.CurTime;
2020-06-12 16:22:36 +02:00
SetStatusEffect();
2020-05-13 16:53:01 +02:00
}
2020-05-14 17:49:40 +02:00
/// <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>
2020-05-13 19:04:50 +02:00
public void Knockdown(float seconds)
2020-05-13 16:53:01 +02:00
{
2020-05-14 18:03:08 +02:00
seconds = MathF.Min(_knockdownTimer + (seconds * KnockdownTimeModifier), _knockdownCap);
if (seconds <= 0f)
return;
2020-05-13 16:53:01 +02:00
2020-05-13 19:04:50 +02:00
StandingStateHelper.Down(Owner);
2020-05-13 16:53:01 +02:00
2020-05-13 19:04:50 +02:00
_knockdownTimer = seconds;
2020-05-14 17:26:08 +02:00
_lastStun = _gameTiming.CurTime;
2020-06-12 16:22:36 +02:00
SetStatusEffect();
2020-05-13 16:53:01 +02:00
}
2020-05-14 17:49:40 +02:00
/// <summary>
/// Applies knockdown and stun to the mob temporarily
/// </summary>
/// <param name="seconds">How many seconds the mob will be paralyzed</param>
2020-05-13 19:04:50 +02:00
public void Paralyze(float seconds)
2020-05-13 16:53:01 +02:00
{
2020-05-13 19:04:50 +02:00
Stun(seconds);
Knockdown(seconds);
2020-05-13 16:53:01 +02:00
}
2020-05-14 17:49:40 +02:00
/// <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>
2020-05-13 22:35:23 +02:00
public void Slowdown(float seconds, float walkModifierOverride = 0f, float runModifierOverride = 0f)
{
2020-05-14 18:03:08 +02:00
seconds = MathF.Min(_slowdownTimer + (seconds * SlowdownTimeModifier), _slowdownCap);
if (seconds <= 0f)
return;
2020-05-13 22:35:23 +02:00
_walkModifierOverride = walkModifierOverride;
_runModifierOverride = runModifierOverride;
_slowdownTimer = seconds;
2020-05-14 17:26:08 +02:00
_lastStun = _gameTiming.CurTime;
2020-05-13 22:35:23 +02:00
if(Owner.TryGetComponent(out MovementSpeedModifierComponent movement))
movement.RefreshMovementSpeedModifiers();
2020-06-12 16:22:36 +02:00
SetStatusEffect();
2020-05-13 22:35:23 +02:00
}
2020-05-13 19:04:50 +02:00
/// <summary>
/// Used when
/// </summary>
public void CancelAll()
2020-05-13 16:53:01 +02:00
{
2020-05-13 19:04:50 +02:00
_knockdownTimer = 0f;
_stunnedTimer = 0f;
2020-05-13 16:53:01 +02:00
}
2020-05-23 17:23:25 +02:00
public bool InteractHand(InteractHandEventArgs eventArgs)
2020-05-13 16:53:01 +02:00
{
2020-05-13 19:10:29 +02:00
if (!_canHelp || !KnockedDown)
2020-05-13 19:04:50 +02:00
return false;
2020-05-13 16:53:01 +02:00
2020-05-13 19:04:50 +02:00
_canHelp = false;
Timer.Spawn(((int)_helpInterval*1000), () => _canHelp = true);
EntitySystem.Get<AudioSystem>()
.PlayFromEntity("/Audio/effects/thudswoosh.ogg", Owner, AudioHelpers.WithVariation(0.25f));
2020-05-13 19:04:50 +02:00
_knockdownTimer -= _helpKnockdownRemove;
2020-06-12 16:22:36 +02:00
SetStatusEffect();
2020-05-13 19:04:50 +02:00
return true;
2020-05-13 16:53:01 +02:00
}
2020-06-12 16:22:36 +02:00
private void SetStatusEffect()
{
if (!Owner.TryGetComponent(out ServerStatusEffectsComponent status))
return;
status.ChangeStatusEffect(StatusEffect.Stun, _stunTexture, (StunStart == null || StunEnd == null) ? default : (StunStart.Value, StunEnd.Value));
_statusRemoveCancellation.Cancel();
_statusRemoveCancellation = new CancellationTokenSource();
}
2020-05-13 19:04:50 +02:00
public void Update(float delta)
2020-05-13 16:53:01 +02:00
{
2020-05-13 22:35:23 +02:00
if (Stunned)
{
_stunnedTimer -= delta;
if (_stunnedTimer <= 0)
{
_stunnedTimer = 0f;
}
}
2020-05-13 22:13:22 +02:00
if (KnockedDown)
2020-05-13 19:04:50 +02:00
{
_knockdownTimer -= delta;
if (_knockdownTimer <= 0f)
{
StandingStateHelper.Standing(Owner);
2020-05-13 22:13:22 +02:00
_knockdownTimer = 0f;
2020-05-13 19:04:50 +02:00
}
}
2020-05-13 22:35:23 +02:00
if (SlowedDown)
2020-05-13 19:04:50 +02:00
{
2020-05-13 22:35:23 +02:00
_slowdownTimer -= delta;
2020-05-13 19:04:50 +02:00
2020-05-13 22:35:23 +02:00
if (_slowdownTimer <= 0f)
2020-05-13 19:04:50 +02:00
{
2020-05-13 22:35:23 +02:00
_slowdownTimer = 0f;
if(Owner.TryGetComponent(out MovementSpeedModifierComponent movement))
movement.RefreshMovementSpeedModifiers();
2020-05-13 19:04:50 +02:00
}
}
2020-05-14 17:26:08 +02:00
2020-06-12 16:22:36 +02:00
if (!StunStart.HasValue || !StunEnd.HasValue || !Owner.TryGetComponent(out ServerStatusEffectsComponent status))
2020-05-14 17:26:08 +02:00
return;
2020-06-12 16:22:36 +02:00
var start = StunStart.Value;
2020-05-14 17:26:08 +02:00
var end = StunEnd.Value;
var length = (end - start).TotalSeconds;
var progress = (_gameTiming.CurTime - start).TotalSeconds;
2020-06-12 16:22:36 +02:00
if (progress >= length)
2020-05-14 17:26:08 +02:00
{
2020-06-12 16:22:36 +02:00
Timer.Spawn(250, () => status.RemoveStatusEffect(StatusEffect.Stun), _statusRemoveCancellation.Token);
2020-05-14 17:26:08 +02:00
_lastStun = null;
}
2020-05-13 16:53:01 +02:00
}
#region ActionBlockers
public bool CanMove() => (!Stunned);
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);
2020-05-13 19:26:39 +02:00
public bool CanChangeDirection() => true;
2020-05-13 16:53:01 +02:00
#endregion
2020-05-13 22:35:23 +02:00
2020-05-14 18:03:08 +02:00
public float StunTimeModifier
{
get
{
var modifier = 1.0f;
var components = Owner.GetAllComponents<IStunModifier>();
foreach (var component in components)
{
modifier *= component.StunTimeModifier;
}
return modifier;
}
}
public float KnockdownTimeModifier
{
get
{
var modifier = 1.0f;
var components = Owner.GetAllComponents<IStunModifier>();
foreach (var component in components)
{
modifier *= component.KnockdownTimeModifier;
}
return modifier;
}
}
public float SlowdownTimeModifier
{
get
{
var modifier = 1.0f;
var components = Owner.GetAllComponents<IStunModifier>();
foreach (var component in components)
{
modifier *= component.SlowdownTimeModifier;
}
return modifier;
}
}
2020-05-13 22:35:23 +02:00
public float WalkSpeedModifier => (SlowedDown ? (_walkModifierOverride <= 0f ? 0.5f : _walkModifierOverride) : 1f);
public float SprintSpeedModifier => (SlowedDown ? (_runModifierOverride <= 0f ? 0.5f : _runModifierOverride) : 1f);
2020-05-13 16:53:01 +02:00
}
2020-05-14 18:03:08 +02:00
/// <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;
}
2020-05-13 16:53:01 +02:00
}