Add prediction for standing states and mob states (#1937)

This commit is contained in:
DrSmugleaf
2020-08-29 13:20:37 +02:00
committed by GitHub
parent 4d23bbf4df
commit 1ecf8aad1a
38 changed files with 1001 additions and 626 deletions

View File

@@ -14,7 +14,7 @@ namespace Content.Client.GameObjects.Components.Body
{
[RegisterComponent]
[ComponentReference(typeof(IDamageableComponent))]
[ComponentReference(typeof(IBodyManagerComponent))]
[ComponentReference(typeof(ISharedBodyManagerComponent))]
public class BodyManagerComponent : SharedBodyManagerComponent, IClientDraggable
{
[Dependency] private readonly IEntityManager _entityManager = default!;

View File

@@ -0,0 +1,30 @@
using Content.Client.GameObjects.EntitySystems;
using Content.Shared.GameObjects.Components.Damage;
using Content.Shared.GameObjects.Components.Mobs;
using Content.Shared.GameObjects.Components.Mobs.State;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Client.GameObjects.Components.Mobs.State
{
public class CriticalState : SharedCriticalState
{
public override void EnterState(IEntity entity)
{
if (entity.TryGetComponent(out AppearanceComponent appearance))
{
appearance.SetData(DamageStateVisuals.State, DamageState.Critical);
}
EntitySystem.Get<StandingStateSystem>().Down(entity);
}
public override void ExitState(IEntity entity)
{
EntitySystem.Get<StandingStateSystem>().Standing(entity);
}
public override void UpdateState(IEntity entity) { }
}
}

View File

@@ -0,0 +1,41 @@
using Content.Client.GameObjects.EntitySystems;
using Content.Shared.GameObjects.Components.Damage;
using Content.Shared.GameObjects.Components.Mobs;
using Content.Shared.GameObjects.Components.Mobs.State;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects.Components;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Client.GameObjects.Components.Mobs.State
{
public class DeadState : SharedDeadState
{
public override void EnterState(IEntity entity)
{
if (entity.TryGetComponent(out AppearanceComponent appearance))
{
appearance.SetData(DamageStateVisuals.State, DamageState.Dead);
}
EntitySystem.Get<StandingStateSystem>().Down(entity);
if (entity.TryGetComponent(out CollidableComponent collidable))
{
collidable.CanCollide = false;
}
}
public override void ExitState(IEntity entity)
{
EntitySystem.Get<StandingStateSystem>().Standing(entity);
if (entity.TryGetComponent(out CollidableComponent collidable))
{
collidable.CanCollide = true;
}
}
public override void UpdateState(IEntity entity) { }
}
}

View File

@@ -0,0 +1,62 @@
#nullable enable
using System.Collections.Generic;
using Content.Shared.GameObjects.Components.Damage;
using Content.Shared.GameObjects.Components.Mobs.State;
using Robust.Shared.GameObjects;
namespace Content.Client.GameObjects.Components.Mobs.State
{
[RegisterComponent]
[ComponentReference(typeof(SharedMobStateManagerComponent))]
public class MobStateManagerComponent : SharedMobStateManagerComponent
{
private readonly Dictionary<DamageState, IMobState> _behavior = new Dictionary<DamageState, IMobState>
{
{DamageState.Alive, new NormalState()},
{DamageState.Critical, new CriticalState()},
{DamageState.Dead, new DeadState()}
};
private DamageState _currentDamageState;
protected override IReadOnlyDictionary<DamageState, IMobState> Behavior => _behavior;
public override DamageState CurrentDamageState
{
get => _currentDamageState;
protected set
{
if (_currentDamageState == value)
{
return;
}
if (_currentDamageState != DamageState.Invalid)
{
CurrentMobState.ExitState(Owner);
}
_currentDamageState = value;
CurrentMobState = Behavior[CurrentDamageState];
CurrentMobState.EnterState(Owner);
Dirty();
}
}
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
base.HandleComponentState(curState, nextState);
if (!(curState is MobStateManagerComponentState state))
{
return;
}
_currentDamageState = state.DamageState;
CurrentMobState.ExitState(Owner);
CurrentMobState = Behavior[CurrentDamageState];
CurrentMobState.EnterState(Owner);
}
}
}

View File

@@ -0,0 +1,25 @@
using Content.Shared.GameObjects.Components.Damage;
using Content.Shared.GameObjects.Components.Mobs;
using Content.Shared.GameObjects.Components.Mobs.State;
using Robust.Client.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Client.GameObjects.Components.Mobs.State
{
public class NormalState : SharedNormalState
{
public override void EnterState(IEntity entity)
{
if (entity.TryGetComponent(out AppearanceComponent appearance))
{
appearance.SetData(DamageStateVisuals.State, DamageState.Alive);
}
UpdateState(entity);
}
public override void ExitState(IEntity entity) { }
public override void UpdateState(IEntity entity) { }
}
}

View File

@@ -0,0 +1,50 @@
using Content.Shared.Audio;
using Content.Shared.GameObjects.Components.Rotation;
using Content.Shared.GameObjects.EntitySystems;
using Robust.Client.GameObjects;
using Robust.Client.GameObjects.EntitySystems;
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Client.GameObjects.EntitySystems
{
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");
Get<AudioSystem>().Play(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;
}
}
}

View File

@@ -156,7 +156,6 @@
"Barotrauma",
"GasSprayer",
"GasVapor",
"MobStateManager",
"Metabolism",
"AiFactionTag",
"PressureProtection",

View File

@@ -21,7 +21,7 @@ namespace Content.Server.AI.WorldState.States.Mobs
return result;
}
foreach (var entity in Visibility.GetEntitiesInRange(Owner.Transform.GridPosition, typeof(IBodyManagerComponent), controller.VisionRadius))
foreach (var entity in Visibility.GetEntitiesInRange(Owner.Transform.GridPosition, typeof(ISharedBodyManagerComponent), controller.VisionRadius))
{
if (entity == Owner) continue;
result.Add(entity);

View File

@@ -20,6 +20,7 @@ using Content.Shared.GameObjects.Components.Damage;
using Content.Shared.GameObjects.Components.Movement;
using Robust.Server.Interfaces.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Reflection;
using Robust.Shared.IoC;
@@ -38,7 +39,7 @@ namespace Content.Server.GameObjects.Components.Body
/// </summary>
[RegisterComponent]
[ComponentReference(typeof(IDamageableComponent))]
[ComponentReference(typeof(IBodyManagerComponent))]
[ComponentReference(typeof(ISharedBodyManagerComponent))]
public class BodyManagerComponent : SharedBodyManagerComponent, IBodyPartContainer, IRelayMoveInput
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
@@ -289,14 +290,14 @@ namespace Content.Server.GameObjects.Components.Body
if (speedSum <= 0.001f || _activeLegs.Count <= 0)
{
// Case: no way of moving. Fall down.
StandingStateHelper.Down(Owner);
EntitySystem.Get<StandingStateSystem>().Down(Owner);
playerMover.BaseWalkSpeed = 0.8f;
playerMover.BaseSprintSpeed = 2.0f;
}
else
{
// Case: have at least one leg. Set move speed.
StandingStateHelper.Standing(Owner);
EntitySystem.Get<StandingStateSystem>().Standing(Owner);
// Extra legs stack diminishingly.
// Final speed = speed sum/(leg count-log4(leg count))

View File

@@ -3,9 +3,10 @@ using System;
using System.Diagnostics.CodeAnalysis;
using Content.Server.GameObjects.Components.GUI;
using Content.Server.GameObjects.Components.Mobs;
using Content.Server.GameObjects.Components.Mobs.State;
using Content.Server.GameObjects.Components.Strap;
using Content.Server.GameObjects.EntitySystems;
using Content.Server.Interfaces;
using Content.Server.Mobs;
using Content.Server.Utility;
using Content.Shared.GameObjects.Components.Buckle;
using Content.Shared.GameObjects.Components.Mobs;
@@ -136,11 +137,11 @@ namespace Content.Server.GameObjects.Components.Buckle
ownTransform.WorldRotation = strapTransform.WorldRotation;
break;
case StrapPosition.Stand:
StandingStateHelper.Standing(Owner);
EntitySystem.Get<StandingStateSystem>().Standing(Owner);
ownTransform.WorldRotation = strapTransform.WorldRotation;
break;
case StrapPosition.Down:
StandingStateHelper.Down(Owner, force: true);
EntitySystem.Get<StandingStateSystem>().Down(Owner, force: true);
ownTransform.WorldRotation = Angle.South;
break;
}
@@ -364,11 +365,11 @@ namespace Content.Server.GameObjects.Components.Buckle
if (Owner.TryGetComponent(out StunnableComponent? stunnable) && stunnable.KnockedDown)
{
StandingStateHelper.Down(Owner);
EntitySystem.Get<StandingStateSystem>().Down(Owner);
}
else
{
StandingStateHelper.Standing(Owner);
EntitySystem.Get<StandingStateSystem>().Standing(Owner);
}
if (Owner.TryGetComponent(out MobStateManagerComponent? stateManager))

View File

@@ -63,7 +63,7 @@ namespace Content.Server.GameObjects.Components.Disposal
}
return entity.HasComponent<ItemComponent>() ||
entity.HasComponent<IBodyManagerComponent>();
entity.HasComponent<ISharedBodyManagerComponent>();
}
public bool TryInsert(IEntity entity)

