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,Vector3 pos,
  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. CombatHeroGameObjectPool poolInterface =
  70. await GObjectPool.Instance.FetchAsync<CombatHeroGameObjectPool>(modelName + ".prefab", null);
  71. #if !COMBAT_SERVER
  72. if (poolInterface == null || poolInterface.own == null)
  73. {
  74. return null;
  75. }
  76. poolInterface.own.transform.position= pos;
  77. if (!IsEnemy)
  78. {
  79. GameObjectPool fx_hero_quan =
  80. await GObjectPool.Instance.FetchAsync<GameObjectPool>("fx_hero_quan.prefab", null);
  81. fx_hero_quan.own.transform.SetParent(poolInterface.own.transform);
  82. }
  83. poolInterface.own.SetActive(false);
  84. AssetHandle assetHandle =
  85. await AssetBundleLoadManager.Instance.LoadAssetAsyncTask<TextAsset>(combatHeroInfo.modelName + "_TD.txt");
  86. TextAsset textAsset = assetHandle.AssetObject<TextAsset>();
  87. combatHeroGameObject = new CombatHeroGameObject();
  88. combatHeroGameObject.Init(this, poolInterface);
  89. TimeLienData timeLienData = JsonManager.FromJson<TimeLienData>(textAsset.text);
  90. timeLienData.DeserializeData();
  91. assetHandle.Release();
  92. combatHeroTimeLineControl = new CombatHeroTimeLineControl();
  93. combatHeroTimeLineControl.Init(this, timeLienData);
  94. NavMeshAgent navMeshAgent = poolInterface.own.GetComponent<NavMeshAgent>();
  95. if (combatAIBasic == null)
  96. {
  97. combatAIBasic = new CombatAIBasic();
  98. }
  99. HeroEntityMono heroEntityMono = poolInterface.own.GetComponent<HeroEntityMono>();
  100. if (heroEntityMono == null)
  101. {
  102. heroEntityMono = poolInterface.own.AddComponent<HeroEntityMono>();
  103. }
  104. heroEntityMono.combatHeroEntity = this;
  105. CombatAIBasic = combatAIBasic;
  106. CombatAIBasic.Init(this, navMeshAgent);
  107. CombatHeroSkillControl = new CombatHeroSkillControl();
  108. CombatHeroSkillControl.Init(this);
  109. AnimancerComponent animancerComponent = poolInterface.own.GetComponent<AnimancerComponent>();
  110. if (animancerComponent != null)
  111. {
  112. combatHeroAnimtion = new CombatHeroAnimtion();
  113. }
  114. else
  115. {
  116. combatHeroAnimtion = new CombatHeroGPUAnimtion();
  117. }
  118. combatHeroAnimtion.Init(this);
  119. CombatAIBasic.ChangeState(CombatHeroStateType.XiuMian);
  120. if (!IsEnemy)
  121. {
  122. CreateHeroHpEventData createHeroHpEventData = CreateHeroHpEventData.Create();
  123. createHeroHpEventData.combatHeroEntity = this;
  124. EventManager.Instance.Dispatch(CustomEventType.CreateHeroHp, createHeroHpEventData);
  125. }
  126. poolInterface.own.SetActive(true);
  127. callBack?.Invoke(this);
  128. #endif
  129. return this;
  130. }
  131. public void Update(float t)
  132. {
  133. if (!CombatController.currActiveCombat.isStopAi)
  134. {
  135. CombatAIBasic.Update(t);
  136. }
  137. CombatHeroSkillControl.Update(t);
  138. combatHeroTimeLineControl.Update(t);
  139. combatHeroAnimtion.Update(t);
  140. if (combatHeroGameObject.HeroGPUMono != null)
  141. {
  142. if (_injuriedShowTime > 0)
  143. {
  144. _injuriedShowTime -= t;
  145. if (_isAddinjuriedShow)
  146. {
  147. combatHeroGameObject.HeroGPUMono.injuriedStrength += t * (_addInjuiedValue);
  148. if (combatHeroGameObject.HeroGPUMono.injuriedStrength >= 1f)
  149. {
  150. _isAddinjuriedShow = false;
  151. }
  152. }
  153. else
  154. {
  155. combatHeroGameObject.HeroGPUMono.injuriedStrength -= t * (_addInjuiedValue);
  156. }
  157. }
  158. else
  159. {
  160. combatHeroGameObject.HeroGPUMono.injuriedStrength = 0;
  161. }
  162. }
  163. }
  164. public T This<T>()
  165. {
  166. return (T)(object)this;
  167. }
  168. public T GetILifetCycleHitPoint<T>(string hitPoinName, bool isStandType, bool isIgnoreHind)
  169. where T : ILifetCycleHitPoint
  170. {
  171. return combatHeroGameObject.GetILifetCycleHitPoint<T>(hitPoinName, isStandType, isIgnoreHind);
  172. }
  173. public T GetMainHotPoin<T>(bool isIgnoreHind = false) where T : ILifetCycleHitPoint
  174. {
  175. return combatHeroGameObject.GetMainHotPoin<T>(isIgnoreHind);
  176. }
  177. public void PlayAnim(string animName, bool isLoop, int layerId, bool repeat, float speed)
  178. {
  179. combatHeroAnimtion.Play(animName, speed);
  180. }
  181. public SpecialDotInfo GetSpecialDotInfo(string specialDotName)
  182. {
  183. return combatHeroGameObject.GetMainHotPoin<CombatHeroHitPoint>(true).GetSpecialDotInfo(specialDotName);
  184. }
  185. public float GetAttSpeed()
  186. {
  187. return CombatHeroSkillControl.NormalAttSpeedScale;
  188. }
  189. public void HeroResurrection()
  190. {
  191. isDie = false;
  192. CombatAIBasic.ChangeState(CombatHeroStateType.idle);
  193. HeroHpUpdateEventData heroHpUpdateEventData = HeroHpUpdateEventData.Create();
  194. heroHpUpdateEventData.combatHeroEntity = this;
  195. CombatEventManager.Instance.Dispatch(CombatEventType.HeroHpUpdate, heroHpUpdateEventData);
  196. }
  197. public void HeroDie(HarmReturnInfo harmReturnInfo)
  198. {
  199. heroDieNodeId = CombatController.currActiveCombat.CombatTypeBasic.allWinNodeCount;
  200. isDie = true;
  201. HeroDieEventData heroDieEventData = HeroDieEventData.Create();
  202. heroDieEventData.combatHeroEntity = this;
  203. heroDieEventData.HarmReturnInfo = harmReturnInfo;
  204. CombatEventManager.Instance.Dispatch(CombatEventType.HeroDie, heroDieEventData);
  205. combatHeroGameObject.HeroDie();
  206. CombatAIBasic.ChangeState(CombatHeroStateType.dile);
  207. }
  208. public void HeroHurt(HarmReturnInfo harmReturnInfo)
  209. {
  210. CurrCombatHeroInfo.hp -= harmReturnInfo.att;
  211. UpdateHarmText(harmReturnInfo);
  212. if (combatHeroGameObject.HeroGPUMono != null)
  213. {
  214. _injuriedShowTime = 0.4f;
  215. combatHeroGameObject.HeroGPUMono.injuriedStrength = 0;
  216. _isAddinjuriedShow = true;
  217. }
  218. HeroHpUpdateEventData heroHpUpdateEventData = HeroHpUpdateEventData.Create();
  219. heroHpUpdateEventData.combatHeroEntity = this;
  220. CombatEventManager.Instance.Dispatch(CombatEventType.HeroHpUpdate, heroHpUpdateEventData);
  221. if (CurrCombatHeroInfo.hp <= 0)
  222. {
  223. HeroDie(harmReturnInfo);
  224. }
  225. }
  226. private void UpdateHarmText(HarmReturnInfo harmReturnInfo)
  227. {
  228. float currTime = Time.time;
  229. if (currTime - _lasetShowHarmTime < 0.1f)
  230. {
  231. return;
  232. }
  233. _lasetShowHarmTime = currTime;
  234. HarmUpdateEventData harmUpdateEventData = HarmUpdateEventData.Create();
  235. harmUpdateEventData.HarmReturnInfo = harmReturnInfo;
  236. CombatEventManager.Instance.Dispatch(CombatEventType.HarmUpdate, harmUpdateEventData);
  237. }
  238. public void Recover(HarmReturnInfo harmReturnInfo)
  239. {
  240. CurrCombatHeroInfo.hp += harmReturnInfo.att;
  241. HarmUpdateEventData harmUpdateEventData = HarmUpdateEventData.Create();
  242. harmUpdateEventData.HarmReturnInfo = harmReturnInfo;
  243. CombatEventManager.Instance.Dispatch(CombatEventType.RecoverUpdate, harmUpdateEventData);
  244. HeroHpUpdateEventData heroHpUpdateEventData = HeroHpUpdateEventData.Create();
  245. heroHpUpdateEventData.combatHeroEntity = this;
  246. CombatEventManager.Instance.Dispatch(CombatEventType.HeroHpUpdate, heroHpUpdateEventData);
  247. }
  248. public void CloseLoopFx()
  249. {
  250. for (int i = 0; i < heroLoopParticle.Count; i++)
  251. {
  252. GObjectPool.Instance.Recycle(heroLoopParticle[i]);
  253. }
  254. heroLoopParticle.Clear();
  255. }
  256. public bool IsAttDis(Vector3 pos)
  257. {
  258. return Vector3.SqrMagnitude(pos - combatHeroGameObject.transform.position) < CurrCombatHeroInfo.maxDisTo;
  259. }
  260. public void Dispose()
  261. {
  262. CombatHeroSkillControl.Dispose();
  263. combatHeroGameObject.Dispose();
  264. CombatAIBasic.Dispose();
  265. combatHeroTimeLineControl.Dispose();
  266. combatHeroAnimtion.Dispose();
  267. CloseLoopFx();
  268. }
  269. }