114 lines
2.6 KiB
C#
114 lines
2.6 KiB
C#
using System;
|
||
|
||
namespace Models;
|
||
|
||
/// <summary>
|
||
/// 状态值 (StatusValue)
|
||
/// 设计说明:
|
||
/// 1) 这是“带阈值的数值”,用于压力、体力、忠诚等需要触发事件的状态。
|
||
/// 2) 当前值由 PropertyValue 承担“范围约束”,阈值只负责触发行为。
|
||
/// 3) 事件由 Model 层抛出,Controller/系统层订阅,View 不直接耦合。
|
||
/// 注意事项:
|
||
/// - Upper/Lower 默认等于当前范围的上/下限,保持语义直观。
|
||
/// - 如果需要“软阈值”,请在系统中根据 Normalized 手动判断。
|
||
/// 未来扩展:
|
||
/// - 可增加“阈值触发冷却/抑制器”,避免频繁触发。
|
||
/// - 可加入“变化速率”以支持回合/时间驱动的增减。
|
||
/// </summary>
|
||
public sealed class StatusValue
|
||
{
|
||
private PropertyValue _current;
|
||
|
||
/// <summary>
|
||
/// 达到上限事件
|
||
/// </summary>
|
||
public event Action OnUpperThresholdReached;
|
||
/// <summary>
|
||
/// 达到下限事件
|
||
/// </summary>
|
||
public event Action OnLowerThresholdReached;
|
||
|
||
/// <summary>
|
||
/// 当前值
|
||
/// </summary>
|
||
public PropertyValue Current
|
||
{
|
||
get => _current;
|
||
set
|
||
{
|
||
_current = value;
|
||
CheckThresholds();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 上限阈值
|
||
/// </summary>
|
||
public float UpperThreshold { get; set; }
|
||
/// <summary>
|
||
/// 下限阈值
|
||
/// </summary>
|
||
public float LowerThreshold { get; set; }
|
||
|
||
/// <summary>
|
||
/// 构造函数(整数)
|
||
/// </summary>
|
||
public StatusValue(int current = 0, int upper = 100, int lower = 0)
|
||
{
|
||
_current = new PropertyValue(current, lower, upper);
|
||
UpperThreshold = upper;
|
||
LowerThreshold = lower;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 构造函数(浮点数)
|
||
/// </summary>
|
||
public StatusValue(float current, float upper, float lower)
|
||
{
|
||
_current = new PropertyValue(current, lower, upper);
|
||
UpperThreshold = upper;
|
||
LowerThreshold = lower;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 构造函数(PropertyValue)
|
||
/// </summary>
|
||
public StatusValue(PropertyValue current, float upperThreshold, float lowerThreshold)
|
||
{
|
||
_current = current ?? new PropertyValue(0);
|
||
UpperThreshold = upperThreshold;
|
||
LowerThreshold = lowerThreshold;
|
||
CheckThresholds();
|
||
}
|
||
|
||
private void CheckThresholds()
|
||
{
|
||
if (_current.Value >= UpperThreshold)
|
||
{
|
||
OnUpperThresholdReached?.Invoke();
|
||
}
|
||
|
||
if (_current.Value <= LowerThreshold)
|
||
{
|
||
OnLowerThresholdReached?.Invoke();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 增加值
|
||
/// </summary>
|
||
public void Add(float value)
|
||
{
|
||
_current.Add(value);
|
||
CheckThresholds();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 减少值
|
||
/// </summary>
|
||
public void Subtract(float value)
|
||
{
|
||
_current.Subtract(value);
|
||
CheckThresholds();
|
||
}
|
||
} |