View File

@@ -143,7 +143,7 @@ namespace Content.Server.GameObjects.Components.Disposal
}
if (!entity.HasComponent<ItemComponent>() &&
!entity.HasComponent<IBodyManagerComponent>())
!entity.HasComponent<ISharedBodyManagerComponent>())
{
return false;
}

View File

@@ -100,7 +100,7 @@ namespace Content.Server.GameObjects.Components.Doors
// Disabled because it makes it suck hard to walk through double doors.
if (entity.HasComponent<IBodyManagerComponent>())
if (entity.HasComponent<ISharedBodyManagerComponent>())
{
if (!entity.TryGetComponent<IMoverComponent>(out var mover)) return;

View File

@@ -2,6 +2,7 @@
using System;
using System.Linq;
using Content.Server.GameObjects.Components.Mobs;
using Content.Server.GameObjects.EntitySystems;
using Content.Server.Interfaces;
using Content.Server.Mobs;
using Content.Server.Utility;
@@ -15,6 +16,7 @@ using Robust.Server.Interfaces.Player;
using Robust.Server.Player;
using Robust.Shared.Enums;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.Network;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC;
@@ -323,7 +325,7 @@ namespace Content.Server.GameObjects.Components.Instruments
}
else
{
StandingStateHelper.DropAllItemsInHands(mob, false);
EntitySystem.Get<StandingStateSystem>().DropAllItemsInHands(mob, false);
}
InstrumentPlayer = null;

