using System.Collections; using System.Collections.Generic; using UnityEngine; /// /// 数值实例 /// public class ValueInstance { /// 数值类型 public readonly ValueType type; /// 数值名称 public readonly string name; /// 最小值 public float minValue; /// 最大值 public float maxValue; /// 最终数值 public float value; /// 默认值 public float defaultValue; /// 基础值 public float baseValue; /// 附加值% public float addedValue; /// 当前值 public float currentValue; /// 修改器列表 public List modifiers = new List(); /// 是布尔值 public bool isBoolean => type == ValueType.Boolean && value == maxValue; public ValueInstance(ValueType type, string name) { this.type = type; this.name = name; if (type == ValueType.Float) { minValue = 0; maxValue = float.MaxValue; } if (type == ValueType.Integer) { minValue = 0; maxValue = float.MaxValue; } if (type == ValueType.Boolean) { minValue = 0; maxValue = 1; } if (type == ValueType.Percentage) { minValue = 0; maxValue = 100; } } /// 添加修改器 public void AddModifier(ValueModifier modifier, bool isUpdate) { modifiers.Add(modifier); if (isUpdate) { RecalculateValue(); } } /// 添加修改器 public void AddModifier(List modifier, bool isUpdate) { modifiers.AddRange(modifier); if (isUpdate) { RecalculateValue(); } } /// 重新计算值 public void RecalculateValue() { addedValue = 1; baseValue = defaultValue; modifiers.ForEach(Modify); } /// 修改 public void Modify(ValueModifier modifier) { baseValue += modifier.FixedValue; addedValue += modifier.AddedValue; float temp = baseValue * addedValue; value = Mathf.Clamp(temp, minValue, maxValue); } }