CombatHeroEntity.cs 10 KB

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