PartnersData.cs 9.9 KB

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