CombatHeroEntity.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Animancer;
  4. using Common.Utility.CombatEvent;
  5. using Fort23.Common;
  6. using Fort23.Core;
  7. using Fort23.UTool;
  8. using GameLogic.Combat.CombatTool;
  9. using GameLogic.Combat.Hero;
  10. using GameLogic.Combat.Hero.HeroGPU;
  11. using UnityEngine;
  12. using UnityEngine.AI;
  13. using UnityEngine.Rendering;
  14. using UTool.CustomizeTimeLogic.FxLogic.TimeLineEventinterface;
  15. public class CombatHeroEntity : ITimeLineSpecialDotPos, ILifeCycle, ITimeLineAnimtion, ITimeLineGetAttSpeed, IHero
  16. {
  17. /// <summary>
  18. /// 死亡时的节点ID
  19. /// </summary>
  20. public int heroDieNodeId;
  21. public bool IsEnemy { get; set; }
  22. public bool isDie { get; set; }
  23. public int number;
  24. public CombatAIBasic CombatAIBasic;
  25. public CombatHeroGameObject combatHeroGameObject;
  26. public CombatHeroInfo CurrCombatHeroInfo;
  27. public CombatHeroInfo MaxCombatHeroInfo;
  28. public CombatHeroTimeLineControl combatHeroTimeLineControl;
  29. public HeroAnimtionBasic combatHeroAnimtion;
  30. public CombatHeroSkillControl CombatHeroSkillControl;
  31. public bool isFollowState;
  32. public BetterList<CombatParticleSystemPool> heroLoopParticle = new BetterList<CombatParticleSystemPool>();
  33. private float _lasetShowHarmTime;
  34. private float _injuriedShowTime;
  35. private bool _isAddinjuriedShow;
  36. private float _addInjuiedValue = (1.0f / 0.2f) * 1f;
  37. public Vector3 dotPos
  38. {
  39. get { return combatHeroGameObject.position; }
  40. }
  41. public T GetThis<T>() where T : IHero
  42. {
  43. return (T)(object)this;
  44. }
  45. public GameObject GameObject
  46. {
  47. get { return combatHeroGameObject.transform.gameObject; }
  48. }
  49. public Vector3 faceDir
  50. {
  51. get { return combatHeroGameObject.transform.forward; }
  52. }
  53. public async CTask<CombatHeroEntity> Init(CombatAIBasic combatAIBasic, CombatHeroInfo combatHeroInfo,
  54. System.Action<CombatHeroEntity> callBack = null)
  55. {
  56. //后面记到检查战斗里面不要出现异步加载,也不要出现同步IO加载
  57. string modelName = combatHeroInfo.modelName;
  58. if (combatHeroInfo.isGpu)
  59. {
  60. modelName += "_gpu";
  61. }
  62. CurrCombatHeroInfo = combatHeroInfo.Copy();
  63. MaxCombatHeroInfo = combatHeroInfo.Copy();
  64. // GameTimeLineParticleFactory
  65. GameObjectPool poolInterface =
  66. await GObjectPool.Instance.FetchAsync<GameObjectPool>(modelName + ".prefab", null);
  67. #if !COMBAT_SERVER
  68. if (poolInterface == null || poolInterface.own == null)
  69. {
  70. return null;
  71. }
  72. if (!IsEnemy)
  73. {
  74. GameObjectPool fx_hero_quan =
  75. await GObjectPool.Instance.FetchAsync<GameObjectPool>("fx_hero_quan.prefab", null);
  76. fx_hero_quan.own.transform.SetParent(poolInterface.own.transform);
  77. }
  78. poolInterface.own.SetActive(false);
  79. AssetHandle assetHandle =
  80. await AssetBundleLoadManager.Instance.LoadAssetAsyncTask<TextAsset>(combatHeroInfo.modelName + "_TD.txt");
  81. TextAsset textAsset = assetHandle.AssetObject<TextAsset>();
  82. combatHeroGameObject = new CombatHeroGameObject();
  83. combatHeroGameObject.Init(this, poolInterface);
  84. TimeLienData timeLienData = JsonManager.FromJson<TimeLienData>(textAsset.text);
  85. timeLienData.DeserializeData();
  86. assetHandle.Release();
  87. combatHeroTimeLineControl = new CombatHeroTimeLineControl();
  88. combatHeroTimeLineControl.Init(this, timeLienData);
  89. NavMeshAgent navMeshAgent = poolInterface.own.GetComponent<NavMeshAgent>();
  90. if (combatAIBasic == null)
  91. {
  92. combatAIBasic = new CombatAIBasic();
  93. }
  94. HeroEntityMono heroEntityMono = poolInterface.own.GetComponent<HeroEntityMono>();
  95. if (heroEntityMono == null)
  96. {
  97. heroEntityMono = poolInterface.own.AddComponent<HeroEntityMono>();
  98. }
  99. heroEntityMono.combatHeroEntity = this;
  100. CombatAIBasic = combatAIBasic;
  101. CombatAIBasic.Init(this, navMeshAgent);
  102. CombatHeroSkillControl = new CombatHeroSkillControl();
  103. CombatHeroSkillControl.Init(this);
  104. AnimancerComponent animancerComponent = poolInterface.own.GetComponent<AnimancerComponent>();
  105. if (animancerComponent != null)
  106. {
  107. combatHeroAnimtion = new CombatHeroAnimtion();
  108. }
  109. else
  110. {
  111. combatHeroAnimtion = new CombatHeroGPUAnimtion();
  112. }
  113. combatHeroAnimtion.Init(this);
  114. CombatAIBasic.ChangeState(CombatHeroStateType.XiuMian);
  115. if (!IsEnemy)
  116. {
  117. CreateHeroHpEventData createHeroHpEventData = CreateHeroHpEventData.Create();
  118. createHeroHpEventData.combatHeroEntity = this;
  119. EventManager.Instance.Dispatch(CustomEventType.CreateHeroHp, createHeroHpEventData);
  120. }
  121. poolInterface.own.SetActive(true);
  122. callBack?.Invoke(this);
  123. #endif
  124. return this;
  125. }
  126. public void Update(float t)
  127. {
  128. if (!CombatController.currActiveCombat.isStopAi)
  129. {
  130. CombatAIBasic.Update(t);
  131. }
  132. CombatHeroSkillControl.Update(t);
  133. combatHeroTimeLineControl.Update(t);
  134. combatHeroAnimtion.Update(t);
  135. if (combatHeroGameObject.HeroGPUMono != null)
  136. {
  137. if (_injuriedShowTime > 0)
  138. {
  139. _injuriedShowTime -= t;
  140. if (_isAddinjuriedShow)
  141. {
  142. combatHeroGameObject.HeroGPUMono.injuriedStrength += t * (_addInjuiedValue);
  143. if (combatHeroGameObject.HeroGPUMono.injuriedStrength >= 1f)
  144. {
  145. _isAddinjuriedShow = false;
  146. }
  147. }
  148. else
  149. {
  150. combatHeroGameObject.HeroGPUMono.injuriedStrength -= t * (_addInjuiedValue);
  151. }
  152. }
  153. else
  154. {
  155. combatHeroGameObject.HeroGPUMono.injuriedStrength = 0;
  156. }
  157. }
  158. }
  159. public T This<T>()
  160. {
  161. return (T)(object)this;
  162. }
  163. public T GetILifetCycleHitPoint<T>(string hitPoinName, bool isStandType, bool isIgnoreHind)
  164. where T : ILifetCycleHitPoint
  165. {
  166. return combatHeroGameObject.GetILifetCycleHitPoint<T>(hitPoinName, isStandType, isIgnoreHind);
  167. }
  168. public T GetMainHotPoin<T>(bool isIgnoreHind = false) where T : ILifetCycleHitPoint
  169. {
  170. return combatHeroGameObject.GetMainHotPoin<T>(isIgnoreHind);
  171. }
  172. public void PlayAnim(string animName, bool isLoop, int layerId, bool repeat, float speed)
  173. {
  174. combatHeroAnimtion.Play(animName, speed);
  175. }
  176. public SpecialDotInfo GetSpecialDotInfo(string specialDotName)
  177. {
  178. return combatHeroGameObject.GetMainHotPoin<CombatHeroHitPoint>(true).GetSpecialDotInfo(specialDotName);
  179. }
  180. public float GetAttSpeed()
  181. {
  182. return CombatHeroSkillControl.NormalAttSpeedScale;
  183. }
  184. public void HeroResurrection()
  185. {
  186. isDie = false;
  187. CombatAIBasic.ChangeState(CombatHeroStateType.idle);
  188. HeroHpUpdateEventData heroHpUpdateEventData = HeroHpUpdateEventData.Create();
  189. heroHpUpdateEventData.combatHeroEntity = this;
  190. CombatEventManager.Instance.Dispatch(CombatEventType.HeroHpUpdate, heroHpUpdateEventData);
  191. }
  192. public void HeroDie(HarmReturnInfo harmReturnInfo)
  193. {
  194. heroDieNodeId = CombatController.currActiveCombat.CombatTypeBasic.allWinNodeCount;
  195. isDie = true;
  196. HeroDieEventData heroDieEventData = HeroDieEventData.Create();
  197. heroDieEventData.combatHeroEntity = this;
  198. heroDieEventData.HarmReturnInfo = harmReturnInfo;
  199. CombatEventManager.Instance.Dispatch(CombatEventType.HeroDie, heroDieEventData);
  200. combatHeroGameObject.HeroDie();
  201. CombatAIBasic.ChangeState(CombatHeroStateType.dile);
  202. }
  203. public void HeroHurt(HarmReturnInfo harmReturnInfo)
  204. {
  205. CurrCombatHeroInfo.hp -= harmReturnInfo.att;
  206. UpdateHarmText(harmReturnInfo);
  207. if (combatHeroGameObject.HeroGPUMono != null)
  208. {
  209. _injuriedShowTime = 0.4f;
  210. combatHeroGameObject.HeroGPUMono.injuriedStrength = 0;
  211. _isAddinjuriedShow = true;
  212. }
  213. HeroHpUpdateEventData heroHpUpdateEventData = HeroHpUpdateEventData.Create();
  214. heroHpUpdateEventData.combatHeroEntity = this;
  215. CombatEventManager.Instance.Dispatch(CombatEventType.HeroHpUpdate, heroHpUpdateEventData);
  216. if (CurrCombatHeroInfo.hp <= 0)
  217. {
  218. HeroDie(harmReturnInfo);
  219. }
  220. }
  221. private void UpdateHarmText(HarmReturnInfo harmReturnInfo)
  222. {
  223. float currTime = Time.time;
  224. if (currTime - _lasetShowHarmTime < 0.1f)
  225. {
  226. return;
  227. }
  228. _lasetShowHarmTime = currTime;
  229. HarmUpdateEventData harmUpdateEventData = HarmUpdateEventData.Create();
  230. harmUpdateEventData.HarmReturnInfo = harmReturnInfo;
  231. CombatEventManager.Instance.Dispatch(CombatEventType.HarmUpdate, harmUpdateEventData);
  232. }
  233. public void Recover(HarmReturnInfo harmReturnInfo)
  234. {
  235. CurrCombatHeroInfo.hp += harmReturnInfo.att;
  236. HarmUpdateEventData harmUpdateEventData = HarmUpdateEventData.Create();
  237. harmUpdateEventData.HarmReturnInfo = harmReturnInfo;
  238. CombatEventManager.Instance.Dispatch(CombatEventType.RecoverUpdate, harmUpdateEventData);
  239. HeroHpUpdateEventData heroHpUpdateEventData = HeroHpUpdateEventData.Create();
  240. heroHpUpdateEventData.combatHeroEntity = this;
  241. CombatEventManager.Instance.Dispatch(CombatEventType.HeroHpUpdate, heroHpUpdateEventData);
  242. }
  243. public void CloseLoopFx()
  244. {
  245. for (int i = 0; i < heroLoopParticle.Count; i++)
  246. {
  247. GObjectPool.Instance.Recycle(heroLoopParticle[i]);
  248. }
  249. heroLoopParticle.Clear();
  250. }
  251. public bool IsAttDis(Vector3 pos)
  252. {
  253. return Vector3.SqrMagnitude(pos - combatHeroGameObject.transform.position) < CurrCombatHeroInfo.maxDisTo;
  254. }
  255. public void Dispose()
  256. {
  257. CombatHeroSkillControl.Dispose();
  258. combatHeroGameObject.Dispose();
  259. CombatAIBasic.Dispose();
  260. combatHeroTimeLineControl.Dispose();
  261. combatHeroAnimtion.Dispose();
  262. CloseLoopFx();
  263. }
  264. }