View File

@@ -32,7 +32,7 @@ namespace Content.Server.GameObjects.Components.Medical
return;
}
if (!eventArgs.Target.TryGetComponent(out IBodyManagerComponent body))
if (!eventArgs.Target.TryGetComponent(out ISharedBodyManagerComponent body))
{
return;
}

View File

@@ -1,513 +0,0 @@
using System.Collections.Generic;
using Content.Server.GameObjects.Components.Body;
using Content.Server.GameObjects.Components.Damage;
using Content.Server.Mobs;
using Content.Shared.GameObjects.Components.Damage;
using Content.Shared.GameObjects.Components.Mobs;
using Content.Shared.GameObjects.EntitySystems;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components;
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Server.GameObjects.Components.Mobs
{
/// <summary>
/// When attacked to an <see cref="IDamageableComponent"/>, this component will handle critical and death behaviors
/// for mobs.
/// Additionally, it handles sending effects to clients (such as blur effect for unconsciousness) and managing the
/// health HUD.
/// </summary>
[RegisterComponent]
internal class MobStateManagerComponent : Component, IOnHealthChangedBehavior, IActionBlocker
{
private readonly Dictionary<DamageState, IMobState> _behavior = new Dictionary<DamageState, IMobState>
{
{DamageState.Alive, new NormalState()},
{DamageState.Critical, new CriticalState()},
{DamageState.Dead, new DeadState()}
};
public override string Name => "MobStateManager";
private DamageState _currentDamageState;
public IMobState CurrentMobState { get; private set; } = new NormalState();
bool IActionBlocker.CanInteract()
{
return CurrentMobState.CanInteract();
}
bool IActionBlocker.CanMove()
{
return CurrentMobState.CanMove();
}
bool IActionBlocker.CanUse()
{
return CurrentMobState.CanUse();
}
bool IActionBlocker.CanThrow()
{
return CurrentMobState.CanThrow();
}
bool IActionBlocker.CanSpeak()
{
return CurrentMobState.CanSpeak();
}
bool IActionBlocker.CanDrop()
{
return CurrentMobState.CanDrop();
}
bool IActionBlocker.CanPickup()
{
return CurrentMobState.CanPickup();
}
bool IActionBlocker.CanEmote()
{
return CurrentMobState.CanEmote();
}
bool IActionBlocker.CanAttack()
{
return CurrentMobState.CanAttack();
}
bool IActionBlocker.CanEquip()
{
return CurrentMobState.CanEquip();
}
bool IActionBlocker.CanUnequip()
{
return CurrentMobState.CanUnequip();
}
bool IActionBlocker.CanChangeDirection()
{
return CurrentMobState.CanChangeDirection();
}
public void OnHealthChanged(HealthChangedEventArgs e)
{
if (e.Damageable.CurrentDamageState != _currentDamageState)
{
_currentDamageState = e.Damageable.CurrentDamageState;
CurrentMobState.ExitState(Owner);
CurrentMobState = _behavior[_currentDamageState];
CurrentMobState.EnterState(Owner);
}
CurrentMobState.UpdateState(Owner);
}
public override void Initialize()
{
base.Initialize();
_currentDamageState = DamageState.Alive;
CurrentMobState = _behavior[_currentDamageState];
CurrentMobState.EnterState(Owner);
CurrentMobState.UpdateState(Owner);
}
public override void OnRemove()
{
// TODO: Might want to add an OnRemove() to IMobState since those are where these components are being used
base.OnRemove();
if (Owner.TryGetComponent(out ServerStatusEffectsComponent status))
{
status.RemoveStatusEffect(StatusEffect.Health);
}
if (Owner.TryGetComponent(out ServerOverlayEffectsComponent overlay))
{
overlay.ClearOverlays();
}
}
}
/// <summary>
/// Defines the blocking effects of an associated <see cref="DamageState"/>
/// (i.e. Normal, Critical, Dead) and what effects to apply upon entering or
/// exiting the state.
/// </summary>
public interface IMobState : IActionBlocker
{
/// <summary>
/// Called when this state is entered.
/// </summary>
void EnterState(IEntity entity);
/// <summary>
/// Called when this state is left for a different state.
/// </summary>
void ExitState(IEntity entity);
/// <summary>
/// Called when this state is updated.
/// </summary>
void UpdateState(IEntity entity);
}
/// <summary>
/// The standard state an entity is in; no negative effects.
/// </summary>
public struct NormalState : IMobState
{
public void EnterState(IEntity entity)
{
if (entity.TryGetComponent(out AppearanceComponent appearance))
{
appearance.SetData(DamageStateVisuals.State, DamageState.Alive);
}
UpdateState(entity);
}
public void ExitState(IEntity entity) { }
public void UpdateState(IEntity entity)
{
if (!entity.TryGetComponent(out ServerStatusEffectsComponent status))
{
return;
}
if (!entity.TryGetComponent(out IDamageableComponent damageable))
{
status.ChangeStatusEffectIcon(StatusEffect.Health,
"/Textures/Interface/StatusEffects/Human/human0.png");
return;
}
// TODO
switch (damageable)
{
case RuinableComponent ruinable:
{
if (ruinable.DeadThreshold == null)
{
break;
}
var modifier = (int) (ruinable.TotalDamage / (ruinable.DeadThreshold / 7f));
status.ChangeStatusEffectIcon(StatusEffect.Health,
"/Textures/Interface/StatusEffects/Human/human" + modifier + ".png");
break;
}
case BodyManagerComponent body:
{
if (body.CriticalThreshold == null)
{
return;
}
var modifier = (int) (body.TotalDamage / (body.CriticalThreshold / 7f));
status.ChangeStatusEffectIcon(StatusEffect.Health,
"/Textures/Interface/StatusEffects/Human/human" + modifier + ".png");
break;
}
default:
{
status.ChangeStatusEffectIcon(StatusEffect.Health,
"/Textures/Interface/StatusEffects/Human/human0.png");
break;
}
}
}
bool IActionBlocker.CanInteract()
{
return true;
}
bool IActionBlocker.CanMove()
{
return true;
}
bool IActionBlocker.CanUse()
{
return true;
}
bool IActionBlocker.CanThrow()
{
return true;
}
bool IActionBlocker.CanSpeak()
{
return true;
}
bool IActionBlocker.CanDrop()
{
return true;
}
bool IActionBlocker.CanPickup()
{
return true;
}
bool IActionBlocker.CanEmote()
{
return true;
}
bool IActionBlocker.CanAttack()
{
return true;
}
bool IActionBlocker.CanEquip()
{
return true;
}
bool IActionBlocker.CanUnequip()
{
return true;
}
bool IActionBlocker.CanChangeDirection()
{
return true;
}
}
/// <summary>
/// A state in which an entity is disabled from acting due to sufficient damage (considered unconscious).
/// </summary>
public struct CriticalState : IMobState
{
public void EnterState(IEntity entity)
{
if (entity.TryGetComponent(out AppearanceComponent appearance))
{
appearance.SetData(DamageStateVisuals.State, DamageState.Critical);
}
if (entity.TryGetComponent(out ServerStatusEffectsComponent status))
{
status.ChangeStatusEffectIcon(StatusEffect.Health,
"/Textures/Interface/StatusEffects/Human/humancrit-0.png"); //Todo: combine humancrit-0 and humancrit-1 into a gif and display it
}
if (entity.TryGetComponent(out ServerOverlayEffectsComponent overlay))
{
overlay.AddOverlay(SharedOverlayID.GradientCircleMaskOverlay);
}
if (entity.TryGetComponent(out StunnableComponent stun))
{
stun.CancelAll();
}
StandingStateHelper.Down(entity);
}
public void ExitState(IEntity entity)
{
StandingStateHelper.Standing(entity);
if (entity.TryGetComponent(out ServerOverlayEffectsComponent overlay))
{
overlay.ClearOverlays();
}
}
public void UpdateState(IEntity entity)
{
}
bool IActionBlocker.CanInteract()
{
return false;
}
bool IActionBlocker.CanMove()
{
return false;
}
bool IActionBlocker.CanUse()
{
return false;
}
bool IActionBlocker.CanThrow()
{
return false;
}
bool IActionBlocker.CanSpeak()
{
return false;
}
bool IActionBlocker.CanDrop()
{
return false;
}
bool IActionBlocker.CanPickup()
{
return false;
}
bool IActionBlocker.CanEmote()
{
return false;
}
bool IActionBlocker.CanAttack()
{
return false;
}
bool IActionBlocker.CanEquip()
{
return false;
}
bool IActionBlocker.CanUnequip()
{
return false;
}
bool IActionBlocker.CanChangeDirection()
{
return false;
}
}
/// <summary>
/// The state representing a dead entity; allows for ghosting.
/// </summary>
public struct DeadState : IMobState
{
public void EnterState(IEntity entity)
{
if (entity.TryGetComponent(out AppearanceComponent appearance))
{
appearance.SetData(DamageStateVisuals.State, DamageState.Dead);
}
if (entity.TryGetComponent(out ServerStatusEffectsComponent status))
{
status.ChangeStatusEffectIcon(StatusEffect.Health,
"/Textures/Interface/StatusEffects/Human/humandead.png");
}
if (entity.TryGetComponent(out ServerOverlayEffectsComponent overlayComponent))
{
overlayComponent.AddOverlay(SharedOverlayID.CircleMaskOverlay);
}
if (entity.TryGetComponent(out StunnableComponent stun))
{
stun.CancelAll();
}
StandingStateHelper.Down(entity);
if (entity.TryGetComponent(out CollidableComponent collidable))
{
collidable.CanCollide = false;
}
}
public void ExitState(IEntity entity)
{
StandingStateHelper.Standing(entity);
if (entity.TryGetComponent(out CollidableComponent collidable))
{
collidable.CanCollide = true;
}
if (entity.TryGetComponent(out ServerOverlayEffectsComponent overlay))
{
overlay.ClearOverlays();
}
}
public void UpdateState(IEntity entity)
{
}
bool IActionBlocker.CanInteract()
{
return false;
}
bool IActionBlocker.CanMove()
{
return false;
}
bool IActionBlocker.CanUse()
{
return false;
}
bool IActionBlocker.CanThrow()
{
return false;
}
bool IActionBlocker.CanSpeak()
{
return false;
}
bool IActionBlocker.CanDrop()
{
return false;
}
bool IActionBlocker.CanPickup()
{
return false;
}
bool IActionBlocker.CanEmote()
{
return false;
}
bool IActionBlocker.CanAttack()
{
return false;
}
bool IActionBlocker.CanEquip()
{
return false;
}
bool IActionBlocker.CanUnequip()
{
return false;
}
bool IActionBlocker.CanChangeDirection()
{
return false;
}
}
}

