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