修复BUG
This commit is contained in:
@@ -12,15 +12,22 @@ public class ValueContainer {
|
|||||||
|
|
||||||
/// <summary> 所引器 </summary>
|
/// <summary> 所引器 </summary>
|
||||||
public ValueInstance this[string id] => dictionary[id];
|
public ValueInstance this[string id] => dictionary[id];
|
||||||
|
/// <summary> 所引器(枚举) </summary>
|
||||||
|
public ValueInstance this[Enum id] => dictionary[id.ToString()];
|
||||||
|
|
||||||
/// <summary> 循环集合 </summary>
|
/// <summary> 循环集合 </summary>
|
||||||
public void ForEach(Action<string, ValueInstance> action) {
|
public void ForEach(Action<string, ValueInstance> action) {
|
||||||
foreach (var item in dictionary) { action?.Invoke(item.Key, item.Value); }
|
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>
|
/// <summary> 添加数值 </summary>
|
||||||
public void AddInstance(ValueType type, string name) {
|
public void AddInstance(ValueType type, string name) {
|
||||||
var instance = new ValueInstance(type, name);
|
var instance = ValueInstance.Create(type, name);
|
||||||
AddInstance(instance);
|
AddInstance(instance);
|
||||||
}
|
}
|
||||||
/// <summary> 添加数值 </summary>
|
/// <summary> 添加数值 </summary>
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
@@ -11,59 +12,87 @@ public class ValueInstance {
|
|||||||
/// <summary> 数值名称 </summary>
|
/// <summary> 数值名称 </summary>
|
||||||
public readonly string name;
|
public readonly string name;
|
||||||
|
|
||||||
/// <summary> 最小值 </summary>
|
/// <summary> 自定义数值 </summary>
|
||||||
public float minValue;
|
|
||||||
/// <summary> 最大值 </summary>
|
|
||||||
public float maxValue;
|
|
||||||
|
|
||||||
/// <summary> 最终数值 </summary>
|
|
||||||
public float value;
|
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>
|
/// <summary> 修改器列表 </summary>
|
||||||
public List<ValueModifier> modifiers = new List<ValueModifier>();
|
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>
|
/// <summary> 是布尔值 </summary>
|
||||||
public bool isBoolean => type == ValueType.Boolean && value == maxValue;
|
public bool IsBoolean => type == ValueType.Boolean && CurrentValue == MaxValue;
|
||||||
|
|
||||||
public ValueInstance(ValueType type, string name) {
|
/// <summary> 创建数值(用枚举命名) </summary>
|
||||||
this.type = type;
|
public static ValueInstance Create(ValueType type, Enum name) {
|
||||||
this.name = name;
|
return Create(type, name.ToString());
|
||||||
if (type == ValueType.Float) { minValue = 0; maxValue = float.MaxValue; }
|
}
|
||||||
if (type == ValueType.Integer) { minValue = 0; maxValue = float.MaxValue; }
|
/// <summary> 创建数值 </summary>
|
||||||
if (type == ValueType.Boolean) { minValue = 0; maxValue = 1; }
|
public static ValueInstance Create(ValueType type, string name) {
|
||||||
if (type == ValueType.Percentage) { minValue = 0; maxValue = 100; }
|
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>
|
/// <summary> 添加修改器 </summary>
|
||||||
public void AddModifier(ValueModifier modifier, bool isUpdate) {
|
public void AddModifier(ValueModifier modifier, bool isUpdate = true) {
|
||||||
modifiers.Add(modifier);
|
modifiers.Add(modifier);
|
||||||
if (isUpdate) { RecalculateValue(); }
|
if (isUpdate) { RecalculateValue(); }
|
||||||
}
|
}
|
||||||
/// <summary> 添加修改器 </summary>
|
/// <summary> 添加修改器 </summary>
|
||||||
public void AddModifier(List<ValueModifier> modifier, bool isUpdate) {
|
public void AddModifier(List<ValueModifier> modifier, bool isUpdate = true) {
|
||||||
modifiers.AddRange(modifier);
|
modifiers.AddRange(modifier);
|
||||||
if (isUpdate) { RecalculateValue(); }
|
if (isUpdate) { RecalculateValue(); }
|
||||||
}
|
}
|
||||||
/// <summary> 重新计算值 </summary>
|
/// <summary> 重新计算值 </summary>
|
||||||
public void RecalculateValue() {
|
public void RecalculateValue() {
|
||||||
addedValue = 1;
|
AddedValue = 1;
|
||||||
baseValue = defaultValue;
|
BaseValue = DefaultValue;
|
||||||
modifiers.ForEach(Modify);
|
modifiers.ForEach(Modify);
|
||||||
|
float temp = BaseValue * AddedValue;
|
||||||
|
CurrentValue = Mathf.Clamp(temp, MinValue, MaxValue);
|
||||||
|
if (type == ValueType.Integer) { CurrentValue = Mathf.Round(CurrentValue); }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> 修改 </summary>
|
/// <summary> 修改 </summary>
|
||||||
public void Modify(ValueModifier modifier) {
|
private void Modify(ValueModifier modifier) {
|
||||||
baseValue += modifier.FixedValue;
|
BaseValue += modifier.FixedValue;
|
||||||
addedValue += modifier.AddedValue;
|
AddedValue += modifier.AddedValue;
|
||||||
float temp = baseValue * addedValue;
|
|
||||||
value = Mathf.Clamp(temp, minValue, maxValue);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,37 +23,28 @@ public class ValueInstanceConst : ScriptableObject {
|
|||||||
public ValueContainerConst container;
|
public ValueContainerConst container;
|
||||||
|
|
||||||
public void InitialFloat() {
|
public void InitialFloat() {
|
||||||
type = ValueType.Float;
|
Initial(ValueType.Float, 0, 9999, 0);
|
||||||
minValue = 0;
|
|
||||||
maxValue = 9999;
|
|
||||||
baseValue = 0;
|
|
||||||
}
|
}
|
||||||
public void InitialInteger() {
|
public void InitialInteger() {
|
||||||
type = ValueType.Integer;
|
Initial(ValueType.Integer, 0, 9999, 0);
|
||||||
minValue = 0;
|
|
||||||
maxValue = 9999;
|
|
||||||
baseValue = 0;
|
|
||||||
}
|
}
|
||||||
public void InitialBoolean() {
|
public void InitialBoolean() {
|
||||||
type = ValueType.Boolean;
|
Initial(ValueType.Boolean, 0, 1, 0);
|
||||||
minValue = 0;
|
|
||||||
maxValue = 1;
|
|
||||||
baseValue = 0;
|
|
||||||
}
|
}
|
||||||
public void InitialPercentage() {
|
public void InitialPercentage() {
|
||||||
type = ValueType.Percentage;
|
Initial(ValueType.Percentage, 0, 100, 0);
|
||||||
minValue = 0;
|
}
|
||||||
maxValue = 100;
|
public void Initial(ValueType type, float minValue, float maxValue, float baseValue) {
|
||||||
baseValue = 0;
|
this.type = type;
|
||||||
|
this.minValue = minValue;
|
||||||
|
this.maxValue = maxValue;
|
||||||
|
this.baseValue = baseValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> 转换数据 </summary>
|
/// <summary> 转换数据 </summary>
|
||||||
public ValueInstance To() {
|
public ValueInstance To() {
|
||||||
ValueInstance instance = new ValueInstance(type, name);
|
var instance = ValueInstance.Create(type, name);
|
||||||
instance.minValue = minValue;
|
instance.Settings(baseValue, minValue, maxValue);
|
||||||
instance.maxValue = maxValue;
|
|
||||||
instance.defaultValue = baseValue;
|
|
||||||
instance.RecalculateValue();
|
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user