View File

@@ -0,0 +1,51 @@
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.GameObjects.Components.Damage;
using Content.Shared.GameObjects.Components.Mobs;
using Content.Shared.GameObjects.Components.Mobs.State;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Server.GameObjects.Components.Mobs.State
{
public class CriticalState : SharedCriticalState
{
public override void EnterState(IEntity entity)
{
if (entity.TryGetComponent(out AppearanceComponent appearance))
{
appearance.SetData(DamageStateVisuals.State, DamageState.Critical);
}
if (entity.TryGetComponent(out ServerStatusEffectsComponent status))
{
status.ChangeStatusEffectIcon(StatusEffect.Health,
"/Textures/Interface/StatusEffects/Human/humancrit-0.png"); //Todo: combine humancrit-0 and humancrit-1 into a gif and display it
}
if (entity.TryGetComponent(out ServerOverlayEffectsComponent overlay))
{
overlay.AddOverlay(SharedOverlayID.GradientCircleMaskOverlay);
}
if (entity.TryGetComponent(out StunnableComponent stun))
{
stun.CancelAll();
}
EntitySystem.Get<StandingStateSystem>().Down(entity);
}
public override void ExitState(IEntity entity)
{
EntitySystem.Get<StandingStateSystem>().Standing(entity);
if (entity.TryGetComponent(out ServerOverlayEffectsComponent overlay))
{
overlay.ClearOverlays();
}
}
public override void UpdateState(IEntity entity) { }
}
}

