CombatHeroEntity.cs 7.7 KB

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