CombatHeroEntity.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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,
  67. Vector3 pos,
  68. System.Action<CombatHeroEntity> callBack = null)
  69. {
  70. //后面记到检查战斗里面不要出现异步加载,也不要出现同步IO加载
  71. string modelName = combatHeroInfo.modelName;
  72. if (combatHeroInfo.isGpu)
  73. {
  74. modelName += "_gpu";
  75. }
  76. CurrCombatHeroInfo = combatHeroInfo.Copy();
  77. MaxCombatHeroInfo = combatHeroInfo.Copy();
  78. // GameTimeLineParticleFactory
  79. CombatHeroGameObjectPool poolInterface =
  80. await GObjectPool.Instance.FetchAsync<CombatHeroGameObjectPool>(modelName + ".prefab", null);
  81. #if !COMBAT_SERVER
  82. if (poolInterface == null || poolInterface.own == null)
  83. {
  84. return null;
  85. }
  86. poolInterface.own.transform.position = pos;
  87. if (!IsEnemy)
  88. {
  89. GameObjectPool fx_hero_quan =
  90. await GObjectPool.Instance.FetchAsync<GameObjectPool>("fx_hero_quan.prefab", null);
  91. fx_hero_quan.own.transform.SetParent(poolInterface.own.transform);
  92. fx_hero_quan.own.transform.localPosition = Vector3.zero;
  93. }
  94. poolInterface.own.SetActive(false);
  95. AssetHandle assetHandle =
  96. await AssetBundleLoadManager.Instance.LoadAssetAsyncTask<TextAsset>(combatHeroInfo.modelName + "_TD.txt");
  97. TextAsset textAsset = assetHandle.AssetObject<TextAsset>();
  98. combatHeroGameObject = new CombatHeroGameObject();
  99. combatHeroGameObject.Init(this, poolInterface);
  100. TimeLienData timeLienData = JsonManager.FromJson<TimeLienData>(textAsset.text);
  101. timeLienData.DeserializeData();
  102. assetHandle.Release();
  103. combatHeroTimeLineControl = new CombatHeroTimeLineControl();
  104. combatHeroTimeLineControl.Init(this);
  105. combatHeroTimeLineControl.AddTimeLienData(timeLienData);
  106. BuffControl = new BuffControl();
  107. BuffControl.Init(this);
  108. NavMeshAgent navMeshAgent = poolInterface.own.GetComponent<NavMeshAgent>();
  109. if (combatAIBasic == null)
  110. {
  111. combatAIBasic = new CombatAIBasic();
  112. }
  113. HeroEntityMono heroEntityMono = poolInterface.own.GetComponent<HeroEntityMono>();
  114. if (heroEntityMono == null)
  115. {
  116. heroEntityMono = poolInterface.own.AddComponent<HeroEntityMono>();
  117. }
  118. heroEntityMono.combatHeroEntity = this;
  119. CombatAIBasic = combatAIBasic;
  120. CombatAIBasic.Init(this);
  121. CombatHeroSkillControl = new CombatHeroSkillControl();
  122. CombatHeroSkillControl.Init(this);
  123. AnimancerComponent animancerComponent = poolInterface.own.GetComponent<AnimancerComponent>();
  124. if (animancerComponent != null)
  125. {
  126. combatHeroAnimtion = new CombatHeroAnimtion();
  127. }
  128. else
  129. {
  130. combatHeroAnimtion = new CombatHeroGPUAnimtion();
  131. }
  132. poolInterface.own.SetActive(true);
  133. combatHeroAnimtion.Init(this);
  134. CombatAIBasic.ChangeState(CombatHeroStateType.XiuMian);
  135. if (!IsEnemy || CurrCombatHeroInfo.heroType == 3)
  136. {
  137. CreateHeroHpEventData createHeroHpEventData = CreateHeroHpEventData.Create();
  138. createHeroHpEventData.combatHeroEntity = this;
  139. EventManager.Instance.Dispatch(CustomEventType.CreateHeroHp, createHeroHpEventData);
  140. }
  141. callBack?.Invoke(this);
  142. #endif
  143. return this;
  144. }
  145. public virtual void Update(float t)
  146. {
  147. CombatAIBasic.Update(t);
  148. CombatHeroSkillControl.Update(t);
  149. combatHeroTimeLineControl.Update(t);
  150. combatHeroAnimtion.Update(t);
  151. combatHeroGameObject.Update(t);
  152. BuffControl.Update(t);
  153. if (combatHeroGameObject.HeroGPUMono != null)
  154. {
  155. if (_injuriedShowTime > 0)
  156. {
  157. _injuriedShowTime -= t;
  158. if (_isAddinjuriedShow)
  159. {
  160. combatHeroGameObject.HeroGPUMono.injuriedStrength += t * (_addInjuiedValue);
  161. if (combatHeroGameObject.HeroGPUMono.injuriedStrength >= 0.3f)
  162. {
  163. _isAddinjuriedShow = false;
  164. }
  165. }
  166. else
  167. {
  168. combatHeroGameObject.HeroGPUMono.injuriedStrength -= t * (_addInjuiedValue);
  169. if (combatHeroGameObject.HeroGPUMono.injuriedStrength < 0)
  170. {
  171. combatHeroGameObject.HeroGPUMono.injuriedStrength = 0;
  172. }
  173. }
  174. }
  175. else
  176. {
  177. combatHeroGameObject.HeroGPUMono.injuriedStrength = 0;
  178. }
  179. }
  180. }
  181. public T This<T>()
  182. {
  183. return (T)(object)this;
  184. }
  185. public T GetILifetCycleHitPoint<T>(string hitPoinName, bool isStandType, bool isIgnoreHind)
  186. where T : ILifetCycleHitPoint
  187. {
  188. return combatHeroGameObject.GetILifetCycleHitPoint<T>(hitPoinName, isStandType, isIgnoreHind);
  189. }
  190. public T GetMainHotPoin<T>(bool isIgnoreHind = false) where T : ILifetCycleHitPoint
  191. {
  192. return combatHeroGameObject.GetMainHotPoin<T>(isIgnoreHind);
  193. }
  194. public void PlayAnim(string animName, bool isLoop, int layerId, bool repeat, float speed)
  195. {
  196. combatHeroAnimtion.Play(animName, speed);
  197. }
  198. public SpecialDotInfo GetSpecialDotInfo(string specialDotName)
  199. {
  200. return combatHeroGameObject.GetMainHotPoin<CombatHeroHitPoint>(true).GetSpecialDotInfo(specialDotName);
  201. }
  202. public float GetAttSpeed()
  203. {
  204. return CombatHeroSkillControl.NormalAttSpeedScale;
  205. }
  206. public void HeroResurrection()
  207. {
  208. isDie = false;
  209. CombatAIBasic.ChangeState(CombatHeroStateType.idle, isQiangZhi: true);
  210. HeroHpUpdateEventData heroHpUpdateEventData = HeroHpUpdateEventData.Create();
  211. heroHpUpdateEventData.combatHeroEntity = this;
  212. CombatEventManager.Instance.Dispatch(CombatEventType.HeroHpUpdate, heroHpUpdateEventData);
  213. HeroResurrectionEventData heroResurrectionEventData = HeroResurrectionEventData.Create();
  214. heroResurrectionEventData.combatHeroEntity = this;
  215. CombatEventManager.Instance.Dispatch(CombatEventType.HeroResurrection, heroResurrectionEventData);
  216. }
  217. public void HeroDie(HarmReturnInfo harmReturnInfo)
  218. {
  219. heroDieNodeId = CombatController.currActiveCombat.CombatTypeBasic.allWinNodeCount;
  220. isDie = true;
  221. HeroDieEventData heroDieEventData = HeroDieEventData.Create();
  222. heroDieEventData.combatHeroEntity = this;
  223. heroDieEventData.HarmReturnInfo = harmReturnInfo;
  224. CombatEventManager.Instance.Dispatch(CombatEventType.HeroDie, heroDieEventData);
  225. combatHeroGameObject.HeroDie();
  226. CombatAIBasic.ChangeState(CombatHeroStateType.dile);
  227. CombatEventManager.Instance.Dispatch(CombatEventType.HeroDieFinish, heroDieEventData);
  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. }