CombatHeroEntity.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 : ShowBaiscEntity, 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 isFollowState;
  28. private float _lasetShowHarmTime;
  29. private float _injuriedShowTime;
  30. private bool _isAddinjuriedShow;
  31. private float _addInjuiedValue = (1.0f / 0.2f) * 0.3f;
  32. private bool _isDis;
  33. public float DisTime;
  34. public float HpBl
  35. {
  36. get { return CurrCombatHeroInfo.hp.Value * 100f / MaxCombatHeroInfo.hp.Value; }
  37. }
  38. public Vector3 dotPos
  39. {
  40. get { return combatHeroGameObject.position; }
  41. }
  42. public T GetThis<T>() where T : IHero
  43. {
  44. return (T)(object)this;
  45. }
  46. public GameObject GameObject
  47. {
  48. get { return combatHeroGameObject.transform.gameObject; }
  49. }
  50. public Vector3 faceDir
  51. {
  52. get { return combatHeroGameObject.transform.forward; }
  53. }
  54. public virtual async CTask<CombatHeroEntity> Init(CombatAIBasic combatAIBasic, CombatHeroInfo combatHeroInfo,
  55. Vector3 pos,
  56. System.Action<CombatHeroEntity> callBack = null)
  57. {
  58. //后面记到检查战斗里面不要出现异步加载,也不要出现同步IO加载
  59. string modelName = combatHeroInfo.modelName;
  60. if (combatHeroInfo.isGpu)
  61. {
  62. modelName += "_gpu";
  63. }
  64. CurrCombatHeroInfo = combatHeroInfo.Copy();
  65. MaxCombatHeroInfo = combatHeroInfo.Copy();
  66. // GameTimeLineParticleFactory
  67. CombatHeroGameObjectPool poolInterface =
  68. await GObjectPool.Instance.FetchAsync<CombatHeroGameObjectPool>(modelName + ".prefab", null);
  69. #if !COMBAT_SERVER
  70. if (poolInterface == null || poolInterface.own == null)
  71. {
  72. return null;
  73. }
  74. poolInterface.own.transform.position = pos;
  75. // if (!IsEnemy)
  76. // {
  77. // GameObjectPool fx_hero_quan =
  78. // await GObjectPool.Instance.FetchAsync<GameObjectPool>("fx_hero_quan.prefab", null);
  79. // fx_hero_quan.own.transform.SetParent(poolInterface.own.transform);
  80. // fx_hero_quan.own.transform.localPosition = Vector3.zero;
  81. // }
  82. poolInterface.own.SetActive(false);
  83. combatHeroTimeLineControl = new CombatHeroTimeLineControl();
  84. combatHeroTimeLineControl.Init(this);
  85. AssetHandle assetHandle =
  86. await AssetBundleLoadManager.Instance.LoadAssetAsyncTask<TextAsset>(combatHeroInfo.modelName + "_TD.txt");
  87. if (assetHandle != null)
  88. {
  89. TextAsset textAsset = assetHandle.AssetObject<TextAsset>();
  90. TimeLienData timeLienData = JsonManager.FromJson<TimeLienData>(textAsset.text);
  91. timeLienData.DeserializeData();
  92. assetHandle.Release();
  93. combatHeroTimeLineControl.AddTimeLienData(timeLienData);
  94. }
  95. combatHeroGameObject = new CombatHeroGameObject();
  96. combatHeroGameObject.Init(this, poolInterface);
  97. BuffControl = new BuffControl();
  98. BuffControl.Init(this);
  99. NavMeshAgent navMeshAgent = poolInterface.own.GetComponent<NavMeshAgent>();
  100. if (combatAIBasic == null)
  101. {
  102. combatAIBasic = new CombatAIBasic();
  103. }
  104. HeroEntityMono heroEntityMono = poolInterface.own.GetComponent<HeroEntityMono>();
  105. if (heroEntityMono == null)
  106. {
  107. heroEntityMono = poolInterface.own.AddComponent<HeroEntityMono>();
  108. }
  109. heroEntityMono.combatHeroEntity = this;
  110. CombatAIBasic = combatAIBasic;
  111. CombatAIBasic.Init(this);
  112. CombatHeroSkillControl = new CombatHeroSkillControl();
  113. await CombatHeroSkillControl.Init(this);
  114. AnimancerComponent animancerComponent = poolInterface.own.GetComponent<AnimancerComponent>();
  115. combatHeroAnimtion = new CombatHeroAnimtion();
  116. // if (animancerComponent != null)
  117. {
  118. }
  119. // else
  120. // {
  121. // combatHeroAnimtion = new CombatHeroGPUAnimtion();
  122. // }
  123. poolInterface.own.SetActive(true);
  124. combatHeroAnimtion.Init(this);
  125. CombatAIBasic.ChangeState(CombatHeroStateType.XiuMian);
  126. if (!IsEnemy || CurrCombatHeroInfo.heroType == 3)
  127. {
  128. CreateHeroHpEventData createHeroHpEventData = CreateHeroHpEventData.Create();
  129. createHeroHpEventData.combatHeroEntity = this;
  130. EventManager.Instance.Dispatch(CustomEventType.CreateHeroHp, createHeroHpEventData);
  131. }
  132. callBack?.Invoke(this);
  133. #endif
  134. return this;
  135. }
  136. public virtual void Update(float t)
  137. {
  138. CombatAIBasic.Update(t);
  139. CombatHeroSkillControl.Update(t);
  140. combatHeroTimeLineControl.Update(t);
  141. combatHeroAnimtion.Update(t);
  142. combatHeroGameObject.Update(t);
  143. BuffControl.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, isQiangZhi: true);
  201. HeroHpUpdateEventData heroHpUpdateEventData = HeroHpUpdateEventData.Create();
  202. heroHpUpdateEventData.combatHeroEntity = this;
  203. CombatEventManager.Instance.Dispatch(CombatEventType.HeroHpUpdate, heroHpUpdateEventData);
  204. HeroResurrectionEventData heroResurrectionEventData = HeroResurrectionEventData.Create();
  205. heroResurrectionEventData.combatHeroEntity = this;
  206. CombatEventManager.Instance.Dispatch(CombatEventType.HeroResurrection, heroResurrectionEventData);
  207. }
  208. public void HeroDie(HarmReturnInfo harmReturnInfo)
  209. {
  210. heroDieNodeId = CombatController.currActiveCombat.CombatTypeBasic.allWinNodeCount;
  211. isDie = true;
  212. HeroDieEventData heroDieEventData = HeroDieEventData.Create();
  213. heroDieEventData.combatHeroEntity = this;
  214. heroDieEventData.HarmReturnInfo = harmReturnInfo;
  215. CombatEventManager.Instance.Dispatch(CombatEventType.HeroDie, heroDieEventData);
  216. combatHeroGameObject.HeroDie();
  217. CombatAIBasic.ChangeState(CombatHeroStateType.dile);
  218. CombatEventManager.Instance.Dispatch(CombatEventType.HeroDieFinish, heroDieEventData);
  219. }
  220. public void HeroHurt(HarmReturnInfo harmReturnInfo)
  221. {
  222. CurrCombatHeroInfo.hp -= harmReturnInfo.att;
  223. UpdateHarmText(harmReturnInfo);
  224. if (combatHeroGameObject.HeroGPUMono != null)
  225. {
  226. _injuriedShowTime = 0.4f;
  227. combatHeroGameObject.HeroGPUMono.injuriedStrength = 0;
  228. _isAddinjuriedShow = true;
  229. }
  230. HeroHpUpdateEventData heroHpUpdateEventData = HeroHpUpdateEventData.Create();
  231. heroHpUpdateEventData.combatHeroEntity = this;
  232. CombatEventManager.Instance.Dispatch(CombatEventType.HeroHpUpdate, heroHpUpdateEventData);
  233. if (CurrCombatHeroInfo.hp <= 0)
  234. {
  235. HeroDie(harmReturnInfo);
  236. }
  237. }
  238. private void UpdateHarmText(HarmReturnInfo harmReturnInfo)
  239. {
  240. float currTime = Time.time;
  241. if (currTime - _lasetShowHarmTime < 0.1f)
  242. {
  243. return;
  244. }
  245. _lasetShowHarmTime = currTime;
  246. HarmUpdateEventData harmUpdateEventData = HarmUpdateEventData.Create();
  247. harmUpdateEventData.HarmReturnInfo = harmReturnInfo;
  248. CombatEventManager.Instance.Dispatch(CombatEventType.HarmUpdate, harmUpdateEventData);
  249. }
  250. public void Recover(HarmReturnInfo harmReturnInfo)
  251. {
  252. CurrCombatHeroInfo.hp += harmReturnInfo.att;
  253. HarmUpdateEventData harmUpdateEventData = HarmUpdateEventData.Create();
  254. harmUpdateEventData.HarmReturnInfo = harmReturnInfo;
  255. CombatEventManager.Instance.Dispatch(CombatEventType.RecoverUpdate, harmUpdateEventData);
  256. HeroHpUpdateEventData heroHpUpdateEventData = HeroHpUpdateEventData.Create();
  257. heroHpUpdateEventData.combatHeroEntity = this;
  258. CombatEventManager.Instance.Dispatch(CombatEventType.HeroHpUpdate, heroHpUpdateEventData);
  259. }
  260. public void CloseLoopFx()
  261. {
  262. for (int i = 0; i < heroLoopParticle.Count; i++)
  263. {
  264. GObjectPool.Instance.Recycle(heroLoopParticle[i]);
  265. }
  266. heroLoopParticle.Clear();
  267. }
  268. public bool IsAttDis(Vector3 pos)
  269. {
  270. return Vector3.SqrMagnitude(pos - combatHeroGameObject.transform.position) < CurrCombatHeroInfo.maxDisTo;
  271. }
  272. public void Dispose()
  273. {
  274. if (_isDis)
  275. {
  276. return;
  277. }
  278. _isDis = true;
  279. combatHeroGameObject.Dispose();
  280. isDie = true;
  281. CombatHeroSkillControl.Dispose();
  282. CombatAIBasic.Dispose();
  283. combatHeroTimeLineControl.Dispose();
  284. combatHeroAnimtion.Dispose();
  285. CloseLoopFx();
  286. }
  287. private void ProDormancyObj()
  288. {
  289. CombatHeroSkillControl.ProDormancyObj();
  290. }
  291. public override void ActiveObj()
  292. {
  293. isDie = false;
  294. _isDis = false;
  295. }
  296. public override void DormancyObj()
  297. {
  298. Dispose();
  299. ProDormancyObj();
  300. _isDis = false;
  301. CombatAIBasic = null;
  302. combatHeroGameObject = null;
  303. CurrCombatHeroInfo = null;
  304. MaxCombatHeroInfo = null;
  305. combatHeroTimeLineControl = null;
  306. combatHeroAnimtion = null;
  307. CombatHeroSkillControl = null;
  308. isFollowState = false;
  309. heroLoopParticle.Clear();
  310. isDie = true;
  311. }
  312. }