CombatHeroEntity.cs 7.6 KB

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