CombatCalculateTool.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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, ISkillFeatures skillFeatures,
  68. HarmType harmType = HarmType.Null)
  69. {
  70. return Harm(source, target.GetMainHotPoin<CombatHeroHitPoint>(), att,
  71. attType, triggerData, WuXingType, skillFeatures, harmType);
  72. }
  73. /// <summary>
  74. /// 造成伤害
  75. /// </summary>n
  76. /// <param name="source">攻击方</param>
  77. /// <param name="target">被攻击方</param>
  78. /// <param name="att">伤害值</param>
  79. public HarmReturnInfo Harm(CombatHeroEntity source, CombatHeroHitPoint target, long att,
  80. AttType attType, TriggerData triggerData, WuXingType WuXingType, ISkillFeatures skillFeatures,
  81. HarmType harmType = HarmType.Default, HarmReturnInfo harmReturnInfo = null)
  82. {
  83. if (harmReturnInfo == null)
  84. {
  85. harmReturnInfo = CObjectPool.Instance.Fetch<HarmReturnInfo>();
  86. }
  87. harmReturnInfo.yuanShiAtt = att;
  88. harmReturnInfo.source = source;
  89. harmReturnInfo.target = target;
  90. harmReturnInfo.att = att;
  91. harmReturnInfo.attType = attType;
  92. harmReturnInfo.WuXingType = WuXingType;
  93. harmReturnInfo.harmType = harmType;
  94. harmReturnInfo.triggerData = triggerData;
  95. if (target.combatHeroEntity.isDie)
  96. {
  97. return harmReturnInfo;
  98. }
  99. if (CombatController.currActiveCombat.IsGameOver)
  100. {
  101. return harmReturnInfo;
  102. }
  103. float qiangDu = 100;
  104. SkillFeaturesData skillFeaturesData = skillFeatures as SkillFeaturesData;
  105. if (skillFeaturesData != null)
  106. {
  107. qiangDu = (skillFeaturesData.hp * 100) / skillFeaturesData.maxHp;
  108. }
  109. harmReturnInfo.QiangDu = qiangDu;
  110. att = GetVlaueRatioForLong(att, qiangDu);
  111. float wuxing = source.CurrCombatHeroInfo.GetWuXingShuXing(WuXingType);
  112. // int index = GeWuXingTypeIndex(WuXingType);
  113. // WuXingType kzWuXing = Restrain[index];
  114. // float direnWuXing = target.combatHeroEntity.CurrCombatHeroInfo.GetWuXingShuXing(kzWuXing);
  115. att = GetVlaueRatioForLong(att, wuxing);
  116. long targetDef = target.combatHeroEntity.CurrCombatHeroInfo.defense.Value;
  117. targetDef -= GetVlaueRatioForLong(targetDef, harmReturnInfo.ArmorPiercing);
  118. float def =
  119. (targetDef * 100.0f / source.CurrCombatHeroInfo.k);
  120. int p1_id = (int)def;
  121. p1_id = Math.Min(100, Math.Max(1, p1_id));
  122. MitigationParaConfig mitigationParaConfig = ConfigComponent.Instance.Get<MitigationParaConfig>(p1_id);
  123. float p1 = (def * mitigationParaConfig.mitigationPara) / 100f;
  124. att = GetVlaueRatioForLong(att, 100 - p1);
  125. harmReturnInfo.att = att;
  126. StartInjuredEventData startInjuredEventData = StartInjuredEventData.Create();
  127. startInjuredEventData.HarmReturnInfo = harmReturnInfo;
  128. CombatEventManager.Instance.Dispatch(CombatEventType.StartInjured, startInjuredEventData);
  129. if (harmReturnInfo.isInvalid)
  130. {
  131. return harmReturnInfo;
  132. }
  133. target.combatHeroEntity.This<CombatHeroEntity>().HeroHurt(harmReturnInfo);
  134. return harmReturnInfo;
  135. }
  136. public HarmReturnInfo TrueHarm(CombatHeroEntity source, CombatHeroHitPoint target, long att,
  137. AttType attType, TriggerData triggerData,
  138. HarmType harmType = HarmType.Default)
  139. {
  140. HarmReturnInfo harmReturnInfo = CObjectPool.Instance.Fetch<HarmReturnInfo>();
  141. harmReturnInfo.source = source;
  142. harmReturnInfo.target = target;
  143. harmReturnInfo.att = att;
  144. harmReturnInfo.attType = attType;
  145. harmReturnInfo.harmType = harmType;
  146. harmReturnInfo.triggerData = triggerData;
  147. if (target.combatHeroEntity.isDie)
  148. {
  149. return harmReturnInfo;
  150. }
  151. target.combatHeroEntity.This<CombatHeroEntity>().HeroHurt(harmReturnInfo);
  152. return harmReturnInfo;
  153. }
  154. public HarmReturnInfo Recover(CombatHeroEntity source, CombatHeroHitPoint target, long att,
  155. AttType attType, HarmType harmType, TriggerData triggerData)
  156. {
  157. harmType |= HarmType.Recover;
  158. HarmReturnInfo harmReturnInfo = new HarmReturnInfo();
  159. harmReturnInfo.source = source;
  160. harmReturnInfo.target = target;
  161. harmReturnInfo.att = att;
  162. harmReturnInfo.attType = attType;
  163. harmReturnInfo.harmType = harmType;
  164. harmReturnInfo.triggerData = triggerData;
  165. if (target.combatHeroEntity.isDie)
  166. {
  167. return harmReturnInfo;
  168. }
  169. target.combatHeroEntity.This<CombatHeroEntity>().Recover(harmReturnInfo);
  170. return harmReturnInfo;
  171. }
  172. public void ShangSHi(WuXingType wuXingType, ShowBaiscEntity target, int vInjury)
  173. {
  174. switch (wuXingType)
  175. {
  176. case WuXingType.Gold:
  177. target.CurrCombatHeroInfo.Metal_Injury += vInjury;
  178. break;
  179. case WuXingType.Wood:
  180. target.CurrCombatHeroInfo.Wood_Injury += vInjury;
  181. break;
  182. case WuXingType.Water:
  183. target.CurrCombatHeroInfo.Water_Injury += vInjury;
  184. break;
  185. case WuXingType.Fire:
  186. target.CurrCombatHeroInfo.Fire_Injury += vInjury;
  187. break;
  188. case WuXingType.Earth:
  189. target.CurrCombatHeroInfo.Earth_Injury += vInjury;
  190. break;
  191. }
  192. }
  193. public ILifetCycleHitPoint[] GetMinHpHero(ILifetCycleHitPoint[] allLifetCycleHitPoints, int count)
  194. {
  195. if (allLifetCycleHitPoints == null)
  196. {
  197. return null;
  198. }
  199. BetterList<ILifetCycleHitPoint> findHero = new BetterList<ILifetCycleHitPoint>();
  200. findHero.AddRange(allLifetCycleHitPoints);
  201. int currCount = Math.Min(allLifetCycleHitPoints.Length, count);
  202. ILifetCycleHitPoint[] minHpHero = new ILifetCycleHitPoint[currCount];
  203. for (int k = 0; k < currCount; k++)
  204. {
  205. CombatHeroEntity lifetCycleHitPoint = null;
  206. int index = 0;
  207. if (findHero.Count <= 0)
  208. {
  209. return minHpHero;
  210. }
  211. lifetCycleHitPoint = findHero[0].IfLifeCycle.This<CombatHeroEntity>();
  212. for (int j = 0; j < findHero.Count; j++)
  213. {
  214. CombatHeroEntity lifetCycleHitPoint2 =
  215. findHero[j].IfLifeCycle.This<CombatHeroEntity>();
  216. if (lifetCycleHitPoint2.HpBl < lifetCycleHitPoint.HpBl)
  217. {
  218. lifetCycleHitPoint = lifetCycleHitPoint2;
  219. index = j;
  220. }
  221. }
  222. ILifetCycleHitPoint currFindHitPoint = lifetCycleHitPoint.GetMainHotPoin<ILifetCycleHitPoint>(true);
  223. minHpHero[k] = currFindHitPoint;
  224. findHero.RemoveAt(index);
  225. }
  226. return minHpHero;
  227. }
  228. private int GeWuXingTypeIndex(WuXingType e)
  229. {
  230. switch (e)
  231. {
  232. case WuXingType.Gold: return 0;
  233. case WuXingType.Wood: return 1;
  234. case WuXingType.Water: return 2;
  235. case WuXingType.Fire: return 3;
  236. case WuXingType.Earth: return 4;
  237. default: return 5; // 无效元素
  238. }
  239. }
  240. public int GetRestrained(WuXingType WuXingType, WuXingType targetWuXingType)
  241. {
  242. int c = 0;
  243. if (WuXingType.HasFlag(WuXingType.Gold))
  244. {
  245. if (targetWuXingType.HasFlag(WuXingType.Wood))
  246. {
  247. c++;
  248. }
  249. }
  250. if (WuXingType.HasFlag(WuXingType.Wood))
  251. {
  252. if (targetWuXingType.HasFlag(WuXingType.Earth))
  253. {
  254. c++;
  255. }
  256. }
  257. if (WuXingType.HasFlag(WuXingType.Water))
  258. {
  259. if (targetWuXingType.HasFlag(WuXingType.Fire))
  260. {
  261. c++;
  262. }
  263. }
  264. if (WuXingType.HasFlag(WuXingType.Fire))
  265. {
  266. if (targetWuXingType.HasFlag(WuXingType.Gold))
  267. {
  268. c++;
  269. }
  270. }
  271. if (WuXingType.HasFlag(WuXingType.Earth))
  272. {
  273. if (targetWuXingType.HasFlag(WuXingType.Wood))
  274. {
  275. c++;
  276. }
  277. }
  278. return c;
  279. }
  280. public void FaBaoPengZhuang(CombatMagicWeaponEntity a, CombatMagicWeaponEntity b)
  281. {
  282. CombatHeroEntity heroEntityA = a.RootMagicWeaponControl.combatHeroEntity;
  283. CombatHeroEntity heroEntityB = b.RootMagicWeaponControl.combatHeroEntity;
  284. int myRestrained = GetRestrained(a.WuXingType, b.WuXingType);
  285. int targetRestrained = GetRestrained(b.WuXingType, a.WuXingType);
  286. float lg_a = heroEntityA.CurrCombatHeroInfo.GetWuXingShuXing(a.WuXingType);
  287. float lg_b = heroEntityB.CurrCombatHeroInfo.GetWuXingShuXing(b.WuXingType);
  288. int c = myRestrained - targetRestrained;
  289. long myHp = (long)(a.HpBl);
  290. long targetHp = (long)(b.HpBl);
  291. float p2 = 100;
  292. if (c < 0) //a被压制
  293. {
  294. p2 = Mathf.Max(100, Mathf.Min(200, 30 + lg_b - lg_a));
  295. targetHp = (long)(targetHp * p2);
  296. }
  297. else if (c > 0)
  298. {
  299. p2 = Mathf.Max(100, Mathf.Min(200, 30 + lg_a - lg_b));
  300. myHp = (long)(myHp * p2);
  301. }
  302. if (myHp > targetHp)
  303. {
  304. myHp -= targetHp;
  305. a.HpBl = myHp;
  306. }
  307. else if (myHp < targetHp)
  308. {
  309. targetHp -= myHp;
  310. b.HpBl = targetHp;
  311. }
  312. else if (myHp == targetHp)
  313. {
  314. a.HpBl = 0;
  315. b.HpBl = 0;
  316. }
  317. }
  318. public void GongFaPengZhuang(SkillFeaturesData a, SkillFeaturesData b, CombatHeroEntity heroEntityA,
  319. CombatHeroEntity heroEntityB)
  320. {
  321. int myRestrained = GetRestrained(a.WuXingType, b.WuXingType);
  322. int targetRestrained = GetRestrained(b.WuXingType, a.WuXingType);
  323. float lg_a = heroEntityA.CurrCombatHeroInfo.GetWuXingShuXing(a.WuXingType);
  324. float lg_b = heroEntityB.CurrCombatHeroInfo.GetWuXingShuXing(b.WuXingType);
  325. int c = myRestrained - targetRestrained;
  326. long myHp = (long)(a.hp);
  327. long targetHp = (long)(b.hp);
  328. a.pengZhuangHp = myHp;
  329. b.pengZhuangHp = targetHp;
  330. float p2 = 100;
  331. if (c < 0) //a被压制
  332. {
  333. p2 = Mathf.Max(100, Mathf.Min(200, 130 + lg_b - lg_a));
  334. targetHp = GetVlaueRatioForLong(targetHp, p2);
  335. }
  336. else if (c > 0)
  337. {
  338. p2 = Mathf.Max(100, Mathf.Min(200, 130 + lg_a - lg_b));
  339. myHp = GetVlaueRatioForLong(myHp, p2);
  340. }
  341. if (myHp > targetHp)
  342. {
  343. myHp -= targetHp;
  344. a.hp = myHp;
  345. b.hp = 0;
  346. }
  347. else if (myHp < targetHp)
  348. {
  349. targetHp -= myHp;
  350. b.hp = targetHp;
  351. a.hp = 0;
  352. }
  353. else if (myHp == targetHp)
  354. {
  355. a.hp = 0;
  356. b.hp = 0;
  357. }
  358. GongFaPengZhuangFinishEventData gongFaPengZhu = GongFaPengZhuangFinishEventData.Create();
  359. gongFaPengZhu.a = a;
  360. gongFaPengZhu.b = b;
  361. CombatEventManager.Instance.Dispatch(CombatEventType.GongFaPengZhuangFinish, gongFaPengZhu, false);
  362. CombatEventManager.Instance.Dispatch(CombatEventType.GongFaPengZhuangFinish2, gongFaPengZhu);
  363. }
  364. /// <summary>
  365. /// 是否相生
  366. /// </summary>
  367. /// <returns></returns>
  368. public bool IsSymbiosis(WuXingType a, WuXingType b)
  369. {
  370. int index = GeWuXingTypeIndex(a);
  371. if (index < 0)
  372. {
  373. return false;
  374. }
  375. return (b & Symbiosis[index]) != 0;
  376. }
  377. /// <summary>
  378. /// 是否相克制
  379. /// </summary>
  380. /// <returns></returns>
  381. public bool IsRestrain(WuXingType a, WuXingType b)
  382. {
  383. int index = GeWuXingTypeIndex(a);
  384. if (index < 0)
  385. {
  386. return false;
  387. }
  388. return (b & Restrain[index]) != 0;
  389. }
  390. public Color GetColor(WuXingType wuXingType)
  391. {
  392. switch ((int)wuXingType)
  393. {
  394. case 1:
  395. return new Color(1f, 0.98f, 0.09f);
  396. break;
  397. case 2:
  398. return new Color(0.19f, 0.51f, 1f);
  399. break;
  400. case 4:
  401. return new Color(0.17f, 1f, 0.35f);
  402. break;
  403. case 8:
  404. return new Color(1f, 0.19f, 0.04f);
  405. break;
  406. case 16:
  407. return new Color(1f, 0.65f, 0.17f);
  408. break;
  409. }
  410. return Color.white;
  411. }
  412. public Color GetColor(int wuXingType)
  413. {
  414. switch ((int)wuXingType)
  415. {
  416. case 1:
  417. return new Color(1f, 0.98f, 0.09f);
  418. break;
  419. case 2:
  420. return new Color(0.19f, 0.51f, 1f);
  421. break;
  422. case 3:
  423. return new Color(0.17f, 1f, 0.35f);
  424. break;
  425. case 4:
  426. return new Color(1f, 0.19f, 0.04f);
  427. break;
  428. case 5:
  429. return new Color(1f, 0.65f, 0.17f);
  430. break;
  431. }
  432. return Color.white;
  433. }
  434. }
  435. }