修复BUG
This commit is contained in:
@@ -12,15 +12,22 @@ public class ValueContainer {
|
||||
|
||||
/// <summary> 所引器 </summary>
|
||||
public ValueInstance this[string id] => dictionary[id];
|
||||
/// <summary> 所引器(枚举) </summary>
|
||||
public ValueInstance this[Enum id] => dictionary[id.ToString()];
|
||||
|
||||
/// <summary> 循环集合 </summary>
|
||||
public void ForEach(Action<string, ValueInstance> action) {
|
||||
foreach (var item in dictionary) { action?.Invoke(item.Key, item.Value); }
|
||||
}
|
||||
|
||||
/// <summary> 添加数值 </summary>
|
||||
public void AddInstance(ValueType type, Enum name) {
|
||||
var instance = ValueInstance.Create(type, name);
|
||||
AddInstance(instance);
|
||||
}
|
||||
/// <summary> 添加数值 </summary>
|
||||
public void AddInstance(ValueType type, string name) {
|
||||
var instance = new ValueInstance(type, name);
|
||||
var instance = ValueInstance.Create(type, name);
|
||||
AddInstance(instance);
|
||||
}
|
||||
/// <summary> 添加数值 </summary>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/// <summary> 转换数据 </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user