Refactors stunnable to be ECS. (#4819)

Also cleans up StandingStatesystem.
This commit is contained in:
Vera Aguilera Puerto
2021-10-10 12:47:26 +02:00
committed by GitHub
parent 19a588a70a
commit 6eee256b11
34 changed files with 776 additions and 633 deletions

View File

@@ -1,3 +1,4 @@
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
@@ -6,18 +7,26 @@ namespace Content.Server.Stunnable.Components
/// <summary>
/// Adds stun when it collides with an entity
/// </summary>
[RegisterComponent]
internal sealed class StunOnCollideComponent : Component
[RegisterComponent, Friend(typeof(StunOnCollideSystem))]
public sealed class StunOnCollideComponent : Component
{
// TODO: Can probably predict this.
public override string Name => "StunOnCollide";
// See stunnable for what these do
[DataField("stunAmount")]
internal int StunAmount = default;
public int StunAmount;
[DataField("knockdownAmount")]
internal int KnockdownAmount = default;
public int KnockdownAmount;
[DataField("slowdownAmount")]
internal int SlowdownAmount = default;
public int SlowdownAmount;
[DataField("walkSpeedMultiplier")]
public float WalkSpeedMultiplier = 1f;
[DataField("runSpeedMultiplier")]
public float RunSpeedMultiplier = 1f;
}
}

View File

@@ -1,86 +0,0 @@
using Content.Server.Act;
using Content.Server.Popups;
using Content.Shared.Audio;
using Content.Shared.MobState;
using Content.Shared.Popups;
using Content.Shared.Sound;
using Content.Shared.Standing;
using Content.Shared.Stunnable;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Player;
using Robust.Shared.Random;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.Stunnable.Components
{
[RegisterComponent]
[ComponentReference(typeof(SharedStunnableComponent))]
public class StunnableComponent : SharedStunnableComponent, IDisarmedAct
{
[DataField("stunAttemptSound")] private SoundSpecifier _stunAttemptSound = new SoundPathSpecifier("/Audio/Effects/thudswoosh.ogg");
protected override void OnKnockdown()
{
EntitySystem.Get<StandingStateSystem>().Down(Owner);
}
protected override void OnKnockdownEnd()
{
if (Owner.TryGetComponent(out IMobStateComponent? mobState) && !mobState.IsIncapacitated())
EntitySystem.Get<StandingStateSystem>().Stand(Owner);
}
public void CancelAll()
{
KnockdownTimer = null;
StunnedTimer = null;
Dirty();
}
public void ResetStuns()
{
StunnedTimer = null;
SlowdownTimer = null;
if (KnockedDown &&
Owner.TryGetComponent(out IMobStateComponent? mobState) && !mobState.IsIncapacitated())
{
EntitySystem.Get<StandingStateSystem>().Stand(Owner);
}
KnockdownTimer = null;
Dirty();
}
protected override void OnInteractHand()
{
SoundSystem.Play(Filter.Pvs(Owner), _stunAttemptSound.GetSound(), Owner, AudioHelpers.WithVariation(0.05f));
}
bool IDisarmedAct.Disarmed(DisarmedActEventArgs eventArgs)
{
if (!IoCManager.Resolve<IRobustRandom>().Prob(eventArgs.PushProbability))
return false;
Paralyze(4f);
var source = eventArgs.Source;
var target = eventArgs.Target;
if (source != null)
{
SoundSystem.Play(Filter.Pvs(source), _stunAttemptSound.GetSound(), source, AudioHelpers.WithVariation(0.025f));
if (target != null)
{
source.PopupMessageOtherClients(Loc.GetString("stunnable-component-disarm-success-others", ("source", source.Name), ("target", target.Name)));
source.PopupMessageCursor(Loc.GetString("stunnable-component-disarm-success", ("target", target.Name)));
}
}
return true;
}
}
}

View File

