Damage rework (#2525)
* Make damage work through messages and events, make destructible not inherit ruinable or reference damageable * Copy sound logic to destructible component for now * Fix typo * Fix prototype error * Remove breakable component damageable reference * Remove breakable construction reference * Remove ruinable component * Move thresholds to individual components and away from damageable * Add threshold property to damageable component code * Add thresholds to destructible component, add states to damageable, remove damage container, fix up mob states * Being alive isn't normal * Fix not reading the id * Merge fixes * YAML fixes * Grammar moment * Remove unnecessary dependency * Update thresholds doc * Change naming of thresholds to states in MobStateComponent * Being alive is once again normal * Make DamageState a byte * Bring out classes structs and enums from DestructibleComponent * Add test for destructible thresholds * Merge fixes * More merge fixes and fix rejuvenate test * Remove IMobState.IsConscious * More merge fixes someone please god review this shit already * Fix rejuvenate test * Update outdated destructible in YAML * Fix repeatedly entering the current state * Fix repeatedly entering the current state, add Threshold.TriggersOnce and expand test * Update saltern
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
using Content.Shared.Damage;
|
||||
|
||||
namespace Content.Shared.GameObjects.Components.Damage
|
||||
{
|
||||
/// <summary>
|
||||
/// Data class with information on how the value of a
|
||||
/// single <see cref="DamageType"/> has changed.
|
||||
/// </summary>
|
||||
public struct DamageChangeData
|
||||
{
|
||||
/// <summary>
|
||||
/// Type of damage that changed.
|
||||
/// </summary>
|
||||
public DamageType Type;
|
||||
|
||||
/// <summary>
|
||||
/// The new current value for that damage.
|
||||
/// </summary>
|
||||
public int NewValue;
|
||||
|
||||
/// <summary>
|
||||
/// How much the health value changed from its last value (negative is heals, positive is damage).
|
||||
/// </summary>
|
||||
public int Delta;
|
||||
|
||||
public DamageChangeData(DamageType type, int newValue, int delta)
|
||||
{
|
||||
Type = type;
|
||||
NewValue = newValue;
|
||||
Delta = delta;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using Content.Shared.GameObjects.Components.Body;
|
||||
|
||||
namespace Content.Shared.GameObjects.Components.Damage
|
||||
{
|
||||
/// <summary>
|
||||
/// Data class with information on how to damage a
|
||||
/// <see cref="IDamageableComponent"/>.
|
||||
/// While not necessary to damage for all instances, classes such as
|
||||
/// <see cref="SharedBodyComponent"/> may require it for extra data
|
||||
/// (such as selecting which limb to target).
|
||||
/// </summary>
|
||||
public class DamageChangeParams : EventArgs
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Content.Shared.Damage;
|
||||
|
||||
namespace Content.Shared.GameObjects.Components.Damage
|
||||
{
|
||||
public class DamageChangedEventArgs : EventArgs
|
||||
{
|
||||
public DamageChangedEventArgs(IDamageableComponent damageable, IReadOnlyList<DamageChangeData> data)
|
||||
{
|
||||
Damageable = damageable;
|
||||
Data = data;
|
||||
}
|
||||
|
||||
public DamageChangedEventArgs(IDamageableComponent damageable, DamageType type, int newValue, int delta)
|
||||
{
|
||||
Damageable = damageable;
|
||||
|
||||
var datum = new DamageChangeData(type, newValue, delta);
|
||||
var data = new List<DamageChangeData> {datum};
|
||||
|
||||
Data = data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the <see cref="IDamageableComponent"/> that invoked the event.
|
||||
/// </summary>
|
||||
public IDamageableComponent Damageable { get; }
|
||||
|
||||
/// <summary>
|
||||
/// List containing data on each <see cref="DamageType"/> that was changed.
|
||||
/// </summary>
|
||||
public IReadOnlyList<DamageChangeData> Data { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using System.Collections.Generic;
|
||||
using Content.Shared.Damage;
|
||||
using Robust.Shared.GameObjects;
|
||||
|
||||
namespace Content.Shared.GameObjects.Components.Damage
|
||||
{
|
||||
public class DamageChangedMessage : ComponentMessage
|
||||
{
|
||||
public DamageChangedMessage(IDamageableComponent damageable, IReadOnlyList<DamageChangeData> data)
|
||||
{
|
||||
Damageable = damageable;
|
||||
Data = data;
|
||||
}
|
||||
|
||||
public DamageChangedMessage(IDamageableComponent damageable, DamageType type, int newValue, int delta)
|
||||
{
|
||||
Damageable = damageable;
|
||||
|
||||
var datum = new DamageChangeData(type, newValue, delta);
|
||||
var data = new List<DamageChangeData> {datum};
|
||||
|
||||
Data = data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reference to the <see cref="IDamageableComponent"/> that invoked the event.
|
||||
/// </summary>
|
||||
public IDamageableComponent Damageable { get; }
|
||||
|
||||
/// <summary>
|
||||
/// List containing data on each <see cref="DamageType"/> that was changed.
|
||||
/// </summary>
|
||||
public IReadOnlyList<DamageChangeData> Data { get; }
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
using System;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
using Robust.Shared.Serialization;
|
||||
|
||||
namespace Content.Shared.GameObjects.Components.Damage
|
||||
{
|
||||
// TODO: Fix summary
|
||||
/// <summary>
|
||||
/// Defines what state an <see cref="IEntity"/> with a
|
||||
/// <see cref="IDamageableComponent"/> is in.
|
||||
/// Not all states must be supported - for instance, the
|
||||
/// <see cref="RuinableComponent"/> only supports
|
||||
/// <see cref="DamageState.Alive"/> and <see cref="DamageState.Dead"/>,
|
||||
/// as inanimate objects don't go into crit.
|
||||
/// </summary>
|
||||
[Serializable, NetSerializable]
|
||||
public enum DamageState
|
||||
{
|
||||
Invalid = 0,
|
||||
Alive,
|
||||
Critical,
|
||||
Dead
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.Damage.DamageContainer;
|
||||
using Content.Shared.Damage.ResistanceSet;
|
||||
@@ -31,51 +32,28 @@ namespace Content.Shared.GameObjects.Components.Damage
|
||||
|
||||
public override string Name => "Damageable";
|
||||
|
||||
private DamageState _damageState;
|
||||
public override uint? NetID => ContentNetIDs.DAMAGEABLE;
|
||||
|
||||
private readonly Dictionary<DamageType, int> _damageList = DamageTypeExtensions.ToDictionary();
|
||||
private readonly HashSet<DamageType> _supportedTypes = new();
|
||||
private readonly HashSet<DamageClass> _supportedClasses = new();
|
||||
private DamageFlag _flags;
|
||||
|
||||
public event Action<HealthChangedEventArgs>? HealthChangedEvent;
|
||||
public event Action<DamageChangedEventArgs>? HealthChangedEvent;
|
||||
|
||||
// TODO DAMAGE Use as default values, specify overrides in a separate property through yaml for better (de)serialization
|
||||
[ViewVariables] public string DamageContainerId { get; set; } = default!;
|
||||
|
||||
[ViewVariables] private ResistanceSet Resistances { get; set; } = default!;
|
||||
|
||||
[ViewVariables] private DamageContainer Damage { get; set; } = default!;
|
||||
// TODO DAMAGE Cache this
|
||||
[ViewVariables] public int TotalDamage => _damageList.Values.Sum();
|
||||
|
||||
public Dictionary<DamageState, int> Thresholds { get; set; } = new();
|
||||
[ViewVariables]
|
||||
public IReadOnlyDictionary<DamageClass, int> DamageClasses =>
|
||||
DamageTypeExtensions.ToClassDictionary(_damageList);
|
||||
|
||||
public virtual List<DamageState> SupportedDamageStates
|
||||
{
|
||||
get
|
||||
{
|
||||
var states = new List<DamageState> {DamageState.Alive};
|
||||
|
||||
states.AddRange(Thresholds.Keys);
|
||||
|
||||
return states;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual DamageState CurrentState
|
||||
{
|
||||
get => _damageState;
|
||||
set
|
||||
{
|
||||
var old = _damageState;
|
||||
_damageState = value;
|
||||
|
||||
if (old != value)
|
||||
{
|
||||
EnterState(value);
|
||||
}
|
||||
|
||||
Dirty();
|
||||
}
|
||||
}
|
||||
|
||||
[ViewVariables] public int TotalDamage => Damage.TotalDamage;
|
||||
|
||||
public IReadOnlyDictionary<DamageClass, int> DamageClasses => Damage.DamageClasses;
|
||||
|
||||
public IReadOnlyDictionary<DamageType, int> DamageTypes => Damage.DamageTypes;
|
||||
[ViewVariables] public IReadOnlyDictionary<DamageType, int> DamageTypes => _damageList;
|
||||
|
||||
public DamageFlag Flags
|
||||
{
|
||||
@@ -107,41 +85,20 @@ namespace Content.Shared.GameObjects.Components.Damage
|
||||
Flags &= ~flag;
|
||||
}
|
||||
|
||||
public bool SupportsDamageClass(DamageClass @class)
|
||||
{
|
||||
return _supportedClasses.Contains(@class);
|
||||
}
|
||||
|
||||
public bool SupportsDamageType(DamageType type)
|
||||
{
|
||||
return _supportedTypes.Contains(type);
|
||||
}
|
||||
|
||||
public override void ExposeData(ObjectSerializer serializer)
|
||||
{
|
||||
base.ExposeData(serializer);
|
||||
|
||||
// TODO DAMAGE Serialize as a dictionary of damage states to thresholds
|
||||
serializer.DataReadWriteFunction(
|
||||
"criticalThreshold",
|
||||
null,
|
||||
t =>
|
||||
{
|
||||
if (t == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Thresholds[DamageState.Critical] = t.Value;
|
||||
},
|
||||
() => Thresholds.TryGetValue(DamageState.Critical, out var value) ? value : (int?) null);
|
||||
|
||||
serializer.DataReadWriteFunction(
|
||||
"deadThreshold",
|
||||
null,
|
||||
t =>
|
||||
{
|
||||
if (t == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Thresholds[DamageState.Dead] = t.Value;
|
||||
},
|
||||
() => Thresholds.TryGetValue(DamageState.Dead, out var value) ? value : (int?) null);
|
||||
|
||||
serializer.DataField(ref _damageState, "damageState", DamageState.Alive);
|
||||
|
||||
serializer.DataReadWriteFunction(
|
||||
"flags",
|
||||
new List<DamageFlag>(),
|
||||
@@ -161,7 +118,9 @@ namespace Content.Shared.GameObjects.Components.Damage
|
||||
var writeFlags = new List<DamageFlag>();
|
||||
|
||||
if (Flags == DamageFlag.None)
|
||||
{
|
||||
return writeFlags;
|
||||
}
|
||||
|
||||
foreach (var flag in (DamageFlag[]) Enum.GetValues(typeof(DamageFlag)))
|
||||
{
|
||||
@@ -181,9 +140,15 @@ namespace Content.Shared.GameObjects.Components.Damage
|
||||
prototype =>
|
||||
{
|
||||
var damagePrototype = _prototypeManager.Index<DamageContainerPrototype>(prototype);
|
||||
Damage = new DamageContainer(OnHealthChanged, damagePrototype);
|
||||
|
||||
_supportedClasses.Clear();
|
||||
_supportedTypes.Clear();
|
||||
|
||||
DamageContainerId = damagePrototype.ID;
|
||||
_supportedClasses.UnionWith(damagePrototype.SupportedClasses);
|
||||
_supportedTypes.UnionWith(damagePrototype.SupportedTypes);
|
||||
},
|
||||
() => Damage.ID);
|
||||
() => DamageContainerId);
|
||||
|
||||
serializer.DataReadWriteFunction(
|
||||
"resistancePrototype",
|
||||
@@ -196,16 +161,6 @@ namespace Content.Shared.GameObjects.Components.Damage
|
||||
() => Resistances.ID);
|
||||
}
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
foreach (var behavior in Owner.GetAllComponents<IOnHealthChangedBehavior>())
|
||||
{
|
||||
HealthChangedEvent += behavior.OnHealthChanged;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Startup()
|
||||
{
|
||||
base.Startup();
|
||||
@@ -213,215 +168,302 @@ namespace Content.Shared.GameObjects.Components.Damage
|
||||
ForceHealthChangedEvent();
|
||||
}
|
||||
|
||||
public bool TryGetDamage(DamageType type, out int damage)
|
||||
public override ComponentState GetComponentState()
|
||||
{
|
||||
return Damage.TryGetDamageValue(type, out damage);
|
||||
return new DamageableComponentState(_damageList, _flags);
|
||||
}
|
||||
|
||||
public bool ChangeDamage(DamageType type, int amount, bool ignoreResistances,
|
||||
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
|
||||
{
|
||||
base.HandleComponentState(curState, nextState);
|
||||
|
||||
if (!(curState is DamageableComponentState state))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_damageList.Clear();
|
||||
|
||||
foreach (var (type, damage) in state.DamageList)
|
||||
{
|
||||
_damageList[type] = damage;
|
||||
}
|
||||
|
||||
_flags = state.Flags;
|
||||
}
|
||||
|
||||
public int GetDamage(DamageType type)
|
||||
{
|
||||
return _damageList.GetValueOrDefault(type);
|
||||
}
|
||||
|
||||
public bool TryGetDamage(DamageType type, out int damage)
|
||||
{
|
||||
return _damageList.TryGetValue(type, out damage);
|
||||
}
|
||||
|
||||
public int GetDamage(DamageClass @class)
|
||||
{
|
||||
if (!SupportsDamageClass(@class))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
var damage = 0;
|
||||
|
||||
foreach (var type in @class.ToTypes())
|
||||
{
|
||||
damage += GetDamage(type);
|
||||
}
|
||||
|
||||
return damage;
|
||||
}
|
||||
|
||||
public bool TryGetDamage(DamageClass @class, out int damage)
|
||||
{
|
||||
if (!SupportsDamageClass(@class))
|
||||
{
|
||||
damage = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
damage = GetDamage(@class);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to set the damage value for the given <see cref="DamageType"/>.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// True if successful, false if this container does not support that type.
|
||||
/// </returns>
|
||||
public bool TrySetDamage(DamageType type, int newValue)
|
||||
{
|
||||
if (newValue < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var damageClass = type.ToClass();
|
||||
|
||||
if (_supportedClasses.Contains(damageClass))
|
||||
{
|
||||
var old = _damageList[type] = newValue;
|
||||
_damageList[type] = newValue;
|
||||
|
||||
var delta = newValue - old;
|
||||
var datum = new DamageChangeData(type, newValue, delta);
|
||||
var data = new List<DamageChangeData> {datum};
|
||||
|
||||
OnHealthChanged(data);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Heal(DamageType type)
|
||||
{
|
||||
SetDamage(type, 0);
|
||||
}
|
||||
|
||||
public void Heal()
|
||||
{
|
||||
foreach (var type in _supportedTypes)
|
||||
{
|
||||
Heal(type);
|
||||
}
|
||||
}
|
||||
|
||||
public bool ChangeDamage(
|
||||
DamageType type,
|
||||
int amount,
|
||||
bool ignoreResistances,
|
||||
IEntity? source = null,
|
||||
HealthChangeParams? extraParams = null)
|
||||
DamageChangeParams? extraParams = null)
|
||||
{
|
||||
if (amount > 0 && HasFlag(DamageFlag.Invulnerable))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Damage.SupportsDamageType(type))
|
||||
if (!SupportsDamageType(type))
|
||||
{
|
||||
var finalDamage = amount;
|
||||
if (!ignoreResistances)
|
||||
{
|
||||
finalDamage = Resistances.CalculateDamage(type, amount);
|
||||
}
|
||||
|
||||
Damage.ChangeDamageValue(type, finalDamage);
|
||||
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
var finalDamage = amount;
|
||||
|
||||
if (!ignoreResistances)
|
||||
{
|
||||
finalDamage = Resistances.CalculateDamage(type, amount);
|
||||
}
|
||||
|
||||
if (!_damageList.TryGetValue(type, out var current))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_damageList[type] = current + finalDamage;
|
||||
|
||||
if (_damageList[type] < 0)
|
||||
{
|
||||
_damageList[type] = 0;
|
||||
finalDamage = -current;
|
||||
}
|
||||
|
||||
current = _damageList[type];
|
||||
|
||||
var datum = new DamageChangeData(type, current, finalDamage);
|
||||
var data = new List<DamageChangeData> {datum};
|
||||
|
||||
OnHealthChanged(data);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool ChangeDamage(DamageClass @class, int amount, bool ignoreResistances,
|
||||
IEntity? source = null,
|
||||
HealthChangeParams? extraParams = null)
|
||||
DamageChangeParams? extraParams = null)
|
||||
{
|
||||
if (amount > 0 && HasFlag(DamageFlag.Invulnerable))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Damage.SupportsDamageClass(@class))
|
||||
if (!SupportsDamageClass(@class))
|
||||
{
|
||||
var types = @class.ToTypes();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (amount < 0)
|
||||
var types = @class.ToTypes();
|
||||
|
||||
if (amount < 0)
|
||||
{
|
||||
// Changing multiple types is a bit more complicated. Might be a better way (formula?) to do this,
|
||||
// but essentially just loops between each damage category until all healing is used up.
|
||||
var healingLeft = amount;
|
||||
var healThisCycle = 1;
|
||||
|
||||
// While we have healing left...
|
||||
while (healingLeft > 0 && healThisCycle != 0)
|
||||
{
|
||||
// Changing multiple types is a bit more complicated. Might be a better way (formula?) to do this,
|
||||
// but essentially just loops between each damage category until all healing is used up.
|
||||
var healingLeft = amount;
|
||||
var healThisCycle = 1;
|
||||
// Infinite loop fallback, if no healing was done in a cycle
|
||||
// then exit
|
||||
healThisCycle = 0;
|
||||
|
||||
// While we have healing left...
|
||||
while (healingLeft > 0 && healThisCycle != 0)
|
||||
int healPerType;
|
||||
if (healingLeft > -types.Count)
|
||||
{
|
||||
// Infinite loop fallback, if no healing was done in a cycle
|
||||
// then exit
|
||||
healThisCycle = 0;
|
||||
|
||||
int healPerType;
|
||||
if (healingLeft > -types.Count && healingLeft < 0)
|
||||
{
|
||||
// Say we were to distribute 2 healing between 3
|
||||
// this will distribute 1 to each (and stop after 2 are given)
|
||||
healPerType = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Say we were to distribute 62 healing between 3
|
||||
// this will distribute 20 to each, leaving 2 for next loop
|
||||
healPerType = healingLeft / types.Count;
|
||||
}
|
||||
|
||||
foreach (var type in types)
|
||||
{
|
||||
var healAmount =
|
||||
Math.Max(Math.Max(healPerType, -Damage.GetDamageValue(type)),
|
||||
healingLeft);
|
||||
|
||||
Damage.ChangeDamageValue(type, healAmount);
|
||||
healThisCycle += healAmount;
|
||||
healingLeft -= healAmount;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
var damageLeft = amount;
|
||||
|
||||
while (damageLeft > 0)
|
||||
{
|
||||
int damagePerType;
|
||||
|
||||
if (damageLeft < types.Count && damageLeft > 0)
|
||||
{
|
||||
damagePerType = 1;
|
||||
// Say we were to distribute 2 healing between 3
|
||||
// this will distribute 1 to each (and stop after 2 are given)
|
||||
healPerType = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
damagePerType = damageLeft / types.Count;
|
||||
// Say we were to distribute 62 healing between 3
|
||||
// this will distribute 20 to each, leaving 2 for next loop
|
||||
healPerType = healingLeft / types.Count;
|
||||
}
|
||||
|
||||
foreach (var type in types)
|
||||
{
|
||||
var damageAmount = Math.Min(damagePerType, damageLeft);
|
||||
Damage.ChangeDamageValue(type, damageAmount);
|
||||
damageLeft -= damageAmount;
|
||||
var healAmount =
|
||||
Math.Max(Math.Max(healPerType, -GetDamage(type)), healingLeft);
|
||||
|
||||
ChangeDamage(type, healAmount, true);
|
||||
healThisCycle += healAmount;
|
||||
healingLeft -= healAmount;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
var damageLeft = amount;
|
||||
|
||||
while (damageLeft > 0)
|
||||
{
|
||||
int damagePerType;
|
||||
|
||||
if (damageLeft < types.Count)
|
||||
{
|
||||
damagePerType = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
damagePerType = damageLeft / types.Count;
|
||||
}
|
||||
|
||||
foreach (var type in types)
|
||||
{
|
||||
var damageAmount = Math.Min(damagePerType, damageLeft);
|
||||
ChangeDamage(type, damageAmount, true);
|
||||
damageLeft -= damageAmount;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool SetDamage(DamageType type, int newValue, IEntity? source = null,
|
||||
HealthChangeParams? extraParams = null)
|
||||
public bool SetDamage(DamageType type, int newValue, IEntity? source = null, DamageChangeParams? extraParams = null)
|
||||
{
|
||||
if (newValue >= TotalDamage && HasFlag(DamageFlag.Invulnerable))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Damage.SupportsDamageType(type))
|
||||
if (newValue < 0)
|
||||
{
|
||||
Damage.SetDamageValue(type, newValue);
|
||||
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
if (!_damageList.ContainsKey(type))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Heal()
|
||||
{
|
||||
Damage.Heal();
|
||||
var old = _damageList[type];
|
||||
_damageList[type] = newValue;
|
||||
|
||||
var delta = newValue - old;
|
||||
var datum = new DamageChangeData(type, 0, delta);
|
||||
var data = new List<DamageChangeData> {datum};
|
||||
|
||||
OnHealthChanged(data);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void ForceHealthChangedEvent()
|
||||
{
|
||||
var data = new List<HealthChangeData>();
|
||||
var data = new List<DamageChangeData>();
|
||||
|
||||
foreach (var type in Damage.SupportedTypes)
|
||||
foreach (var type in _supportedTypes)
|
||||
{
|
||||
var damage = Damage.GetDamageValue(type);
|
||||
var datum = new HealthChangeData(type, damage, 0);
|
||||
var damage = GetDamage(type);
|
||||
var datum = new DamageChangeData(type, damage, 0);
|
||||
data.Add(datum);
|
||||
}
|
||||
|
||||
OnHealthChanged(data);
|
||||
}
|
||||
|
||||
public (int current, int max)? Health(DamageState threshold)
|
||||
private void OnHealthChanged(List<DamageChangeData> changes)
|
||||
{
|
||||
if (!SupportedDamageStates.Contains(threshold) ||
|
||||
!Thresholds.TryGetValue(threshold, out var thresholdValue))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var current = thresholdValue - TotalDamage;
|
||||
return (current, thresholdValue);
|
||||
}
|
||||
|
||||
public bool TryHealth(DamageState threshold, out (int current, int max) health)
|
||||
{
|
||||
var temp = Health(threshold);
|
||||
|
||||
if (temp == null)
|
||||
{
|
||||
health = (default, default);
|
||||
return false;
|
||||
}
|
||||
|
||||
health = temp.Value;
|
||||
return true;
|
||||
}
|
||||
|
||||
private void OnHealthChanged(List<HealthChangeData> changes)
|
||||
{
|
||||
var args = new HealthChangedEventArgs(this, changes);
|
||||
var args = new DamageChangedEventArgs(this, changes);
|
||||
OnHealthChanged(args);
|
||||
}
|
||||
|
||||
protected virtual void EnterState(DamageState state) { }
|
||||
|
||||
protected virtual void OnHealthChanged(HealthChangedEventArgs e)
|
||||
protected virtual void OnHealthChanged(DamageChangedEventArgs e)
|
||||
{
|
||||
if (CurrentState != DamageState.Dead)
|
||||
{
|
||||
if (Thresholds.TryGetValue(DamageState.Dead, out var deadThreshold) &&
|
||||
TotalDamage > deadThreshold)
|
||||
{
|
||||
CurrentState = DamageState.Dead;
|
||||
}
|
||||
else if (Thresholds.TryGetValue(DamageState.Critical, out var critThreshold) &&
|
||||
TotalDamage > critThreshold)
|
||||
{
|
||||
CurrentState = DamageState.Critical;
|
||||
}
|
||||
else
|
||||
{
|
||||
CurrentState = DamageState.Alive;
|
||||
}
|
||||
}
|
||||
|
||||
Owner.EntityManager.EventBus.RaiseEvent(EventSource.Local, e);
|
||||
HealthChangedEvent?.Invoke(e);
|
||||
|
||||
var message = new DamageChangedMessage(this, e.Data);
|
||||
SendMessage(message);
|
||||
|
||||
Dirty();
|
||||
}
|
||||
|
||||
@@ -446,4 +488,17 @@ namespace Content.Shared.GameObjects.Components.Damage
|
||||
ChangeDamage(DamageType.Heat, damage, false);
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable, NetSerializable]
|
||||
public class DamageableComponentState : ComponentState
|
||||
{
|
||||
public readonly Dictionary<DamageType, int> DamageList;
|
||||
public readonly DamageFlag Flags;
|
||||
|
||||
public DamageableComponentState(Dictionary<DamageType, int> damageList, DamageFlag flags) : base(ContentNetIDs.DAMAGEABLE)
|
||||
{
|
||||
DamageList = damageList;
|
||||
Flags = flags;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Content.Shared.Damage;
|
||||
using Content.Shared.GameObjects.Components.Body;
|
||||
using Content.Shared.GameObjects.EntitySystems;
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
|
||||
@@ -17,20 +15,7 @@ namespace Content.Shared.GameObjects.Components.Damage
|
||||
/// (including both damage negated by resistance or simply inputting 0 as
|
||||
/// the amount of damage to deal).
|
||||
/// </summary>
|
||||
event Action<HealthChangedEventArgs> HealthChangedEvent;
|
||||
|
||||
Dictionary<DamageState, int> Thresholds { get; }
|
||||
|
||||
/// <summary>
|
||||
/// List of all <see cref="Damage.DamageState">DamageStates</see> that
|
||||
/// <see cref="CurrentState"/> can be.
|
||||
/// </summary>
|
||||
List<DamageState> SupportedDamageStates { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="Damage.DamageState"/> currently representing this component.
|
||||
/// </summary>
|
||||
DamageState CurrentState { get; set; }
|
||||
event Action<DamageChangedEventArgs> HealthChangedEvent;
|
||||
|
||||
/// <summary>
|
||||
/// Sum of all damages taken.
|
||||
@@ -71,6 +56,10 @@ namespace Content.Shared.GameObjects.Components.Damage
|
||||
/// <param name="flag">The flag to remove.</param>
|
||||
void RemoveFlag(DamageFlag flag);
|
||||
|
||||
bool SupportsDamageClass(DamageClass @class);
|
||||
|
||||
bool SupportsDamageType(DamageType type);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the amount of damage of a type.
|
||||
/// </summary>
|
||||
@@ -79,7 +68,7 @@ namespace Content.Shared.GameObjects.Components.Damage
|
||||
/// <returns>
|
||||
/// True if the given <see cref="type"/> is supported, false otherwise.
|
||||
/// </returns>
|
||||
bool TryGetDamage(DamageType type, [NotNullWhen(true)] out int damage);
|
||||
bool TryGetDamage(DamageType type, out int damage);
|
||||
|
||||
/// <summary>
|
||||
/// Changes the specified <see cref="DamageType"/>, applying
|
||||
@@ -101,10 +90,14 @@ namespace Content.Shared.GameObjects.Components.Damage
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// False if the given type is not supported or improper
|
||||
/// <see cref="HealthChangeParams"/> were provided; true otherwise.
|
||||
/// <see cref="DamageChangeParams"/> were provided; true otherwise.
|
||||
/// </returns>
|
||||
bool ChangeDamage(DamageType type, int amount, bool ignoreResistances, IEntity? source = null,
|
||||
HealthChangeParams? extraParams = null);
|
||||
bool ChangeDamage(
|
||||
DamageType type,
|
||||
int amount,
|
||||
bool ignoreResistances,
|
||||
IEntity? source = null,
|
||||
DamageChangeParams? extraParams = null);
|
||||
|
||||
/// <summary>
|
||||
/// Changes the specified <see cref="DamageClass"/>, applying
|
||||
@@ -127,10 +120,14 @@ namespace Content.Shared.GameObjects.Components.Damage
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// Returns false if the given class is not supported or improper
|
||||
/// <see cref="HealthChangeParams"/> were provided; true otherwise.
|
||||
/// <see cref="DamageChangeParams"/> were provided; true otherwise.
|
||||
/// </returns>
|
||||
bool ChangeDamage(DamageClass @class, int amount, bool ignoreResistances, IEntity? source = null,
|
||||
HealthChangeParams? extraParams = null);
|
||||
bool ChangeDamage(
|
||||
DamageClass @class,
|
||||
int amount,
|
||||
bool ignoreResistances,
|
||||
IEntity? source = null,
|
||||
DamageChangeParams? extraParams = null);
|
||||
|
||||
/// <summary>
|
||||
/// Forcefully sets the specified <see cref="DamageType"/> to the given
|
||||
@@ -145,9 +142,13 @@ namespace Content.Shared.GameObjects.Components.Damage
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// Returns false if the given type is not supported or improper
|
||||
/// <see cref="HealthChangeParams"/> were provided; true otherwise.
|
||||
/// <see cref="DamageChangeParams"/> were provided; true otherwise.
|
||||
/// </returns>
|
||||
bool SetDamage(DamageType type, int newValue, IEntity? source = null, HealthChangeParams? extraParams = null);
|
||||
bool SetDamage(
|
||||
DamageType type,
|
||||
int newValue,
|
||||
IEntity? source = null,
|
||||
DamageChangeParams? extraParams = null);
|
||||
|
||||
/// <summary>
|
||||
/// Sets all damage values to zero.
|
||||
@@ -158,103 +159,5 @@ namespace Content.Shared.GameObjects.Components.Damage
|
||||
/// Invokes the HealthChangedEvent with the current values of health.
|
||||
/// </summary>
|
||||
void ForceHealthChangedEvent();
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the health of an entity until it enters
|
||||
/// <see cref="threshold"/>.
|
||||
/// </summary>
|
||||
/// <param name="threshold">The state to use as a threshold.</param>
|
||||
/// <returns>
|
||||
/// The current and maximum health on this entity based on
|
||||
/// <see cref="threshold"/>, or null if the state is not supported.
|
||||
/// </returns>
|
||||
(int current, int max)? Health(DamageState threshold);
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the health of an entity until it enters
|
||||
/// <see cref="threshold"/>.
|
||||
/// </summary>
|
||||
/// <param name="threshold">The state to use as a threshold.</param>
|
||||
/// <param name="health">
|
||||
/// The current and maximum health on this entity based on
|
||||
/// <see cref="threshold"/>, or null if the state is not supported.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// True if <see cref="threshold"/> is supported, false otherwise.
|
||||
/// </returns>
|
||||
bool TryHealth(DamageState threshold, [NotNullWhen(true)] out (int current, int max) health);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Data class with information on how to damage a
|
||||
/// <see cref="IDamageableComponent"/>.
|
||||
/// While not necessary to damage for all instances, classes such as
|
||||
/// <see cref="SharedBodyComponent"/> may require it for extra data
|
||||
/// (such as selecting which limb to target).
|
||||
/// </summary>
|
||||
public class HealthChangeParams : EventArgs
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Data class with information on how the <see cref="DamageType"/>
|
||||
/// values of a <see cref="IDamageableComponent"/> have changed.
|
||||
/// </summary>
|
||||
public class HealthChangedEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Reference to the <see cref="IDamageableComponent"/> that invoked the event.
|
||||
/// </summary>
|
||||
public readonly IDamageableComponent Damageable;
|
||||
|
||||
/// <summary>
|
||||
/// List containing data on each <see cref="DamageType"/> that was changed.
|
||||
/// </summary>
|
||||
public readonly List<HealthChangeData> Data;
|
||||
|
||||
public HealthChangedEventArgs(IDamageableComponent damageable, List<HealthChangeData> data)
|
||||
{
|
||||
Damageable = damageable;
|
||||
Data = data;
|
||||
}
|
||||
|
||||
public HealthChangedEventArgs(IDamageableComponent damageable, DamageType type, int newValue, int delta)
|
||||
{
|
||||
Damageable = damageable;
|
||||
|
||||
var datum = new HealthChangeData(type, newValue, delta);
|
||||
var data = new List<HealthChangeData> {datum};
|
||||
|
||||
Data = data;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Data class with information on how the value of a
|
||||
/// single <see cref="DamageType"/> has changed.
|
||||
/// </summary>
|
||||
public struct HealthChangeData
|
||||
{
|
||||
/// <summary>
|
||||
/// Type of damage that changed.
|
||||
/// </summary>
|
||||
public DamageType Type;
|
||||
|
||||
/// <summary>
|
||||
/// The new current value for that damage.
|
||||
/// </summary>
|
||||
public int NewValue;
|
||||
|
||||
/// <summary>
|
||||
/// How much the health value changed from its last value (negative is heals, positive is damage).
|
||||
/// </summary>
|
||||
public int Delta;
|
||||
|
||||
public HealthChangeData(DamageType type, int newValue, int delta)
|
||||
{
|
||||
Type = type;
|
||||
NewValue = newValue;
|
||||
Delta = delta;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
using Robust.Shared.Interfaces.GameObjects;
|
||||
|
||||
namespace Content.Shared.GameObjects.Components.Damage
|
||||
{
|
||||
// TODO
|
||||
/// <summary>
|
||||
/// Component interface that gets triggered after the values of a
|
||||
/// <see cref="IDamageableComponent"/> on the same <see cref="IEntity"/> change.
|
||||
/// </summary>
|
||||
public interface IOnHealthChangedBehavior
|
||||
{
|
||||
/// <summary>
|
||||
/// Called when the entity's <see cref="IDamageableComponent"/>
|
||||
/// is healed or hurt.
|
||||
/// Of note is that a "deal 0 damage" call will still trigger
|
||||
/// this function (including both damage negated by resistance or
|
||||
/// simply inputting 0 as the amount of damage to deal).
|
||||
/// </summary>
|
||||
/// <param name="e">Details of how the health changed.</param>
|
||||
public void OnHealthChanged(HealthChangedEventArgs e);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user