Files
Fishing2/Assets/Scripts/Fishing/New/Data/StateEnterParams.cs
Bob.Song cf2e374998 修改
2026-03-10 16:03:38 +08:00

239 lines
6.8 KiB
C#

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