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:
metalgearsloth
2021-06-27 19:02:46 +10:00
committed by GitHub
parent 97f4f0a9bd
commit 50cc526ebd
30 changed files with 328 additions and 268 deletions

View File

@@ -1,6 +1,6 @@
using Content.Client.Standing; using Content.Shared.MobState;
using Content.Shared.MobState;
using Content.Shared.MobState.State; using Content.Shared.MobState.State;
using Content.Shared.Standing;
using Robust.Client.GameObjects; using Robust.Client.GameObjects;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
@@ -16,25 +16,6 @@ namespace Content.Client.MobState.States
{ {
appearance.SetData(DamageStateVisuals.State, DamageState.Dead); appearance.SetData(DamageStateVisuals.State, DamageState.Dead);
} }
EntitySystem.Get<StandingStateSystem>().Down(entity);
if (entity.TryGetComponent(out PhysicsComponent? physics))
{
physics.CanCollide = false;
}
}
public override void ExitState(IEntity entity)
{
base.ExitState(entity);
EntitySystem.Get<StandingStateSystem>().Standing(entity);
if (entity.TryGetComponent(out PhysicsComponent? physics))
{
physics.CanCollide = true;
}
} }
} }
} }

View File

@@ -7,14 +7,5 @@ namespace Content.Client.MobState.States
{ {
public class NormalMobState : SharedNormalMobState public class NormalMobState : SharedNormalMobState
{ {
public override void EnterState(IEntity entity)
{
base.EnterState(entity);
if (entity.TryGetComponent(out AppearanceComponent? appearance))
{
appearance.SetData(DamageStateVisuals.State, DamageState.Alive);
}
}
} }
} }

View File

@@ -1,42 +0,0 @@
using Content.Shared.Rotation;
using Content.Shared.Standing;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;
namespace Content.Client.Standing
{
public class StandingStateSystem : SharedStandingStateSystem
{
protected override bool OnDown(IEntity entity, bool playSound = true, bool dropItems = true, bool force = false)
{
if (!entity.TryGetComponent(out AppearanceComponent? appearance))
{
return false;
}
var newState = RotationState.Horizontal;
appearance.TryGetData<RotationState>(RotationVisuals.RotationState, out var oldState);
if (newState != oldState)
{
appearance.SetData(RotationVisuals.RotationState, newState);
}
return true;
}
protected override bool OnStand(IEntity entity)
{
if (!entity.TryGetComponent(out AppearanceComponent? appearance)) return false;
appearance.TryGetData<RotationState>(RotationVisuals.RotationState, out var oldState);
var newState = RotationState.Vertical;
if (newState == oldState) return false;
appearance.SetData(RotationVisuals.RotationState, newState);
return true;
}
}
}

View File

@@ -27,6 +27,7 @@ namespace Content.IntegrationTests.Tests.Body
template: HumanoidTemplate template: HumanoidTemplate
preset: HumanPreset preset: HumanPreset
centerSlot: torso centerSlot: torso
- type: StandingState
"; ";
[Test] [Test]

View File

@@ -39,6 +39,7 @@ namespace Content.IntegrationTests.Tests.Buckle
template: HumanoidTemplate template: HumanoidTemplate
preset: HumanPreset preset: HumanPreset
centerSlot: torso centerSlot: torso
- type: StandingState
- type: entity - type: entity
name: {StrapDummyId} name: {StrapDummyId}

View File

