CombatHeroEntity.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Animancer;
  4. using Common.Utility.CombatEvent;
  5. using Core.Utility;
  6. using Fort23.Common;
  7. using Fort23.Core;
  8. using Fort23.UTool;
  9. using GameLogic.Combat.Buff;
  10. using GameLogic.Combat.CombatTool;
  11. using GameLogic.Combat.Hero;
  12. using GameLogic.Combat.Hero.HeroGPU;
  13. using GameLogic.Player;
  14. using UnityEngine;
  15. using UnityEngine.AI;
  16. using UnityEngine.Rendering;
  17. using UTool.CustomizeTimeLogic.FxLogic.TimeLineEventinterface;
  18. public class CombatHeroEntity : ShowBaiscEntity, ITimeLineAnimtion, ITimeLineGetAttSpeed,
  19. IHero
  20. {
  21. /// <summary>
  22. /// 死亡时的节点ID
  23. /// </summary>
  24. public int heroDieNodeId;
  25. public string guidName
  26. {
  27. get { return CurrCombatHeroInfo.modelName; }
  28. }
  29. public bool isFollowState;
  30. private float _lasetShowHarmTime;
  31. private float _injuriedShowTime;
  32. private bool _isAddinjuriedShow;
  33. private float _addInjuiedValue = (1.0f / 0.2f) * 0.3f;
  34. private bool _isDis;
  35. public float DisTime;
  36. public MagicWeaponControl MagicWeaponControl;
  37. public float HpBl
  38. {
  39. get { return CurrCombatHeroInfo.hp.Value * 100f / MaxCombatHeroInfo.hp.Value; }
  40. }
  41. public Vector3 dotPos
  42. {
  43. get { return combatHeroGameObject.position; }
  44. }
  45. public T GetThis<T>() where T : IHero
  46. {
  47. return (T)(object)this;
  48. }
  49. public GameObject GameObject
  50. {
  51. get { return combatHeroGameObject.transform.gameObject; }
  52. }
  53. public Vector3 faceDir
  54. {
  55. get { return combatHeroGameObject.transform.forward; }
  56. }
  57. public virtual async CTask<CombatHeroEntity> Init(CombatAIBasic combatAIBasic, CombatHeroInfo combatHeroInfo,
  58. Vector3 pos,
  59. System.Action<CombatHeroEntity> callBack = null)
  60. {
  61. //后面记到检查战斗里面不要出现异步加载,也不要出现同步IO加载
  62. string modelName = combatHeroInfo.modelName;
  63. if (combatHeroInfo.isGpu)
  64. {
  65. modelName += "_gpu";
  66. }
  67. CurrCombatHeroInfo = combatHeroInfo.Copy();
  68. MaxCombatHeroInfo = combatHeroInfo.Copy();
  69. // GameTimeLineParticleFactory
  70. CombatHeroGameObjectPool poolInterface =
  71. await GObjectPool.Instance.FetchAsync<CombatHeroGameObjectPool>(modelName + ".prefab", null);
  72. #if !COMBAT_SERVER
  73. if (poolInterface == null || poolInterface.own == null)
  74. {
  75. return null;
  76. }
  77. poolInterface.own.transform.position = pos;
  78. // if (!IsEnemy)
  79. // {
  80. // GameObjectPool fx_hero_quan =
  81. // await GObjectPool.Instance.FetchAsync<GameObjectPool>("fx_hero_quan.prefab", null);
  82. // fx_hero_quan.own.transform.SetParent(poolInterface.own.transform);
  83. // fx_hero_quan.own.transform.localPosition = Vector3.zero;
  84. // }
  85. poolInterface.own.SetActive(false);
  86. MagicWeaponControl = new MagicWeaponControl();
  87. combatHeroTimeLineControl = new CombatHeroTimeLineControl();
  88. combatHeroTimeLineControl.Init(this);
  89. AssetHandle assetHandle =
  90. await AssetBundleLoadManager.Instance.LoadAssetAsyncTask<TextAsset>(combatHeroInfo.modelName + "_TD.txt");
  91. if (assetHandle != null)
  92. {
  93. TextAsset textAsset = assetHandle.AssetObject<TextAsset>();
  94. TimeLienData timeLienData = JsonManager.FromJson<TimeLienData>(textAsset.text);
  95. timeLienData.DeserializeData();
  96. assetHandle.Release();
  97. combatHeroTimeLineControl.AddTimeLienData(timeLienData);
  98. }
  99. combatHeroGameObject = new CombatHeroGameObject();
  100. combatHeroGameObject.Init(this, poolInterface);
  101. BuffControl = new BuffControl();
  102. BuffControl.Init(this);
  103. NavMeshAgent navMeshAgent = poolInterface.own.GetComponent<NavMeshAgent>();
  104. if (combatAIBasic == null)
  105. {
  106. combatAIBasic = new CombatAIBasic();
  107. }
  108. HeroEntityMono heroEntityMono = poolInterface.own.GetComponent<HeroEntityMono>();
  109. if (heroEntityMono == null)
  110. {
  111. heroEntityMono = poolInterface.own.AddComponent<HeroEntityMono>();
  112. }
  113. heroEntityMono.combatHeroEntity = this;
  114. CombatAIBasic = combatAIBasic;
  115. CombatAIBasic.Init(this);
  116. CombatHeroSkillControl = new CombatHeroSkillControl();
  117. await CombatHeroSkillControl.Init(this);
  118. CurrCombatHeroInfo.Shield = (EncryptionLong)300;
  119. await InitShieldsFx();
  120. AnimancerComponent animancerComponent = poolInterface.own.GetComponent<AnimancerComponent>();
  121. combatHeroAnimtion = new CombatHeroAnimtion();
  122. // if (animancerComponent != null)
  123. {
  124. }
  125. // else
  126. // {
  127. // combatHeroAnimtion = new CombatHeroGPUAnimtion();
  128. // }
  129. poolInterface.own.SetActive(true);
  130. combatHeroAnimtion.Init(this);
  131. CombatAIBasic.ChangeState(CombatHeroStateType.XiuMian);
  132. if (IsEnemy )
  133. {
  134. CreateHeroHpEventData createHeroHpEventData = CreateHeroHpEventData.Create();
  135. createHeroHpEventData.combatHeroEntity = this;
  136. EventManager.Instance.Dispatch(CustomEventType.CreateHeroHp, createHeroHpEventData);
  137. }
  138. await MagicWeaponControl.InitMagicWeapon(this);
  139. callBack?.Invoke(this);
  140. #endif
  141. return this;
  142. }
  143. public virtual void Update(float t)
  144. {
  145. CombatAIBasic.Update(t);
  146. CombatHeroSkillControl.Update(t);
  147. combatHeroTimeLineControl.Update(t);
  148. combatHeroAnimtion.Update(t);
  149. combatHeroGameObject.Update(t);
  150. BuffControl.Update(t);
  151. MagicWeaponControl?.Update(t);
  152. if (combatHeroGameObject.HeroGPUMono != null)
  153. {
  154. if (_injuriedShowTime > 0)
  155. {
  156. _injuriedShowTime -= t;
  157. if (_isAddinjuriedShow)
  158. {
  159. combatHeroGameObject.HeroGPUMono.injuriedStrength += t * (_addInjuiedValue);
  160. if (combatHeroGameObject.HeroGPUMono.injuriedStrength >= 0.3f)
  161. {
  162. _isAddinjuriedShow = false;
  163. }
  164. }
  165. else
  166. {
  167. combatHeroGameObject.HeroGPUMono.injuriedStrength -= t * (_addInjuiedValue);
  168. if (combatHeroGameObject.HeroGPUMono.injuriedStrength < 0)
  169. {
  170. combatHeroGameObject.HeroGPUMono.injuriedStrength = 0;
  171. }
  172. }
  173. }
  174. else
  175. {
  176. combatHeroGameObject.HeroGPUMono.injuriedStrength = 0;
  177. }
  178. }
  179. }
  180. public T This<T>()
  181. {
  182. return (T)(object)this;
  183. }
  184. public override T GetILifetCycleHitPoint<T>(string hitPoinName, bool isStandType, bool isIgnoreHind)
  185. {
  186. return combatHeroGameObject.GetILifetCycleHitPoint<T>(hitPoinName, isStandType, isIgnoreHind);
  187. }
  188. public override T GetMainHotPoin<T>(bool isIgnoreHind = false)
  189. {
  190. return combatHeroGameObject.GetMainHotPoin<T>(isIgnoreHind);
  191. }
  192. public void PlayAnim(string animName, bool isLoop, int layerId, bool repeat, float speed)
  193. {
  194. combatHeroAnimtion.Play(animName, speed);
  195. }
  196. public SpecialDotInfo GetSpecialDotInfo(string specialDotName)
  197. {
  198. return combatHeroGameObject.GetMainHotPoin<CombatHeroHitPoint>(true).GetSpecialDotInfo(specialDotName);
  199. }
  200. public virtual float GetAttSpeed()
  201. {
  202. CombatHeroSkillControl combatHeroSkillControl = CombatHeroSkillControl as CombatHeroSkillControl;
  203. if (combatHeroSkillControl != null)
  204. {
  205. if (combatHeroSkillControl.useSkillCount > 1)
  206. {
  207. return 4;
  208. }
  209. return CurrCombatHeroInfo.attSpeed.Value;
  210. }
  211. return 1;
  212. }
  213. public void HeroResurrection()
  214. {
  215. isDie = false;
  216. CombatAIBasic.ChangeState(CombatHeroStateType.idle, isQiangZhi: true);
  217. HeroHpUpdateEventData heroHpUpdateEventData = HeroHpUpdateEventData.Create();
  218. heroHpUpdateEventData.combatHeroEntity = this;
  219. CombatEventManager.Instance.Dispatch(CombatEventType.HeroHpUpdate, heroHpUpdateEventData);
  220. HeroResurrectionEventData heroResurrectionEventData = HeroResurrectionEventData.Create();
  221. heroResurrectionEventData.combatHeroEntity = this;
  222. CombatEventManager.Instance.Dispatch(CombatEventType.HeroResurrection, heroResurrectionEventData);
  223. }
  224. public void HeroDie(HarmReturnInfo harmReturnInfo)
  225. {
  226. heroDieNodeId = CombatController.currActiveCombat.CombatTypeBasic.allWinNodeCount;
  227. isDie = true;
  228. HeroDieEventData heroDieEventData = HeroDieEventData.Create();
  229. heroDieEventData.combatHeroEntity = this;
  230. heroDieEventData.HarmReturnInfo = harmReturnInfo;
  231. CombatEventManager.Instance.Dispatch(CombatEventType.HeroDie, heroDieEventData);
  232. combatHeroGameObject.HeroDie();
  233. CombatAIBasic.ChangeState(CombatHeroStateType.dile);
  234. CombatEventManager.Instance.Dispatch(CombatEventType.HeroDieFinish, heroDieEventData);
  235. }
  236. public void HeroHurt(HarmReturnInfo harmReturnInfo)
  237. {
  238. if (isDie)
  239. {
  240. return;
  241. }
  242. if (harmReturnInfo.WuXingType.HasFlag(WuXingType.Gold))
  243. {
  244. int odds = Random.Range(0, 100);
  245. if (odds < 50)
  246. {
  247. CurrCombatHeroInfo.Wood_Injury += Random.Range(1, 5);
  248. }
  249. }
  250. if (harmReturnInfo.WuXingType.HasFlag(WuXingType.Wood))
  251. {
  252. int odds = Random.Range(0, 100);
  253. if (odds < 50)
  254. {
  255. CurrCombatHeroInfo.Earth_Injury += Random.Range(1, 5);
  256. }
  257. }
  258. if (harmReturnInfo.WuXingType.HasFlag(WuXingType.Water))
  259. {
  260. int odds = Random.Range(0, 100);
  261. if (odds < 50)
  262. {
  263. CurrCombatHeroInfo.Fire_Injury += Random.Range(1, 5);
  264. }
  265. }
  266. if (harmReturnInfo.WuXingType.HasFlag(WuXingType.Fire))
  267. {
  268. int odds = Random.Range(0, 100);
  269. if (odds < 50)
  270. {
  271. CurrCombatHeroInfo.Metal_Injury += Random.Range(1, 5);
  272. }
  273. }
  274. if (harmReturnInfo.WuXingType.HasFlag(WuXingType.Earth))
  275. {
  276. int odds = Random.Range(0, 100);
  277. if (odds < 50)
  278. {
  279. CurrCombatHeroInfo.Water_Injury += Random.Range(1, 5);
  280. }
  281. }
  282. if (harmReturnInfo.triggerData.IBarrier != null && ShieldsBarrier != null)
  283. {
  284. ShieldsBarrier.Play();
  285. CurrCombatHeroInfo.Shield -= harmReturnInfo.att;
  286. if (CurrCombatHeroInfo.Shield <= 0)
  287. {
  288. ShieldsBarrier.Rupture();
  289. }
  290. }
  291. else
  292. {
  293. CurrCombatHeroInfo.hp -= harmReturnInfo.att;
  294. }
  295. UpdateHarmText(harmReturnInfo);
  296. if (combatHeroGameObject.HeroGPUMono != null)
  297. {
  298. _injuriedShowTime = 0.4f;
  299. combatHeroGameObject.HeroGPUMono.injuriedStrength = 0;
  300. _isAddinjuriedShow = true;
  301. }
  302. HeroHpUpdateEventData heroHpUpdateEventData = HeroHpUpdateEventData.Create();
  303. heroHpUpdateEventData.combatHeroEntity = this;
  304. CombatEventManager.Instance.Dispatch(CombatEventType.HeroHpUpdate, heroHpUpdateEventData);
  305. if (CurrCombatHeroInfo.hp <= 0)
  306. {
  307. HeroDie(harmReturnInfo);
  308. }
  309. }
  310. private void UpdateHarmText(HarmReturnInfo harmReturnInfo)
  311. {
  312. float currTime = Time.time;
  313. if (currTime - _lasetShowHarmTime < 0.1f)
  314. {
  315. return;
  316. }
  317. _lasetShowHarmTime = currTime;
  318. HarmUpdateEventData harmUpdateEventData = HarmUpdateEventData.Create();
  319. harmUpdateEventData.HarmReturnInfo = harmReturnInfo;
  320. CombatEventManager.Instance.Dispatch(CombatEventType.HarmUpdate, harmUpdateEventData);
  321. }
  322. public void Recover(HarmReturnInfo harmReturnInfo)
  323. {
  324. CurrCombatHeroInfo.hp += harmReturnInfo.att;
  325. HarmUpdateEventData harmUpdateEventData = HarmUpdateEventData.Create();
  326. harmUpdateEventData.HarmReturnInfo = harmReturnInfo;
  327. CombatEventManager.Instance.Dispatch(CombatEventType.RecoverUpdate, harmUpdateEventData);
  328. HeroHpUpdateEventData heroHpUpdateEventData = HeroHpUpdateEventData.Create();
  329. heroHpUpdateEventData.combatHeroEntity = this;
  330. CombatEventManager.Instance.Dispatch(CombatEventType.HeroHpUpdate, heroHpUpdateEventData);
  331. }
  332. public void CloseLoopFx()
  333. {
  334. for (int i = 0; i < heroLoopParticle.Count; i++)
  335. {
  336. GObjectPool.Instance.Recycle(heroLoopParticle[i]);
  337. }
  338. heroLoopParticle.Clear();
  339. }
  340. public void Dispose()
  341. {
  342. if (_isDis)
  343. {
  344. return;
  345. }
  346. _isDis = true;
  347. combatHeroGameObject.Dispose();
  348. isDie = true;
  349. CombatHeroSkillControl.Dispose();
  350. CombatAIBasic.Dispose();
  351. combatHeroTimeLineControl.Dispose();
  352. combatHeroAnimtion.Dispose();
  353. CloseLoopFx();
  354. ShieldsBarrier?.Dispose();
  355. ShieldsBarrier = null;
  356. }
  357. private void ProDormancyObj()
  358. {
  359. CombatHeroSkillControl.ProDormancyObj();
  360. }
  361. public override void ActiveObj()
  362. {
  363. isDie = false;
  364. _isDis = false;
  365. }
  366. public override void DormancyObj()
  367. {
  368. Dispose();
  369. ProDormancyObj();
  370. _isDis = false;
  371. CombatAIBasic = null;
  372. combatHeroGameObject = null;
  373. CurrCombatHeroInfo = null;
  374. MaxCombatHeroInfo = null;
  375. combatHeroTimeLineControl = null;
  376. combatHeroAnimtion = null;
  377. CombatHeroSkillControl = null;
  378. isFollowState = false;
  379. heroLoopParticle.Clear();
  380. isDie = true;
  381. }
  382. }