using System; using System.Collections.Generic; using UnityEngine; namespace NBF { public class StateParamsConst { public const string ChargedProgress = "ChargedProgress"; } /// /// 状态进入参数(用于网络同步和动画/表现播放) /// [Serializable] public class StateEnterParams { // 序列化友好的数据存储 [SerializeField] private List _keys = new(); [SerializeField] private List _intValues = new(); [SerializeField] private List _floatValues = new(); [SerializeField] private List _vector3Values = new(); [SerializeField] private List _quaternionValues = new(); // 快速访问缓存 private Dictionary _intCache; private Dictionary _floatCache; private Dictionary _vector3Cache; private Dictionary _quaternionCache; public StateEnterParams() { InitializeCaches(); } private void InitializeCaches() { _intCache = new Dictionary(); _floatCache = new Dictionary(); _vector3Cache = new Dictionary(); _quaternionCache = new Dictionary(); } /// /// 清空所有参数 /// public void Clear() { _keys.Clear(); _intValues.Clear(); _floatValues.Clear(); _vector3Values.Clear(); _quaternionValues.Clear(); _intCache.Clear(); _floatCache.Clear(); _vector3Cache.Clear(); _quaternionCache.Clear(); } /// /// 设置 int 参数 /// public void SetInt(string key, int value) { if (_intCache.TryGetValue(key, out int index)) { _intValues[index] = value; } else { _keys.Add(key); _intValues.Add(value); _intCache[key] = _intValues.Count - 1; } } /// /// 设置 float 参数 /// public void SetFloat(string key, float value) { if (_floatCache.TryGetValue(key, out int index)) { _floatValues[index] = value; } else { _keys.Add(key); _floatValues.Add(value); _floatCache[key] = _floatValues.Count - 1; } } /// /// 设置 Vector3 参数 /// public void SetVector3(string key, Vector3 value) { if (_vector3Cache.TryGetValue(key, out int index)) { _vector3Values[index] = value; } else { _keys.Add(key); _vector3Values.Add(value); _vector3Cache[key] = _vector3Values.Count - 1; } } /// /// 设置 Quaternion 参数 /// public void SetQuaternion(string key, Quaternion value) { if (_quaternionCache.TryGetValue(key, out int index)) { _quaternionValues[index] = value; } else { _keys.Add(key); _quaternionValues.Add(value); _quaternionCache[key] = _quaternionValues.Count - 1; } } /// /// 设置 bool 参数 /// public void SetBool(string key, bool value) { if (_intCache.TryGetValue(key, out int index)) { _intValues[index] = value ? 1 : 0; } else { _keys.Add(key); _intValues.Add(value ? 1 : 0); _intCache[key] = _intValues.Count - 1; } } /// /// 获取 int 参数 /// public int GetInt(string key, int defaultValue = 0) { if (_intCache.TryGetValue(key, out int index) && index < _intValues.Count) { return _intValues[index]; } return defaultValue; } /// /// 获取 float 参数 /// public float GetFloat(string key, float defaultValue = 0f) { if (_floatCache.TryGetValue(key, out int index) && index < _floatValues.Count) { return _floatValues[index]; } return defaultValue; } /// /// 获取 Vector3 参数 /// public Vector3 GetVector3(string key, Vector3 defaultValue = default) { if (_vector3Cache.TryGetValue(key, out int index) && index < _vector3Values.Count) { return _vector3Values[index]; } return defaultValue; } /// /// 获取 Quaternion 参数 /// public Quaternion GetQuaternion(string key, Quaternion defaultValue = default) { if (_quaternionCache.TryGetValue(key, out int index) && index < _quaternionValues.Count) { return _quaternionValues[index]; } return defaultValue; } /// /// 获取 bool 参数 /// public bool GetBool(string key, bool defaultValue = false) { if (_intCache.TryGetValue(key, out int index) && index < _intValues.Count) { return _intValues[index] == 1; } return defaultValue; } /// /// 是否包含某个参数 /// public bool HasKey(string key) { return _intCache.ContainsKey(key) || _floatCache.ContainsKey(key) || _vector3Cache.ContainsKey(key) || _quaternionCache.ContainsKey(key); } /// /// 复制当前参数 /// public StateEnterParams Clone() { var copy = new StateEnterParams { _keys = new List(_keys), _intValues = new List(_intValues), _floatValues = new List(_floatValues), _vector3Values = new List(_vector3Values), _quaternionValues = new List(_quaternionValues) }; copy.InitializeCaches(); return copy; } } }