PartnersData.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using XGame;
  5. using XGame.Database;
  6. using XGame.Framework.Data;
  7. namespace FL.Data
  8. {
  9. public class PartnersData : DataSingleton<PartnersData>, IDisposable
  10. {
  11. /// <summary>
  12. ///
  13. /// </summary>
  14. public const byte GoneUpLimit = 6;
  15. /// <summary>
  16. /// 每个星级(黄|紫|红)最多拥有5个星星数量
  17. /// </summary>
  18. public const int MaxStarNum = 5;
  19. /// <summary>
  20. /// 最大星星阶级(颜色)
  21. /// </summary>
  22. public const int MaxStarStep = 8;
  23. //private Dictionary<long, int> _goneUpMap;
  24. ///// <summary>
  25. ///// 已上阵的随从->位置
  26. ///// key:随从的uid
  27. ///// value:位置索引(0-5)
  28. ///// </summary>
  29. //public Dictionary<long, int> GoneUpMap => _goneUpMap ??= new Dictionary<long, int>();
  30. //private Dictionary<long, PartnerAttributes> _partnersMap;
  31. ///// <summary>
  32. ///// 拥有的所有随从
  33. ///// </summary>
  34. //public Dictionary<long, PartnerAttributes> PartnersMap =>
  35. // _partnersMap ??= new Dictionary<long, PartnerAttributes>();
  36. /// <summary>
  37. /// 剑的偏移值,只记录左边的,右边对称获取
  38. /// </summary>
  39. private Vector3[] _partnerOffsets = new Vector3[]
  40. {
  41. new Vector3(-4.5f, 1.5f, 0),
  42. new Vector3(-3.5f, 0.6f, 0),
  43. new Vector3(-2.5f, -0.3f, 0),
  44. };
  45. #region 圣兵上阵 新
  46. /// <summary>
  47. /// 已上阵的随从->位置
  48. /// key:随从的tableId
  49. /// value:位置索引(0-5)
  50. /// 没有上阵为-1
  51. /// </summary>
  52. public Dictionary<int, int> UpSbPosMap { get; private set; } = new();
  53. /// <summary>
  54. /// key:uid
  55. /// value:位置
  56. /// </summary>
  57. public Dictionary<long, int> UpSbUIDPosMap { get; private set; } = new();
  58. /// <summary>
  59. /// key:位置
  60. /// value:uid
  61. /// </summary>
  62. public Dictionary<int, int> UpSbMap { get; private set; } = new();
  63. public void ClearSbUpMap()
  64. {
  65. UpSbMap.Clear();
  66. }
  67. public void UpdateUpSbPosMap()
  68. {
  69. UpSbUIDPosMap.Clear();
  70. UpSbPosMap.Clear();
  71. foreach (var pair in UpSbMap)
  72. {
  73. if (pair.Value > 0)
  74. {
  75. var attr = GetPartnerAttrByTableId(pair.Value);
  76. UpSbPosMap.Add(attr.TableId, pair.Key);
  77. UpSbUIDPosMap.Add(attr.UID, pair.Key);
  78. }
  79. }
  80. }
  81. /// <summary>
  82. /// 获取已上阵的随从->位置
  83. /// key:随从的id
  84. /// value:位置索引(0-5)
  85. /// 没有上阵返回为-1
  86. /// </summary>
  87. /// <param name="id">圣兵id</param>
  88. public int GetPartnerUpPosById(int id)
  89. {
  90. return UpSbPosMap.GetValueOrDefault(id, -1);
  91. }
  92. /// <summary>
  93. /// 获取已上阵的随从->位置
  94. /// key:随从的UID
  95. /// value:位置索引(0-5)
  96. /// 没有上阵返回为-1
  97. /// </summary>
  98. /// <param name="uid">圣兵uid</param>
  99. public int GetPartnerUpPosByUID(long uid)
  100. {
  101. return UpSbUIDPosMap.GetValueOrDefault(uid, -1);
  102. }
  103. #endregion
  104. #region 圣兵库
  105. private Dictionary<int, PartnerAttributes> _partnerMap = new();
  106. private Dictionary<long, PartnerAttributes> _partnerUIDMap = new();
  107. /// <summary>
  108. /// 圣兵库列表
  109. /// </summary>
  110. public List<PartnerAttributes> PartnerList
  111. {
  112. get
  113. {
  114. var list = new List<PartnerAttributes>();
  115. foreach (var attr in _partnerMap.Values)
  116. {
  117. list.Add(attr);
  118. }
  119. return list;
  120. }
  121. }
  122. public PartnerAttributes GetPartnerAttrByTableId(int id)
  123. {
  124. return _partnerMap.GetValueOrDefault(id);
  125. }
  126. public bool TryGetPartnerAttrByTableId(int id, out PartnerAttributes attr)
  127. {
  128. return _partnerMap.TryGetValue(id, out attr);
  129. }
  130. public PartnerAttributes GetPartnerAttrByUID(long uid)
  131. {
  132. return _partnerUIDMap.GetValueOrDefault(uid);
  133. }
  134. public bool TryGetPartnerAttrByUID(long uid, out PartnerAttributes attr)
  135. {
  136. return _partnerUIDMap.TryGetValue(uid, out attr);
  137. }
  138. public void AddPartnerAttr(PartnerAttributes attr)
  139. {
  140. _partnerMap.Add(attr.TableId, attr);
  141. _partnerUIDMap.Add(attr.UID, attr);
  142. }
  143. public PartnerAttributes CreatePartnerAttr(
  144. int sbId,
  145. int level,
  146. int starLv,
  147. int mingwenId,
  148. int status
  149. )
  150. {
  151. var table = PartnerTableRepo.Get(sbId);
  152. if (table == null)
  153. {
  154. Log.Debug($"圣兵的id表内不存在${sbId}");
  155. }
  156. var size = table != null ? table.Size * 0.5f : 0;
  157. var skillId = GetSkillId(starLv, table?.Skill);
  158. var attr = new PartnerAttributes()
  159. {
  160. UID = UIDDefine.New(),
  161. TableId = sbId,
  162. Level = level,
  163. RisingStarLv = starLv,
  164. EpigraphId = mingwenId,
  165. Status = status,
  166. SkillId = skillId,
  167. Radius = size,
  168. };
  169. attr.Attr.Hp = 500;
  170. attr.Attr.HpLimit = 500;
  171. attr.Attr.MoveSpeed = 5;
  172. attr.Attr.Attack = UnityEngine.Random.Range(5, 10);
  173. return attr;
  174. }
  175. /// <summary>
  176. /// 获取
  177. /// </summary>
  178. /// <param name="starLv"></param>
  179. /// <param name="skillIdList"></param>
  180. public int GetSkillId(int starLv, int[] skillIdList)
  181. {
  182. if (skillIdList == null)
  183. return 0;
  184. if (skillIdList?.Length > 1)
  185. {
  186. int count = skillIdList.Length - 1;
  187. for (int i = count; i >= 0; i -= 2)
  188. {
  189. //XGame.Log.Info($"索引:{i}的技能id:{skillIdList[i]},需要的星级:{skillIdList[i-1]}");
  190. if (starLv >= skillIdList[i - 1])
  191. {
  192. return skillIdList[i];
  193. }
  194. }
  195. }
  196. return skillIdList[0];
  197. }
  198. public void ClearPartnerMap()
  199. {
  200. foreach (var partner in _partnerMap.Values)
  201. {
  202. (partner as IDisposable).Dispose();
  203. }
  204. _partnerMap.Clear();
  205. _partnerUIDMap.Clear();
  206. }
  207. #endregion
  208. #region 羁绊
  209. public readonly Dictionary<int, bool> JibanMap = new Dictionary<int, bool>();
  210. public void ClearJibanActive()
  211. {
  212. JibanMap.Clear();
  213. }
  214. public void SetJibanActiveState(int jibanId, int level)
  215. {
  216. var id = 100000 + jibanId * 1000 + level;
  217. if (JibanMap.ContainsKey(jibanId))
  218. {
  219. JibanMap.Remove(id);
  220. }
  221. JibanMap.Add(id, true);
  222. }
  223. public bool GetJibaIsActive(int jibanId, int level)
  224. {
  225. var id = 100000 + jibanId * 1000 + level;
  226. return JibanMap.GetValueOrDefault(id, false);
  227. }
  228. #endregion
  229. /// <summary>
  230. /// 圣兵待下阵特殊状态(圣兵主界面中)
  231. /// </summary>
  232. public int UpModeSelect = 0;
  233. public Vector3 GetPartnerOffset(long partnerUID)
  234. {
  235. var index = GetPartnerUpPosByUID(partnerUID);
  236. if (index == -1)
  237. {
  238. Log.Debug($"上阵UID不存在:{partnerUID}");
  239. index = 0;
  240. }
  241. var tmpIdx = index % 3;
  242. tmpIdx = index >= 3 ? 2 - tmpIdx : tmpIdx;
  243. var offset = _partnerOffsets[tmpIdx];
  244. if (index >= 3)
  245. {
  246. offset.x *= -1;
  247. }
  248. //XGame.Log.Info($"PartnerOffset UID:{partnerUID} index:{index} offset:{tmpIdx}");
  249. return offset;
  250. }
  251. /// <summary>
  252. /// 是否达到激活上限
  253. /// </summary>
  254. public bool IsReachedLimit => UpSbPosMap.Count == GoneUpLimit;
  255. void IDisposable.Dispose()
  256. {
  257. UpSbMap?.Clear();
  258. UpSbPosMap?.Clear();
  259. UpSbUIDPosMap?.Clear();
  260. if (_partnerMap != null)
  261. {
  262. foreach (var val in _partnerMap.Values)
  263. {
  264. (val as IDisposable)?.Dispose();
  265. }
  266. _partnerMap.Clear();
  267. }
  268. }
  269. }
  270. }