@@ -1,6 +1,14 @@
using System;
using Content.Server.Alert;
using Content.Server.Stunnable.Components;
using Content.Shared.Alert;
using Content.Shared.Movement.Components;
using Content.Shared.Standing;
using Content.Shared.Stunnable;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Physics.Dynamics;
namespace Content.Server.Stunnable
@@ -8,6 +16,8 @@ namespace Content.Server.Stunnable
[UsedImplicitly]
internal sealed class StunOnCollideSystem : EntitySystem
{
[Dependency] private readonly StunSystem _stunSystem = default!;
public override void Initialize()
{
base.Initialize();
@@ -16,11 +26,25 @@ namespace Content.Server.Stunnable
private void HandleCollide(EntityUid uid, StunOnCollideComponent component, StartCollideEvent args)
{
if (args.OtherFixture.Body.Owner.TryGetComponent(out StunnableComponent? stunnableComponent))
var otherUid = args.OtherFixture.Body.Owner.Uid;
if (EntityManager.TryGetComponent(otherUid, out StunnableComponent? stunnableComponent))
{
stunnableComponent.Stun(component.StunAmount);
stunnableComponent.Knockdown(component.KnockdownAmount);
stunnableComponent.Slowdown(component.SlowdownAmount);
ServerAlertsComponent? alerts = null;
StandingStateComponent? standingState = null;
AppearanceComponent? appearance = null;
MovementSpeedModifierComponent? speedModifier = null;
// Let the actual methods log errors for these.
Resolve(otherUid, ref alerts, ref standingState, ref appearance, ref speedModifier, false);
_stunSystem.Stun(otherUid, TimeSpan.FromSeconds(component.StunAmount), stunnableComponent, alerts);
_stunSystem.Knockdown(otherUid, TimeSpan.FromSeconds(component.KnockdownAmount), stunnableComponent,
alerts, standingState, appearance);
_stunSystem.Slowdown(otherUid, TimeSpan.FromSeconds(component.SlowdownAmount),
component.WalkSpeedMultiplier, component.RunSpeedMultiplier, stunnableComponent, speedModifier, alerts);
}
}
}

View File

@@ -0,0 +1,52 @@
using System;
using Content.Server.Act;
using Content.Server.Popups;
using Content.Shared.Audio;
using Content.Shared.Popups;
using Content.Shared.Stunnable;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Player;
using Robust.Shared.Random;
namespace Content.Server.Stunnable
{
public sealed class StunSystem : SharedStunSystem
{
[Dependency] private readonly IRobustRandom _random = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<StunnableComponent, DisarmedActEvent>(OnDisarmed);
}
private void OnDisarmed(EntityUid uid, StunnableComponent stunnable, DisarmedActEvent args)
{
if (args.Handled || !_random.Prob(args.PushProbability))
return;
Paralyze(uid, TimeSpan.FromSeconds(4f), stunnable);
var source = args.Source;
var target = args.Target;
if (source != null)
{
SoundSystem.Play(Filter.Pvs(source), stunnable.StunAttemptSound.GetSound(), source, AudioHelpers.WithVariation(0.025f));
if (target != null)
{
// TODO: Use PopupSystem
source.PopupMessageOtherClients(Loc.GetString("stunnable-component-disarm-success-others", ("source", source.Name), ("target", target.Name)));
source.PopupMessageCursor(Loc.GetString("stunnable-component-disarm-success", ("target", target.Name)));
}
}
args.Handled = true;
}
}
}

View File

@@ -10,6 +10,7 @@ using Content.Shared.Audio;
using Content.Shared.Examine;
using Content.Shared.Interaction;
using Content.Shared.Popups;
using Content.Shared.Stunnable;
using Content.Shared.Throwing;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
@@ -23,6 +24,7 @@ namespace Content.Server.Stunnable
{
public class StunbatonSystem : EntitySystem
{
[Dependency] private readonly StunSystem _stunSystem = default!;
[Dependency] private readonly IRobustRandom _robustRandom = default!;
public override void Initialize()
@@ -119,20 +121,22 @@ namespace Content.Server.Stunnable
{
if (!entity.TryGetComponent(out StunnableComponent? stunnable) || !comp.Activated) return;
// TODO: Make slowdown inflicted customizable.
SoundSystem.Play(Filter.Pvs(comp.Owner), comp.StunSound.GetSound(), comp.Owner, AudioHelpers.WithVariation(0.25f));
if (!stunnable.SlowedDown)
{
if (_robustRandom.Prob(comp.ParalyzeChanceNoSlowdown))
stunnable.Paralyze(comp.ParalyzeTime);
_stunSystem.Paralyze(entity.Uid, TimeSpan.FromSeconds(comp.ParalyzeTime), stunnable);
else
stunnable.Slowdown(comp.SlowdownTime);
_stunSystem.Slowdown(entity.Uid, TimeSpan.FromSeconds(comp.SlowdownTime), 0.5f, 0.5f, stunnable);
}
else
{
if (_robustRandom.Prob(comp.ParalyzeChanceWithSlowdown))
stunnable.Paralyze(comp.ParalyzeTime);
_stunSystem.Paralyze(entity.Uid, TimeSpan.FromSeconds(comp.ParalyzeTime), stunnable);
else
stunnable.Slowdown(comp.SlowdownTime);
_stunSystem.Slowdown(entity.Uid, TimeSpan.FromSeconds(comp.SlowdownTime), 0.5f, 0.5f, stunnable);
}