CombatHeroEntity.cs 12 KB

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