From b79a8eb068a5124c68ed0087dfa3a69bf8550529 Mon Sep 17 00:00:00 2001 From: Greenstack Date: Fri, 20 Feb 2026 20:51:47 -0700 Subject: [PATCH] #4: Deprecate empty ResourceStat constructor and introduce one to give a max value --- src/Stats.cs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Stats.cs b/src/Stats.cs index ad68edd..a4a1152 100644 --- a/src/Stats.cs +++ b/src/Stats.cs @@ -20,6 +20,7 @@ * IN THE SOFTWARE. */ +using System.Diagnostics.CodeAnalysis; using System.Numerics; namespace Greenstack.GameStats @@ -276,16 +277,19 @@ public override int GetHashCode() public T Max { get; set; } = T.MaxValue; - private T _currentValue; + private T _currentValue = T.Zero; public event StatValueChanged? OnStatChanged; public required T CurrentValue { get => _currentValue; + [MemberNotNull(nameof(_currentValue))] set { - T oldValue = _currentValue; + // Supressing null here because the first time this is called, + // we're not going to have any event subscribers yet + T oldValue = _currentValue!; _currentValue = T.Clamp(value, Min, Max); if (_currentValue != oldValue) { @@ -301,16 +305,23 @@ public required T CurrentValue /// public bool IsDepleted => CurrentValue == Min; -#pragma warning disable 8618 // CurrentValue is a setter for _currentValue /// - /// + /// Empty constructor. /// - /// + [Obsolete($"Use constructor that accepts a parameter for the MaxValue instead")] public ResourceStat() -#pragma warning restore 8618 { } + /// + /// Creates a resource stat with the maximum value set. + /// + /// The maximum value for this resource. + public ResourceStat(T maxValue) + { + Max = maxValue; + } + public void Increase(T amount) { CurrentValue += amount;