View File

@@ -0,0 +1,62 @@
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.GameObjects.Components.Damage;
using Content.Shared.GameObjects.Components.Mobs;
using Content.Shared.GameObjects.Components.Mobs.State;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects.Components;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Server.GameObjects.Components.Mobs.State
{
public class DeadState : SharedDeadState
{
public override void EnterState(IEntity entity)
{
if (entity.TryGetComponent(out AppearanceComponent appearance))
{
appearance.SetData(DamageStateVisuals.State, DamageState.Dead);
}
if (entity.TryGetComponent(out ServerStatusEffectsComponent status))
{
status.ChangeStatusEffectIcon(StatusEffect.Health,
"/Textures/Interface/StatusEffects/Human/humandead.png");
}
if (entity.TryGetComponent(out ServerOverlayEffectsComponent overlayComponent))
{
overlayComponent.AddOverlay(SharedOverlayID.CircleMaskOverlay);
}
if (entity.TryGetComponent(out StunnableComponent stun))
{
stun.CancelAll();
}
EntitySystem.Get<StandingStateSystem>().Down(entity);
if (entity.TryGetComponent(out CollidableComponent collidable))
{
collidable.CanCollide = false;
}
}
public override void ExitState(IEntity entity)
{
EntitySystem.Get<StandingStateSystem>().Standing(entity);
if (entity.TryGetComponent(out CollidableComponent collidable))
{
collidable.CanCollide = true;
}
if (entity.TryGetComponent(out ServerOverlayEffectsComponent overlay))
{
overlay.ClearOverlays();
}
}
public override void UpdateState(IEntity entity) { }
}
}

View File

@@ -0,0 +1,70 @@
using System.Collections.Generic;
using Content.Shared.GameObjects.Components.Damage;
using Content.Shared.GameObjects.Components.Mobs;
using Content.Shared.GameObjects.Components.Mobs.State;
using Robust.Shared.GameObjects;
namespace Content.Server.GameObjects.Components.Mobs.State
{
[RegisterComponent]
[ComponentReference(typeof(SharedMobStateManagerComponent))]
public class MobStateManagerComponent : SharedMobStateManagerComponent
{
private readonly Dictionary<DamageState, IMobState> _behavior = new Dictionary<DamageState, IMobState>
{
{DamageState.Alive, new NormalState()},
{DamageState.Critical, new CriticalState()},
{DamageState.Dead, new DeadState()}
};
private DamageState _currentDamageState;
protected override IReadOnlyDictionary<DamageState, IMobState> Behavior => _behavior;
public override IMobState CurrentMobState { get; protected set; }
public override DamageState CurrentDamageState
{
get => _currentDamageState;
protected set
{
if (_currentDamageState == value)
{
return;
}
if (_currentDamageState != DamageState.Invalid)
{
CurrentMobState.ExitState(Owner);
}
_currentDamageState = value;
CurrentMobState = Behavior[CurrentDamageState];
CurrentMobState.EnterState(Owner);
Dirty();
}
}
public override void OnRemove()
{
// TODO: Might want to add an OnRemove() to IMobState since those are where these components are being used
base.OnRemove();
if (Owner.TryGetComponent(out ServerStatusEffectsComponent status))
{
status.RemoveStatusEffect(StatusEffect.Health);
}
if (Owner.TryGetComponent(out ServerOverlayEffectsComponent overlay))
{
overlay.ClearOverlays();
}
}
public override ComponentState GetComponentState()
{
return new MobStateManagerComponentState(CurrentDamageState);
}
}
}

