CombatHeroEntity.cs 12 KB

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