CombatHeroEntity.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 UnityEngine;
  11. using UnityEngine.AI;
  12. using UnityEngine.Rendering;
  13. using UTool.CustomizeTimeLogic.FxLogic.TimeLineEventinterface;
  14. public class CombatHeroEntity : ITimeLineSpecialDotPos, ILifeCycle, ITimeLineAnimtion, ITimeLineGetAttSpeed, IHero
  15. {
  16. /// <summary>
  17. /// 死亡时的节点ID
  18. /// </summary>
  19. public int heroDieNodeId;
  20. public bool IsEnemy { get; set; }
  21. public bool isDie { get; set; }
  22. public int number;
  23. public CombatAIBasic CombatAIBasic;
  24. public CombatHeroGameObject combatHeroGameObject;
  25. public CombatHeroInfo CurrCombatHeroInfo;
  26. public CombatHeroInfo MaxCombatHeroInfo;
  27. public CombatHeroTimeLineControl combatHeroTimeLineControl;
  28. public HeroAnimtionBasic combatHeroAnimtion;
  29. public CombatHeroSkillControl CombatHeroSkillControl;
  30. public bool isFollowState;
  31. public BetterList<CombatParticleSystemPool> heroLoopParticle = new BetterList<CombatParticleSystemPool>();
  32. public Vector3 dotPos
  33. {
  34. get { return combatHeroGameObject.position; }
  35. }
  36. public T GetThis<T>() where T : IHero
  37. {
  38. return (T)(object)this;
  39. }
  40. public GameObject GameObject
  41. {
  42. get { return combatHeroGameObject.transform.gameObject; }
  43. }
  44. public Vector3 faceDir { get; }
  45. public async CTask<CombatHeroEntity> Init(CombatAIBasic combatAIBasic, CombatHeroInfo combatHeroInfo,
  46. System.Action<CombatHeroEntity> callBack = null)
  47. {
  48. //后面记到检查战斗里面不要出现异步加载,也不要出现同步IO加载
  49. string modelName = combatHeroInfo.modelName;
  50. if (combatHeroInfo.isGpu)
  51. {
  52. modelName += "_gpu";
  53. }
  54. // GameTimeLineParticleFactory
  55. GameObjectPool poolInterface =
  56. await GObjectPool.Instance.FetchAsync<GameObjectPool>(modelName + ".prefab", null);
  57. #if !COMBAT_SERVER
  58. if (poolInterface == null || poolInterface.own == null)
  59. {
  60. return null;
  61. }
  62. poolInterface.own.SetActive(false);
  63. AssetHandle assetHandle =
  64. await AssetBundleLoadManager.Instance.LoadAssetAsyncTask<TextAsset>(combatHeroInfo.modelName + "_TD.txt");
  65. TextAsset textAsset = assetHandle.AssetObject<TextAsset>();
  66. TimeLienData timeLienData = JsonManager.FromJson<TimeLienData>(textAsset.text);
  67. timeLienData.DeserializeData();
  68. assetHandle.Release();
  69. combatHeroTimeLineControl = new CombatHeroTimeLineControl();
  70. combatHeroTimeLineControl.Init(this,timeLienData);
  71. combatHeroGameObject = new CombatHeroGameObject();
  72. combatHeroGameObject.Init(this, poolInterface);
  73. NavMeshAgent navMeshAgent = poolInterface.own.GetComponent<NavMeshAgent>();
  74. if (combatAIBasic == null)
  75. {
  76. combatAIBasic = new CombatAIBasic();
  77. }
  78. HeroEntityMono heroEntityMono = poolInterface.own.GetComponent<HeroEntityMono>();
  79. if (heroEntityMono == null)
  80. {
  81. heroEntityMono = poolInterface.own.AddComponent<HeroEntityMono>();
  82. }
  83. heroEntityMono.combatHeroEntity = this;
  84. CombatAIBasic = combatAIBasic;
  85. CombatAIBasic.Init(this, navMeshAgent);
  86. CurrCombatHeroInfo = combatHeroInfo.Copy();
  87. MaxCombatHeroInfo = combatHeroInfo.Copy();
  88. CombatHeroSkillControl = new CombatHeroSkillControl();
  89. CombatHeroSkillControl.Init(this);
  90. AnimancerComponent animancerComponent = poolInterface.own.GetComponent<AnimancerComponent>();
  91. if (animancerComponent != null)
  92. {
  93. combatHeroAnimtion = new CombatHeroAnimtion();
  94. }
  95. else
  96. {
  97. combatHeroAnimtion = new CombatHeroGPUAnimtion();
  98. }
  99. combatHeroAnimtion.Init(this);
  100. CombatAIBasic.ChangeState(CombatHeroStateType.XiuMian);
  101. CreateHeroHpEventData createHeroHpEventData = CreateHeroHpEventData.Create();
  102. createHeroHpEventData.combatHeroEntity = this;
  103. EventManager.Instance.Dispatch(CustomEventType.CreateHeroHp, createHeroHpEventData);
  104. poolInterface.own.SetActive(true);
  105. callBack?.Invoke(this);
  106. #endif
  107. return this;
  108. }
  109. public void Update(float t)
  110. {
  111. CombatAIBasic.Update(t);
  112. CombatHeroSkillControl.Update(t);
  113. combatHeroTimeLineControl.Update(t);
  114. combatHeroAnimtion.Update(t);
  115. }
  116. public T This<T>()
  117. {
  118. return (T)(object)this;
  119. }
  120. public T GetILifetCycleHitPoint<T>(string hitPoinName, bool isStandType, bool isIgnoreHind)
  121. where T : ILifetCycleHitPoint
  122. {
  123. return combatHeroGameObject.GetILifetCycleHitPoint<T>(hitPoinName, isStandType, isIgnoreHind);
  124. }
  125. public T GetMainHotPoin<T>(bool isIgnoreHind = false) where T : ILifetCycleHitPoint
  126. {
  127. return combatHeroGameObject.GetMainHotPoin<T>(isIgnoreHind);
  128. }
  129. public void PlayAnim(string animName, bool isLoop, int layerId, bool repeat, float speed)
  130. {
  131. combatHeroAnimtion.Play(animName, speed);
  132. }
  133. public SpecialDotInfo GetSpecialDotInfo(string specialDotName)
  134. {
  135. return combatHeroGameObject.GetMainHotPoin<CombatHeroHitPoint>(true).GetSpecialDotInfo(specialDotName);
  136. }
  137. public float GetAttSpeed()
  138. {
  139. return CombatHeroSkillControl.NormalAttSpeedScale;
  140. }
  141. public void HeroResurrection()
  142. {
  143. isDie = false;
  144. CombatAIBasic.ChangeState(CombatHeroStateType.idle);
  145. HeroHpUpdateEventData heroHpUpdateEventData = HeroHpUpdateEventData.Create();
  146. heroHpUpdateEventData.combatHeroEntity = this;
  147. CombatEventManager.Instance.Dispatch(CombatEventType.HeroHpUpdate, heroHpUpdateEventData);
  148. }
  149. public void HeroDie(HarmReturnInfo harmReturnInfo)
  150. {
  151. heroDieNodeId = CombatController.currActiveCombat.CombatTypeBasic.allWinNodeCount;
  152. isDie = true;
  153. HeroDieEventData heroDieEventData = HeroDieEventData.Create();
  154. heroDieEventData.combatHeroEntity = this;
  155. heroDieEventData.HarmReturnInfo = harmReturnInfo;
  156. CombatEventManager.Instance.Dispatch(CombatEventType.HeroDie, heroDieEventData);
  157. combatHeroGameObject.HeroDie();
  158. CombatAIBasic.ChangeState(CombatHeroStateType.dile);
  159. }
  160. public void HeroHurt(HarmReturnInfo harmReturnInfo)
  161. {
  162. CurrCombatHeroInfo.hp -= harmReturnInfo.att;
  163. HarmUpdateEventData harmUpdateEventData = HarmUpdateEventData.Create();
  164. harmUpdateEventData.HarmReturnInfo = harmReturnInfo;
  165. CombatEventManager.Instance.Dispatch(CombatEventType.HarmUpdate, harmUpdateEventData);
  166. HeroHpUpdateEventData heroHpUpdateEventData = HeroHpUpdateEventData.Create();
  167. heroHpUpdateEventData.combatHeroEntity = this;
  168. CombatEventManager.Instance.Dispatch(CombatEventType.HeroHpUpdate, heroHpUpdateEventData);
  169. if (CurrCombatHeroInfo.hp <= 0)
  170. {
  171. HeroDie(harmReturnInfo);
  172. }
  173. }
  174. public void Recover(HarmReturnInfo harmReturnInfo)
  175. {
  176. CurrCombatHeroInfo.hp += harmReturnInfo.att;
  177. HarmUpdateEventData harmUpdateEventData = HarmUpdateEventData.Create();
  178. harmUpdateEventData.HarmReturnInfo = harmReturnInfo;
  179. CombatEventManager.Instance.Dispatch(CombatEventType.RecoverUpdate, harmUpdateEventData);
  180. HeroHpUpdateEventData heroHpUpdateEventData = HeroHpUpdateEventData.Create();
  181. heroHpUpdateEventData.combatHeroEntity = this;
  182. CombatEventManager.Instance.Dispatch(CombatEventType.HeroHpUpdate, heroHpUpdateEventData);
  183. }
  184. public void CloseLoopFx()
  185. {
  186. for (int i = 0; i < heroLoopParticle.Count; i++)
  187. {
  188. GObjectPool.Instance.Recycle(heroLoopParticle[i]);
  189. }
  190. heroLoopParticle.Clear();
  191. }
  192. public bool IsAttDis(Vector3 pos)
  193. {
  194. return Vector3.SqrMagnitude(pos - combatHeroGameObject.transform.position) < CurrCombatHeroInfo.maxDisTo;
  195. }
  196. public void Dispose()
  197. {
  198. CombatHeroSkillControl.Dispose();
  199. combatHeroGameObject.Dispose();
  200. CombatAIBasic.Dispose();
  201. combatHeroTimeLineControl.Dispose();
  202. combatHeroAnimtion.Dispose();
  203. CloseLoopFx();
  204. }
  205. }