View File

@@ -0,0 +1,79 @@
using Content.Server.GameObjects.Components.Body;
using Content.Server.GameObjects.Components.Damage;
using Content.Shared.GameObjects.Components.Damage;
using Content.Shared.GameObjects.Components.Mobs;
using Content.Shared.GameObjects.Components.Mobs.State;
using Robust.Server.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Server.GameObjects.Components.Mobs.State
{
public class NormalState : SharedNormalState
{
public override void EnterState(IEntity entity)
{
if (entity.TryGetComponent(out AppearanceComponent appearance))
{
appearance.SetData(DamageStateVisuals.State, DamageState.Alive);
}
UpdateState(entity);
}
public override void ExitState(IEntity entity) { }
public override void UpdateState(IEntity entity)
{
if (!entity.TryGetComponent(out ServerStatusEffectsComponent status))
{
return;
}
if (!entity.TryGetComponent(out IDamageableComponent damageable))
{
status.ChangeStatusEffectIcon(StatusEffect.Health,
"/Textures/Interface/StatusEffects/Human/human0.png");
return;
}
// TODO
switch (damageable)
{
case RuinableComponent ruinable:
{
if (ruinable.DeadThreshold == null)
{
break;
}
var modifier = (int) (ruinable.TotalDamage / (ruinable.DeadThreshold / 7f));
status.ChangeStatusEffectIcon(StatusEffect.Health,
"/Textures/Interface/StatusEffects/Human/human" + modifier + ".png");
break;
}
case BodyManagerComponent body:
{
if (body.CriticalThreshold == null)
{
return;
}
var modifier = (int) (body.TotalDamage / (body.CriticalThreshold / 7f));
status.ChangeStatusEffectIcon(StatusEffect.Health,
"/Textures/Interface/StatusEffects/Human/human" + modifier + ".png");
break;
}
default:
{
status.ChangeStatusEffectIcon(StatusEffect.Health,
"/Textures/Interface/StatusEffects/Human/human0.png");
break;
}
}
}
}
}

View File

@@ -1,7 +1,8 @@
using Content.Server.Mobs;
using Content.Server.GameObjects.EntitySystems;
using Content.Shared.GameObjects.Components.Mobs;
using Content.Shared.GameObjects.Components.Movement;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC;
using Robust.Shared.Timers;
@@ -16,7 +17,7 @@ namespace Content.Server.GameObjects.Components.Mobs
protected override void OnKnockdown()
{
StandingStateHelper.Down(Owner);
EntitySystem.Get<StandingStateSystem>().Down(Owner);
}
public void CancelAll()
@@ -33,7 +34,7 @@ namespace Content.Server.GameObjects.Components.Mobs
if (KnockedDown)
{
StandingStateHelper.Standing(Owner);
EntitySystem.Get<StandingStateSystem>().Standing(Owner);
}
KnockdownTimer = 0f;
@@ -58,7 +59,7 @@ namespace Content.Server.GameObjects.Components.Mobs
if (KnockdownTimer <= 0f)
{
StandingStateHelper.Standing(Owner);
EntitySystem.Get<StandingStateSystem>().Standing(Owner);
KnockdownTimer = 0f;
Dirty();

View File

@@ -69,7 +69,7 @@ namespace Content.Server.GameObjects.Components.Recycling
private bool CanGib(IEntity entity)
{
return entity.HasComponent<IBodyManagerComponent>() && !_safe && Powered;
return entity.HasComponent<ISharedBodyManagerComponent>() && !_safe && Powered;
}
private bool CanRecycle(IEntity entity, [MaybeNullWhen(false)] out ConstructionPrototype prototype)

View File

@@ -0,0 +1,65 @@
using Content.Server.Interfaces.GameObjects.Components.Items;
using Content.Shared.Audio;
using Content.Shared.GameObjects.Components.Rotation;
using Content.Shared.GameObjects.EntitySystems;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Server.GameObjects.EntitySystems;
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Server.GameObjects.EntitySystems
{
[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");
Get<AudioSystem>().PlayFromEntity(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);
}
}
}
}

View File

