StandingStateHelper, StunSystem
This commit is contained in:
@@ -16,7 +16,7 @@ namespace Content.Client.GameObjects.Components.Mobs
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case SharedSpeciesComponent.MobState.Stand:
|
||||
case SharedSpeciesComponent.MobState.Standing:
|
||||
sprite.Rotation = 0;
|
||||
break;
|
||||
case SharedSpeciesComponent.MobState.Down:
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
using Content.Server.GameObjects.EntitySystems;
|
||||
using Content.Server.GameObjects.Components.Mobs;
|
||||
using Content.Server.GameObjects.EntitySystems;
|
||||
using Content.Server.Mobs;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.GameObjects.Components.Mobs;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.GameObjects.EntitySystems;
|
||||
using Robust.Shared.GameObjects.Components;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
|
||||
namespace Content.Server.GameObjects
|
||||
{
|
||||
@@ -96,10 +102,15 @@ namespace Content.Server.GameObjects
|
||||
{
|
||||
public void EnterState(IEntity entity)
|
||||
{
|
||||
if(entity.TryGetComponent(out StunnableComponent stun))
|
||||
stun.CancelAll();
|
||||
|
||||
StandingStateHelper.Down(entity);
|
||||
}
|
||||
|
||||
public void ExitState(IEntity entity)
|
||||
{
|
||||
StandingStateHelper.Standing(entity);
|
||||
}
|
||||
|
||||
public bool IsConscious => false;
|
||||
@@ -167,11 +178,10 @@ namespace Content.Server.GameObjects
|
||||
{
|
||||
public void EnterState(IEntity entity)
|
||||
{
|
||||
if (entity.TryGetComponent(out AppearanceComponent appearance))
|
||||
{
|
||||
var newState = SharedSpeciesComponent.MobState.Down;
|
||||
appearance.SetData(SharedSpeciesComponent.MobVisuals.RotationState, newState);
|
||||
}
|
||||
if(entity.TryGetComponent(out StunnableComponent stun))
|
||||
stun.CancelAll();
|
||||
|
||||
StandingStateHelper.Down(entity);
|
||||
|
||||
if (entity.TryGetComponent(out CollidableComponent collidable))
|
||||
{
|
||||
@@ -181,11 +191,7 @@ namespace Content.Server.GameObjects
|
||||
|
||||
public void ExitState(IEntity entity)
|
||||
{
|
||||
if (entity.TryGetComponent(out AppearanceComponent appearance))
|
||||
{
|
||||
var newState = SharedSpeciesComponent.MobState.Stand;
|
||||
appearance.SetData(SharedSpeciesComponent.MobVisuals.RotationState, newState);
|
||||
}
|
||||
StandingStateHelper.Standing(entity);
|
||||
|
||||
if (entity.TryGetComponent(out CollidableComponent collidable))
|
||||
{
|
||||
|
||||
@@ -2,9 +2,14 @@ using System;
|
||||
using System.Threading;
|
||||
using Content.Server.GameObjects.EntitySystems;
|
||||
using Content.Server.Interfaces.GameObjects;
|
||||
using Content.Server.Mobs;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.GameObjects.Components.Mobs;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.GameObjects.EntitySystems;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Interfaces.Timers;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.ViewVariables;
|
||||
@@ -13,109 +18,111 @@ using Timer = Robust.Shared.Timers.Timer;
|
||||
namespace Content.Server.GameObjects.Components.Mobs
|
||||
{
|
||||
[RegisterComponent]
|
||||
public class StunnableComponent : Component, IActionBlocker
|
||||
public class StunnableComponent : Component, IActionBlocker, IAttackHand
|
||||
{
|
||||
[Dependency] private IEntitySystemManager _entitySystemManager;
|
||||
[Dependency] private ITimerManager _timerManager;
|
||||
|
||||
private bool _stunned = false;
|
||||
private bool _knocked = false;
|
||||
private bool _canHelp = true;
|
||||
|
||||
private int _stunCapMs = 20000;
|
||||
private int _knockdownCapMs = 20000;
|
||||
private float _stunCap = 20f;
|
||||
private float _knockdownCap = 20f;
|
||||
private float _helpKnockdownRemove = 1f;
|
||||
private float _helpInterval = 1f;
|
||||
|
||||
private Timer _stunTimer;
|
||||
private Timer _knockdownTimer;
|
||||
|
||||
private CancellationTokenSource _stunTimerCancellation;
|
||||
private CancellationTokenSource _knockdownTimerCancellation;
|
||||
private float _stunnedTimer = 0f;
|
||||
private float _knockdownTimer = 0f;
|
||||
|
||||
public override string Name => "Stunnable";
|
||||
|
||||
[ViewVariables] public bool Stunned => _stunned;
|
||||
[ViewVariables] public bool KnockedDown => _knocked;
|
||||
|
||||
public void Stun(int milliseconds)
|
||||
public void Stun(float seconds)
|
||||
{
|
||||
if (_stunTimer != null)
|
||||
{
|
||||
_stunTimerCancellation.Cancel();
|
||||
milliseconds += _stunTimer.Time;
|
||||
}
|
||||
seconds = Math.Min(seconds + _stunnedTimer, _stunCap);
|
||||
|
||||
milliseconds = Math.Min(milliseconds, _stunCapMs);
|
||||
|
||||
DropItemsInHands();
|
||||
StandingStateHelper.DropAllItemsInHands(Owner);
|
||||
|
||||
_stunned = true;
|
||||
_stunTimerCancellation = new CancellationTokenSource();
|
||||
_stunTimer = new Timer(milliseconds, false, OnStunTimerFired);
|
||||
_timerManager.AddTimer(_stunTimer, _stunTimerCancellation.Token);
|
||||
_stunnedTimer = seconds;
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
Timer.Spawn(10000, () => Paralyze(5000));
|
||||
Timer.Spawn(10000, () => Paralyze(15f));
|
||||
}
|
||||
|
||||
public void Knockdown(int milliseconds)
|
||||
public void Knockdown(float seconds)
|
||||
{
|
||||
if (_knockdownTimer != null)
|
||||
{
|
||||
_knockdownTimerCancellation.Cancel();
|
||||
milliseconds += _knockdownTimer.Time;
|
||||
}
|
||||
seconds = MathF.Min(_knockdownTimer + seconds, _knockdownCap);
|
||||
|
||||
if (Owner.TryGetComponent(out AppearanceComponent appearance))
|
||||
{
|
||||
var state = SharedSpeciesComponent.MobState.Down;
|
||||
appearance.SetData(SharedSpeciesComponent.MobVisuals.RotationState, state);
|
||||
}
|
||||
|
||||
milliseconds = Math.Min(milliseconds, _knockdownCapMs);
|
||||
|
||||
DropItemsInHands();
|
||||
StandingStateHelper.Down(Owner);
|
||||
|
||||
_knocked = true;
|
||||
_knockdownTimerCancellation = new CancellationTokenSource();
|
||||
_knockdownTimer = new Timer(milliseconds, false, OnKnockdownTimerFired);
|
||||
_timerManager.AddTimer(_knockdownTimer, _knockdownTimerCancellation.Token);
|
||||
_knockdownTimer = seconds;
|
||||
}
|
||||
|
||||
private void DropItemsInHands()
|
||||
public void Paralyze(float seconds)
|
||||
{
|
||||
if (!Owner.TryGetComponent(out IHandsComponent hands)) return;
|
||||
|
||||
foreach (var heldItem in hands.GetAllHeldItems())
|
||||
{
|
||||
hands.Drop(heldItem.Owner);
|
||||
}
|
||||
Stun(seconds);
|
||||
Knockdown(seconds);
|
||||
}
|
||||
|
||||
private void OnStunTimerFired()
|
||||
/// <summary>
|
||||
/// Used when
|
||||
/// </summary>
|
||||
public void CancelAll()
|
||||
{
|
||||
_knocked = false;
|
||||
_stunned = false;
|
||||
_stunTimer = null;
|
||||
_stunTimerCancellation = null;
|
||||
|
||||
_knockdownTimer = 0f;
|
||||
_stunnedTimer = 0f;
|
||||
}
|
||||
|
||||
private void OnKnockdownTimerFired()
|
||||
public bool AttackHand(AttackHandEventArgs eventArgs)
|
||||
{
|
||||
if (Owner.TryGetComponent(out AppearanceComponent appearance))
|
||||
{
|
||||
var state = SharedSpeciesComponent.MobState.Stand;
|
||||
appearance.SetData(SharedSpeciesComponent.MobVisuals.RotationState, state);
|
||||
if (!_canHelp || KnockedDown)
|
||||
return false;
|
||||
|
||||
_canHelp = false;
|
||||
Timer.Spawn(((int)_helpInterval*1000), () => _canHelp = true);
|
||||
|
||||
IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<AudioSystem>()
|
||||
.Play("/Audio/effects/thudswoosh.ogg", Owner, AudioHelpers.WithVariation(0.5f));
|
||||
|
||||
_knockdownTimer -= _helpKnockdownRemove;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Update(float delta)
|
||||
{
|
||||
if (_knocked)
|
||||
{
|
||||
_knockdownTimer -= delta;
|
||||
|
||||
if (_knockdownTimer <= 0f)
|
||||
{
|
||||
StandingStateHelper.Standing(Owner);
|
||||
|
||||
_knocked = false;
|
||||
_knockdownTimer = null;
|
||||
_knockdownTimerCancellation = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void Paralyze(int milliseconds)
|
||||
if (_stunned)
|
||||
{
|
||||
Stun(milliseconds);
|
||||
Knockdown(milliseconds);
|
||||
_stunnedTimer -= delta;
|
||||
|
||||
if (_stunnedTimer <= 0)
|
||||
{
|
||||
_stunned = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#region ActionBlockers
|
||||
|
||||
27
Content.Server/GameObjects/EntitySystems/StunSystem.cs
Normal file
27
Content.Server/GameObjects/EntitySystems/StunSystem.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using Content.Server.GameObjects.Components.Mobs;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameObjects.Systems;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
|
||||
namespace Content.Server.GameObjects.EntitySystems
|
||||
{
|
||||
public class StunSystem : EntitySystem
|
||||
{
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
EntityQuery = new TypeEntityQuery(typeof(StunnableComponent));
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
foreach (var entity in RelevantEntities)
|
||||
{
|
||||
entity.GetComponent<StunnableComponent>().Update(frameTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
81
Content.Server/Mobs/StandingStateHelper.cs
Normal file
81
Content.Server/Mobs/StandingStateHelper.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using Content.Server.Interfaces.GameObjects;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.GameObjects.Components.Mobs;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Server.GameObjects.EntitySystems;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Interfaces.Random;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.Mobs
|
||||
{
|
||||
public static class StandingStateHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Set's the mob standing state to down.
|
||||
/// </summary>
|
||||
/// <param name="entity">The mob in question</param>
|
||||
/// <param name="playSound">Whether to play a sound when falling down or not</param>
|
||||
/// <param name="dropItems">Whether to make the mob drop all the items on his hands</param>
|
||||
/// <returns>False if the mob was already downed or couldn't set the state</returns>
|
||||
public static bool Down(IEntity entity, bool playSound = true, bool dropItems = true)
|
||||
{
|
||||
if (!entity.TryGetComponent(out AppearanceComponent appearance)) return false;
|
||||
|
||||
appearance.TryGetData<SharedSpeciesComponent.MobState>(SharedSpeciesComponent.MobVisuals.RotationState, out var oldState);
|
||||
|
||||
var newState = SharedSpeciesComponent.MobState.Down;
|
||||
if (newState == oldState)
|
||||
return false;
|
||||
|
||||
appearance.SetData(SharedSpeciesComponent.MobVisuals.RotationState, newState);
|
||||
|
||||
if (playSound)
|
||||
PlaySoundCollection("bodyfall", AudioHelpers.WithVariation(0.5f));
|
||||
|
||||
if(dropItems)
|
||||
DropAllItemsInHands(entity);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the mob's standing state to standing.
|
||||
/// </summary>
|
||||
/// <param name="entity">The mob in question.</param>
|
||||
/// <returns>False if the mob was already standing or couldn't set the state</returns>
|
||||
public static bool Standing(IEntity entity)
|
||||
{
|
||||
if (!entity.TryGetComponent(out AppearanceComponent appearance)) return false;
|
||||
appearance.TryGetData<SharedSpeciesComponent.MobState>(SharedSpeciesComponent.MobVisuals.RotationState, out var oldState);
|
||||
var newState = SharedSpeciesComponent.MobState.Standing;
|
||||
if (newState == oldState)
|
||||
return false;
|
||||
|
||||
appearance.SetData(SharedSpeciesComponent.MobVisuals.RotationState, newState);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void DropAllItemsInHands(IEntity entity)
|
||||
{
|
||||
if (!entity.TryGetComponent(out IHandsComponent hands)) return;
|
||||
|
||||
foreach (var heldItem in hands.GetAllHeldItems())
|
||||
{
|
||||
hands.Drop(heldItem.Owner);
|
||||
}
|
||||
}
|
||||
|
||||
private static void PlaySoundCollection(string name, AudioParams parameters = default)
|
||||
{
|
||||
var soundCollection = IoCManager.Resolve<IPrototypeManager>().Index<SoundCollectionPrototype>(name);
|
||||
var file = IoCManager.Resolve<IRobustRandom>().Pick(soundCollection.PickFiles);
|
||||
IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<AudioSystem>()
|
||||
.Play(file, parameters);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ namespace Content.Shared.GameObjects.Components.Mobs
|
||||
/// <summary>
|
||||
/// Mob is standing up
|
||||
/// </summary>
|
||||
Stand,
|
||||
Standing,
|
||||
|
||||
/// <summary>
|
||||
/// Mob is laying down
|
||||
|
||||
Reference in New Issue
Block a user