CombatHeroEntity.cs 13 KB

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