@@ -34,7 +34,7 @@ namespace Content.Server.GameObjects.EntitySystems.StationEvents
public override void Initialize()
{
base.Initialize();
_speciesQuery = new TypeEntityQuery(typeof(IBodyManagerComponent));
_speciesQuery = new TypeEntityQuery(typeof(ISharedBodyManagerComponent));
}
public override void Update(float frameTime)

View File

@@ -1,84 +0,0 @@
using Content.Server.Interfaces.GameObjects.Components.Items;
using Content.Shared.Audio;
using Content.Shared.GameObjects.Components.Rotation;
using Content.Shared.GameObjects.EntitySystems;
using Robust.Server.GameObjects;
using Robust.Server.GameObjects.EntitySystems;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
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>
/// <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 static 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;
}
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)
{
IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<AudioSystem>()
.PlayFromEntity(AudioHelpers.GetRandomFileFromSoundCollection("bodyfall"), entity, AudioHelpers.WithVariation(0.25f));
}
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<RotationState>(RotationVisuals.RotationState, out var oldState);
var newState = RotationState.Vertical;
if (newState == oldState)
return false;
appearance.SetData(RotationVisuals.RotationState, newState);
return true;
}
public static void DropAllItemsInHands(IEntity entity, bool doMobChecks = true)
{
if (!entity.TryGetComponent(out IHandsComponent hands)) return;
foreach (var heldItem in hands.GetAllHeldItems())
{
hands.Drop(heldItem.Owner, doMobChecks);
}
}
}
}

View File

@@ -1,9 +1,8 @@
using Content.Shared.GameObjects.Components.Damage;
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Shared.GameObjects.Components.Body
{
public interface IBodyManagerComponent : IDamageableComponent
public interface ISharedBodyManagerComponent : IDamageableComponent
{
}
}

View File

@@ -5,7 +5,7 @@ using Robust.Shared.Serialization;
namespace Content.Shared.GameObjects.Components.Body
{
public abstract class SharedBodyManagerComponent : DamageableComponent, IBodyManagerComponent
public abstract class SharedBodyManagerComponent : DamageableComponent, ISharedBodyManagerComponent
{
public override string Name => "BodyManager";

View File

@@ -16,6 +16,7 @@ namespace Content.Shared.GameObjects.Components.Damage
[Serializable, NetSerializable]
public enum DamageState
{
Invalid = 0,
Alive,
Critical,
Dead

View File

@@ -143,8 +143,8 @@ namespace Content.Shared.GameObjects.Components.Damage
_ => throw new ArgumentOutOfRangeException()
};
ChangeDamage(DamageType.Piercing, damage, false, null);
ChangeDamage(DamageType.Heat, damage, false, null);
ChangeDamage(DamageType.Piercing, damage, false);
ChangeDamage(DamageType.Heat, damage, false);
}
}

View File

@@ -0,0 +1,29 @@
using Content.Shared.GameObjects.Components.Damage;
using Content.Shared.GameObjects.EntitySystems;
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Shared.GameObjects.Components.Mobs.State
{
/// <summary>
/// Defines the blocking effects of an associated <see cref="DamageState"/>
/// (i.e. Normal, Critical, Dead) and what effects to apply upon entering or
/// exiting the state.
/// </summary>
public interface IMobState : IActionBlocker
{
/// <summary>
/// Called when this state is entered.
/// </summary>
void EnterState(IEntity entity);
/// <summary>
/// Called when this state is left for a different state.
/// </summary>
void ExitState(IEntity entity);
/// <summary>
/// Called when this state is updated.
/// </summary>
void UpdateState(IEntity entity);
}
}

View File

@@ -0,0 +1,76 @@
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Shared.GameObjects.Components.Mobs.State
{
/// <summary>
/// A state in which an entity is disabled from acting due to sufficient damage (considered unconscious).
/// </summary>
public abstract class SharedCriticalState : IMobState
{
public abstract void EnterState(IEntity entity);
public abstract void ExitState(IEntity entity);
public abstract void UpdateState(IEntity entity);
public bool CanInteract()
{
return false;
}
public bool CanMove()
{
return false;
}
public bool CanUse()
{
return false;
}
public bool CanThrow()
{
return false;
}
public bool CanSpeak()
{
return false;
}
public bool CanDrop()
{
return false;
}
public bool CanPickup()
{
return false;
}
public bool CanEmote()
{
return false;
}
public bool CanAttack()
{
return false;
}
public bool CanEquip()
{
return false;
}
public bool CanUnequip()
{
return false;
}
public bool CanChangeDirection()
{
return false;
}
}
}

View File

@@ -0,0 +1,73 @@
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Shared.GameObjects.Components.Mobs.State
{
public abstract class SharedDeadState : IMobState
{
public abstract void EnterState(IEntity entity);
public abstract void ExitState(IEntity entity);
public abstract void UpdateState(IEntity entity);
public bool CanInteract()
{
return false;
}
public bool CanMove()
{
return false;
}
public bool CanUse()
{
return false;
}
public bool CanThrow()
{
return false;
}
public bool CanSpeak()
{
return false;
}
public bool CanDrop()
{
return false;
}
public bool CanPickup()
{
return false;
}
public bool CanEmote()
{
return false;
}
public bool CanAttack()
{
return false;
}
public bool CanEquip()
{
return false;
}
public bool CanUnequip()
{
return false;
}
public bool CanChangeDirection()
{
return false;
}
}
}

