Damage masks (#9402)

This commit is contained in:
metalgearsloth
2022-07-06 17:58:14 +10:00
committed by GitHub
parent 0bbdd0c1a3
commit a2a5df1990
55 changed files with 1160 additions and 930 deletions

View File

@@ -2,6 +2,7 @@ using System.Linq;
using Content.Shared.Damage.Prototypes;
using Content.Shared.FixedPoint;
using Content.Shared.Inventory;
using Content.Shared.MobState;
using Content.Shared.MobState.Components;
using Content.Shared.Radiation.Events;
using Robust.Shared.GameStates;
@@ -256,7 +257,7 @@ namespace Content.Shared.Damage
int ent1DeadState = 0;
foreach (var state in oldstate._highestToLowestStates)
{
if (state.Value.IsDead())
if (state.Value == DamageState.Dead)
{
ent1DeadState = state.Key;
}
@@ -265,7 +266,7 @@ namespace Content.Shared.Damage
int ent2DeadState = 0;
foreach (var state in newstate._highestToLowestStates)
{
if (state.Value.IsDead())
if (state.Value == DamageState.Dead)
{
ent2DeadState = state.Key;
}

View File

@@ -1,11 +1,8 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Shared.Alert;
using Content.Shared.Damage;
using Content.Shared.FixedPoint;
using Content.Shared.MobState.State;
using Content.Shared.MobState.EntitySystems;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
namespace Content.Shared.MobState.Components
{
@@ -19,8 +16,6 @@ namespace Content.Shared.MobState.Components
[NetworkedComponent]
public sealed class MobStateComponent : Component
{
[Dependency] private readonly IEntityManager _entMan = default!;
/// <summary>
/// States that this <see cref="MobStateComponent"/> mapped to
/// the amount of damage at which they are triggered.
@@ -30,294 +25,43 @@ namespace Content.Shared.MobState.Components
/// </summary>
[ViewVariables]
[DataField("thresholds")]
private readonly SortedDictionary<int, IMobState> _lowestToHighestStates = new();
public readonly SortedDictionary<int, DamageState> _lowestToHighestStates = new();
// TODO Remove Nullability?
[ViewVariables]
public IMobState? CurrentState { get; private set; }
public DamageState? CurrentState { get; set; }
[ViewVariables]
public FixedPoint2? CurrentThreshold { get; private set; }
public FixedPoint2? CurrentThreshold { get; set; }
public IEnumerable<KeyValuePair<int, IMobState>> _highestToLowestStates => _lowestToHighestStates.Reverse();
protected override void Startup()
{
base.Startup();
if (CurrentState != null && CurrentThreshold != null)
{
// Initialize with given states
SetMobState(null, (CurrentState, CurrentThreshold.Value));
}
else
{
// Initialize with some amount of damage, defaulting to 0.
UpdateState(_entMan.GetComponentOrNull<DamageableComponent>(Owner)?.TotalDamage ?? FixedPoint2.Zero);
}
}
protected override void OnRemove()
{
EntitySystem.Get<AlertsSystem>().ClearAlert(Owner, AlertType.HumanHealth);
base.OnRemove();
}
public override ComponentState GetComponentState()
{
return new MobStateComponentState(CurrentThreshold);
}
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
base.HandleComponentState(curState, nextState);
if (curState is not MobStateComponentState state)
{
return;
}
if (CurrentThreshold == state.CurrentThreshold)
{
return;
}
if (state.CurrentThreshold == null)
{
RemoveState();
}
else
{
UpdateState(state.CurrentThreshold.Value);
}
}
public IEnumerable<KeyValuePair<int, DamageState>> _highestToLowestStates => _lowestToHighestStates.Reverse();
[Obsolete("Use MobStateSystem")]
public bool IsAlive()
{
return CurrentState?.IsAlive() ?? false;
return IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<SharedMobStateSystem>()
.IsAlive(Owner, this);
}
[Obsolete("Use MobStateSystem")]
public bool IsCritical()
{
return CurrentState?.IsCritical() ?? false;
return IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<SharedMobStateSystem>()
.IsCritical(Owner, this);
}
[Obsolete("Use MobStateSystem")]
public bool IsDead()
{
return CurrentState?.IsDead() ?? false;
return IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<SharedMobStateSystem>()
.IsDead(Owner, this);
}
[Obsolete("Use MobStateSystem")]
public bool IsIncapacitated()
{
return CurrentState?.IsIncapacitated() ?? false;
}
public (IMobState state, FixedPoint2 threshold)? GetState(FixedPoint2 damage)
{
foreach (var (threshold, state) in _highestToLowestStates)
{
if (damage >= threshold)
{
return (state, threshold);
}
}
return null;
}
public bool TryGetState(
FixedPoint2 damage,
[NotNullWhen(true)] out IMobState? state,
out FixedPoint2 threshold)
{
var highestState = GetState(damage);
if (highestState == null)
{
state = default;
threshold = default;
return false;
}
(state, threshold) = highestState.Value;
return true;
}
private (IMobState state, FixedPoint2 threshold)? GetEarliestState(FixedPoint2 minimumDamage, Predicate<IMobState> predicate)
{
foreach (var (threshold, state) in _lowestToHighestStates)
{
if (threshold < minimumDamage ||
!predicate(state))
{
continue;
}
return (state, threshold);
}
return null;
}
private (IMobState state, FixedPoint2 threshold)? GetPreviousState(FixedPoint2 maximumDamage, Predicate<IMobState> predicate)
{
foreach (var (threshold, state) in _highestToLowestStates)
{
if (threshold > maximumDamage ||
!predicate(state))
{
continue;
}
return (state, threshold);
}
return null;
}
public (IMobState state, FixedPoint2 threshold)? GetEarliestCriticalState(FixedPoint2 minimumDamage)
{
return GetEarliestState(minimumDamage, s => s.IsCritical());
}
public (IMobState state, FixedPoint2 threshold)? GetEarliestIncapacitatedState(FixedPoint2 minimumDamage)
{
return GetEarliestState(minimumDamage, s => s.IsIncapacitated());
}
public (IMobState state, FixedPoint2 threshold)? GetEarliestDeadState(FixedPoint2 minimumDamage)
{
return GetEarliestState(minimumDamage, s => s.IsDead());
}
public (IMobState state, FixedPoint2 threshold)? GetPreviousCriticalState(FixedPoint2 minimumDamage)
{
return GetPreviousState(minimumDamage, s => s.IsCritical());
}
private bool TryGetState(
(IMobState state, FixedPoint2 threshold)? tuple,
[NotNullWhen(true)] out IMobState? state,
out FixedPoint2 threshold)
{
if (tuple == null)
{
state = default;
threshold = default;
return false;
}
(state, threshold) = tuple.Value;
return true;
}
public bool TryGetEarliestCriticalState(
FixedPoint2 minimumDamage,
[NotNullWhen(true)] out IMobState? state,
out FixedPoint2 threshold)
{
var earliestState = GetEarliestCriticalState(minimumDamage);
return TryGetState(earliestState, out state, out threshold);
}
public bool TryGetEarliestIncapacitatedState(
FixedPoint2 minimumDamage,
[NotNullWhen(true)] out IMobState? state,
out FixedPoint2 threshold)
{
var earliestState = GetEarliestIncapacitatedState(minimumDamage);
return TryGetState(earliestState, out state, out threshold);
}
public bool TryGetEarliestDeadState(
FixedPoint2 minimumDamage,
[NotNullWhen(true)] out IMobState? state,
out FixedPoint2 threshold)
{
var earliestState = GetEarliestDeadState(minimumDamage);
return TryGetState(earliestState, out state, out threshold);
}
public bool TryGetPreviousCriticalState(
FixedPoint2 maximumDamage,
[NotNullWhen(true)] out IMobState? state,
out FixedPoint2 threshold)
{
var earliestState = GetPreviousCriticalState(maximumDamage);
return TryGetState(earliestState, out state, out threshold);
}
private void RemoveState()
{
var old = CurrentState;
CurrentState = null;
CurrentThreshold = null;
SetMobState(old, null);
}
/// <summary>
/// Updates the mob state..
/// </summary>
public void UpdateState(FixedPoint2 damage)
{
if (!TryGetState(damage, out var newState, out var threshold))
{
return;
}
SetMobState(CurrentState, (newState, threshold));
}
/// <summary>
/// Sets the mob state and marks the component as dirty.
/// </summary>
private void SetMobState(IMobState? old, (IMobState state, FixedPoint2 threshold)? current)
{
var entMan = _entMan;
if (!current.HasValue)
{
old?.ExitState(Owner, entMan);
return;
}
var (state, threshold) = current.Value;
CurrentThreshold = threshold;
if (state == old)
{
state.UpdateState(Owner, threshold, entMan);
return;
}
old?.ExitState(Owner, entMan);
CurrentState = state;
state.EnterState(Owner, entMan);
state.UpdateState(Owner, threshold, entMan);
var message = new MobStateChangedEvent(this, old, state);
entMan.EventBus.RaiseLocalEvent(Owner, message, true);
Dirty();
}
}
[Serializable, NetSerializable]
public sealed class MobStateComponentState : ComponentState
{
public readonly FixedPoint2? CurrentThreshold;
public MobStateComponentState(FixedPoint2? currentThreshold)
{
CurrentThreshold = currentThreshold;
return IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<SharedMobStateSystem>()
.IsIncapacitated(Owner, this);
}
}
}

View File

@@ -3,7 +3,7 @@ using Robust.Shared.Serialization;
namespace Content.Shared.MobState
{
[Serializable, NetSerializable]
public enum DamageStateVisuals
public enum DamageStateVisuals : byte
{
State
}

View File

@@ -1,148 +0,0 @@
using Content.Shared.ActionBlocker;
using Content.Shared.Damage;
using Content.Shared.DragDrop;
using Content.Shared.Emoting;
using Content.Shared.Interaction.Events;
using Content.Shared.Inventory.Events;
using Content.Shared.Item;
using Content.Shared.MobState.Components;
using Content.Shared.MobState.State;
using Content.Shared.Movement;
using Content.Shared.Movement.Events;
using Content.Shared.Pulling.Events;
using Content.Shared.Speech;
using Content.Shared.Standing;
using Content.Shared.Throwing;
namespace Content.Shared.MobState.EntitySystems
{
public sealed class MobStateSystem : EntitySystem
{
[Dependency] private readonly ActionBlockerSystem _blocker = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<MobStateComponent, ChangeDirectionAttemptEvent>(OnChangeDirectionAttempt);
SubscribeLocalEvent<MobStateComponent, UseAttemptEvent>(OnUseAttempt);
SubscribeLocalEvent<MobStateComponent, InteractionAttemptEvent>(OnInteractAttempt);
SubscribeLocalEvent<MobStateComponent, ThrowAttemptEvent>(OnThrowAttempt);
SubscribeLocalEvent<MobStateComponent, SpeakAttemptEvent>(OnSpeakAttempt);
SubscribeLocalEvent<MobStateComponent, IsEquippingAttemptEvent>(OnEquipAttempt);
SubscribeLocalEvent<MobStateComponent, EmoteAttemptEvent>(OnEmoteAttempt);
SubscribeLocalEvent<MobStateComponent, IsUnequippingAttemptEvent>(OnUnequipAttempt);
SubscribeLocalEvent<MobStateComponent, DropAttemptEvent>(OnDropAttempt);
SubscribeLocalEvent<MobStateComponent, PickupAttemptEvent>(OnPickupAttempt);
SubscribeLocalEvent<MobStateComponent, StartPullAttemptEvent>(OnStartPullAttempt);
SubscribeLocalEvent<MobStateComponent, DamageChangedEvent>(UpdateState);
SubscribeLocalEvent<MobStateComponent, UpdateCanMoveEvent>(OnMoveAttempt);
SubscribeLocalEvent<MobStateComponent, StandAttemptEvent>(OnStandAttempt);
SubscribeLocalEvent<MobStateChangedEvent>(OnStateChanged);
// Note that there's no check for Down attempts because if a mob's in crit or dead, they can be downed...
}
#region ActionBlocker
private void OnStateChanged(MobStateChangedEvent ev)
{
_blocker.UpdateCanMove(ev.Entity);
}
private void CheckAct(EntityUid uid, MobStateComponent component, CancellableEntityEventArgs args)
{
switch (component.CurrentState)
{
case SharedDeadMobState:
case SharedCriticalMobState:
args.Cancel();
break;
}
}
private void OnChangeDirectionAttempt(EntityUid uid, MobStateComponent component, ChangeDirectionAttemptEvent args)
{
CheckAct(uid, component, args);
}
private void OnUseAttempt(EntityUid uid, MobStateComponent component, UseAttemptEvent args)
{
CheckAct(uid, component, args);
}
private void OnInteractAttempt(EntityUid uid, MobStateComponent component, InteractionAttemptEvent args)
{
CheckAct(uid, component, args);
}
private void OnThrowAttempt(EntityUid uid, MobStateComponent component, ThrowAttemptEvent args)
{
CheckAct(uid, component, args);
}
private void OnSpeakAttempt(EntityUid uid, MobStateComponent component, SpeakAttemptEvent args)
{
CheckAct(uid, component, args);
}
private void OnEquipAttempt(EntityUid uid, MobStateComponent component, IsEquippingAttemptEvent args)
{
// is this a self-equip, or are they being stripped?
if (args.Equipee == uid)
CheckAct(uid, component, args);
}
private void OnEmoteAttempt(EntityUid uid, MobStateComponent component, EmoteAttemptEvent args)
{
CheckAct(uid, component, args);
}
private void OnUnequipAttempt(EntityUid uid, MobStateComponent component, IsUnequippingAttemptEvent args)
{
// is this a self-equip, or are they being stripped?
if (args.Unequipee == uid)
CheckAct(uid, component, args);
}
private void OnDropAttempt(EntityUid uid, MobStateComponent component, DropAttemptEvent args)
{
CheckAct(uid, component, args);
}
private void OnPickupAttempt(EntityUid uid, MobStateComponent component, PickupAttemptEvent args)
{
CheckAct(uid, component, args);
}
#endregion
private void OnStartPullAttempt(EntityUid uid, MobStateComponent component, StartPullAttemptEvent args)
{
if (component.IsIncapacitated())
args.Cancel();
}
public void UpdateState(EntityUid _, MobStateComponent component, DamageChangedEvent args)
{
component.UpdateState(args.Damageable.TotalDamage);
}
private void OnMoveAttempt(EntityUid uid, MobStateComponent component, UpdateCanMoveEvent args)
{
switch (component.CurrentState)
{
case SharedCriticalMobState:
case SharedDeadMobState:
args.Cancel();
return;
default:
return;
}
}
private void OnStandAttempt(EntityUid uid, MobStateComponent component, StandAttemptEvent args)
{
if (component.IsIncapacitated())
args.Cancel();
}
}
}

View File

@@ -0,0 +1,26 @@
using Content.Shared.Alert;
using Content.Shared.FixedPoint;
namespace Content.Shared.MobState.EntitySystems;
public abstract partial class SharedMobStateSystem
{
public virtual void EnterCritState(EntityUid uid)
{
Alerts.ShowAlert(uid, AlertType.HumanCrit);
Standing.Down(uid);
if (TryComp<AppearanceComponent>(uid, out var appearance))
{
appearance.SetData(DamageStateVisuals.State, DamageState.Critical);
}
}
public virtual void ExitCritState(EntityUid uid)
{
Standing.Stand(uid);
}
public virtual void UpdateCritState(EntityUid entity, FixedPoint2 threshold) {}
}

View File

@@ -0,0 +1,36 @@
using Content.Shared.FixedPoint;
namespace Content.Shared.MobState.EntitySystems;
public abstract partial class SharedMobStateSystem
{
public virtual void EnterDeadState(EntityUid uid)
{
EnsureComp<CollisionWakeComponent>(uid);
Standing.Down(uid);
if (Standing.IsDown(uid) && TryComp<PhysicsComponent>(uid, out var physics))
{
physics.CanCollide = false;
}
if (TryComp<AppearanceComponent>(uid, out var appearance))
{
appearance.SetData(DamageStateVisuals.State, DamageState.Dead);
}
}
public virtual void ExitDeadState(EntityUid uid)
{
RemComp<CollisionWakeComponent>(uid);
Standing.Stand(uid);
if (!Standing.IsDown(uid) && TryComp<PhysicsComponent>(uid, out var physics))
{
physics.CanCollide = true;
}
}
public virtual void UpdateDeadState(EntityUid entity, FixedPoint2 threshold) {}
}

View File

@@ -0,0 +1,20 @@
using Content.Shared.FixedPoint;
namespace Content.Shared.MobState.EntitySystems;
public abstract partial class SharedMobStateSystem
{
public virtual void EnterNormState(EntityUid uid)
{
Standing.Stand(uid);
if (TryComp<AppearanceComponent>(uid, out var appearance))
{
appearance.SetData(DamageStateVisuals.State, DamageState.Alive);
}
}
public virtual void UpdateNormState(EntityUid entity, FixedPoint2 threshold) {}
public virtual void ExitNormState(EntityUid uid) {}
}

View File

@@ -0,0 +1,473 @@
using System.Diagnostics.CodeAnalysis;
using Content.Shared.ActionBlocker;
using Content.Shared.Alert;
using Content.Shared.Damage;
using Content.Shared.DragDrop;
using Content.Shared.Emoting;
using Content.Shared.FixedPoint;
using Content.Shared.Interaction.Events;
using Content.Shared.Inventory.Events;
using Content.Shared.Item;
using Content.Shared.MobState.Components;
using Content.Shared.Movement.Events;
using Content.Shared.Pulling.Events;
using Content.Shared.Speech;
using Content.Shared.Standing;
using Content.Shared.StatusEffect;
using Content.Shared.Throwing;
using Robust.Shared.Serialization;
namespace Content.Shared.MobState.EntitySystems
{
public abstract partial class SharedMobStateSystem : EntitySystem
{
[Dependency] protected readonly AlertsSystem Alerts = default!;
[Dependency] private readonly ActionBlockerSystem _blocker = default!;
[Dependency] protected readonly StandingStateSystem Standing = default!;
[Dependency] protected readonly StatusEffectsSystem Status = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<MobStateComponent, ComponentShutdown>(OnMobShutdown);
SubscribeLocalEvent<MobStateComponent, ComponentStartup>(OnMobStartup);
SubscribeLocalEvent<MobStateComponent, ChangeDirectionAttemptEvent>(OnChangeDirectionAttempt);
SubscribeLocalEvent<MobStateComponent, UseAttemptEvent>(OnUseAttempt);
SubscribeLocalEvent<MobStateComponent, InteractionAttemptEvent>(OnInteractAttempt);
SubscribeLocalEvent<MobStateComponent, ThrowAttemptEvent>(OnThrowAttempt);
SubscribeLocalEvent<MobStateComponent, SpeakAttemptEvent>(OnSpeakAttempt);
SubscribeLocalEvent<MobStateComponent, IsEquippingAttemptEvent>(OnEquipAttempt);
SubscribeLocalEvent<MobStateComponent, EmoteAttemptEvent>(OnEmoteAttempt);
SubscribeLocalEvent<MobStateComponent, IsUnequippingAttemptEvent>(OnUnequipAttempt);
SubscribeLocalEvent<MobStateComponent, DropAttemptEvent>(OnDropAttempt);
SubscribeLocalEvent<MobStateComponent, PickupAttemptEvent>(OnPickupAttempt);
SubscribeLocalEvent<MobStateComponent, StartPullAttemptEvent>(OnStartPullAttempt);
SubscribeLocalEvent<MobStateComponent, DamageChangedEvent>(UpdateState);
SubscribeLocalEvent<MobStateComponent, UpdateCanMoveEvent>(OnMoveAttempt);
SubscribeLocalEvent<MobStateComponent, StandAttemptEvent>(OnStandAttempt);
SubscribeLocalEvent<MobStateChangedEvent>(OnStateChanged);
// Note that there's no check for Down attempts because if a mob's in crit or dead, they can be downed...
}
private void OnMobStartup(EntityUid uid, MobStateComponent component, ComponentStartup args)
{
if (component.CurrentState != null && component.CurrentThreshold != null)
{
// Initialize with given states
SetMobState(component, null, (component.CurrentState.Value, component.CurrentThreshold.Value));
}
else
{
// Initialize with some amount of damage, defaulting to 0.
UpdateState(component, CompOrNull<DamageableComponent>(uid)?.TotalDamage ?? FixedPoint2.Zero);
}
}
private void OnMobShutdown(EntityUid uid, MobStateComponent component, ComponentShutdown args)
{
Alerts.ClearAlert(uid, AlertType.HumanHealth);
}
public bool IsAlive(EntityUid uid, MobStateComponent? component = null)
{
if (!Resolve(uid, ref component, false)) return false;
return component.CurrentState == DamageState.Alive;
}
public bool IsCritical(EntityUid uid, MobStateComponent? component = null)
{
if (!Resolve(uid, ref component, false)) return false;
return component.CurrentState == DamageState.Critical;
}
public bool IsDead(EntityUid uid, MobStateComponent? component = null)
{
if (!Resolve(uid, ref component, false)) return false;
return component.CurrentState == DamageState.Dead;
}
public bool IsIncapacitated(EntityUid uid, MobStateComponent? component = null)
{
if (!Resolve(uid, ref component, false)) return false;
return component.CurrentState is DamageState.Critical or DamageState.Dead;
}
#region ActionBlocker
private void OnStateChanged(MobStateChangedEvent ev)
{
_blocker.UpdateCanMove(ev.Entity);
}
private void CheckAct(EntityUid uid, MobStateComponent component, CancellableEntityEventArgs args)
{
switch (component.CurrentState)
{
case DamageState.Dead:
case DamageState.Critical:
args.Cancel();
break;
}
}
private void OnChangeDirectionAttempt(EntityUid uid, MobStateComponent component, ChangeDirectionAttemptEvent args)
{
CheckAct(uid, component, args);
}
private void OnUseAttempt(EntityUid uid, MobStateComponent component, UseAttemptEvent args)
{
CheckAct(uid, component, args);
}
private void OnInteractAttempt(EntityUid uid, MobStateComponent component, InteractionAttemptEvent args)
{
CheckAct(uid, component, args);
}
private void OnThrowAttempt(EntityUid uid, MobStateComponent component, ThrowAttemptEvent args)
{
CheckAct(uid, component, args);
}
private void OnSpeakAttempt(EntityUid uid, MobStateComponent component, SpeakAttemptEvent args)
{
CheckAct(uid, component, args);
}
private void OnEquipAttempt(EntityUid uid, MobStateComponent component, IsEquippingAttemptEvent args)
{
// is this a self-equip, or are they being stripped?
if (args.Equipee == uid)
CheckAct(uid, component, args);
}
private void OnEmoteAttempt(EntityUid uid, MobStateComponent component, EmoteAttemptEvent args)
{
CheckAct(uid, component, args);
}
private void OnUnequipAttempt(EntityUid uid, MobStateComponent component, IsUnequippingAttemptEvent args)
{
// is this a self-equip, or are they being stripped?
if (args.Unequipee == uid)
CheckAct(uid, component, args);
}
private void OnDropAttempt(EntityUid uid, MobStateComponent component, DropAttemptEvent args)
{
CheckAct(uid, component, args);
}
private void OnPickupAttempt(EntityUid uid, MobStateComponent component, PickupAttemptEvent args)
{
CheckAct(uid, component, args);
}
#endregion
private void OnStartPullAttempt(EntityUid uid, MobStateComponent component, StartPullAttemptEvent args)
{
if (IsIncapacitated(uid, component))
args.Cancel();
}
public void UpdateState(EntityUid _, MobStateComponent component, DamageChangedEvent args)
{
UpdateState(component, args.Damageable.TotalDamage);
}
private void OnMoveAttempt(EntityUid uid, MobStateComponent component, UpdateCanMoveEvent args)
{
switch (component.CurrentState)
{
case DamageState.Critical:
case DamageState.Dead:
args.Cancel();
return;
default:
return;
}
}
private void OnStandAttempt(EntityUid uid, MobStateComponent component, StandAttemptEvent args)
{
if (IsIncapacitated(uid, component))
args.Cancel();
}
public virtual void RemoveState(MobStateComponent component)
{
var old = component.CurrentState;
component.CurrentState = null;
component.CurrentThreshold = null;
SetMobState(component, old, null);
}
public virtual void EnterState(MobStateComponent? component, DamageState? state)
{
// TODO: Thanks buckle
if (component == null) return;
switch (state)
{
case DamageState.Alive:
EnterNormState(component.Owner);
break;
case DamageState.Critical:
EnterCritState(component.Owner);
break;
case DamageState.Dead:
EnterDeadState(component.Owner);
break;
case null:
break;
default:
throw new NotImplementedException();
}
}
protected virtual void UpdateState(MobStateComponent component, DamageState? state, FixedPoint2 threshold)
{
switch (state)
{
case DamageState.Alive:
UpdateNormState(component.Owner, threshold);
break;
case DamageState.Critical:
UpdateCritState(component.Owner, threshold);
break;
case DamageState.Dead:
UpdateDeadState(component.Owner, threshold);
break;
case null:
break;
default:
throw new NotImplementedException();
}
}
protected virtual void ExitState(MobStateComponent component, DamageState? state)
{
switch (state)
{
case DamageState.Alive:
ExitNormState(component.Owner);
break;
case DamageState.Critical:
ExitCritState(component.Owner);
break;
case DamageState.Dead:
ExitDeadState(component.Owner);
break;
case null:
break;
default:
throw new NotImplementedException();
}
}
/// <summary>
/// Updates the mob state..
/// </summary>
public void UpdateState(MobStateComponent component, FixedPoint2 damage)
{
if (!TryGetState(component, damage, out var newState, out var threshold))
{
return;
}
SetMobState(component, component.CurrentState, (newState.Value, threshold));
}
/// <summary>
/// Sets the mob state and marks the component as dirty.
/// </summary>
private void SetMobState(MobStateComponent component, DamageState? old, (DamageState state, FixedPoint2 threshold)? current)
{
if (!current.HasValue)
{
ExitState(component, old);
return;
}
var (state, threshold) = current.Value;
component.CurrentThreshold = threshold;
if (state == old)
{
UpdateState(component, state, threshold);
return;
}
ExitState(component, old);
component.CurrentState = state;
EnterState(component, state);
UpdateState(component, state, threshold);
var message = new MobStateChangedEvent(component, old, state);
RaiseLocalEvent(component.Owner, message, true);
Dirty(component);
}
public (DamageState state, FixedPoint2 threshold)? GetState(MobStateComponent component, FixedPoint2 damage)
{
foreach (var (threshold, state) in component._highestToLowestStates)
{
if (damage >= threshold)
{
return (state, threshold);
}
}
return null;
}
public bool TryGetState(
MobStateComponent component,
FixedPoint2 damage,
[NotNullWhen(true)] out DamageState? state,
out FixedPoint2 threshold)
{
var highestState = GetState(component, damage);
if (highestState == null)
{
state = default;
threshold = default;
return false;
}
(state, threshold) = highestState.Value;
return true;
}
private (DamageState state, FixedPoint2 threshold)? GetEarliestState(MobStateComponent component, FixedPoint2 minimumDamage, Predicate<DamageState> predicate)
{
foreach (var (threshold, state) in component._lowestToHighestStates)
{
if (threshold < minimumDamage ||
!predicate(state))
{
continue;
}
return (state, threshold);
}
return null;
}
private (DamageState state, FixedPoint2 threshold)? GetPreviousState(MobStateComponent component, FixedPoint2 maximumDamage, Predicate<DamageState> predicate)
{
foreach (var (threshold, state) in component._highestToLowestStates)
{
if (threshold > maximumDamage ||
!predicate(state))
{
continue;
}
return (state, threshold);
}
return null;
}
public (DamageState state, FixedPoint2 threshold)? GetEarliestCriticalState(MobStateComponent component, FixedPoint2 minimumDamage)
{
return GetEarliestState(component, minimumDamage, s => s == DamageState.Critical);
}
public (DamageState state, FixedPoint2 threshold)? GetEarliestIncapacitatedState(MobStateComponent component, FixedPoint2 minimumDamage)
{
return GetEarliestState(component, minimumDamage, s => s is DamageState.Critical or DamageState.Dead);
}
public (DamageState state, FixedPoint2 threshold)? GetEarliestDeadState(MobStateComponent component, FixedPoint2 minimumDamage)
{
return GetEarliestState(component, minimumDamage, s => s == DamageState.Dead);
}
public (DamageState state, FixedPoint2 threshold)? GetPreviousCriticalState(MobStateComponent component, FixedPoint2 minimumDamage)
{
return GetPreviousState(component, minimumDamage, s => s == DamageState.Critical);
}
private bool TryGetState(
(DamageState state, FixedPoint2 threshold)? tuple,
[NotNullWhen(true)] out DamageState? state,
out FixedPoint2 threshold)
{
if (tuple == null)
{
state = default;
threshold = default;
return false;
}
(state, threshold) = tuple.Value;
return true;
}
public bool TryGetEarliestCriticalState(
MobStateComponent component,
FixedPoint2 minimumDamage,
[NotNullWhen(true)] out DamageState? state,
out FixedPoint2 threshold)
{
var earliestState = GetEarliestCriticalState(component, minimumDamage);
return TryGetState(earliestState, out state, out threshold);
}
public bool TryGetEarliestIncapacitatedState(
MobStateComponent component,
FixedPoint2 minimumDamage,
[NotNullWhen(true)] out DamageState? state,
out FixedPoint2 threshold)
{
var earliestState = GetEarliestIncapacitatedState(component, minimumDamage);
return TryGetState(earliestState, out state, out threshold);
}
public bool TryGetEarliestDeadState(
MobStateComponent component,
FixedPoint2 minimumDamage,
[NotNullWhen(true)] out DamageState? state,
out FixedPoint2 threshold)
{
var earliestState = GetEarliestDeadState(component, minimumDamage);
return TryGetState(earliestState, out state, out threshold);
}
public bool TryGetPreviousCriticalState(
MobStateComponent component,
FixedPoint2 maximumDamage,
[NotNullWhen(true)] out DamageState? state,
out FixedPoint2 threshold)
{
var earliestState = GetPreviousCriticalState(component, maximumDamage);
return TryGetState(earliestState, out state, out threshold);
}
[Serializable, NetSerializable]
protected sealed class MobStateComponentState : ComponentState
{
public readonly FixedPoint2? CurrentThreshold;
public MobStateComponentState(FixedPoint2? currentThreshold)
{
CurrentThreshold = currentThreshold;
}
}
}
}

View File

@@ -1,5 +1,4 @@
using Content.Shared.MobState.Components;
using Content.Shared.MobState.State;
namespace Content.Shared.MobState
{
@@ -7,8 +6,8 @@ namespace Content.Shared.MobState
{
public MobStateChangedEvent(
MobStateComponent component,
IMobState? oldMobState,
IMobState currentMobState)
DamageState? oldMobState,
DamageState currentMobState)
{
Component = component;
OldMobState = oldMobState;
@@ -19,8 +18,35 @@ namespace Content.Shared.MobState
public MobStateComponent Component { get; }
public IMobState? OldMobState { get; }
public DamageState? OldMobState { get; }
public IMobState CurrentMobState { get; }
public DamageState CurrentMobState { get; }
}
public static class A
{
[Obsolete("Just check for the enum value instead")]
public static bool IsAlive(this DamageState state)
{
return state == DamageState.Alive;
}
[Obsolete("Just check for the enum value instead")]
public static bool IsCritical(this DamageState state)
{
return state == DamageState.Critical;
}
[Obsolete("Just check for the enum value instead")]
public static bool IsDead(this DamageState state)
{
return state == DamageState.Dead;
}
[Obsolete("Just check for the enum value instead")]
public static bool IsIncapacitated(this DamageState state)
{
return state is DamageState.Dead or DamageState.Critical;
}
}
}

View File

@@ -1,36 +0,0 @@
using Content.Shared.FixedPoint;
namespace Content.Shared.MobState.State
{
[ImplicitDataDefinitionForInheritors]
public abstract class BaseMobState : IMobState
{
protected abstract DamageState DamageState { get; }
public virtual bool IsAlive()
{
return DamageState == DamageState.Alive;
}
public virtual bool IsCritical()
{
return DamageState == DamageState.Critical;
}
public virtual bool IsDead()
{
return DamageState == DamageState.Dead;
}
public virtual bool IsIncapacitated()
{
return IsCritical() || IsDead();
}
public virtual void EnterState(EntityUid uid, IEntityManager entityManager) { }
public virtual void ExitState(EntityUid uid, IEntityManager entityManager) { }
public virtual void UpdateState(EntityUid entity, FixedPoint2 threshold, IEntityManager entityManager) { }
}
}

View File

@@ -1,40 +0,0 @@
using Content.Shared.FixedPoint;
namespace Content.Shared.MobState.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
{
bool IsAlive();
bool IsCritical();
bool IsDead();
/// <summary>
/// Checks if the mob is in a critical or dead state.
/// See <see cref="IsCritical"/> and <see cref="IsDead"/>.
/// </summary>
/// <returns>true if it is, false otherwise.</returns>
bool IsIncapacitated();
/// <summary>
/// Called when this state is entered.
/// </summary>
void EnterState(EntityUid uid, IEntityManager entityManager);
/// <summary>
/// Called when this state is left for a different state.
/// </summary>
void ExitState(EntityUid uid, IEntityManager entityManager);
/// <summary>
/// Called when this state is updated.
/// </summary>
void UpdateState(EntityUid entity, FixedPoint2 threshold, IEntityManager entityManager);
}
}

