CombatHeroEntity.cs 11 KB

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