CombatCalculateTool.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. using System;
  2. using System.Collections.Generic;
  3. using CombatLibrary.CombatLibrary.CombatCore.CustomizeTimeLogic.FxLogic;
  4. using Common.Utility.CombatEvent;
  5. using Excel2Json;
  6. using Fort23.Core;
  7. using Fort23.UTool;
  8. using GameLogic.Combat.Hero;
  9. using GameLogic.Combat.Skill;
  10. using GameLogic.Player;
  11. using UnityEngine;
  12. using Utility;
  13. using UTool.CustomizeTimeLogic.FxLogic.TimeLineEventinterface;
  14. using Random = System.Random;
  15. namespace GameLogic.Combat.CombatTool
  16. {
  17. public class CombatCalculateTool : Singleton<CombatCalculateTool>
  18. {
  19. public Random Random = new Random();
  20. static readonly WuXingType[] Symbiosis = new WuXingType[6]
  21. {
  22. WuXingType.Water, // 金生水
  23. WuXingType.Fire, // 木生火
  24. WuXingType.Wood, // 水生木
  25. WuXingType.Earth, // 火生土
  26. WuXingType.Gold, // 土生金
  27. WuXingType.Null
  28. };
  29. // 相克关系表(用位表示)
  30. static readonly WuXingType[] Restrain = new WuXingType[6]
  31. {
  32. WuXingType.Wood, // 金克木
  33. WuXingType.Earth, // 木克土
  34. WuXingType.Fire, // 水克火
  35. WuXingType.Gold, // 火克金
  36. WuXingType.Water, // 土克水
  37. WuXingType.Null
  38. };
  39. public CombatCalculateTool()
  40. {
  41. Random = new Random(System.DateTime.Now.Millisecond);
  42. }
  43. public int GetOdd()
  44. {
  45. return Random.Next(0, 100);
  46. }
  47. public int GetOdd(int min, int max)
  48. {
  49. return Random.Next(min, max);
  50. }
  51. public long GetVlaueRatioForLong(long value, float ration)
  52. {
  53. long v = (value * (long)(ration * 100)) / 10000;
  54. return v;
  55. }
  56. public float GetVlaueRatioForFloat(float value, float ration)
  57. {
  58. float v = (value * ration) / 100f;
  59. return v;
  60. }
  61. public int GetVlaueRatioForInt(int value, float ration)
  62. {
  63. int v = (value * (int)(ration * 100)) / 10000;
  64. return v;
  65. }
  66. public HarmReturnInfo Harm(CombatHeroEntity source, CombatHeroEntity target, long att, AttType attType,
  67. TriggerData triggerData, WuXingType WuXingType, HarmType harmType = HarmType.Null)
  68. {
  69. return Harm(source, target.GetMainHotPoin<CombatHeroHitPoint>(), att,
  70. attType, triggerData, WuXingType, harmType);
  71. }
  72. /// <summary>
  73. /// 造成伤害
  74. /// </summary>n
  75. /// <param name="source">攻击方</param>
  76. /// <param name="target">被攻击方</param>
  77. /// <param name="att">伤害值</param>
  78. public HarmReturnInfo Harm(CombatHeroEntity source, CombatHeroHitPoint target, long att,
  79. AttType attType, TriggerData triggerData, WuXingType WuXingType,
  80. HarmType harmType = HarmType.Default)
  81. {
  82. HarmReturnInfo harmReturnInfo = CObjectPool.Instance.Fetch<HarmReturnInfo>();
  83. harmReturnInfo.source = source;
  84. harmReturnInfo.target = target;
  85. harmReturnInfo.att = att;
  86. harmReturnInfo.attType = attType;
  87. harmReturnInfo.WuXingType = WuXingType;
  88. harmReturnInfo.harmType = harmType;
  89. harmReturnInfo.triggerData = triggerData;
  90. if (target.combatHeroEntity.isDie)
  91. {
  92. return harmReturnInfo;
  93. }
  94. if (CombatController.currActiveCombat.IsGameOver)
  95. {
  96. return harmReturnInfo;
  97. }
  98. float wuxing = source.CurrCombatHeroInfo.GetWuXingShuXing(WuXingType);
  99. // int index = GeWuXingTypeIndex(WuXingType);
  100. // WuXingType kzWuXing = Restrain[index];
  101. // float direnWuXing = target.combatHeroEntity.CurrCombatHeroInfo.GetWuXingShuXing(kzWuXing);
  102. att += GetVlaueRatioForLong(att, wuxing);
  103. float def =
  104. (target.combatHeroEntity.CurrCombatHeroInfo.defense.Value * 100.0f / source.CurrCombatHeroInfo.k);
  105. int p1_id = (int)def;
  106. p1_id = Math.Min(100, Math.Max(1, p1_id));
  107. MitigationParaConfig mitigationParaConfig = ConfigComponent.Instance.Get<MitigationParaConfig>(p1_id);
  108. float p1 = (def * mitigationParaConfig.mitigationPara) / 100f;
  109. att = GetVlaueRatioForLong(att, 100 - p1);
  110. harmReturnInfo.att = att;
  111. StartInjuredEventData startInjuredEventData = StartInjuredEventData.Create();
  112. startInjuredEventData.HarmReturnInfo = harmReturnInfo;
  113. CombatEventManager.Instance.Dispatch(CombatEventType.StartInjured, startInjuredEventData);
  114. target.combatHeroEntity.This<CombatHeroEntity>().HeroHurt(harmReturnInfo);
  115. return harmReturnInfo;
  116. }
  117. public HarmReturnInfo TrueHarm(CombatHeroEntity source, CombatHeroHitPoint target, long att,
  118. AttType attType, TriggerData triggerData,
  119. HarmType harmType = HarmType.Default)
  120. {
  121. HarmReturnInfo harmReturnInfo = CObjectPool.Instance.Fetch<HarmReturnInfo>();
  122. harmReturnInfo.source = source;
  123. harmReturnInfo.target = target;
  124. harmReturnInfo.att = att;
  125. harmReturnInfo.attType = attType;
  126. harmReturnInfo.harmType = harmType;
  127. harmReturnInfo.triggerData = triggerData;
  128. if (target.combatHeroEntity.isDie)
  129. {
  130. return harmReturnInfo;
  131. }
  132. target.combatHeroEntity.This<CombatHeroEntity>().HeroHurt(harmReturnInfo);
  133. return harmReturnInfo;
  134. }
  135. public HarmReturnInfo Recover(CombatHeroEntity source, CombatHeroHitPoint target, long att,
  136. AttType attType, HarmType harmType, TriggerData triggerData)
  137. {
  138. HarmReturnInfo harmReturnInfo = new HarmReturnInfo();
  139. harmReturnInfo.source = source;
  140. harmReturnInfo.target = target;
  141. harmReturnInfo.att = att;
  142. harmReturnInfo.attType = attType;
  143. harmReturnInfo.harmType = harmType;
  144. harmReturnInfo.triggerData = triggerData;
  145. if (target.combatHeroEntity.isDie)
  146. {
  147. return harmReturnInfo;
  148. }
  149. target.combatHeroEntity.This<CombatHeroEntity>().Recover(harmReturnInfo);
  150. return harmReturnInfo;
  151. }
  152. public ILifetCycleHitPoint[] GetMinHpHero(ILifetCycleHitPoint[] allLifetCycleHitPoints, int count)
  153. {
  154. if (allLifetCycleHitPoints == null)
  155. {
  156. return null;
  157. }
  158. BetterList<ILifetCycleHitPoint> findHero = new BetterList<ILifetCycleHitPoint>();
  159. findHero.AddRange(allLifetCycleHitPoints);
  160. int currCount = Math.Min(allLifetCycleHitPoints.Length, count);
  161. ILifetCycleHitPoint[] minHpHero = new ILifetCycleHitPoint[currCount];
  162. for (int k = 0; k < currCount; k++)
  163. {
  164. CombatHeroEntity lifetCycleHitPoint = null;
  165. int index = 0;
  166. if (findHero.Count <= 0)
  167. {
  168. return minHpHero;
  169. }
  170. lifetCycleHitPoint = findHero[0].IfLifeCycle.This<CombatHeroEntity>();
  171. for (int j = 0; j < findHero.Count; j++)
  172. {
  173. CombatHeroEntity lifetCycleHitPoint2 =
  174. findHero[j].IfLifeCycle.This<CombatHeroEntity>();
  175. if (lifetCycleHitPoint2.HpBl < lifetCycleHitPoint.HpBl)
  176. {
  177. lifetCycleHitPoint = lifetCycleHitPoint2;
  178. index = j;
  179. }
  180. }
  181. ILifetCycleHitPoint currFindHitPoint = lifetCycleHitPoint.GetMainHotPoin<ILifetCycleHitPoint>(true);
  182. minHpHero[k] = currFindHitPoint;
  183. findHero.RemoveAt(index);
  184. }
  185. return minHpHero;
  186. }
  187. private int GeWuXingTypeIndex(WuXingType e)
  188. {
  189. switch (e)
  190. {
  191. case WuXingType.Gold: return 0;
  192. case WuXingType.Wood: return 1;
  193. case WuXingType.Water: return 2;
  194. case WuXingType.Fire: return 3;
  195. case WuXingType.Earth: return 4;
  196. default: return 5; // 无效元素
  197. }
  198. }
  199. public int GetRestrained(WuXingType WuXingType,WuXingType targetWuXingType)
  200. {
  201. int c = 0;
  202. if (WuXingType.HasFlag(WuXingType.Gold))
  203. {
  204. if (targetWuXingType.HasFlag(WuXingType.Wood))
  205. {
  206. c++;
  207. }
  208. }
  209. if (WuXingType.HasFlag(WuXingType.Wood))
  210. {
  211. if (targetWuXingType.HasFlag(WuXingType.Earth))
  212. {
  213. c++;
  214. }
  215. }
  216. if (WuXingType.HasFlag(WuXingType.Water))
  217. {
  218. if (targetWuXingType.HasFlag(WuXingType.Fire))
  219. {
  220. c++;
  221. }
  222. }
  223. if (WuXingType.HasFlag(WuXingType.Fire))
  224. {
  225. if (targetWuXingType.HasFlag(WuXingType.Gold))
  226. {
  227. c++;
  228. }
  229. }
  230. if (WuXingType.HasFlag(WuXingType.Earth))
  231. {
  232. if (targetWuXingType.HasFlag(WuXingType.Wood))
  233. {
  234. c++;
  235. }
  236. }
  237. return c;
  238. }
  239. public void FaBaoPengZhuang(CombatMagicWeaponEntity a, CombatMagicWeaponEntity b)
  240. {
  241. CombatHeroEntity heroEntityA= a.MagicWeaponControl.combatHeroEntity;
  242. CombatHeroEntity heroEntityB = b.MagicWeaponControl.combatHeroEntity;
  243. int myRestrained = GetRestrained(a.WuXingType, b.WuXingType);
  244. int targetRestrained = GetRestrained(b.WuXingType,a.WuXingType);
  245. float lg_a = heroEntityA.CurrCombatHeroInfo.GetWuXingShuXing(a.WuXingType);
  246. float lg_b = heroEntityB.CurrCombatHeroInfo.GetWuXingShuXing(b.WuXingType);
  247. int c = myRestrained - targetRestrained;
  248. long myHp = (long)(a.HpBl);
  249. long targetHp = (long)(b.HpBl);
  250. float p2 = 100;
  251. if (c < 0) //a被压制
  252. {
  253. p2 = Mathf.Max(100, Mathf.Min(200, 30 + lg_b - lg_a));
  254. targetHp = (long)(targetHp * p2);
  255. }
  256. else if (c > 0)
  257. {
  258. p2 = Mathf.Max(100, Mathf.Min(200, 30 + lg_a - lg_b));
  259. myHp = (long)(myHp * p2);
  260. }
  261. if (myHp > targetHp)
  262. {
  263. myHp -= targetHp;
  264. a.HpBl = myHp;
  265. }
  266. else if (myHp < targetHp)
  267. {
  268. targetHp -= myHp;
  269. b.HpBl = targetHp;
  270. }
  271. else if (myHp == targetHp)
  272. {
  273. a.HpBl = 0;
  274. b.HpBl = 0;
  275. }
  276. }
  277. public void GongFaPengZhuang(SkillFeaturesData a, SkillFeaturesData b, CombatHeroEntity heroEntityA,
  278. CombatHeroEntity heroEntityB)
  279. {
  280. int myRestrained = GetRestrained(a.WuXingType, b.WuXingType);
  281. int targetRestrained = GetRestrained(b.WuXingType,a.WuXingType);
  282. float lg_a = heroEntityA.CurrCombatHeroInfo.GetWuXingShuXing(a.WuXingType);
  283. float lg_b = heroEntityB.CurrCombatHeroInfo.GetWuXingShuXing(b.WuXingType);
  284. int c = myRestrained - targetRestrained;
  285. long myHp = (long)(a.hp);
  286. long targetHp = (long)(b.hp);
  287. float p2 = 100;
  288. if (c < 0) //a被压制
  289. {
  290. p2 = Mathf.Max(100, Mathf.Min(200, 30 + lg_b - lg_a));
  291. targetHp = (long)(targetHp * p2);
  292. }
  293. else if (c > 0)
  294. {
  295. p2 = Mathf.Max(100, Mathf.Min(200, 30 + lg_a - lg_b));
  296. myHp = (long)(myHp * p2);
  297. }
  298. if (myHp > targetHp)
  299. {
  300. myHp -= targetHp;
  301. a.hp = myHp;
  302. }
  303. else if (myHp < targetHp)
  304. {
  305. targetHp -= myHp;
  306. b.hp = targetHp;
  307. }
  308. else if (myHp == targetHp)
  309. {
  310. a.hp = 0;
  311. b.hp = 0;
  312. }
  313. }
  314. /// <summary>
  315. /// 是否相生
  316. /// </summary>
  317. /// <returns></returns>
  318. public bool IsSymbiosis(WuXingType a, WuXingType b)
  319. {
  320. int index = GeWuXingTypeIndex(a);
  321. if (index < 0)
  322. {
  323. return false;
  324. }
  325. return (b & Symbiosis[index]) != 0;
  326. }
  327. /// <summary>
  328. /// 是否相生
  329. /// </summary>
  330. /// <returns></returns>
  331. public bool IsRestrain(WuXingType a, WuXingType b)
  332. {
  333. int index = GeWuXingTypeIndex(a);
  334. if (index < 0)
  335. {
  336. return false;
  337. }
  338. return (b & Restrain[index]) != 0;
  339. }
  340. }
  341. }