Generalize ReagentUnit into FixedPoint2 and use it for damage calculations (#5151)
* Damage units * sum ext method
This commit is contained in:
@@ -4,6 +4,7 @@ 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 Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
@@ -43,7 +44,7 @@ namespace Content.Shared.MobState.Components
|
||||
public IMobState? CurrentState { get; private set; }
|
||||
|
||||
[ViewVariables]
|
||||
public int? CurrentThreshold { get; private set; }
|
||||
public FixedPoint2? CurrentThreshold { get; private set; }
|
||||
|
||||
public IEnumerable<KeyValuePair<int, IMobState>> _highestToLowestStates => _lowestToHighestStates.Reverse();
|
||||
|
||||
@@ -59,7 +60,7 @@ namespace Content.Shared.MobState.Components
|
||||
else
|
||||
{
|
||||
// Initialize with some amount of damage, defaulting to 0.
|
||||
UpdateState(Owner.GetComponentOrNull<DamageableComponent>()?.TotalDamage ?? 0);
|
||||
UpdateState(Owner.GetComponentOrNull<DamageableComponent>()?.TotalDamage ?? FixedPoint2.Zero);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,7 +123,7 @@ namespace Content.Shared.MobState.Components
|
||||
return CurrentState?.IsIncapacitated() ?? false;
|
||||
}
|
||||
|
||||
public (IMobState state, int threshold)? GetState(int damage)
|
||||
public (IMobState state, FixedPoint2 threshold)? GetState(FixedPoint2 damage)
|
||||
{
|
||||
foreach (var (threshold, state) in _highestToLowestStates)
|
||||
{
|
||||
@@ -136,9 +137,9 @@ namespace Content.Shared.MobState.Components
|
||||
}
|
||||
|
||||
public bool TryGetState(
|
||||
int damage,
|
||||
FixedPoint2 damage,
|
||||
[NotNullWhen(true)] out IMobState? state,
|
||||
out int threshold)
|
||||
out FixedPoint2 threshold)
|
||||
{
|
||||
var highestState = GetState(damage);
|
||||
|
||||
@@ -153,7 +154,7 @@ namespace Content.Shared.MobState.Components
|
||||
return true;
|
||||
}
|
||||
|
||||
private (IMobState state, int threshold)? GetEarliestState(int minimumDamage, Predicate<IMobState> predicate)
|
||||
private (IMobState state, FixedPoint2 threshold)? GetEarliestState(FixedPoint2 minimumDamage, Predicate<IMobState> predicate)
|
||||
{
|
||||
foreach (var (threshold, state) in _lowestToHighestStates)
|
||||
{
|
||||
@@ -169,7 +170,7 @@ namespace Content.Shared.MobState.Components
|
||||
return null;
|
||||
}
|
||||
|
||||
private (IMobState state, int threshold)? GetPreviousState(int maximumDamage, Predicate<IMobState> predicate)
|
||||
private (IMobState state, FixedPoint2 threshold)? GetPreviousState(FixedPoint2 maximumDamage, Predicate<IMobState> predicate)
|
||||
{
|
||||
foreach (var (threshold, state) in _highestToLowestStates)
|
||||
{
|
||||
@@ -185,30 +186,30 @@ namespace Content.Shared.MobState.Components
|
||||
return null;
|
||||
}
|
||||
|
||||
public (IMobState state, int threshold)? GetEarliestCriticalState(int minimumDamage)
|
||||
public (IMobState state, FixedPoint2 threshold)? GetEarliestCriticalState(FixedPoint2 minimumDamage)
|
||||
{
|
||||
return GetEarliestState(minimumDamage, s => s.IsCritical());
|
||||
}
|
||||
|
||||
public (IMobState state, int threshold)? GetEarliestIncapacitatedState(int minimumDamage)
|
||||
public (IMobState state, FixedPoint2 threshold)? GetEarliestIncapacitatedState(FixedPoint2 minimumDamage)
|
||||
{
|
||||
return GetEarliestState(minimumDamage, s => s.IsIncapacitated());
|
||||
}
|
||||
|
||||
public (IMobState state, int threshold)? GetEarliestDeadState(int minimumDamage)
|
||||
public (IMobState state, FixedPoint2 threshold)? GetEarliestDeadState(FixedPoint2 minimumDamage)
|
||||
{
|
||||
return GetEarliestState(minimumDamage, s => s.IsDead());
|
||||
}
|
||||
|
||||
public (IMobState state, int threshold)? GetPreviousCriticalState(int minimumDamage)
|
||||
public (IMobState state, FixedPoint2 threshold)? GetPreviousCriticalState(FixedPoint2 minimumDamage)
|
||||
{
|
||||
return GetPreviousState(minimumDamage, s => s.IsCritical());
|
||||
}
|
||||
|
||||
private bool TryGetState(
|
||||
(IMobState state, int threshold)? tuple,
|
||||
(IMobState state, FixedPoint2 threshold)? tuple,
|
||||
[NotNullWhen(true)] out IMobState? state,
|
||||
out int threshold)
|
||||
out FixedPoint2 threshold)
|
||||
{
|
||||
if (tuple == null)
|
||||
{
|
||||
@@ -222,9 +223,9 @@ namespace Content.Shared.MobState.Components
|
||||
}
|
||||
|
||||
public bool TryGetEarliestCriticalState(
|
||||
int minimumDamage,
|
||||
FixedPoint2 minimumDamage,
|
||||
[NotNullWhen(true)] out IMobState? state,
|
||||
out int threshold)
|
||||
out FixedPoint2 threshold)
|
||||
{
|
||||
var earliestState = GetEarliestCriticalState(minimumDamage);
|
||||
|
||||
@@ -232,9 +233,9 @@ namespace Content.Shared.MobState.Components
|
||||
}
|
||||
|
||||
public bool TryGetEarliestIncapacitatedState(
|
||||
int minimumDamage,
|
||||
FixedPoint2 minimumDamage,
|
||||
[NotNullWhen(true)] out IMobState? state,
|
||||
out int threshold)
|
||||
out FixedPoint2 threshold)
|
||||
{
|
||||
var earliestState = GetEarliestIncapacitatedState(minimumDamage);
|
||||
|
||||
@@ -242,9 +243,9 @@ namespace Content.Shared.MobState.Components
|
||||
}
|
||||
|
||||
public bool TryGetEarliestDeadState(
|
||||
int minimumDamage,
|
||||
FixedPoint2 minimumDamage,
|
||||
[NotNullWhen(true)] out IMobState? state,
|
||||
out int threshold)
|
||||
out FixedPoint2 threshold)
|
||||
{
|
||||
var earliestState = GetEarliestDeadState(minimumDamage);
|
||||
|
||||
@@ -252,9 +253,9 @@ namespace Content.Shared.MobState.Components
|
||||
}
|
||||
|
||||
public bool TryGetPreviousCriticalState(
|
||||
int maximumDamage,
|
||||
FixedPoint2 maximumDamage,
|
||||
[NotNullWhen(true)] out IMobState? state,
|
||||
out int threshold)
|
||||
out FixedPoint2 threshold)
|
||||
{
|
||||
var earliestState = GetPreviousCriticalState(maximumDamage);
|
||||
|
||||
@@ -273,7 +274,7 @@ namespace Content.Shared.MobState.Components
|
||||
/// <summary>
|
||||
/// Updates the mob state..
|
||||
/// </summary>
|
||||
public void UpdateState(int damage)
|
||||
public void UpdateState(FixedPoint2 damage)
|
||||
{
|
||||
if (!TryGetState(damage, out var newState, out var threshold))
|
||||
{
|
||||
@@ -286,7 +287,7 @@ namespace Content.Shared.MobState.Components
|
||||
/// <summary>
|
||||
/// Sets the mob state and marks the component as dirty.
|
||||
/// </summary>
|
||||
private void SetMobState(IMobState? old, (IMobState state, int threshold)? current)
|
||||
private void SetMobState(IMobState? old, (IMobState state, FixedPoint2 threshold)? current)
|
||||
{
|
||||
if (!current.HasValue)
|
||||
{
|
||||
@@ -324,9 +325,9 @@ namespace Content.Shared.MobState.Components
|
||||
[Serializable, NetSerializable]
|
||||
public class MobStateComponentState : ComponentState
|
||||
{
|
||||
public readonly int? CurrentThreshold;
|
||||
public readonly FixedPoint2? CurrentThreshold;
|
||||
|
||||
public MobStateComponentState(int? currentThreshold)
|
||||
public MobStateComponentState(FixedPoint2? currentThreshold)
|
||||
{
|
||||
CurrentThreshold = currentThreshold;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user