View File

@@ -1,34 +0,0 @@
using Content.Shared.Alert;
using Content.Shared.Standing;
namespace Content.Shared.MobState.State
{
/// <summary>
/// A state in which an entity is disabled from acting due to sufficient damage (considered unconscious).
/// </summary>
public abstract class SharedCriticalMobState : BaseMobState
{
protected override DamageState DamageState => DamageState.Critical;
public override void EnterState(EntityUid uid, IEntityManager entityManager)
{
base.EnterState(uid, entityManager);
EntitySystem.Get<AlertsSystem>().ShowAlert(uid, AlertType.HumanCrit); // TODO: combine humancrit-0 and humancrit-1 into a gif and display it
EntitySystem.Get<StandingStateSystem>().Down(uid);
if (entityManager.TryGetComponent(uid, out AppearanceComponent? appearance))
{
appearance.SetData(DamageStateVisuals.State, DamageState.Critical);
}
}
public override void ExitState(EntityUid uid, IEntityManager entityManager)
{
base.ExitState(uid, entityManager);
EntitySystem.Get<StandingStateSystem>().Stand(uid);
}
}
}

View File

@@ -1,44 +0,0 @@
using Content.Shared.Standing;
namespace Content.Shared.MobState.State
{
public abstract class SharedDeadMobState : BaseMobState
{
protected override DamageState DamageState => DamageState.Dead;
public override void EnterState(EntityUid uid, IEntityManager entityManager)
{
base.EnterState(uid, entityManager);
entityManager.EnsureComponent<CollisionWakeComponent>(uid);
var standingState = EntitySystem.Get<StandingStateSystem>();
standingState.Down(uid);
if (standingState.IsDown(uid) && entityManager.TryGetComponent(uid, out PhysicsComponent? physics))
{
physics.CanCollide = false;
}
if (entityManager.TryGetComponent(uid, out AppearanceComponent? appearance))
{
appearance.SetData(DamageStateVisuals.State, DamageState.Dead);
}
}
public override void ExitState(EntityUid uid, IEntityManager entityManager)
{
base.ExitState(uid, entityManager);
if (entityManager.HasComponent<CollisionWakeComponent>(uid))
{
entityManager.RemoveComponent<CollisionWakeComponent>(uid);
}
var standingState = EntitySystem.Get<StandingStateSystem>();
standingState.Stand(uid);
if (!standingState.IsDown(uid) && entityManager.TryGetComponent(uid, out PhysicsComponent? physics))
{
physics.CanCollide = true;
}
}
}
}

View File

@@ -1,24 +0,0 @@
using Content.Shared.Standing;
namespace Content.Shared.MobState.State
{
/// <summary>
/// The standard state an entity is in; no negative effects.
/// </summary>
public abstract class SharedNormalMobState : BaseMobState
{
protected override DamageState DamageState => DamageState.Alive;
public override void EnterState(EntityUid uid, IEntityManager entityManager)
{
base.EnterState(uid, entityManager);
EntitySystem.Get<StandingStateSystem>().Stand(uid);
if (entityManager.TryGetComponent(uid, out AppearanceComponent? appearance))
{
appearance.SetData(DamageStateVisuals.State, DamageState.Alive);
}
}
}
}