using System; using System.Collections.Generic; using UnityEngine; using XGame; using XGame.Database; using XGame.Framework.Data; namespace FL.Data { public class PartnersData : DataSingleton, IDisposable { /// /// /// public const byte GoneUpLimit = 6; /// /// 每个星级(黄|紫|红)最多拥有5个星星数量 /// public const int MaxStarNum = 5; /// /// 最大星星阶级(颜色) /// public const int MaxStarStep = 8; //private Dictionary _goneUpMap; ///// ///// 已上阵的随从->位置 ///// key:随从的uid ///// value:位置索引(0-5) ///// //public Dictionary GoneUpMap => _goneUpMap ??= new Dictionary(); //private Dictionary _partnersMap; ///// ///// 拥有的所有随从 ///// //public Dictionary PartnersMap => // _partnersMap ??= new Dictionary(); /// /// 剑的偏移值,只记录左边的,右边对称获取 /// 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 圣兵上阵 新 /// /// 已上阵的随从->位置 /// key:随从的tableId /// value:位置索引(0-5) /// 没有上阵为-1 /// public Dictionary UpSbPosMap { get; private set; } = new(); /// /// key:uid /// value:位置 /// public Dictionary UpSbUIDPosMap { get; private set; } = new(); /// /// key:位置 /// value:uid /// public Dictionary 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); } } } /// /// 获取已上阵的随从->位置 /// key:随从的id /// value:位置索引(0-5) /// 没有上阵返回为-1 /// /// 圣兵id public int GetPartnerUpPosById(int id) { return UpSbPosMap.GetValueOrDefault(id, -1); } /// /// 获取已上阵的随从->位置 /// key:随从的UID /// value:位置索引(0-5) /// 没有上阵返回为-1 /// /// 圣兵uid public int GetPartnerUpPosByUID(long uid) { return UpSbUIDPosMap.GetValueOrDefault(uid, -1); } #endregion #region 圣兵库 private Dictionary _partnerMap = new(); private Dictionary _partnerUIDMap = new(); /// /// 圣兵库列表 /// public List PartnerList { get { var list = new List(); 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; } /// /// 获取 /// /// /// 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 JibanMap = new Dictionary(); 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 /// /// 圣兵待下阵特殊状态(圣兵主界面中) /// 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; } /// /// 是否达到激活上限 /// 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(); } } } }