View File

@@ -0,0 +1,128 @@
using System;
using System.Collections.Generic;
using Content.Shared.GameObjects.Components.Damage;
using Content.Shared.GameObjects.EntitySystems;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
namespace Content.Shared.GameObjects.Components.Mobs.State
{
/// <summary>
/// When attacked to an <see cref="IDamageableComponent"/>, this component will
/// handle critical and death behaviors for mobs.
/// Additionally, it handles sending effects to clients
/// (such as blur effect for unconsciousness) and managing the health HUD.
/// </summary>
public abstract class SharedMobStateManagerComponent : Component, IOnHealthChangedBehavior, IActionBlocker
{
public override string Name => "MobStateManager";
public override uint? NetID => ContentNetIDs.MOB_STATE_MANAGER;
protected abstract IReadOnlyDictionary<DamageState, IMobState> Behavior { get; }
public virtual IMobState CurrentMobState { get; protected set; }
public virtual DamageState CurrentDamageState { get; protected set; }
public override void OnAdd()
{
base.OnAdd();
CurrentDamageState = DamageState.Alive;
}
public override void Initialize()
{
base.Initialize();
CurrentMobState = Behavior[CurrentDamageState];
CurrentMobState.EnterState(Owner);
CurrentMobState.UpdateState(Owner);
}
bool IActionBlocker.CanInteract()
{
return CurrentMobState.CanInteract();
}
bool IActionBlocker.CanMove()
{
return CurrentMobState.CanMove();
}
bool IActionBlocker.CanUse()
{
return CurrentMobState.CanUse();
}
bool IActionBlocker.CanThrow()
{
return CurrentMobState.CanThrow();
}
bool IActionBlocker.CanSpeak()
{
return CurrentMobState.CanSpeak();
}
bool IActionBlocker.CanDrop()
{
return CurrentMobState.CanDrop();
}
bool IActionBlocker.CanPickup()
{
return CurrentMobState.CanPickup();
}
bool IActionBlocker.CanEmote()
{
return CurrentMobState.CanEmote();
}
bool IActionBlocker.CanAttack()
{
return CurrentMobState.CanAttack();
}
bool IActionBlocker.CanEquip()
{
return CurrentMobState.CanEquip();
}
bool IActionBlocker.CanUnequip()
{
return CurrentMobState.CanUnequip();
}
bool IActionBlocker.CanChangeDirection()
{
return CurrentMobState.CanChangeDirection();
}
public void OnHealthChanged(HealthChangedEventArgs e)
{
if (e.Damageable.CurrentDamageState != CurrentDamageState)
{
CurrentDamageState = e.Damageable.CurrentDamageState;
CurrentMobState.ExitState(Owner);
CurrentMobState = Behavior[CurrentDamageState];
CurrentMobState.EnterState(Owner);
}
CurrentMobState.UpdateState(Owner);
}
}
[Serializable, NetSerializable]
public class MobStateManagerComponentState : ComponentState
{
public readonly DamageState DamageState;
public MobStateManagerComponentState(DamageState damageState) : base(ContentNetIDs.MOB_STATE_MANAGER)
{
DamageState = damageState;
}
}
}

View File

@@ -0,0 +1,76 @@
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Shared.GameObjects.Components.Mobs.State
{
/// <summary>
/// The standard state an entity is in; no negative effects.
/// </summary>
public abstract class SharedNormalState : IMobState
{
public abstract void EnterState(IEntity entity);
public abstract void ExitState(IEntity entity);
public abstract void UpdateState(IEntity entity);
public bool CanInteract()
{
return true;
}
public bool CanMove()
{
return true;
}
public bool CanUse()
{
return true;
}
public bool CanThrow()
{
return true;
}
public bool CanSpeak()
{
return true;
}
public bool CanDrop()
{
return true;
}
public bool CanPickup()
{
return true;
}
public bool CanEmote()
{
return true;
}
public bool CanAttack()
{
return true;
}
public bool CanEquip()
{
return true;
}
public bool CanUnequip()
{
return true;
}
public bool CanChangeDirection()
{
return true;
}
}
}

View File

@@ -270,7 +270,7 @@ namespace Content.Shared.GameObjects.Components.Movement
bool ICollideSpecial.PreventCollide(IPhysBody collidedWith)
{
// Don't collide with other mobs
return collidedWith.Entity.HasComponent<IBodyManagerComponent>();
return collidedWith.Entity.HasComponent<ISharedBodyManagerComponent>();
}
[Serializable, NetSerializable]

View File

@@ -1,5 +1,4 @@
using System;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
namespace Content.Shared.GameObjects.Components.Rotation

View File

@@ -72,6 +72,8 @@
public const uint HANDCUFFS = 1066;
public const uint BATTERY_BARREL = 1067;
public const uint SUSPICION_ROLE = 1068;
public const uint ROTATION = 1069;
public const uint MOB_STATE_MANAGER = 1070;
// Net IDs for integration tests.
public const uint PREDICTION_TEST = 10001;

View File

@@ -0,0 +1,50 @@
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Shared.GameObjects.EntitySystems
{
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)
{
}
}
}