修复BUG
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
@@ -11,59 +12,87 @@ public class ValueInstance {
|
||||
/// <summary> 数值名称 </summary>
|
||||
public readonly string name;
|
||||
|
||||
/// <summary> 最小值 </summary>
|
||||
public float minValue;
|
||||
/// <summary> 最大值 </summary>
|
||||
public float maxValue;
|
||||
|
||||
/// <summary> 最终数值 </summary>
|
||||
/// <summary> 自定义数值 </summary>
|
||||
public float value;
|
||||
/// <summary> 默认值 </summary>
|
||||
public float defaultValue;
|
||||
/// <summary> 基础值 </summary>
|
||||
public float baseValue;
|
||||
/// <summary> 附加值% </summary>
|
||||
public float addedValue;
|
||||
/// <summary> 当前值 </summary>
|
||||
public float currentValue;
|
||||
|
||||
/// <summary> 修改器列表 </summary>
|
||||
public List<ValueModifier> modifiers = new List<ValueModifier>();
|
||||
|
||||
/// <summary> 默认值 </summary>
|
||||
public float DefaultValue { get; protected set; }
|
||||
/// <summary> 最小值 </summary>
|
||||
public float MinValue { get; protected set; }
|
||||
/// <summary> 最大值 </summary>
|
||||
public float MaxValue { get; protected set; }
|
||||
/// <summary> 基础值 = 默认值 + 全部修改器 </summary>
|
||||
public float BaseValue { get; protected set; }
|
||||
/// <summary> 附加值% = 1 + 全部修改器 </summary>
|
||||
public float AddedValue { get; protected set; }
|
||||
/// <summary> 当前值 = Clamp(基础值 * 附加值%) </summary>
|
||||
public float CurrentValue { get; protected set; }
|
||||
/// <summary> 是布尔值 </summary>
|
||||
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; }
|
||||
/// <summary> 创建数值(用枚举命名) </summary>
|
||||
public static ValueInstance Create(ValueType type, Enum name) {
|
||||
return Create(type, name.ToString());
|
||||
}
|
||||
/// <summary> 创建数值 </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary> 设置默认值 </summary>
|
||||
public void Settings(float defaultValue) {
|
||||
this.DefaultValue = defaultValue;
|
||||
RecalculateValue();
|
||||
}
|
||||
/// <summary> 设置数值范围 </summary>
|
||||
public void Settings(float minValue, float maxValue) {
|
||||
this.MinValue = minValue;
|
||||
this.MaxValue = maxValue;
|
||||
RecalculateValue();
|
||||
}
|
||||
/// <summary> 设置默认值和数值范围 </summary>
|
||||
public void Settings(float defaultValue, float minValue, float maxValue) {
|
||||
this.DefaultValue = defaultValue;
|
||||
this.MinValue = minValue;
|
||||
this.MaxValue = maxValue;
|
||||
RecalculateValue();
|
||||
}
|
||||
/// <summary> 添加修改器 </summary>
|
||||
public void AddModifier(ValueModifier modifier, bool isUpdate) {
|
||||
public void AddModifier(ValueModifier modifier, bool isUpdate = true) {
|
||||
modifiers.Add(modifier);
|
||||
if (isUpdate) { RecalculateValue(); }
|
||||
}
|
||||
/// <summary> 添加修改器 </summary>
|
||||
public void AddModifier(List<ValueModifier> modifier, bool isUpdate) {
|
||||
public void AddModifier(List<ValueModifier> modifier, bool isUpdate = true) {
|
||||
modifiers.AddRange(modifier);
|
||||
if (isUpdate) { RecalculateValue(); }
|
||||
}
|
||||
/// <summary> 重新计算值 </summary>
|
||||
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); }
|
||||
}
|
||||
|
||||
/// <summary> 修改 </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user