@@ -5,14 +5,13 @@ using Content.Server.Alert;
using Content.Server.Hands.Components; using Content.Server.Hands.Components;
using Content.Server.MobState.States; using Content.Server.MobState.States;
using Content.Server.Pulling; using Content.Server.Pulling;
using Content.Server.Standing;
using Content.Server.Stunnable.Components; using Content.Server.Stunnable.Components;
using Content.Shared.ActionBlocker; using Content.Shared.ActionBlocker;
using Content.Shared.Alert; using Content.Shared.Alert;
using Content.Shared.Buckle.Components; using Content.Shared.Buckle.Components;
using Content.Shared.Interaction.Events;
using Content.Shared.Interaction.Helpers; using Content.Shared.Interaction.Helpers;
using Content.Shared.Notification.Managers; using Content.Shared.Notification.Managers;
using Content.Shared.Standing;
using Content.Shared.Verbs; using Content.Shared.Verbs;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Shared.Audio; using Robust.Shared.Audio;
@@ -130,12 +129,12 @@ namespace Content.Server.Buckle.Components
ownTransform.WorldRotation = strapTransform.WorldRotation; ownTransform.WorldRotation = strapTransform.WorldRotation;
break; break;
case StrapPosition.Stand: case StrapPosition.Stand:
EntitySystem.Get<StandingStateSystem>().Standing(Owner); EntitySystem.Get<StandingStateSystem>().Stand(Owner);
ownTransform.WorldRotation = strapTransform.WorldRotation; ownTransform.WorldRotation = strapTransform.WorldRotation;
break; break;
case StrapPosition.Down: case StrapPosition.Down:
EntitySystem.Get<StandingStateSystem>().Down(Owner, force: true); EntitySystem.Get<StandingStateSystem>().Down(Owner, false, false);
ownTransform.WorldRotation = Angle.South; ownTransform.LocalRotation = Angle.Zero;
break; break;
} }
@@ -343,7 +342,7 @@ namespace Content.Server.Buckle.Components
} }
else else
{ {
EntitySystem.Get<StandingStateSystem>().Standing(Owner); EntitySystem.Get<StandingStateSystem>().Stand(Owner);
} }
_mobState?.CurrentState?.EnterState(Owner); _mobState?.CurrentState?.EnterState(Owner);

View File

@@ -50,6 +50,18 @@ namespace Content.Server.Hands
CommandBinds.Unregister<HandsSystem>(); CommandBinds.Unregister<HandsSystem>();
} }
protected override void DropAllItemsInHands(IEntity entity, bool doMobChecks = true)
{
base.DropAllItemsInHands(entity, doMobChecks);
if (!entity.TryGetComponent(out IHandsComponent? hands)) return;
foreach (var heldItem in hands.GetAllHeldItems())
{
hands.Drop(heldItem.Owner, doMobChecks, intentional:false);
}
}
//TODO: Actually shows all items/clothing/etc. //TODO: Actually shows all items/clothing/etc.
private void HandleExamined(EntityUid uid, HandsComponent component, ExaminedEvent args) private void HandleExamined(EntityUid uid, HandsComponent component, ExaminedEvent args)
{ {

View File

@@ -1,7 +1,6 @@
#nullable enable #nullable enable
using System; using System;
using System.Linq; using System.Linq;
using Content.Server.Standing;
using Content.Server.Stunnable.Components; using Content.Server.Stunnable.Components;
using Content.Server.UserInterface; using Content.Server.UserInterface;
using Content.Shared.ActionBlocker; using Content.Shared.ActionBlocker;
@@ -9,8 +8,8 @@ using Content.Shared.DragDrop;
using Content.Shared.Hands; using Content.Shared.Hands;
using Content.Shared.Instruments; using Content.Shared.Instruments;
using Content.Shared.Interaction; using Content.Shared.Interaction;
using Content.Shared.Interaction.Events;
using Content.Shared.Notification.Managers; using Content.Shared.Notification.Managers;
using Content.Shared.Standing;
using Content.Shared.Throwing; using Content.Shared.Throwing;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Server.Player; using Robust.Server.Player;
@@ -357,7 +356,7 @@ namespace Content.Server.Instruments
if (mob != null) if (mob != null)
{ {
if (Handheld) if (Handheld)
EntitySystem.Get<StandingStateSystem>().DropAllItemsInHands(mob, false); EntitySystem.Get<StandingStateSystem>().Down(mob, false);
if (mob.TryGetComponent(out StunnableComponent? stun)) if (mob.TryGetComponent(out StunnableComponent? stun))
{ {

View File

@@ -1,8 +1,5 @@
using Content.Server.Standing; using Content.Server.Stunnable.Components;
using Content.Server.Stunnable.Components;
using Content.Shared.MobState;
using Content.Shared.MobState.State; using Content.Shared.MobState.State;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
namespace Content.Server.MobState.States namespace Content.Server.MobState.States
@@ -13,17 +10,10 @@ namespace Content.Server.MobState.States
{ {
base.EnterState(entity); base.EnterState(entity);
if (entity.TryGetComponent(out AppearanceComponent? appearance))
{
appearance.SetData(DamageStateVisuals.State, DamageState.Critical);
}
if (entity.TryGetComponent(out StunnableComponent? stun)) if (entity.TryGetComponent(out StunnableComponent? stun))
{ {
stun.CancelAll(); stun.CancelAll();
} }
EntitySystem.Get<StandingStateSystem>().Down(entity);
} }
} }
} }

View File

@@ -1,5 +1,4 @@
using Content.Server.Alert; using Content.Server.Alert;
using Content.Server.Standing;
using Content.Server.Stunnable.Components; using Content.Server.Stunnable.Components;
using Content.Shared.Alert; using Content.Shared.Alert;
using Content.Shared.MobState; using Content.Shared.MobState;
@@ -15,11 +14,6 @@ namespace Content.Server.MobState.States
{ {
base.EnterState(entity); base.EnterState(entity);
if (entity.TryGetComponent(out AppearanceComponent? appearance))
{
appearance.SetData(DamageStateVisuals.State, DamageState.Dead);
}
if (entity.TryGetComponent(out ServerAlertsComponent? status)) if (entity.TryGetComponent(out ServerAlertsComponent? status))
{ {
status.ShowAlert(AlertType.HumanDead); status.ShowAlert(AlertType.HumanDead);
@@ -29,8 +23,6 @@ namespace Content.Server.MobState.States
{ {
stun.CancelAll(); stun.CancelAll();
} }
EntitySystem.Get<StandingStateSystem>().Down(entity);
} }
} }
} }

