CombatHeroEntity.cs 10 KB

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