CombatHeroEntity.cs 10 KB

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