From 62e3374187ad62d7572aa95ecc73ca2f6f50eeec Mon Sep 17 00:00:00 2001 From: MuHua-123 <136542559+MuHua-123@users.noreply.github.com> Date: Sat, 11 Oct 2025 10:19:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DBUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ModuleValue/Runtime/ValueContainer.cs | 9 +- .../ModuleValue/Runtime/ValueInstance.cs | 91 ++++++++++++------- .../ModuleValue/Runtime/ValueInstanceConst.cs | 33 +++---- 3 files changed, 80 insertions(+), 53 deletions(-) diff --git a/Assets/ModuleCore/ModuleValue/Runtime/ValueContainer.cs b/Assets/ModuleCore/ModuleValue/Runtime/ValueContainer.cs index 9711c79..d6e8cd4 100644 --- a/Assets/ModuleCore/ModuleValue/Runtime/ValueContainer.cs +++ b/Assets/ModuleCore/ModuleValue/Runtime/ValueContainer.cs @@ -12,15 +12,22 @@ public class ValueContainer { /// 所引器 public ValueInstance this[string id] => dictionary[id]; + /// 所引器(枚举) + public ValueInstance this[Enum id] => dictionary[id.ToString()]; /// 循环集合 public void ForEach(Action action) { foreach (var item in dictionary) { action?.Invoke(item.Key, item.Value); } } + /// 添加数值 + public void AddInstance(ValueType type, Enum name) { + var instance = ValueInstance.Create(type, name); + AddInstance(instance); + } /// 添加数值 public void AddInstance(ValueType type, string name) { - var instance = new ValueInstance(type, name); + var instance = ValueInstance.Create(type, name); AddInstance(instance); } /// 添加数值 diff --git a/Assets/ModuleCore/ModuleValue/Runtime/ValueInstance.cs b/Assets/ModuleCore/ModuleValue/Runtime/ValueInstance.cs index cb32461..8752bc2 100644 --- a/Assets/ModuleCore/ModuleValue/Runtime/ValueInstance.cs +++ b/Assets/ModuleCore/ModuleValue/Runtime/ValueInstance.cs @@ -1,3 +1,4 @@ +using System; using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -11,59 +12,87 @@ public class ValueInstance { /// 数值名称 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 float DefaultValue { get; protected set; } + /// 最小值 + public float MinValue { get; protected set; } + /// 最大值 + public float MaxValue { get; protected set; } + /// 基础值 = 默认值 + 全部修改器 + public float BaseValue { get; protected set; } + /// 附加值% = 1 + 全部修改器 + public float AddedValue { get; protected set; } + /// 当前值 = Clamp(基础值 * 附加值%) + public float CurrentValue { get; protected set; } /// 是布尔值 - public bool isBoolean => type == ValueType.Boolean && value == maxValue; + public bool IsBoolean => type == ValueType.Boolean && CurrentValue == 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 static ValueInstance Create(ValueType type, Enum name) { + return Create(type, name.ToString()); + } + /// 创建数值 + public static ValueInstance Create(ValueType type, string name) { + ValueInstance instance = new ValueInstance(type, name); + float maxValue = float.MaxValue; + if (type == ValueType.Boolean) { maxValue = 1; } + if (type == ValueType.Percentage) { maxValue = 100; } + instance.Settings(0, maxValue); + return instance; } + protected ValueInstance(ValueType type, string name) { + this.type = type; + this.name = name; + } + + /// 设置默认值 + public void Settings(float defaultValue) { + this.DefaultValue = defaultValue; + RecalculateValue(); + } + /// 设置数值范围 + public void Settings(float minValue, float maxValue) { + this.MinValue = minValue; + this.MaxValue = maxValue; + RecalculateValue(); + } + /// 设置默认值和数值范围 + public void Settings(float defaultValue, float minValue, float maxValue) { + this.DefaultValue = defaultValue; + this.MinValue = minValue; + this.MaxValue = maxValue; + RecalculateValue(); + } /// 添加修改器 - public void AddModifier(ValueModifier modifier, bool isUpdate) { + public void AddModifier(ValueModifier modifier, bool isUpdate = true) { modifiers.Add(modifier); if (isUpdate) { RecalculateValue(); } } /// 添加修改器 - public void AddModifier(List modifier, bool isUpdate) { + public void AddModifier(List modifier, bool isUpdate = true) { modifiers.AddRange(modifier); if (isUpdate) { RecalculateValue(); } } /// 重新计算值 public void RecalculateValue() { - addedValue = 1; - baseValue = defaultValue; + AddedValue = 1; + BaseValue = DefaultValue; modifiers.ForEach(Modify); + float temp = BaseValue * AddedValue; + CurrentValue = Mathf.Clamp(temp, MinValue, MaxValue); + if (type == ValueType.Integer) { CurrentValue = Mathf.Round(CurrentValue); } } /// 修改 - public void Modify(ValueModifier modifier) { - baseValue += modifier.FixedValue; - addedValue += modifier.AddedValue; - float temp = baseValue * addedValue; - value = Mathf.Clamp(temp, minValue, maxValue); + private void Modify(ValueModifier modifier) { + BaseValue += modifier.FixedValue; + AddedValue += modifier.AddedValue; } } diff --git a/Assets/ModuleCore/ModuleValue/Runtime/ValueInstanceConst.cs b/Assets/ModuleCore/ModuleValue/Runtime/ValueInstanceConst.cs index c797daa..11cf01d 100644 --- a/Assets/ModuleCore/ModuleValue/Runtime/ValueInstanceConst.cs +++ b/Assets/ModuleCore/ModuleValue/Runtime/ValueInstanceConst.cs @@ -23,37 +23,28 @@ public class ValueInstanceConst : ScriptableObject { public ValueContainerConst container; public void InitialFloat() { - type = ValueType.Float; - minValue = 0; - maxValue = 9999; - baseValue = 0; + Initial(ValueType.Float, 0, 9999, 0); } public void InitialInteger() { - type = ValueType.Integer; - minValue = 0; - maxValue = 9999; - baseValue = 0; + Initial(ValueType.Integer, 0, 9999, 0); } public void InitialBoolean() { - type = ValueType.Boolean; - minValue = 0; - maxValue = 1; - baseValue = 0; + Initial(ValueType.Boolean, 0, 1, 0); } public void InitialPercentage() { - type = ValueType.Percentage; - minValue = 0; - maxValue = 100; - baseValue = 0; + Initial(ValueType.Percentage, 0, 100, 0); + } + public void Initial(ValueType type, float minValue, float maxValue, float baseValue) { + this.type = type; + this.minValue = minValue; + this.maxValue = maxValue; + this.baseValue = baseValue; } /// 转换数据 public ValueInstance To() { - ValueInstance instance = new ValueInstance(type, name); - instance.minValue = minValue; - instance.maxValue = maxValue; - instance.defaultValue = baseValue; - instance.RecalculateValue(); + var instance = ValueInstance.Create(type, name); + instance.Settings(baseValue, minValue, maxValue); return instance; } }