View File

@@ -1,29 +1,15 @@
#nullable enable #nullable enable
using Content.Server.Alert; using Content.Server.Alert;
using Content.Server.Standing;
using Content.Shared.Alert; using Content.Shared.Alert;
using Content.Shared.Damage.Components; using Content.Shared.Damage.Components;
using Content.Shared.MobState; using Content.Shared.MobState;
using Content.Shared.MobState.State; using Content.Shared.MobState.State;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
namespace Content.Server.MobState.States namespace Content.Server.MobState.States
{ {
public class NormalMobState : SharedNormalMobState public class NormalMobState : SharedNormalMobState
{ {
public override void EnterState(IEntity entity)
{
base.EnterState(entity);
EntitySystem.Get<StandingStateSystem>().Standing(entity);
if (entity.TryGetComponent(out AppearanceComponent? appearance))
{
appearance.SetData(DamageStateVisuals.State, DamageState.Alive);
}
}
public override void UpdateState(IEntity entity, int threshold) public override void UpdateState(IEntity entity, int threshold)
{ {
base.UpdateState(entity, threshold); base.UpdateState(entity, threshold);

View File

@@ -3,15 +3,14 @@ using System.Threading.Tasks;
using Content.Server.Hands.Components; using Content.Server.Hands.Components;
using Content.Server.Items; using Content.Server.Items;
using Content.Server.Paper; using Content.Server.Paper;
using Content.Server.Standing;
using Content.Server.Storage.Components; using Content.Server.Storage.Components;
using Content.Shared.ActionBlocker; using Content.Shared.ActionBlocker;
using Content.Shared.Body.Components; using Content.Shared.Body.Components;
using Content.Shared.Examine; using Content.Shared.Examine;
using Content.Shared.Interaction; using Content.Shared.Interaction;
using Content.Shared.Interaction.Events;
using Content.Shared.Morgue; using Content.Shared.Morgue;
using Content.Shared.Notification.Managers; using Content.Shared.Notification.Managers;
using Content.Shared.Standing;
using Content.Shared.Verbs; using Content.Shared.Verbs;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Shared.Containers; using Robust.Shared.Containers;
@@ -39,7 +38,7 @@ namespace Content.Server.Morgue.Components
{ {
base.Initialize(); base.Initialize();
_appearance?.SetData(BodyBagVisuals.Label, false); _appearance?.SetData(BodyBagVisuals.Label, false);
LabelContainer = ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, "body_bag_label", out _); LabelContainer = Owner.EnsureContainer<ContainerSlot>("body_bag_label", out _);
} }
protected override bool AddToContents(IEntity entity) protected override bool AddToContents(IEntity entity)

View File

@@ -131,11 +131,11 @@ namespace Content.Server.Morgue.Components
} }
victim.PopupMessageOtherClients(Loc.GetString("crematorium-entity-storage-component-suicide-message-others", ("victim", victim))); victim.PopupMessageOtherClients(Loc.GetString("crematorium-entity-storage-component-suicide-message-others", ("victim", victim)));
EntitySystem.Get<SharedStandingStateSystem>().Down(victim, false, false, true);
if (CanInsert(victim)) if (CanInsert(victim))
{ {
Insert(victim); Insert(victim);
EntitySystem.Get<StandingStateSystem>().Down(victim, false);
} }
else else
{ {

View File

@@ -1,5 +1,4 @@
#nullable enable #nullable enable
using Content.Server.Standing;
using Content.Server.Storage.Components; using Content.Server.Storage.Components;
using Content.Shared.Body.Components; using Content.Shared.Body.Components;
using Content.Shared.Directions; using Content.Shared.Directions;
@@ -9,6 +8,7 @@ using Content.Shared.Interaction.Helpers;
using Content.Shared.Morgue; using Content.Shared.Morgue;
using Content.Shared.Notification.Managers; using Content.Shared.Notification.Managers;
using Content.Shared.Physics; using Content.Shared.Physics;
using Content.Shared.Standing;
using Robust.Server.GameObjects; using Robust.Server.GameObjects;
using Robust.Shared.Audio; using Robust.Shared.Audio;
using Robust.Shared.Containers; using Robust.Shared.Containers;
@@ -51,7 +51,7 @@ namespace Content.Server.Morgue.Components
{ {
base.Initialize(); base.Initialize();
Appearance?.SetData(MorgueVisuals.Open, false); Appearance?.SetData(MorgueVisuals.Open, false);
TrayContainer = ContainerHelpers.EnsureContainer<ContainerSlot>(Owner, "morgue_tray", out _); TrayContainer = Owner.EnsureContainer<ContainerSlot>("morgue_tray", out _);
} }
public override Vector2 ContentsDumpPosition() public override Vector2 ContentsDumpPosition()

View File

@@ -1,83 +0,0 @@
#nullable enable
using Content.Server.Hands.Components;
using Content.Shared.Audio;
using Content.Shared.Rotation;
using Content.Shared.Standing;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.Player;
namespace Content.Server.Standing
{
[UsedImplicitly]
public class StandingStateSystem : SharedStandingStateSystem
{
protected override bool OnDown(IEntity entity, bool playSound = true, bool dropItems = true, bool force = false)
{
if (!entity.TryGetComponent(out AppearanceComponent? appearance))
{
return false;
}
var newState = RotationState.Horizontal;
appearance.TryGetData<RotationState>(RotationVisuals.RotationState, out var oldState);
if (newState != oldState)
{
appearance.SetData(RotationVisuals.RotationState, newState);
if (playSound)
{
var file = AudioHelpers.GetRandomFileFromSoundCollection("bodyfall");
SoundSystem.Play(Filter.Pvs(entity), file, entity, AudioHelpers.WithVariation(0.25f));
}
}
return true;
}
protected override bool OnStand(IEntity entity)
{
if (!entity.TryGetComponent(out AppearanceComponent? appearance)) return false;
appearance.TryGetData<RotationState>(RotationVisuals.RotationState, out var oldState);
var newState = RotationState.Vertical;
if (newState == oldState) return false;
appearance.SetData(RotationVisuals.RotationState, newState);
return true;
}
public override void DropAllItemsInHands(IEntity entity, bool doMobChecks = true)
{
base.DropAllItemsInHands(entity, doMobChecks);
if (!entity.TryGetComponent(out IHandsComponent? hands)) return;
foreach (var heldItem in hands.GetAllHeldItems())
{
hands.Drop(heldItem.Owner, doMobChecks, intentional:false);
}
}
//TODO: RotationState can be null and I want to burn all lifeforms in the universe for this!!!
//If you use these it's atleast slightly less painful (null is treated as false)
public bool IsStanding(IEntity entity)
{
return entity.TryGetComponent<AppearanceComponent>(out var appearance)
&& appearance.TryGetData<RotationState>(RotationVisuals.RotationState, out var rotation)
&& rotation == RotationState.Vertical;
}
public bool IsDown(IEntity entity)
{
return entity.TryGetComponent<AppearanceComponent>(out var appearance)
&& appearance.TryGetData<RotationState>(RotationVisuals.RotationState, out var rotation)
&& rotation == RotationState.Horizontal;
}
}
}

View File

@@ -1,11 +1,10 @@
#nullable enable #nullable enable
using Content.Server.Act; using Content.Server.Act;
using Content.Server.Notification; using Content.Server.Notification;
using Content.Server.Standing;
using Content.Shared.Audio; using Content.Shared.Audio;
using Content.Shared.MobState; using Content.Shared.MobState;
using Content.Shared.Notification;
using Content.Shared.Notification.Managers; using Content.Shared.Notification.Managers;
using Content.Shared.Standing;
using Content.Shared.Stunnable; using Content.Shared.Stunnable;
using Robust.Shared.Audio; using Robust.Shared.Audio;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
@@ -28,7 +27,7 @@ namespace Content.Server.Stunnable.Components
protected override void OnKnockdownEnd() protected override void OnKnockdownEnd()
{ {
if(Owner.TryGetComponent(out IMobStateComponent? mobState) && !mobState.IsIncapacitated()) if(Owner.TryGetComponent(out IMobStateComponent? mobState) && !mobState.IsIncapacitated())
EntitySystem.Get<StandingStateSystem>().Standing(Owner); EntitySystem.Get<StandingStateSystem>().Stand(Owner);
} }
public void CancelAll() public void CancelAll()
@@ -46,7 +45,7 @@ namespace Content.Server.Stunnable.Components
if (KnockedDown && if (KnockedDown &&
Owner.TryGetComponent(out IMobStateComponent? mobState) && !mobState.IsIncapacitated()) Owner.TryGetComponent(out IMobStateComponent? mobState) && !mobState.IsIncapacitated())
{ {
EntitySystem.Get<StandingStateSystem>().Standing(Owner); EntitySystem.Get<StandingStateSystem>().Stand(Owner);
} }
KnockdownTimer = null; KnockdownTimer = null;

View File

@@ -189,7 +189,7 @@ namespace Content.Shared.Body.Components
if (part.PartType == BodyPartType.Leg && if (part.PartType == BodyPartType.Leg &&
GetPartsOfType(BodyPartType.Leg).ToArray().Length == 0) 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 // creadth: immediately kill entity if last vital part removed

View File

@@ -48,10 +48,7 @@ namespace Content.Shared.Buckle.Components
return !Buckled; return !Buckled;
} }
bool IEffectBlocker.CanFall() bool IEffectBlocker.CanFall() => !Buckled;
{
return !Buckled;
}
bool IDraggable.CanDrop(CanDropEvent args) bool IDraggable.CanDrop(CanDropEvent args)
{ {

View File

@@ -1,4 +1,5 @@
using Content.Shared.Buckle.Components; using Content.Shared.Buckle.Components;
using Content.Shared.Standing;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Physics.Dynamics; using Robust.Shared.Physics.Dynamics;
@@ -10,6 +11,24 @@ namespace Content.Shared.Buckle
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<SharedBuckleComponent, PreventCollideEvent>(PreventCollision); 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) private void PreventCollision(EntityUid uid, SharedBuckleComponent component, PreventCollideEvent args)

View File

@@ -1,4 +1,5 @@
#nullable enable #nullable enable
using System;
using Content.Shared.ActionBlocker; using Content.Shared.ActionBlocker;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;

View File

@@ -23,6 +23,38 @@ namespace Content.Shared.Hands
SubscribeNetworkEvent<RequestDropHeldEntityEvent>(HandleDrop); 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) private void HandleSetHand(RequestSetHandEvent msg, EntitySessionEventArgs eventArgs)
{ {
var entity = eventArgs.SenderSession?.AttachedEntity; var entity = eventArgs.SenderSession?.AttachedEntity;
@@ -46,6 +78,18 @@ namespace Content.Shared.Hands
protected abstract void HandleContainerModified(EntityUid uid, SharedHandsComponent component, ContainerModifiedMessage args); 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] [Serializable, NetSerializable]
public class RequestSetHandEvent : EntityEventArgs public class RequestSetHandEvent : EntityEventArgs
{ {

View File

@@ -1,5 +1,6 @@
#nullable enable #nullable enable
using Content.Shared.Alert; using Content.Shared.Alert;
using Content.Shared.Hands;
using Content.Shared.Standing; using Content.Shared.Standing;
using Robust.Shared.GameObjects; 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 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) public override void ExitState(IEntity entity)
{ {
base.ExitState(entity); base.ExitState(entity);
EntitySystem.Get<SharedStandingStateSystem>().Standing(entity); EntitySystem.Get<StandingStateSystem>().Stand(entity);
} }
public override bool CanInteract() public override bool CanInteract()

View File

@@ -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 namespace Content.Shared.MobState.State
{ {
@@ -11,6 +13,18 @@ namespace Content.Shared.MobState.State
base.EnterState(entity); base.EnterState(entity);
var wake = entity.EnsureComponent<CollisionWakeComponent>(); var wake = entity.EnsureComponent<CollisionWakeComponent>();
wake.Enabled = true; 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) public override void ExitState(IEntity entity)
@@ -20,6 +34,14 @@ namespace Content.Shared.MobState.State
{ {
entity.RemoveComponent<CollisionWakeComponent>(); 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() public override bool CanInteract()

View File

@@ -1,4 +1,7 @@
#nullable enable using Content.Shared.Standing;
using Robust.Shared.GameObjects;
#nullable enable
namespace Content.Shared.MobState.State namespace Content.Shared.MobState.State
{ {
@@ -9,6 +12,17 @@ namespace Content.Shared.MobState.State
{ {
protected override DamageState DamageState => DamageState.Alive; 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() public override bool CanInteract()
{ {
return true; return true;

View File

@@ -100,6 +100,7 @@ namespace Content.Shared.NetIDs
public const uint LIGHT_REPLACER = 1090; public const uint LIGHT_REPLACER = 1090;
public const uint SINGULARITY_DISTORTION = 1091; public const uint SINGULARITY_DISTORTION = 1091;
public const uint GRAVITY = 1092; public const uint GRAVITY = 1092;
public const uint STANDING_STATE = 1093;
// Net IDs for integration tests. // Net IDs for integration tests.
public const uint PREDICTION_TEST = 10001; public const uint PREDICTION_TEST = 10001;

View File

@@ -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)
{
}
}
}

View 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;
}
}
}
}

View 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
{
}
}

View File

@@ -237,7 +237,7 @@
- type: Grammar - type: Grammar
attributes: attributes:
proper: true proper: true
- type: StandingState
- type: entity - type: entity
save: false save: false

View File

@@ -1,5 +1,5 @@
- type: soundCollection - type: soundCollection
id: bodyfall id: BodyFall
files: files:
- /Audio/Effects/bodyfall1.ogg - /Audio/Effects/bodyfall1.ogg
- /Audio/Effects/bodyfall2.ogg - /Audio/Effects/bodyfall2.ogg