CombatCalculateTool.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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.Buff;
  9. using GameLogic.Combat.Hero;
  10. using GameLogic.Combat.Skill;
  11. using GameLogic.Player;
  12. using UnityEngine;
  13. using Utility;
  14. using UTool.CustomizeTimeLogic.FxLogic.TimeLineEventinterface;
  15. using Random = System.Random;
  16. namespace GameLogic.Combat.CombatTool
  17. {
  18. public class CombatCalculateTool : Singleton<CombatCalculateTool>
  19. {
  20. public Random Random = new Random();
  21. static readonly WuXingType[] Symbiosis = new WuXingType[6]
  22. {
  23. WuXingType.Water, // 金生水
  24. WuXingType.Fire, // 木生火
  25. WuXingType.Wood, // 水生木
  26. WuXingType.Earth, // 火生土
  27. WuXingType.Gold, // 土生金
  28. WuXingType.Null
  29. };
  30. // 相克关系表(用位表示)
  31. static readonly WuXingType[] Restrain = new WuXingType[6]
  32. {
  33. WuXingType.Wood, // 金克木
  34. WuXingType.Earth, // 木克土
  35. WuXingType.Fire, // 水克火
  36. WuXingType.Gold, // 火克金
  37. WuXingType.Water, // 土克水
  38. WuXingType.Null
  39. };
  40. public CombatCalculateTool()
  41. {
  42. Random = new Random(System.DateTime.Now.Millisecond);
  43. }
  44. public int GetOdd()
  45. {
  46. return Random.Next(0, 100);
  47. }
  48. public int GetOdd(int min, int max)
  49. {
  50. return Random.Next(min, max);
  51. }
  52. public long GetVlaueRatioForLong(long value, float ration)
  53. {
  54. long v = (value * (long)(ration * 100)) / 10000;
  55. return v;
  56. }
  57. public float GetVlaueRatioForFloat(float value, float ration)
  58. {
  59. float v = (value * ration) / 100f;
  60. return v;
  61. }
  62. public int GetVlaueRatioForInt(int value, float ration)
  63. {
  64. int v = (value * (int)(ration * 100)) / 10000;
  65. return v;
  66. }
  67. public HarmReturnInfo Harm(CombatHeroEntity source, CombatHeroEntity target, long att, AttType attType,
  68. TriggerData triggerData, WuXingType WuXingType, ISkillFeatures skillFeatures,
  69. HarmType harmType = HarmType.Null)
  70. {
  71. return Harm(source, target.GetMainHotPoin<CombatHeroHitPoint>(), att,
  72. attType, triggerData, WuXingType, skillFeatures, harmType);
  73. }
  74. /// <summary>
  75. /// 造成伤害
  76. /// </summary>n
  77. /// <param name="source">攻击方</param>
  78. /// <param name="target">被攻击方</param>
  79. /// <param name="att">伤害值</param>
  80. public HarmReturnInfo Harm(CombatHeroEntity source, CombatHeroHitPoint target, long att,
  81. AttType attType, TriggerData triggerData, WuXingType WuXingType, ISkillFeatures skillFeatures,
  82. HarmType harmType = HarmType.Default, HarmReturnInfo harmReturnInfo = null)
  83. {
  84. if (harmReturnInfo == null)
  85. {
  86. harmReturnInfo = CObjectPool.Instance.Fetch<HarmReturnInfo>();
  87. }
  88. harmReturnInfo.yuanShiAtt = att;
  89. harmReturnInfo.source = source;
  90. harmReturnInfo.target = target;
  91. harmReturnInfo.att = att;
  92. harmReturnInfo.attType = attType;
  93. harmReturnInfo.WuXingType = WuXingType;
  94. harmReturnInfo.harmType = harmType;
  95. harmReturnInfo.triggerData = triggerData;
  96. if (target.combatHeroEntity.isDie)
  97. {
  98. return harmReturnInfo;
  99. }
  100. if (CombatController.currActiveCombat.IsGameOver)
  101. {
  102. return harmReturnInfo;
  103. }
  104. float qiangDu = 100;
  105. SkillFeaturesData skillFeaturesData = skillFeatures as SkillFeaturesData;
  106. if (skillFeaturesData != null && skillFeaturesData.maxHp > 0)
  107. {
  108. qiangDu = (skillFeaturesData.hp * 100) / skillFeaturesData.maxHp;
  109. }
  110. harmReturnInfo.QiangDu = qiangDu;
  111. att = GetVlaueRatioForLong(att, qiangDu);
  112. float wuxing = source.CurrCombatHeroInfo.GetWuXingShuXing(WuXingType);
  113. // int index = GeWuXingTypeIndex(WuXingType);
  114. // WuXingType kzWuXing = Restrain[index];
  115. // float direnWuXing = target.combatHeroEntity.CurrCombatHeroInfo.GetWuXingShuXing(kzWuXing);
  116. att = GetVlaueRatioForLong(att, wuxing);
  117. long targetDef = target.combatHeroEntity.CurrCombatHeroInfo.defense.Value;
  118. targetDef -= GetVlaueRatioForLong(targetDef, harmReturnInfo.ArmorPiercing);
  119. float def =
  120. (targetDef * 100.0f / source.CurrCombatHeroInfo.k);
  121. int p1_id = (int)def;
  122. p1_id = Math.Min(100, Math.Max(1, p1_id));
  123. MitigationParaConfig mitigationParaConfig = ConfigComponent.Instance.Get<MitigationParaConfig>(p1_id);
  124. float p1 = (def * mitigationParaConfig.mitigationPara) / 100f;
  125. att = GetVlaueRatioForLong(att, 100 - p1);
  126. harmReturnInfo.att = att;
  127. StartInjuredEventData startInjuredEventData = StartInjuredEventData.Create();
  128. startInjuredEventData.HarmReturnInfo = harmReturnInfo;
  129. CombatEventManager.Instance.Dispatch(CombatEventType.StartInjured, startInjuredEventData);
  130. att = harmReturnInfo.att;
  131. if (harmReturnInfo.isInvalid)
  132. {
  133. return harmReturnInfo;
  134. }
  135. switch (WuXingType)
  136. {
  137. case WuXingType.Gold:
  138. att += GetVlaueRatioForLong(att,
  139. source.CurrCombatHeroInfo.Metal_HarmAdd);
  140. break;
  141. case WuXingType.Water:
  142. att += GetVlaueRatioForLong(att,
  143. source.CurrCombatHeroInfo.Water_HarmAdd);
  144. break;
  145. case WuXingType.Fire:
  146. att += GetVlaueRatioForLong(att,
  147. source.CurrCombatHeroInfo.Fire_HarmAdd);
  148. break;
  149. case WuXingType.Earth:
  150. att += GetVlaueRatioForLong(att,
  151. source.CurrCombatHeroInfo.Earth_HarmAdd);
  152. break;
  153. case WuXingType.Wood:
  154. att += GetVlaueRatioForLong(att,
  155. source.CurrCombatHeroInfo.Wood_HarmAdd);
  156. break;
  157. }
  158. if (!harmReturnInfo.attType.HasFlag(AttType.Buff))
  159. {
  160. att += GetVlaueRatioForLong(att,
  161. source.CurrCombatHeroInfo.finallyHarmAdd -
  162. target.combatHeroEntity.CurrCombatHeroInfo.finallyHarmReduce);
  163. }
  164. else
  165. {
  166. att += GetVlaueRatioForLong(att,
  167. source.CurrCombatHeroInfo.abnormalHarmAdd);
  168. }
  169. harmReturnInfo.att = att;
  170. target.combatHeroEntity.This<CombatHeroEntity>().HeroHurt(harmReturnInfo);
  171. return harmReturnInfo;
  172. }
  173. public HarmReturnInfo TrueHarm(CombatHeroEntity source, CombatHeroHitPoint target, long att,
  174. AttType attType, TriggerData triggerData,
  175. HarmType harmType = HarmType.Default)
  176. {
  177. HarmReturnInfo harmReturnInfo = CObjectPool.Instance.Fetch<HarmReturnInfo>();
  178. harmReturnInfo.source = source;
  179. harmReturnInfo.target = target;
  180. harmReturnInfo.att = att;
  181. harmReturnInfo.attType = attType;
  182. harmReturnInfo.harmType = harmType;
  183. harmReturnInfo.triggerData = triggerData;
  184. if (target.combatHeroEntity.isDie)
  185. {
  186. return harmReturnInfo;
  187. }
  188. target.combatHeroEntity.This<CombatHeroEntity>().HeroHurt(harmReturnInfo);
  189. return harmReturnInfo;
  190. }
  191. public HarmReturnInfo Recover(CombatHeroEntity source, CombatHeroHitPoint target, long att,
  192. AttType attType, HarmType harmType, TriggerData triggerData)
  193. {
  194. harmType |= HarmType.Recover;
  195. HarmReturnInfo harmReturnInfo = new HarmReturnInfo();
  196. harmReturnInfo.source = source;
  197. harmReturnInfo.target = target;
  198. harmReturnInfo.att = att;
  199. harmReturnInfo.attType = attType;
  200. harmReturnInfo.harmType = harmType;
  201. harmReturnInfo.triggerData = triggerData;
  202. if (target.combatHeroEntity.isDie)
  203. {
  204. return harmReturnInfo;
  205. }
  206. target.combatHeroEntity.This<CombatHeroEntity>().Recover(harmReturnInfo);
  207. return harmReturnInfo;
  208. }
  209. public ILifetCycleHitPoint[] GetMinHpHero(ILifetCycleHitPoint[] allLifetCycleHitPoints, int count)
  210. {
  211. if (allLifetCycleHitPoints == null)
  212. {
  213. return null;
  214. }
  215. BetterList<ILifetCycleHitPoint> findHero = new BetterList<ILifetCycleHitPoint>();
  216. findHero.AddRange(allLifetCycleHitPoints);
  217. int currCount = Math.Min(allLifetCycleHitPoints.Length, count);
  218. ILifetCycleHitPoint[] minHpHero = new ILifetCycleHitPoint[currCount];
  219. for (int k = 0; k < currCount; k++)
  220. {
  221. CombatHeroEntity lifetCycleHitPoint = null;
  222. int index = 0;
  223. if (findHero.Count <= 0)
  224. {
  225. return minHpHero;
  226. }
  227. lifetCycleHitPoint = findHero[0].IfLifeCycle.This<CombatHeroEntity>();
  228. for (int j = 0; j < findHero.Count; j++)
  229. {
  230. CombatHeroEntity lifetCycleHitPoint2 =
  231. findHero[j].IfLifeCycle.This<CombatHeroEntity>();
  232. if (lifetCycleHitPoint2.HpBl < lifetCycleHitPoint.HpBl)
  233. {
  234. lifetCycleHitPoint = lifetCycleHitPoint2;
  235. index = j;
  236. }
  237. }
  238. ILifetCycleHitPoint currFindHitPoint = lifetCycleHitPoint.GetMainHotPoin<ILifetCycleHitPoint>(true);
  239. minHpHero[k] = currFindHitPoint;
  240. findHero.RemoveAt(index);
  241. }
  242. return minHpHero;
  243. }
  244. private int GeWuXingTypeIndex(WuXingType e)
  245. {
  246. switch (e)
  247. {
  248. case WuXingType.Gold: return 0;
  249. case WuXingType.Wood: return 1;
  250. case WuXingType.Water: return 2;
  251. case WuXingType.Fire: return 3;
  252. case WuXingType.Earth: return 4;
  253. default: return 5; // 无效元素
  254. }
  255. }
  256. public int GetRestrained(WuXingType WuXingType, WuXingType targetWuXingType)
  257. {
  258. int c = 0;
  259. if (WuXingType.HasFlag(WuXingType.Gold))
  260. {
  261. if (targetWuXingType.HasFlag(WuXingType.Wood))
  262. {
  263. c++;
  264. }
  265. }
  266. if (WuXingType.HasFlag(WuXingType.Wood))
  267. {
  268. if (targetWuXingType.HasFlag(WuXingType.Earth))
  269. {
  270. c++;
  271. }
  272. }
  273. if (WuXingType.HasFlag(WuXingType.Water))
  274. {
  275. if (targetWuXingType.HasFlag(WuXingType.Fire))
  276. {
  277. c++;
  278. }
  279. }
  280. if (WuXingType.HasFlag(WuXingType.Fire))
  281. {
  282. if (targetWuXingType.HasFlag(WuXingType.Gold))
  283. {
  284. c++;
  285. }
  286. }
  287. if (WuXingType.HasFlag(WuXingType.Earth))
  288. {
  289. if (targetWuXingType.HasFlag(WuXingType.Wood))
  290. {
  291. c++;
  292. }
  293. }
  294. return c;
  295. }
  296. public void FaBaoPengZhuang(CombatMagicWeaponEntity a, CombatMagicWeaponEntity b)
  297. {
  298. CombatHeroEntity heroEntityA = a.RootMagicWeaponControl.combatHeroEntity;
  299. CombatHeroEntity heroEntityB = b.RootMagicWeaponControl.combatHeroEntity;
  300. int myRestrained = GetRestrained(a.WuXingType, b.WuXingType);
  301. int targetRestrained = GetRestrained(b.WuXingType, a.WuXingType);
  302. float lg_a = heroEntityA.CurrCombatHeroInfo.GetWuXingShuXing(a.WuXingType);
  303. float lg_b = heroEntityB.CurrCombatHeroInfo.GetWuXingShuXing(b.WuXingType);
  304. int c = myRestrained - targetRestrained;
  305. long myHp = (long)(a.HpBl);
  306. long targetHp = (long)(b.HpBl);
  307. float p2 = 100;
  308. if (c < 0) //a被压制
  309. {
  310. p2 = Mathf.Max(100, Mathf.Min(200, 30 + lg_b - lg_a));
  311. targetHp = (long)(targetHp * p2);
  312. }
  313. else if (c > 0)
  314. {
  315. p2 = Mathf.Max(100, Mathf.Min(200, 30 + lg_a - lg_b));
  316. myHp = (long)(myHp * p2);
  317. }
  318. if (myHp > targetHp)
  319. {
  320. myHp -= targetHp;
  321. a.HpBl = myHp;
  322. }
  323. else if (myHp < targetHp)
  324. {
  325. targetHp -= myHp;
  326. b.HpBl = targetHp;
  327. }
  328. else if (myHp == targetHp)
  329. {
  330. a.HpBl = 0;
  331. b.HpBl = 0;
  332. }
  333. }
  334. public void GongFaPengZhuang(SkillFeaturesData a, SkillFeaturesData b, CombatHeroEntity heroEntityA,
  335. CombatHeroEntity heroEntityB)
  336. {
  337. a.InitPengZhuang();
  338. b.InitPengZhuang();
  339. GongFaPengZhuangFinishEventData gongFaPengZhuStart = GongFaPengZhuangFinishEventData.Create();
  340. gongFaPengZhuStart.a = a;
  341. gongFaPengZhuStart.b = b;
  342. CombatEventManager.Instance.Dispatch(CombatEventType.GongFaPengZhuangStart, gongFaPengZhuStart);
  343. int myRestrained = GetRestrained(a.WuXingType, b.WuXingType);
  344. int targetRestrained = GetRestrained(b.WuXingType, a.WuXingType);
  345. float lg_a = heroEntityA.CurrCombatHeroInfo.GetWuXingShuXing(a.WuXingType);
  346. float lg_b = heroEntityB.CurrCombatHeroInfo.GetWuXingShuXing(b.WuXingType);
  347. int c = myRestrained - targetRestrained;
  348. long myHp = (long)(a.hp);
  349. long targetHp = (long)(b.hp);
  350. a.pengZhuangHp = myHp;
  351. b.pengZhuangHp = targetHp;
  352. float p2 = 100;
  353. if (c < 0) //a被压制
  354. {
  355. p2 = Mathf.Max(100, Mathf.Min(200, 130 + lg_b - lg_a));
  356. targetHp = GetVlaueRatioForLong(targetHp, p2);
  357. }
  358. else if (c > 0)
  359. {
  360. p2 = Mathf.Max(100, Mathf.Min(200, 130 + lg_a - lg_b));
  361. myHp = GetVlaueRatioForLong(myHp, p2);
  362. }
  363. if (a.SkillFeaturesPengZhuangInfo.neutralizeQiangDu > 0)
  364. {
  365. targetHp -= GetVlaueRatioForLong(targetHp, a.SkillFeaturesPengZhuangInfo.neutralizeQiangDu);
  366. }
  367. if (b.SkillFeaturesPengZhuangInfo.neutralizeQiangDu > 0)
  368. {
  369. myHp -= GetVlaueRatioForLong(myHp, b.SkillFeaturesPengZhuangInfo.neutralizeQiangDu);
  370. }
  371. if (targetHp < 0)
  372. {
  373. targetHp = 0;
  374. }
  375. if (myHp < 0)
  376. {
  377. myHp = 0;
  378. }
  379. if (myHp > targetHp)
  380. {
  381. myHp -= targetHp;
  382. a.hp = myHp;
  383. b.hp = 0;
  384. }
  385. else if (myHp < targetHp)
  386. {
  387. targetHp -= myHp;
  388. b.hp = targetHp;
  389. a.hp = 0;
  390. }
  391. else if (myHp == targetHp)
  392. {
  393. a.hp = 0;
  394. b.hp = 0;
  395. }
  396. GongFaPengZhuangFinishEventData gongFaPengZhu = GongFaPengZhuangFinishEventData.Create();
  397. gongFaPengZhu.a = a;
  398. gongFaPengZhu.b = b;
  399. CombatEventManager.Instance.Dispatch(CombatEventType.GongFaPengZhuangFinish, gongFaPengZhu, false);
  400. CombatEventManager.Instance.Dispatch(CombatEventType.GongFaPengZhuangFinish2, gongFaPengZhu);
  401. a.DisposePengZhuang();
  402. b.DisposePengZhuang();
  403. }
  404. /// <summary>
  405. /// 是否相生
  406. /// </summary>
  407. /// <returns></returns>
  408. public bool IsSymbiosis(WuXingType a, WuXingType b)
  409. {
  410. int index = GeWuXingTypeIndex(a);
  411. if (index < 0)
  412. {
  413. return false;
  414. }
  415. return (b & Symbiosis[index]) != 0;
  416. }
  417. /// <summary>
  418. /// 是否相克制
  419. /// </summary>
  420. /// <returns></returns>
  421. public bool IsRestrain(WuXingType a, WuXingType b)
  422. {
  423. int index = GeWuXingTypeIndex(a);
  424. if (index < 0)
  425. {
  426. return false;
  427. }
  428. return (b & Restrain[index]) != 0;
  429. }
  430. public Color GetColor(WuXingType wuXingType)
  431. {
  432. switch ((int)wuXingType)
  433. {
  434. case 1:
  435. return new Color(1f, 0.98f, 0.09f);
  436. break;
  437. case 2:
  438. return new Color(0.19f, 0.51f, 1f);
  439. break;
  440. case 4:
  441. return new Color(0.17f, 1f, 0.35f);
  442. break;
  443. case 8:
  444. return new Color(1f, 0.19f, 0.04f);
  445. break;
  446. case 16:
  447. return new Color(1f, 0.65f, 0.17f);
  448. break;
  449. }
  450. return Color.white;
  451. }
  452. public Color GetColor(int wuXingType)
  453. {
  454. switch ((int)wuXingType)
  455. {
  456. case 1:
  457. return new Color(1f, 0.98f, 0.09f);
  458. break;
  459. case 2:
  460. return new Color(0.19f, 0.51f, 1f);
  461. break;
  462. case 3:
  463. return new Color(0.17f, 1f, 0.35f);
  464. break;
  465. case 4:
  466. return new Color(1f, 0.19f, 0.04f);
  467. break;
  468. case 5:
  469. return new Color(1f, 0.65f, 0.17f);
  470. break;
  471. }
  472. return Color.white;
  473. }
  474. }
  475. }