CombatCalculateTool.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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 && skillFeaturesData.maxHp > 0)
  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. switch (WuXingType)
  134. {
  135. case WuXingType.Gold:
  136. att += GetVlaueRatioForLong(att,
  137. source.CurrCombatHeroInfo.Metal_HarmAdd);
  138. break;
  139. case WuXingType.Water:
  140. att += GetVlaueRatioForLong(att,
  141. source.CurrCombatHeroInfo.Water_HarmAdd);
  142. break;
  143. case WuXingType.Fire:
  144. att += GetVlaueRatioForLong(att,
  145. source.CurrCombatHeroInfo.Fire_HarmAdd);
  146. break;
  147. case WuXingType.Earth:
  148. att += GetVlaueRatioForLong(att,
  149. source.CurrCombatHeroInfo.Earth_HarmAdd);
  150. break;
  151. case WuXingType.Wood:
  152. att += GetVlaueRatioForLong(att,
  153. source.CurrCombatHeroInfo.Wood_HarmAdd);
  154. break;
  155. }
  156. if (!harmReturnInfo.attType.HasFlag(AttType.Buff))
  157. {
  158. att += GetVlaueRatioForLong(att,
  159. source.CurrCombatHeroInfo.finallyHarmAdd -
  160. target.combatHeroEntity.CurrCombatHeroInfo.finallyHarmReduce);
  161. }
  162. else
  163. {
  164. att += GetVlaueRatioForLong(att,
  165. source.CurrCombatHeroInfo.abnormalHarmAdd);
  166. }
  167. harmReturnInfo.att = att;
  168. target.combatHeroEntity.This<CombatHeroEntity>().HeroHurt(harmReturnInfo);
  169. return harmReturnInfo;
  170. }
  171. public HarmReturnInfo TrueHarm(CombatHeroEntity source, CombatHeroHitPoint target, long att,
  172. AttType attType, TriggerData triggerData,
  173. HarmType harmType = HarmType.Default)
  174. {
  175. HarmReturnInfo harmReturnInfo = CObjectPool.Instance.Fetch<HarmReturnInfo>();
  176. harmReturnInfo.source = source;
  177. harmReturnInfo.target = target;
  178. harmReturnInfo.att = att;
  179. harmReturnInfo.attType = attType;
  180. harmReturnInfo.harmType = harmType;
  181. harmReturnInfo.triggerData = triggerData;
  182. if (target.combatHeroEntity.isDie)
  183. {
  184. return harmReturnInfo;
  185. }
  186. target.combatHeroEntity.This<CombatHeroEntity>().HeroHurt(harmReturnInfo);
  187. return harmReturnInfo;
  188. }
  189. public HarmReturnInfo Recover(CombatHeroEntity source, CombatHeroHitPoint target, long att,
  190. AttType attType, HarmType harmType, TriggerData triggerData)
  191. {
  192. harmType |= HarmType.Recover;
  193. HarmReturnInfo harmReturnInfo = new HarmReturnInfo();
  194. harmReturnInfo.source = source;
  195. harmReturnInfo.target = target;
  196. harmReturnInfo.att = att;
  197. harmReturnInfo.attType = attType;
  198. harmReturnInfo.harmType = harmType;
  199. harmReturnInfo.triggerData = triggerData;
  200. if (target.combatHeroEntity.isDie)
  201. {
  202. return harmReturnInfo;
  203. }
  204. target.combatHeroEntity.This<CombatHeroEntity>().Recover(harmReturnInfo);
  205. return harmReturnInfo;
  206. }
  207. public void ShangSHi(WuXingType wuXingType, ShowBaiscEntity target, int vInjury)
  208. {
  209. switch (wuXingType)
  210. {
  211. case WuXingType.Gold:
  212. target.CurrCombatHeroInfo.Metal_Injury += vInjury;
  213. break;
  214. case WuXingType.Wood:
  215. target.CurrCombatHeroInfo.Wood_Injury += vInjury;
  216. break;
  217. case WuXingType.Water:
  218. target.CurrCombatHeroInfo.Water_Injury += vInjury;
  219. break;
  220. case WuXingType.Fire:
  221. target.CurrCombatHeroInfo.Fire_Injury += vInjury;
  222. break;
  223. case WuXingType.Earth:
  224. target.CurrCombatHeroInfo.Earth_Injury += vInjury;
  225. break;
  226. }
  227. }
  228. public ILifetCycleHitPoint[] GetMinHpHero(ILifetCycleHitPoint[] allLifetCycleHitPoints, int count)
  229. {
  230. if (allLifetCycleHitPoints == null)
  231. {
  232. return null;
  233. }
  234. BetterList<ILifetCycleHitPoint> findHero = new BetterList<ILifetCycleHitPoint>();
  235. findHero.AddRange(allLifetCycleHitPoints);
  236. int currCount = Math.Min(allLifetCycleHitPoints.Length, count);
  237. ILifetCycleHitPoint[] minHpHero = new ILifetCycleHitPoint[currCount];
  238. for (int k = 0; k < currCount; k++)
  239. {
  240. CombatHeroEntity lifetCycleHitPoint = null;
  241. int index = 0;
  242. if (findHero.Count <= 0)
  243. {
  244. return minHpHero;
  245. }
  246. lifetCycleHitPoint = findHero[0].IfLifeCycle.This<CombatHeroEntity>();
  247. for (int j = 0; j < findHero.Count; j++)
  248. {
  249. CombatHeroEntity lifetCycleHitPoint2 =
  250. findHero[j].IfLifeCycle.This<CombatHeroEntity>();
  251. if (lifetCycleHitPoint2.HpBl < lifetCycleHitPoint.HpBl)
  252. {
  253. lifetCycleHitPoint = lifetCycleHitPoint2;
  254. index = j;
  255. }
  256. }
  257. ILifetCycleHitPoint currFindHitPoint = lifetCycleHitPoint.GetMainHotPoin<ILifetCycleHitPoint>(true);
  258. minHpHero[k] = currFindHitPoint;
  259. findHero.RemoveAt(index);
  260. }
  261. return minHpHero;
  262. }
  263. private int GeWuXingTypeIndex(WuXingType e)
  264. {
  265. switch (e)
  266. {
  267. case WuXingType.Gold: return 0;
  268. case WuXingType.Wood: return 1;
  269. case WuXingType.Water: return 2;
  270. case WuXingType.Fire: return 3;
  271. case WuXingType.Earth: return 4;
  272. default: return 5; // 无效元素
  273. }
  274. }
  275. public int GetRestrained(WuXingType WuXingType, WuXingType targetWuXingType)
  276. {
  277. int c = 0;
  278. if (WuXingType.HasFlag(WuXingType.Gold))
  279. {
  280. if (targetWuXingType.HasFlag(WuXingType.Wood))
  281. {
  282. c++;
  283. }
  284. }
  285. if (WuXingType.HasFlag(WuXingType.Wood))
  286. {
  287. if (targetWuXingType.HasFlag(WuXingType.Earth))
  288. {
  289. c++;
  290. }
  291. }
  292. if (WuXingType.HasFlag(WuXingType.Water))
  293. {
  294. if (targetWuXingType.HasFlag(WuXingType.Fire))
  295. {
  296. c++;
  297. }
  298. }
  299. if (WuXingType.HasFlag(WuXingType.Fire))
  300. {
  301. if (targetWuXingType.HasFlag(WuXingType.Gold))
  302. {
  303. c++;
  304. }
  305. }
  306. if (WuXingType.HasFlag(WuXingType.Earth))
  307. {
  308. if (targetWuXingType.HasFlag(WuXingType.Wood))
  309. {
  310. c++;
  311. }
  312. }
  313. return c;
  314. }
  315. public void FaBaoPengZhuang(CombatMagicWeaponEntity a, CombatMagicWeaponEntity b)
  316. {
  317. CombatHeroEntity heroEntityA = a.RootMagicWeaponControl.combatHeroEntity;
  318. CombatHeroEntity heroEntityB = b.RootMagicWeaponControl.combatHeroEntity;
  319. int myRestrained = GetRestrained(a.WuXingType, b.WuXingType);
  320. int targetRestrained = GetRestrained(b.WuXingType, a.WuXingType);
  321. float lg_a = heroEntityA.CurrCombatHeroInfo.GetWuXingShuXing(a.WuXingType);
  322. float lg_b = heroEntityB.CurrCombatHeroInfo.GetWuXingShuXing(b.WuXingType);
  323. int c = myRestrained - targetRestrained;
  324. long myHp = (long)(a.HpBl);
  325. long targetHp = (long)(b.HpBl);
  326. float p2 = 100;
  327. if (c < 0) //a被压制
  328. {
  329. p2 = Mathf.Max(100, Mathf.Min(200, 30 + lg_b - lg_a));
  330. targetHp = (long)(targetHp * p2);
  331. }
  332. else if (c > 0)
  333. {
  334. p2 = Mathf.Max(100, Mathf.Min(200, 30 + lg_a - lg_b));
  335. myHp = (long)(myHp * p2);
  336. }
  337. if (myHp > targetHp)
  338. {
  339. myHp -= targetHp;
  340. a.HpBl = myHp;
  341. }
  342. else if (myHp < targetHp)
  343. {
  344. targetHp -= myHp;
  345. b.HpBl = targetHp;
  346. }
  347. else if (myHp == targetHp)
  348. {
  349. a.HpBl = 0;
  350. b.HpBl = 0;
  351. }
  352. }
  353. public void GongFaPengZhuang(SkillFeaturesData a, SkillFeaturesData b, CombatHeroEntity heroEntityA,
  354. CombatHeroEntity heroEntityB)
  355. {
  356. a.InitPengZhuang();
  357. b.InitPengZhuang();
  358. GongFaPengZhuangFinishEventData gongFaPengZhuStart = GongFaPengZhuangFinishEventData.Create();
  359. gongFaPengZhuStart.a = a;
  360. gongFaPengZhuStart.b = b;
  361. CombatEventManager.Instance.Dispatch(CombatEventType.GongFaPengZhuangStart, gongFaPengZhuStart);
  362. int myRestrained = GetRestrained(a.WuXingType, b.WuXingType);
  363. int targetRestrained = GetRestrained(b.WuXingType, a.WuXingType);
  364. float lg_a = heroEntityA.CurrCombatHeroInfo.GetWuXingShuXing(a.WuXingType);
  365. float lg_b = heroEntityB.CurrCombatHeroInfo.GetWuXingShuXing(b.WuXingType);
  366. int c = myRestrained - targetRestrained;
  367. long myHp = (long)(a.hp);
  368. long targetHp = (long)(b.hp);
  369. a.pengZhuangHp = myHp;
  370. b.pengZhuangHp = targetHp;
  371. float p2 = 100;
  372. if (c < 0) //a被压制
  373. {
  374. p2 = Mathf.Max(100, Mathf.Min(200, 130 + lg_b - lg_a));
  375. targetHp = GetVlaueRatioForLong(targetHp, p2);
  376. }
  377. else if (c > 0)
  378. {
  379. p2 = Mathf.Max(100, Mathf.Min(200, 130 + lg_a - lg_b));
  380. myHp = GetVlaueRatioForLong(myHp, p2);
  381. }
  382. if (a.SkillFeaturesPengZhuangInfo.neutralizeQiangDu > 0)
  383. {
  384. targetHp -= GetVlaueRatioForLong(targetHp, a.SkillFeaturesPengZhuangInfo.neutralizeQiangDu);
  385. }
  386. if (b.SkillFeaturesPengZhuangInfo.neutralizeQiangDu > 0)
  387. {
  388. myHp -= GetVlaueRatioForLong(myHp, b.SkillFeaturesPengZhuangInfo.neutralizeQiangDu);
  389. }
  390. if (targetHp < 0)
  391. {
  392. targetHp = 0;
  393. }
  394. if (myHp < 0)
  395. {
  396. myHp = 0;
  397. }
  398. if (myHp > targetHp)
  399. {
  400. myHp -= targetHp;
  401. a.hp = myHp;
  402. b.hp = 0;
  403. }
  404. else if (myHp < targetHp)
  405. {
  406. targetHp -= myHp;
  407. b.hp = targetHp;
  408. a.hp = 0;
  409. }
  410. else if (myHp == targetHp)
  411. {
  412. a.hp = 0;
  413. b.hp = 0;
  414. }
  415. GongFaPengZhuangFinishEventData gongFaPengZhu = GongFaPengZhuangFinishEventData.Create();
  416. gongFaPengZhu.a = a;
  417. gongFaPengZhu.b = b;
  418. CombatEventManager.Instance.Dispatch(CombatEventType.GongFaPengZhuangFinish, gongFaPengZhu, false);
  419. CombatEventManager.Instance.Dispatch(CombatEventType.GongFaPengZhuangFinish2, gongFaPengZhu);
  420. a.DisposePengZhuang();
  421. b.DisposePengZhuang();
  422. }
  423. /// <summary>
  424. /// 是否相生
  425. /// </summary>
  426. /// <returns></returns>
  427. public bool IsSymbiosis(WuXingType a, WuXingType b)
  428. {
  429. int index = GeWuXingTypeIndex(a);
  430. if (index < 0)
  431. {
  432. return false;
  433. }
  434. return (b & Symbiosis[index]) != 0;
  435. }
  436. /// <summary>
  437. /// 是否相克制
  438. /// </summary>
  439. /// <returns></returns>
  440. public bool IsRestrain(WuXingType a, WuXingType b)
  441. {
  442. int index = GeWuXingTypeIndex(a);
  443. if (index < 0)
  444. {
  445. return false;
  446. }
  447. return (b & Restrain[index]) != 0;
  448. }
  449. public Color GetColor(WuXingType wuXingType)
  450. {
  451. switch ((int)wuXingType)
  452. {
  453. case 1:
  454. return new Color(1f, 0.98f, 0.09f);
  455. break;
  456. case 2:
  457. return new Color(0.19f, 0.51f, 1f);
  458. break;
  459. case 4:
  460. return new Color(0.17f, 1f, 0.35f);
  461. break;
  462. case 8:
  463. return new Color(1f, 0.19f, 0.04f);
  464. break;
  465. case 16:
  466. return new Color(1f, 0.65f, 0.17f);
  467. break;
  468. }
  469. return Color.white;
  470. }
  471. public Color GetColor(int wuXingType)
  472. {
  473. switch ((int)wuXingType)
  474. {
  475. case 1:
  476. return new Color(1f, 0.98f, 0.09f);
  477. break;
  478. case 2:
  479. return new Color(0.19f, 0.51f, 1f);
  480. break;
  481. case 3:
  482. return new Color(0.17f, 1f, 0.35f);
  483. break;
  484. case 4:
  485. return new Color(1f, 0.19f, 0.04f);
  486. break;
  487. case 5:
  488. return new Color(1f, 0.65f, 0.17f);
  489. break;
  490. }
  491. return Color.white;
  492. }
  493. }
  494. }