123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- using System;
- using System.Collections.Generic;
- using FL.Network;
- 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>
- /// 使用方案id
- /// </summary>
- public int UsePlanId { get; set; }
- /// <summary>
- /// 已上阵的随从->位置
- /// key:随从的tableId
- /// value:位置索引(0-5)
- /// 没有上阵为-1
- /// </summary>
- public Dictionary<int, int> UpPartnerMap { get; private set; } = new();
- public Dictionary<long, int> UpPartnerUIDMap { get; private set; } = new();
- /// <summary>
- /// 获取已上阵的随从->位置
- /// key:随从的id
- /// value:位置索引(0-5)
- /// 没有上阵返回为-1
- /// </summary>
- /// <param name="id">圣兵id</param>
- public int GetPartnerUpPosById(int id)
- {
- return UpPartnerMap.GetValueOrDefault(id, -1);
- }
- /// <summary>
- /// 获取已上阵的随从->位置
- /// key:随从的UID
- /// value:位置索引(0-5)
- /// 没有上阵返回为-1
- /// </summary>
- /// <param name="uid">圣兵uid</param>
- public int GetPartnerUpPosByUID(long uid)
- {
- return UpPartnerUIDMap.GetValueOrDefault(uid, -1);
- }
- /// <summary>
- /// 圣兵方案
- /// </summary>
- private Dictionary<int, PartnerPlan> _planMap = new Dictionary<int, PartnerPlan>();
- public void ClearPlanMap()
- {
- _planMap.Clear();
- }
- public List<PartnerPlan> PlanList
- {
- get
- {
- var list = new List<PartnerPlan>();
- foreach (var plan in _planMap.Values)
- {
- list.Add(plan);
- }
- return list;
- }
- }
- /// <summary>
- /// 当前使用的圣兵方案
- /// </summary>
- public PartnerPlan CurUsePlan
- {
- get { return GetPlanById(UsePlanId); }
- }
- /// <summary>
- /// 获取方案信息
- /// </summary>
- /// <param name="planId">方案id</param>
- /// <returns></returns>
- ///
- public PartnerPlan GetPlanById(int planId)
- {
- return _planMap.GetValueOrDefault(planId);
- }
- public bool TryGetPlan(int planId, out PartnerPlan plan)
- {
- return _planMap.TryGetValue(planId, out plan);
- }
- public void AddPlanMap(PartnerPlan plan)
- {
- _planMap.Add(plan.Id, plan);
- }
- /// <summary>
- /// 获取方案槽位信息
- /// </summary>
- /// <param name="planId"></param>
- /// <param name="slotId"></param>
- /// <returns></returns>
- public PartnerPlanSlot GetPlanSlot(int planId, int slotId)
- {
- return GetPlanById(planId)?.GetSlot(slotId);
- }
- #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
- /// <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 => UpPartnerMap.Count == GoneUpLimit;
- /// <summary>
- /// 获取星星的色阶
- /// </summary>
- /// <param name="starLv"></param>
- /// <returns></returns>
- public int GetStarStep(int starLevel)
- {
- int step = MaxStarStep - 1;
- for (int i = step; starLevel >= 0; i--)
- {
- if (starLevel > i * MaxStarNum)
- {
- return i + 1;
- }
- }
- return 1;
- }
- void IDisposable.Dispose()
- {
- UpPartnerMap?.Clear();
- if (_partnerMap != null)
- {
- foreach (var val in _partnerMap.Values)
- {
- (val as IDisposable)?.Dispose();
- }
- _partnerMap.Clear();
- }
- _planMap.Clear();
- }
- }
- }
|