CombatHeroEntity.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. }
  229. public void HeroHurt(HarmReturnInfo harmReturnInfo)
  230. {
  231. CurrCombatHeroInfo.hp -= harmReturnInfo.att;
  232. UpdateHarmText(harmReturnInfo);
  233. if (combatHeroGameObject.HeroGPUMono != null)
  234. {
  235. _injuriedShowTime = 0.4f;
  236. combatHeroGameObject.HeroGPUMono.injuriedStrength = 0;
  237. _isAddinjuriedShow = true;
  238. }
  239. HeroHpUpdateEventData heroHpUpdateEventData = HeroHpUpdateEventData.Create();
  240. heroHpUpdateEventData.combatHeroEntity = this;
  241. CombatEventManager.Instance.Dispatch(CombatEventType.HeroHpUpdate, heroHpUpdateEventData);
  242. if (CurrCombatHeroInfo.hp <= 0)
  243. {
  244. HeroDie(harmReturnInfo);
  245. }
  246. }
  247. private void UpdateHarmText(HarmReturnInfo harmReturnInfo)
  248. {
  249. float currTime = Time.time;
  250. if (currTime - _lasetShowHarmTime < 0.1f)
  251. {
  252. return;
  253. }
  254. _lasetShowHarmTime = currTime;
  255. HarmUpdateEventData harmUpdateEventData = HarmUpdateEventData.Create();
  256. harmUpdateEventData.HarmReturnInfo = harmReturnInfo;
  257. CombatEventManager.Instance.Dispatch(CombatEventType.HarmUpdate, harmUpdateEventData);
  258. }
  259. public void Recover(HarmReturnInfo harmReturnInfo)
  260. {
  261. CurrCombatHeroInfo.hp += harmReturnInfo.att;
  262. HarmUpdateEventData harmUpdateEventData = HarmUpdateEventData.Create();
  263. harmUpdateEventData.HarmReturnInfo = harmReturnInfo;
  264. CombatEventManager.Instance.Dispatch(CombatEventType.RecoverUpdate, harmUpdateEventData);
  265. HeroHpUpdateEventData heroHpUpdateEventData = HeroHpUpdateEventData.Create();
  266. heroHpUpdateEventData.combatHeroEntity = this;
  267. CombatEventManager.Instance.Dispatch(CombatEventType.HeroHpUpdate, heroHpUpdateEventData);
  268. }
  269. public void CloseLoopFx()
  270. {
  271. for (int i = 0; i < heroLoopParticle.Count; i++)
  272. {
  273. GObjectPool.Instance.Recycle(heroLoopParticle[i]);
  274. }
  275. heroLoopParticle.Clear();
  276. }
  277. public bool IsAttDis(Vector3 pos)
  278. {
  279. return Vector3.SqrMagnitude(pos - combatHeroGameObject.transform.position) < CurrCombatHeroInfo.maxDisTo;
  280. }
  281. public void Dispose()
  282. {
  283. if (_isDis)
  284. {
  285. return;
  286. }
  287. _isDis = true;
  288. combatHeroGameObject.Dispose();
  289. isDie = true;
  290. CombatHeroSkillControl.Dispose();
  291. CombatAIBasic.Dispose();
  292. combatHeroTimeLineControl.Dispose();
  293. combatHeroAnimtion.Dispose();
  294. CloseLoopFx();
  295. }
  296. private void ProDormancyObj()
  297. {
  298. CombatHeroSkillControl.ProDormancyObj();
  299. }
  300. public override void ActiveObj()
  301. {
  302. isDie = false;
  303. _isDis = false;
  304. }
  305. public override void DormancyObj()
  306. {
  307. Dispose();
  308. ProDormancyObj();
  309. _isDis = false;
  310. CombatAIBasic = null;
  311. combatHeroGameObject = null;
  312. CurrCombatHeroInfo = null;
  313. MaxCombatHeroInfo = null;
  314. combatHeroTimeLineControl = null;
  315. combatHeroAnimtion = null;
  316. CombatHeroSkillControl = null;
  317. isFollowState = false;
  318. heroLoopParticle.Clear();
  319. isDie = true;
  320. }
  321. }