PartnersData.cs 9.8 KB

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