CombatHeroEntity.cs 9.8 KB

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