123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using XGame;
- using XGame.Database;
- using XGame.Framework.Data;
- namespace FL.Data
- {
- public class PartnersData : DataSingleton<PartnersData>, IDisposable
- {
- /// <summary>
- ///
- /// </summary>
- public const byte GoneUpLimit = 6;
- /// <summary>
- /// 每个星级(黄|紫|红)最多拥有5个星星数量
- /// </summary>
- public const int MaxStarNum = 5;
- /// <summary>
- /// 最大星星阶级(颜色)
- /// </summary>
- public const int MaxStarStep = 8;
- //private Dictionary<long, int> _goneUpMap;
- ///// <summary>
- ///// 已上阵的随从->位置
- ///// key:随从的uid
- ///// value:位置索引(0-5)
- ///// </summary>
- //public Dictionary<long, int> GoneUpMap => _goneUpMap ??= new Dictionary<long, int>();
- //private Dictionary<long, PartnerAttributes> _partnersMap;
- ///// <summary>
- ///// 拥有的所有随从
- ///// </summary>
- //public Dictionary<long, PartnerAttributes> PartnersMap =>
- // _partnersMap ??= new Dictionary<long, PartnerAttributes>();
- /// <summary>
- /// 剑的偏移值,只记录左边的,右边对称获取
- /// </summary>
- private Vector3[] _partnerOffsets = new Vector3[]
- {
- new Vector3(-4.5f, 1.5f, 0),
- new Vector3(-3.5f, 0.6f, 0),
- new Vector3(-2.5f, -0.3f, 0),
- };
- #region 圣兵上阵 新
- /// <summary>
- /// 已上阵的随从->位置
- /// key:随从的tableId
- /// value:位置索引(0-5)
- /// 没有上阵为-1
- /// </summary>
- public Dictionary<int, int> UpSbPosMap { get; private set; } = new();
- /// <summary>
- /// key:uid
- /// value:位置
- /// </summary>
- public Dictionary<long, int> UpSbUIDPosMap { get; private set; } = new();
- /// <summary>
- /// key:位置
- /// value:uid
- /// </summary>
- public Dictionary<int, int> UpSbMap { get; private set; } = new();
- public void ClearSbUpMap()
- {
- UpSbMap.Clear();
- }
- public void UpdateUpSbPosMap()
- {
- UpSbUIDPosMap.Clear();
- UpSbPosMap.Clear();
- foreach (var pair in UpSbMap)
- {
- if (pair.Value > 0)
- {
- var attr = GetPartnerAttrByTableId(pair.Value);
- UpSbPosMap.Add(attr.TableId, pair.Key);
- UpSbUIDPosMap.Add(attr.UID, pair.Key);
- }
- }
- }
- /// <summary>
- /// 获取已上阵的随从->位置
- /// key:随从的id
- /// value:位置索引(0-5)
- /// 没有上阵返回为-1
- /// </summary>
- /// <param name="id">圣兵id</param>
- public int GetPartnerUpPosById(int id)
- {
- return UpSbPosMap.GetValueOrDefault(id, -1);
- }
- /// <summary>
- /// 获取已上阵的随从->位置
- /// key:随从的UID
- /// value:位置索引(0-5)
- /// 没有上阵返回为-1
- /// </summary>
- /// <param name="uid">圣兵uid</param>
- public int GetPartnerUpPosByUID(long uid)
- {
- return UpSbUIDPosMap.GetValueOrDefault(uid, -1);
- }
- #endregion
- #region 圣兵库
- private Dictionary<int, PartnerAttributes> _partnerMap = new();
- private Dictionary<long, PartnerAttributes> _partnerUIDMap = new();
- /// <summary>
- /// 圣兵库列表
- /// </summary>
- public List<PartnerAttributes> PartnerList
- {
- get
- {
- var list = new List<PartnerAttributes>();
- foreach (var attr in _partnerMap.Values)
- {
- list.Add(attr);
- }
- return list;
- }
- }
- public PartnerAttributes GetPartnerAttrByTableId(int id)
- {
- return _partnerMap.GetValueOrDefault(id);
- }
- public bool TryGetPartnerAttrByTableId(int id, out PartnerAttributes attr)
- {
- return _partnerMap.TryGetValue(id, out attr);
- }
- public PartnerAttributes GetPartnerAttrByUID(long uid)
- {
- return _partnerUIDMap.GetValueOrDefault(uid);
- }
- public bool TryGetPartnerAttrByUID(long uid, out PartnerAttributes attr)
- {
- return _partnerUIDMap.TryGetValue(uid, out attr);
- }
- public void AddPartnerAttr(PartnerAttributes attr)
- {
- _partnerMap.Add(attr.TableId, attr);
- _partnerUIDMap.Add(attr.UID, attr);
- }
- public PartnerAttributes CreatePartnerAttr(
- int sbId,
- int level,
- int starLv,
- int mingwenId,
- int status
- )
- {
- var table = PartnerTableRepo.Get(sbId);
- if (table == null)
- {
- Log.Debug($"圣兵的id表内不存在${sbId}");
- }
- var size = table != null ? table.Size * 0.5f : 0;
- var skillId = GetSkillId(starLv, table?.Skill);
- var attr = new PartnerAttributes()
- {
- UID = UIDDefine.New(),
- TableId = sbId,
- Level = level,
- RisingStarLv = starLv,
- EpigraphId = mingwenId,
- Status = status,
- SkillId = skillId,
- Radius = size,
- };
- attr.Attr.Hp = 500;
- attr.Attr.HpLimit = 500;
- attr.Attr.MoveSpeed = 5;
- attr.Attr.Attack = UnityEngine.Random.Range(5, 10);
- return attr;
- }
- /// <summary>
- /// 获取
- /// </summary>
- /// <param name="starLv"></param>
- /// <param name="skillIdList"></param>
- public int GetSkillId(int starLv, int[] skillIdList)
- {
- if (skillIdList == null)
- return 0;
- if (skillIdList?.Length > 1)
- {
- int count = skillIdList.Length - 1;
- for (int i = count; i >= 0; i -= 2)
- {
- //XGame.Log.Info($"索引:{i}的技能id:{skillIdList[i]},需要的星级:{skillIdList[i-1]}");
- if (starLv >= skillIdList[i - 1])
- {
- return skillIdList[i];
- }
- }
- }
- return skillIdList[0];
- }
- public void ClearPartnerMap()
- {
- foreach (var partner in _partnerMap.Values)
- {
- (partner as IDisposable).Dispose();
- }
- _partnerMap.Clear();
- _partnerUIDMap.Clear();
- }
- #endregion
- #region 羁绊
- public readonly Dictionary<int, bool> JibanMap = new Dictionary<int, bool>();
- public void ClearJibanActive()
- {
- JibanMap.Clear();
- }
- public void SetJibanActiveState(int jibanId, int level)
- {
- var id = 100000 + jibanId * 1000 + level;
- if (JibanMap.ContainsKey(jibanId))
- {
- JibanMap.Remove(id);
- }
- JibanMap.Add(id, true);
- }
- public bool GetJibaIsActive(int jibanId, int level)
- {
- var id = 100000 + jibanId * 1000 + level;
- return JibanMap.GetValueOrDefault(id, false);
- }
- #endregion
- /// <summary>
- /// 圣兵待下阵特殊状态(圣兵主界面中)
- /// </summary>
- public int UpModeSelect = 0;
- public Vector3 GetPartnerOffset(long partnerUID)
- {
- var index = GetPartnerUpPosByUID(partnerUID);
- if (index == -1)
- {
- Log.Debug($"上阵UID不存在:{partnerUID}");
- index = 0;
- }
- var tmpIdx = index % 3;
- tmpIdx = index >= 3 ? 2 - tmpIdx : tmpIdx;
- var offset = _partnerOffsets[tmpIdx];
- if (index >= 3)
- {
- offset.x *= -1;
- }
- //XGame.Log.Info($"PartnerOffset UID:{partnerUID} index:{index} offset:{tmpIdx}");
- return offset;
- }
- /// <summary>
- /// 是否达到激活上限
- /// </summary>
- public bool IsReachedLimit => UpSbPosMap.Count == GoneUpLimit;
- void IDisposable.Dispose()
- {
- UpSbMap?.Clear();
- UpSbPosMap?.Clear();
- UpSbUIDPosMap?.Clear();
- if (_partnerMap != null)
- {
- foreach (var val in _partnerMap.Values)
- {
- (val as IDisposable)?.Dispose();
- }
- _partnerMap.Clear();
- }
- }
- }
- }
|