CombatHeroEntity.cs 11 KB

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