CombatHeroEntity.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. CombatAIBasic.Update(t);
  120. CombatHeroSkillControl.Update(t);
  121. combatHeroTimeLineControl.Update(t);
  122. combatHeroAnimtion.Update(t);
  123. if (combatHeroGameObject.HeroGPUMono != null)
  124. {
  125. if (_injuriedShowTime > 0)
  126. {
  127. _injuriedShowTime -= t;
  128. if (_isAddinjuriedShow)
  129. {
  130. combatHeroGameObject.HeroGPUMono.injuriedStrength += t * (_addInjuiedValue);
  131. if (combatHeroGameObject.HeroGPUMono.injuriedStrength >= 1f)
  132. {
  133. _isAddinjuriedShow = false;
  134. }
  135. }
  136. else
  137. {
  138. combatHeroGameObject.HeroGPUMono.injuriedStrength -= t * (_addInjuiedValue);
  139. }
  140. }
  141. else
  142. {
  143. combatHeroGameObject.HeroGPUMono.injuriedStrength = 0;
  144. }
  145. }
  146. }
  147. public T This<T>()
  148. {
  149. return (T)(object)this;
  150. }
  151. public T GetILifetCycleHitPoint<T>(string hitPoinName, bool isStandType, bool isIgnoreHind)
  152. where T : ILifetCycleHitPoint
  153. {
  154. return combatHeroGameObject.GetILifetCycleHitPoint<T>(hitPoinName, isStandType, isIgnoreHind);
  155. }
  156. public T GetMainHotPoin<T>(bool isIgnoreHind = false) where T : ILifetCycleHitPoint
  157. {
  158. return combatHeroGameObject.GetMainHotPoin<T>(isIgnoreHind);
  159. }
  160. public void PlayAnim(string animName, bool isLoop, int layerId, bool repeat, float speed)
  161. {
  162. combatHeroAnimtion.Play(animName, speed);
  163. }
  164. public SpecialDotInfo GetSpecialDotInfo(string specialDotName)
  165. {
  166. return combatHeroGameObject.GetMainHotPoin<CombatHeroHitPoint>(true).GetSpecialDotInfo(specialDotName);
  167. }
  168. public float GetAttSpeed()
  169. {
  170. return CombatHeroSkillControl.NormalAttSpeedScale;
  171. }
  172. public void HeroResurrection()
  173. {
  174. isDie = false;
  175. CombatAIBasic.ChangeState(CombatHeroStateType.idle);
  176. HeroHpUpdateEventData heroHpUpdateEventData = HeroHpUpdateEventData.Create();
  177. heroHpUpdateEventData.combatHeroEntity = this;
  178. CombatEventManager.Instance.Dispatch(CombatEventType.HeroHpUpdate, heroHpUpdateEventData);
  179. }
  180. public void HeroDie(HarmReturnInfo harmReturnInfo)
  181. {
  182. heroDieNodeId = CombatController.currActiveCombat.CombatTypeBasic.allWinNodeCount;
  183. isDie = true;
  184. HeroDieEventData heroDieEventData = HeroDieEventData.Create();
  185. heroDieEventData.combatHeroEntity = this;
  186. heroDieEventData.HarmReturnInfo = harmReturnInfo;
  187. CombatEventManager.Instance.Dispatch(CombatEventType.HeroDie, heroDieEventData);
  188. combatHeroGameObject.HeroDie();
  189. CombatAIBasic.ChangeState(CombatHeroStateType.dile);
  190. }
  191. public void HeroHurt(HarmReturnInfo harmReturnInfo)
  192. {
  193. CurrCombatHeroInfo.hp -= harmReturnInfo.att;
  194. UpdateHarmText(harmReturnInfo);
  195. if (combatHeroGameObject.HeroGPUMono != null)
  196. {
  197. _injuriedShowTime = 0.4f;
  198. combatHeroGameObject.HeroGPUMono.injuriedStrength = 0;
  199. _isAddinjuriedShow = true;
  200. }
  201. HeroHpUpdateEventData heroHpUpdateEventData = HeroHpUpdateEventData.Create();
  202. heroHpUpdateEventData.combatHeroEntity = this;
  203. CombatEventManager.Instance.Dispatch(CombatEventType.HeroHpUpdate, heroHpUpdateEventData);
  204. if (CurrCombatHeroInfo.hp <= 0)
  205. {
  206. HeroDie(harmReturnInfo);
  207. }
  208. }
  209. private void UpdateHarmText(HarmReturnInfo harmReturnInfo)
  210. {
  211. float currTime = Time.time;
  212. if (currTime - _lasetShowHarmTime < 0.1f)
  213. {
  214. return;
  215. }
  216. _lasetShowHarmTime = currTime;
  217. HarmUpdateEventData harmUpdateEventData = HarmUpdateEventData.Create();
  218. harmUpdateEventData.HarmReturnInfo = harmReturnInfo;
  219. CombatEventManager.Instance.Dispatch(CombatEventType.HarmUpdate, harmUpdateEventData);
  220. }
  221. public void Recover(HarmReturnInfo harmReturnInfo)
  222. {
  223. CurrCombatHeroInfo.hp += harmReturnInfo.att;
  224. HarmUpdateEventData harmUpdateEventData = HarmUpdateEventData.Create();
  225. harmUpdateEventData.HarmReturnInfo = harmReturnInfo;
  226. CombatEventManager.Instance.Dispatch(CombatEventType.RecoverUpdate, harmUpdateEventData);
  227. HeroHpUpdateEventData heroHpUpdateEventData = HeroHpUpdateEventData.Create();
  228. heroHpUpdateEventData.combatHeroEntity = this;
  229. CombatEventManager.Instance.Dispatch(CombatEventType.HeroHpUpdate, heroHpUpdateEventData);
  230. }
  231. public void CloseLoopFx()
  232. {
  233. for (int i = 0; i < heroLoopParticle.Count; i++)
  234. {
  235. GObjectPool.Instance.Recycle(heroLoopParticle[i]);
  236. }
  237. heroLoopParticle.Clear();
  238. }
  239. public bool IsAttDis(Vector3 pos)
  240. {
  241. return Vector3.SqrMagnitude(pos - combatHeroGameObject.transform.position) < CurrCombatHeroInfo.maxDisTo;
  242. }
  243. public void Dispose()
  244. {
  245. CombatHeroSkillControl.Dispose();
  246. combatHeroGameObject.Dispose();
  247. CombatAIBasic.Dispose();
  248. combatHeroTimeLineControl.Dispose();
  249. combatHeroAnimtion.Dispose();
  250. CloseLoopFx();
  251. }
  252. }