Refactor standing to be ECS (#4142)
* Refactor standing to be ECS E C S B A B Y * DONE * FIX IT FIX IT FIX IT * IsDown event * Change to methods * Fixes * Address some reviews * Last of the Mohicans * Final fixes * Fix tests
This commit is contained in:
@@ -189,7 +189,7 @@ namespace Content.Shared.Body.Components
|
||||
if (part.PartType == BodyPartType.Leg &&
|
||||
GetPartsOfType(BodyPartType.Leg).ToArray().Length == 0)
|
||||
{
|
||||
EntitySystem.Get<SharedStandingStateSystem>().Down(Owner);
|
||||
EntitySystem.Get<StandingStateSystem>().Down(Owner);
|
||||
}
|
||||
|
||||
// creadth: immediately kill entity if last vital part removed
|
||||
|
||||
@@ -48,10 +48,7 @@ namespace Content.Shared.Buckle.Components
|
||||
return !Buckled;
|
||||
}
|
||||
|
||||
bool IEffectBlocker.CanFall()
|
||||
{
|
||||
return !Buckled;
|
||||
}
|
||||
bool IEffectBlocker.CanFall() => !Buckled;
|
||||
|
||||
bool IDraggable.CanDrop(CanDropEvent args)
|
||||
{
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Content.Shared.Buckle.Components;
|
||||
using Content.Shared.Standing;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Physics.Dynamics;
|
||||
|
||||
@@ -10,6 +11,24 @@ namespace Content.Shared.Buckle
|
||||
{
|
||||
base.Initialize();
|
||||
SubscribeLocalEvent<SharedBuckleComponent, PreventCollideEvent>(PreventCollision);
|
||||
SubscribeLocalEvent<SharedBuckleComponent, DownAttemptEvent>(HandleDown);
|
||||
SubscribeLocalEvent<SharedBuckleComponent, StandAttemptEvent>(HandleStand);
|
||||
}
|
||||
|
||||
private void HandleStand(EntityUid uid, SharedBuckleComponent component, StandAttemptEvent args)
|
||||
{
|
||||
if (component.Buckled)
|
||||
{
|
||||
args.Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleDown(EntityUid uid, SharedBuckleComponent component, DownAttemptEvent args)
|
||||
{
|
||||
if (component.Buckled)
|
||||
{
|
||||
args.Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
private void PreventCollision(EntityUid uid, SharedBuckleComponent component, PreventCollideEvent args)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using Content.Shared.ActionBlocker;
|
||||
using JetBrains.Annotations;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
@@ -23,6 +23,38 @@ namespace Content.Shared.Hands
|
||||
SubscribeNetworkEvent<RequestDropHeldEntityEvent>(HandleDrop);
|
||||
}
|
||||
|
||||
public void DropHandItems(IEntity entity, bool doMobChecks = true)
|
||||
{
|
||||
if (!entity.TryGetComponent(out SharedHandsComponent? handsComponent)) return;
|
||||
DropHandItems(handsComponent, doMobChecks);
|
||||
}
|
||||
|
||||
private void DropHandItems(SharedHandsComponent handsComponent, bool doMobChecks = true)
|
||||
{
|
||||
var msg = new DropHandItemsAttemptEvent();
|
||||
var entity = handsComponent.Owner;
|
||||
var uid = entity.Uid;
|
||||
var eventBus = EntityManager.EventBus;
|
||||
|
||||
eventBus.RaiseLocalEvent(uid, msg);
|
||||
|
||||
if (msg.Cancelled) return;
|
||||
|
||||
if (entity.TryGetContainerMan(out var containerManager))
|
||||
{
|
||||
var parentMsg = new ContainedEntityDropHandItemsAttemptEvent(uid);
|
||||
eventBus.RaiseLocalEvent(containerManager.Owner.Uid, parentMsg);
|
||||
|
||||
if (parentMsg.Cancelled) return;
|
||||
}
|
||||
|
||||
DropAllItemsInHands(entity, doMobChecks);
|
||||
}
|
||||
|
||||
protected virtual void DropAllItemsInHands(IEntity entity, bool doMobChecks = true)
|
||||
{
|
||||
}
|
||||
|
||||
private void HandleSetHand(RequestSetHandEvent msg, EntitySessionEventArgs eventArgs)
|
||||
{
|
||||
var entity = eventArgs.SenderSession?.AttachedEntity;
|
||||
@@ -46,6 +78,18 @@ namespace Content.Shared.Hands
|
||||
protected abstract void HandleContainerModified(EntityUid uid, SharedHandsComponent component, ContainerModifiedMessage args);
|
||||
}
|
||||
|
||||
public sealed class ContainedEntityDropHandItemsAttemptEvent : CancellableEntityEventArgs
|
||||
{
|
||||
public EntityUid EntityUid { get; }
|
||||
|
||||
public ContainedEntityDropHandItemsAttemptEvent(EntityUid uid)
|
||||
{
|
||||
EntityUid = uid;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class DropHandItemsAttemptEvent : CancellableEntityEventArgs {}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public class RequestSetHandEvent : EntityEventArgs
|
||||
{
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#nullable enable
|
||||
using Content.Shared.Alert;
|
||||
using Content.Shared.Hands;
|
||||
using Content.Shared.Standing;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
@@ -20,13 +21,20 @@ namespace Content.Shared.MobState.State
|
||||
{
|
||||
status.ShowAlert(AlertType.HumanCrit); // TODO: combine humancrit-0 and humancrit-1 into a gif and display it
|
||||
}
|
||||
|
||||
EntitySystem.Get<StandingStateSystem>().Down(entity);
|
||||
|
||||
if (entity.TryGetComponent(out SharedAppearanceComponent? appearance))
|
||||
{
|
||||
appearance.SetData(DamageStateVisuals.State, DamageState.Critical);
|
||||
}
|
||||
}
|
||||
|
||||
public override void ExitState(IEntity entity)
|
||||
{
|
||||
base.ExitState(entity);
|
||||
|
||||
EntitySystem.Get<SharedStandingStateSystem>().Standing(entity);
|
||||
EntitySystem.Get<StandingStateSystem>().Stand(entity);
|
||||
}
|
||||
|
||||
public override bool CanInteract()
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using Robust.Shared.GameObjects;
|
||||
using Content.Shared.Hands;
|
||||
using Content.Shared.Standing;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Shared.MobState.State
|
||||
{
|
||||
@@ -11,6 +13,18 @@ namespace Content.Shared.MobState.State
|
||||
base.EnterState(entity);
|
||||
var wake = entity.EnsureComponent<CollisionWakeComponent>();
|
||||
wake.Enabled = true;
|
||||
var standingState = EntitySystem.Get<StandingStateSystem>();
|
||||
standingState.Down(entity);
|
||||
|
||||
if (standingState.IsDown(entity) && entity.TryGetComponent(out PhysicsComponent? physics))
|
||||
{
|
||||
physics.CanCollide = false;
|
||||
}
|
||||
|
||||
if (entity.TryGetComponent(out SharedAppearanceComponent? appearance))
|
||||
{
|
||||
appearance.SetData(DamageStateVisuals.State, DamageState.Dead);
|
||||
}
|
||||
}
|
||||
|
||||
public override void ExitState(IEntity entity)
|
||||
@@ -20,6 +34,14 @@ namespace Content.Shared.MobState.State
|
||||
{
|
||||
entity.RemoveComponent<CollisionWakeComponent>();
|
||||
}
|
||||
|
||||
var standingState = EntitySystem.Get<StandingStateSystem>();
|
||||
standingState.Stand(entity);
|
||||
|
||||
if (!standingState.IsDown(entity) && entity.TryGetComponent(out PhysicsComponent? physics))
|
||||
{
|
||||
physics.CanCollide = true;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool CanInteract()
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
#nullable enable
|
||||
using Content.Shared.Standing;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Content.Shared.MobState.State
|
||||
{
|
||||
@@ -9,6 +12,17 @@ namespace Content.Shared.MobState.State
|
||||
{
|
||||
protected override DamageState DamageState => DamageState.Alive;
|
||||
|
||||
public override void EnterState(IEntity entity)
|
||||
{
|
||||
base.EnterState(entity);
|
||||
EntitySystem.Get<StandingStateSystem>().Stand(entity);
|
||||
|
||||
if (entity.TryGetComponent(out SharedAppearanceComponent? appearance))
|
||||
{
|
||||
appearance.SetData(DamageStateVisuals.State, DamageState.Alive);
|
||||
}
|
||||
}
|
||||
|
||||
public override bool CanInteract()
|
||||
{
|
||||
return true;
|
||||
|
||||
@@ -100,6 +100,7 @@ namespace Content.Shared.NetIDs
|
||||
public const uint LIGHT_REPLACER = 1090;
|
||||
public const uint SINGULARITY_DISTORTION = 1091;
|
||||
public const uint GRAVITY = 1092;
|
||||
public const uint STANDING_STATE = 1093;
|
||||
|
||||
// Net IDs for integration tests.
|
||||
public const uint PREDICTION_TEST = 10001;
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
#nullable enable
|
||||
using Content.Shared.EffectBlocker;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Shared.Standing
|
||||
{
|
||||
public abstract class SharedStandingStateSystem : EntitySystem
|
||||
{
|
||||
protected abstract bool OnDown(IEntity entity, bool playSound = true, bool dropItems = true,
|
||||
bool force = false);
|
||||
|
||||
protected abstract bool OnStand(IEntity entity);
|
||||
|
||||
/// <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>
|
||||
/// <param name="force">Whether or not to check if the entity can fall.</param>
|
||||
/// <returns>False if the mob was already downed or couldn't set the state</returns>
|
||||
public bool Down(IEntity entity, bool playSound = true, bool dropItems = true, bool force = false)
|
||||
{
|
||||
if (dropItems)
|
||||
{
|
||||
DropAllItemsInHands(entity, false);
|
||||
}
|
||||
|
||||
if (!force && !EffectBlockerSystem.CanFall(entity))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return OnDown(entity, playSound, dropItems, force);
|
||||
}
|
||||
|
||||
/// <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 bool Standing(IEntity entity)
|
||||
{
|
||||
return OnStand(entity);
|
||||
}
|
||||
|
||||
public virtual void DropAllItemsInHands(IEntity entity, bool doMobChecks = true)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
54
Content.Shared/Standing/StandingStateComponent.cs
Normal file
54
Content.Shared/Standing/StandingStateComponent.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using Content.Shared.EffectBlocker;
|
||||
using Content.Shared.NetIDs;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Players;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Shared.Standing
|
||||
{
|
||||
[RegisterComponent]
|
||||
public sealed class StandingStateComponent : Component, IEffectBlocker
|
||||
{
|
||||
public override string Name => "StandingState";
|
||||
|
||||
public override uint? NetID => ContentNetIDs.STANDING_STATE;
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
[DataField("downSoundCollection")]
|
||||
public string? DownSoundCollection { get; } = "BodyFall";
|
||||
|
||||
[ViewVariables]
|
||||
[DataField("standing")]
|
||||
public bool Standing { get; set; } = true;
|
||||
|
||||
public bool CanFall() => Standing;
|
||||
|
||||
public override ComponentState GetComponentState(ICommonSession player)
|
||||
{
|
||||
return new StandingComponentState(Standing);
|
||||
}
|
||||
|
||||
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
|
||||
{
|
||||
base.HandleComponentState(curState, nextState);
|
||||
if (curState is not StandingComponentState state) return;
|
||||
|
||||
Standing = state.Standing;
|
||||
}
|
||||
|
||||
// I'm not calling it StandingStateComponentState
|
||||
[Serializable, NetSerializable]
|
||||
private sealed class StandingComponentState : ComponentState
|
||||
{
|
||||
public bool Standing { get; }
|
||||
|
||||
public StandingComponentState(bool standing) : base(ContentNetIDs.STANDING_STATE)
|
||||
{
|
||||
Standing = standing;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
126
Content.Shared/Standing/StandingStateSystem.cs
Normal file
126
Content.Shared/Standing/StandingStateSystem.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
#nullable enable
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.Hands;
|
||||
using Content.Shared.Rotation;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.Player;
|
||||
|
||||
namespace Content.Shared.Standing
|
||||
{
|
||||
public sealed class StandingStateSystem : EntitySystem
|
||||
{
|
||||
public bool IsDown(IEntity entity)
|
||||
{
|
||||
if (entity.TryGetComponent(out StandingStateComponent? standingState) &&
|
||||
standingState.Standing) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Down(IEntity entity, bool playSound = true, bool dropHeldItems = true)
|
||||
{
|
||||
if (!entity.TryGetComponent(out StandingStateComponent? comp)) return;
|
||||
Down(comp, playSound, dropHeldItems);
|
||||
}
|
||||
|
||||
public void Stand(IEntity entity)
|
||||
{
|
||||
if (!entity.TryGetComponent(out StandingStateComponent? comp)) return;
|
||||
Stand(comp);
|
||||
}
|
||||
|
||||
public void Down(StandingStateComponent component, bool playSound = true, bool dropHeldItems = true)
|
||||
{
|
||||
if (!component.Standing) return;
|
||||
|
||||
var entity = component.Owner;
|
||||
var uid = entity.Uid;
|
||||
|
||||
// This is just to avoid most callers doing this manually saving boilerplate
|
||||
// 99% of the time you'll want to drop items but in some scenarios (e.g. buckling) you don't want to.
|
||||
// We do this BEFORE downing because something like buckle may be blocking downing but we want to drop hand items anyway
|
||||
// and ultimately this is just to avoid boilerplate in Down callers + keep their behavior consistent.
|
||||
if (dropHeldItems)
|
||||
{
|
||||
Get<SharedHandsSystem>().DropHandItems(entity, false);
|
||||
}
|
||||
|
||||
var msg = new DownAttemptEvent();
|
||||
EntityManager.EventBus.RaiseLocalEvent(uid, msg);
|
||||
|
||||
if (msg.Cancelled) return;
|
||||
|
||||
component.Standing = false;
|
||||
component.Dirty();
|
||||
EntityManager.EventBus.RaiseLocalEvent(uid, new DownedEvent());
|
||||
|
||||
// Seemed like the best place to put it
|
||||
if (entity.TryGetComponent(out SharedAppearanceComponent? appearance))
|
||||
{
|
||||
appearance.SetData(RotationVisuals.RotationState, RotationState.Horizontal);
|
||||
}
|
||||
|
||||
// Currently shit is only downed by server but when it's predicted we can probably only play this on server / client
|
||||
var sound = component.DownSoundCollection;
|
||||
|
||||
if (playSound && !string.IsNullOrEmpty(sound))
|
||||
{
|
||||
var file = AudioHelpers.GetRandomFileFromSoundCollection(sound);
|
||||
SoundSystem.Play(Filter.Pvs(entity), file, entity, AudioHelpers.WithVariation(0.25f));
|
||||
}
|
||||
}
|
||||
|
||||
public void Stand(StandingStateComponent component)
|
||||
{
|
||||
if (component.Standing) return;
|
||||
|
||||
var entity = component.Owner;
|
||||
var uid = entity.Uid;
|
||||
|
||||
var msg = new StandAttemptEvent();
|
||||
EntityManager.EventBus.RaiseLocalEvent(uid, msg);
|
||||
|
||||
if (msg.Cancelled) return;
|
||||
|
||||
component.Standing = true;
|
||||
component.Dirty();
|
||||
EntityManager.EventBus.RaiseLocalEvent(uid, new StoodEvent());
|
||||
|
||||
if (entity.TryGetComponent(out SharedAppearanceComponent? appearance))
|
||||
{
|
||||
appearance.SetData(RotationVisuals.RotationState, RotationState.Vertical);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Subscribe if you can potentially block a down attempt.
|
||||
/// </summary>
|
||||
public sealed class DownAttemptEvent : CancellableEntityEventArgs
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Subscribe if you can potentially block a stand attempt.
|
||||
/// </summary>
|
||||
public sealed class StandAttemptEvent : CancellableEntityEventArgs
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raised when an entity becomes standing
|
||||
/// </summary>
|
||||
public sealed class StoodEvent : EntityEventArgs
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Raised when an entity is not standing
|
||||
/// </summary>
|
||||
public sealed class DownedEvent : EntityEventArgs
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user