CombatHeroEntity.cs 7.3 KB

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