CombatHeroEntity.cs 7.1 KB

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