EnemyBaseAction.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. using System;
  2. using ExtensionMethods;
  3. using UnityEngine;
  4. public abstract class EnemyBaseAction : BaseBehaviour
  5. {
  6. public Transform player
  7. {
  8. get
  9. {
  10. return R.Player.Transform;
  11. }
  12. }
  13. private bool inStiff
  14. {
  15. get
  16. {
  17. return this.eAttr.stiffTime > 0f;
  18. }
  19. }
  20. protected virtual void Awake()
  21. {
  22. this.spineAnim = base.GetComponent<SpineAnimationController>();
  23. this.eAttr = base.GetComponent<EnemyAttribute>();
  24. this.stateMachine = base.GetComponent<StateMachine>();
  25. if (this.shadowPrefab)
  26. {
  27. this.shadow = UnityEngine.Object.Instantiate<Transform>(this.shadowPrefab);
  28. this.shadow.GetComponent<ShadowControl>().SetTarget(base.transform);
  29. }
  30. }
  31. protected abstract void Start();
  32. protected virtual void Update()
  33. {
  34. this.eAttr.stiffTime = Mathf.Clamp(this.eAttr.stiffTime - Time.deltaTime, 0f, float.PositiveInfinity);
  35. if (this.QTE)
  36. {
  37. this.QTE.transform.localScale = ((base.transform.localScale.x >= 0f) ? new Vector3(1f, 1f, 1f) : new Vector3(-1f, 1f, 1f));
  38. }
  39. if (this.IsInWeakSta() && this.weakEffectShow)
  40. {
  41. this.QTEAnim((!this.CurrentCanBeExecute()) ? "Null" : "Show");
  42. }
  43. if (!this.IsInNormalState())
  44. {
  45. this.isAutoMoveing = false;
  46. }
  47. if (!this.isAutoMoveing || SingletonMono<WorldTime>.Instance.IsFrozen)
  48. {
  49. return;
  50. }
  51. float y = 0f;
  52. if (this.eAttr.iCanFly)
  53. {
  54. float distance = Physics2D.Raycast(base.transform.position + Vector3.right * (float)this.eAttr.faceDir, -Vector2.up, 1000f, LayerManager.GroundMask).distance;
  55. if (Mathf.Abs(distance - this.eAttr.flyHeight) > 0.1f)
  56. {
  57. float num = Mathf.Abs(distance - this.eAttr.flyHeight) / (1f / this.eAttr.moveSpeed);
  58. y = num * Time.deltaTime * Mathf.Sign(this.eAttr.flyHeight - distance);
  59. }
  60. }
  61. float num2 = Mathf.Abs(this.eAttr.moveSpeed * Time.deltaTime);
  62. base.transform.position += new Vector3(num2 * (float)this.eAttr.faceDir, y, 0f);
  63. if (!this.eAttr.iCanFly)
  64. {
  65. this.SetToGroundPos();
  66. }
  67. }
  68. public void AnimChangeState(string state, float speed = 1f)
  69. {
  70. if (this.eAttr.isDead && !this.IsInDeadState(state))
  71. {
  72. return;
  73. }
  74. this.animSpeed = speed;
  75. this.stateMachine.SetState(state);
  76. }
  77. public void AnimChangeState(Enum nextState, float speed = 1f)
  78. {
  79. this.AnimChangeState(nextState.ToString(), speed);
  80. }
  81. public virtual bool IsInNormalState()
  82. {
  83. return !this.eAttr.isDead && !this.inStiff && !this.IsInWeakSta();
  84. }
  85. public bool CurrentCanBeExecute()
  86. {
  87. bool flag = this.IsInWeakSta() && (this.eAttr.rankType == EnemyAttribute.RankType.Normal || (this.eAttr.rankType != EnemyAttribute.RankType.Normal && this.eAttr.isOnGround));
  88. bool flag2;
  89. if (R.Player.Attribute.isOnGround)
  90. {
  91. flag2 = (this.eAttr.accpectExecute && (this.eAttr.isOnGround || (!this.eAttr.isOnGround && this.eAttr.timeController.GetCurrentSpeed().y < 0f)) && !this.eAttr.isDead);
  92. }
  93. else
  94. {
  95. flag2 = (this.eAttr.accpectAirExecute && !this.eAttr.isDead);
  96. }
  97. bool flag3 = Vector2.Distance(base.transform.position, R.Player.Transform.position) <= 4f;
  98. return flag && flag2 && flag3;
  99. }
  100. public abstract bool IsInDeadState(string state);
  101. public void ChangeFace(int dir)
  102. {
  103. if (this.eAttr.faceDir == dir)
  104. {
  105. return;
  106. }
  107. this.eAttr.faceDir = dir;
  108. if (dir != -1)
  109. {
  110. if (dir == 1)
  111. {
  112. base.transform.localScale = new Vector3(-1f * Mathf.Abs(base.transform.localScale.x), base.transform.localScale.y, 1f);
  113. }
  114. }
  115. else
  116. {
  117. base.transform.localScale = new Vector3(Mathf.Abs(base.transform.localScale.x), base.transform.localScale.y, 1f);
  118. }
  119. }
  120. public void FaceToPlayer()
  121. {
  122. int dir = InputSetting.JudgeDir(base.transform.position, this.player.position);
  123. this.ChangeFace(dir);
  124. }
  125. public bool TurnRound(int dir)
  126. {
  127. if (!this.IsInNormalState())
  128. {
  129. return false;
  130. }
  131. this.ChangeFace(dir);
  132. return true;
  133. }
  134. public virtual void KillSelf()
  135. {
  136. if (this.eAttr.rankType == EnemyAttribute.RankType.Normal)
  137. {
  138. this.eAttr.currentHp = 0;
  139. }
  140. }
  141. public void SetToGroundPos()
  142. {
  143. if (this.eAttr.isOnGround)
  144. {
  145. return;
  146. }
  147. base.transform.position = base.transform.position.SetY(LayerManager.YNum.GetGroundHeight(base.gameObject) + 0.2f);
  148. }
  149. public bool AutoMove()
  150. {
  151. if (this.isAutoMoveing)
  152. {
  153. return true;
  154. }
  155. if (this.IsInNormalState())
  156. {
  157. this.isAutoMoveing = true;
  158. this.AnimMove();
  159. return true;
  160. }
  161. return false;
  162. }
  163. public bool StopMoveToIdle()
  164. {
  165. this.isAutoMoveing = false;
  166. if (this.IsInNormalState())
  167. {
  168. this.AnimReady();
  169. return true;
  170. }
  171. return false;
  172. }
  173. public void AppearAtPosition(Vector2 pos)
  174. {
  175. base.transform.position = new Vector3(pos.x, pos.y, LayerManager.ZNum.MMiddleE(this.eAttr.rankType));
  176. this.FaceToPlayer();
  177. }
  178. public void AppearEffect(Vector2 pos)
  179. {
  180. float num = 3.18f;
  181. R.Effect.Generate(0, null, new Vector3(pos.x, pos.y + num, LayerManager.ZNum.Fx), Vector3.zero, default(Vector3), true);
  182. R.Audio.PlayEffect(123, new Vector3?(base.transform.position));
  183. }
  184. public abstract void AnimReady();
  185. public abstract void AnimMove();
  186. public virtual void Attack1(int dir)
  187. {
  188. if (this.eAttr.isDead)
  189. {
  190. return;
  191. }
  192. if (!this.IsInNormalState())
  193. {
  194. return;
  195. }
  196. this.ChangeFace(dir);
  197. }
  198. public virtual void Attack2(int dir)
  199. {
  200. if (this.eAttr.isDead)
  201. {
  202. return;
  203. }
  204. if (!this.IsInNormalState())
  205. {
  206. return;
  207. }
  208. this.ChangeFace(dir);
  209. }
  210. public virtual void Attack3(int dir)
  211. {
  212. if (this.eAttr.isDead)
  213. {
  214. return;
  215. }
  216. if (!this.IsInNormalState())
  217. {
  218. return;
  219. }
  220. this.ChangeFace(dir);
  221. }
  222. public virtual void Attack4(int dir)
  223. {
  224. if (this.eAttr.isDead)
  225. {
  226. return;
  227. }
  228. if (!this.IsInNormalState())
  229. {
  230. return;
  231. }
  232. this.ChangeFace(dir);
  233. }
  234. public virtual void Attack5(int dir)
  235. {
  236. if (this.eAttr.isDead)
  237. {
  238. return;
  239. }
  240. if (!this.IsInNormalState())
  241. {
  242. return;
  243. }
  244. this.ChangeFace(dir);
  245. }
  246. public virtual void Attack6(int dir)
  247. {
  248. if (this.eAttr.isDead)
  249. {
  250. return;
  251. }
  252. if (!this.IsInNormalState())
  253. {
  254. return;
  255. }
  256. this.ChangeFace(dir);
  257. }
  258. public virtual void Attack7(int dir)
  259. {
  260. if (this.eAttr.isDead)
  261. {
  262. return;
  263. }
  264. if (!this.IsInNormalState())
  265. {
  266. return;
  267. }
  268. this.ChangeFace(dir);
  269. }
  270. public virtual void Attack8(int dir)
  271. {
  272. if (this.eAttr.isDead)
  273. {
  274. return;
  275. }
  276. if (!this.IsInNormalState())
  277. {
  278. return;
  279. }
  280. this.ChangeFace(dir);
  281. }
  282. public virtual void Attack9(int dir)
  283. {
  284. if (this.eAttr.isDead)
  285. {
  286. return;
  287. }
  288. if (!this.IsInNormalState())
  289. {
  290. return;
  291. }
  292. this.ChangeFace(dir);
  293. }
  294. public virtual void Attack10(int dir)
  295. {
  296. if (this.eAttr.isDead)
  297. {
  298. return;
  299. }
  300. if (!this.IsInNormalState())
  301. {
  302. return;
  303. }
  304. this.ChangeFace(dir);
  305. }
  306. public virtual void Attack11(int dir)
  307. {
  308. if (this.eAttr.isDead)
  309. {
  310. return;
  311. }
  312. if (!this.IsInNormalState())
  313. {
  314. return;
  315. }
  316. this.ChangeFace(dir);
  317. }
  318. public virtual void Attack12(int dir)
  319. {
  320. if (this.eAttr.isDead)
  321. {
  322. return;
  323. }
  324. if (!this.IsInNormalState())
  325. {
  326. return;
  327. }
  328. this.ChangeFace(dir);
  329. }
  330. public virtual void Attack13(int dir)
  331. {
  332. if (this.eAttr.isDead)
  333. {
  334. return;
  335. }
  336. if (!this.IsInNormalState())
  337. {
  338. return;
  339. }
  340. this.ChangeFace(dir);
  341. }
  342. public virtual void Attack14(int dir)
  343. {
  344. if (this.eAttr.isDead)
  345. {
  346. return;
  347. }
  348. if (!this.IsInNormalState())
  349. {
  350. return;
  351. }
  352. this.ChangeFace(dir);
  353. }
  354. public virtual void Attack15(int dir)
  355. {
  356. if (this.eAttr.isDead)
  357. {
  358. return;
  359. }
  360. if (!this.IsInNormalState())
  361. {
  362. return;
  363. }
  364. this.ChangeFace(dir);
  365. }
  366. public virtual void CounterAttack(int dir)
  367. {
  368. }
  369. public virtual void Idle1()
  370. {
  371. throw new NotImplementedException();
  372. }
  373. public virtual void Idle2()
  374. {
  375. this.Idle1();
  376. }
  377. public virtual void Idle3()
  378. {
  379. this.Idle1();
  380. }
  381. public virtual void Idle4()
  382. {
  383. this.Idle1();
  384. }
  385. public virtual void Idle5()
  386. {
  387. this.Idle1();
  388. }
  389. public virtual void Defence()
  390. {
  391. this.eAttr.timeController.SetSpeed(Vector2.zero);
  392. this.eAttr.currentDefence = 0;
  393. this.eAttr.startDefence = false;
  394. }
  395. public virtual void DefenceSuccess()
  396. {
  397. }
  398. public virtual void SideStep()
  399. {
  400. this.FaceToPlayer();
  401. this.eAttr.currentSideStep = 0;
  402. }
  403. public void QTEAnim(string anim)
  404. {
  405. this.QTESetSkin();
  406. if (!this.QTE || this.QTE.AnimationName == anim)
  407. {
  408. return;
  409. }
  410. this.QTESetText(anim);
  411. this.QTE.state.SetAnimation(0, anim, true);
  412. this.QTE.skeleton.SetToSetupPose();
  413. }
  414. private void QTESetSkin()
  415. {
  416. if (this.QTE)
  417. {
  418. this.QTE.skeleton.SetSkin((!UILanguage.IsChinese) ? "Mobile" : "Mobile_zh");
  419. }
  420. }
  421. private void QTESetText(string anim)
  422. {
  423. }
  424. public void ExitWeakState(bool forceQuit = false)
  425. {
  426. if (!this.eAttr.accpectExecute)
  427. {
  428. return;
  429. }
  430. if (this.eAttr.isDead)
  431. {
  432. return;
  433. }
  434. if (!this.eAttr.willBeExecute || forceQuit)
  435. {
  436. this.eAttr.inWeakState = false;
  437. this.WeakEffectDisappear("RollEnd");
  438. }
  439. }
  440. public void WeakEffectDisappear(string disappear)
  441. {
  442. if (!this.eAttr.accpectExecute)
  443. {
  444. return;
  445. }
  446. if (this.weakThunder)
  447. {
  448. this.weakThunder.SetActive(false);
  449. }
  450. if (this.weakEffect)
  451. {
  452. this.weakEffect.state.SetAnimation(0, disappear, false);
  453. this.weakEffect.skeleton.SetSlotsToSetupPose();
  454. this.weakEffect.Update(0f);
  455. }
  456. this.weakEffectShow = false;
  457. this.QTEAnim("Null");
  458. }
  459. public virtual void EnterWeakState()
  460. {
  461. if (!this.eAttr.accpectExecute)
  462. {
  463. return;
  464. }
  465. this.eAttr.inWeakState = true;
  466. this.WeakEffectAppear();
  467. }
  468. protected virtual void WeakEffectAppear()
  469. {
  470. if (!this.eAttr.accpectExecute)
  471. {
  472. return;
  473. }
  474. if (this.eAttr.isDead)
  475. {
  476. return;
  477. }
  478. R.Audio.PlayEffect(104, new Vector3?(base.transform.position));
  479. if (this.weakThunder)
  480. {
  481. this.weakThunder.SetActive(true);
  482. }
  483. if (this.weakEffect)
  484. {
  485. this.weakEffect.state.SetAnimation(0, "Roll", true);
  486. this.weakEffect.skeleton.SetSlotsToSetupPose();
  487. this.weakEffect.Update(0f);
  488. }
  489. this.weakEffectShow = true;
  490. this.QTEAnim("Show");
  491. }
  492. public void PlayEffect(int effectId)
  493. {
  494. if (!R.Effect.fxData.ContainsKey(effectId))
  495. {
  496. Log.Alert(string.Format("Enemy {0} try to play No.{1} Effect, but not exist", base.name, effectId));
  497. return;
  498. }
  499. EnemyAtkEffector component = R.Effect.fxData[effectId].effect.GetComponent<EnemyAtkEffector>();
  500. Vector3 position = new Vector3(component.pos.x * base.transform.localScale.x, component.pos.y, component.pos.z);
  501. R.Effect.Generate(effectId, base.transform, position, default(Vector3), default(Vector3), true);
  502. }
  503. public virtual bool IsInDefenceState()
  504. {
  505. return false;
  506. }
  507. public virtual bool IsInSideStepState()
  508. {
  509. return false;
  510. }
  511. public abstract bool IsInWeakSta();
  512. public abstract bool IsInAttackState();
  513. protected abstract bool EnterAtkSta(string lastState, string nextState);
  514. protected abstract bool ExitAtkSta(string lastState, string nextState);
  515. public virtual bool IsInIdle()
  516. {
  517. return false;
  518. }
  519. public virtual void AnimExecute()
  520. {
  521. Vector3 position = base.transform.position;
  522. position.y = LayerManager.YNum.GetGroundHeight(base.gameObject);
  523. base.transform.position = position;
  524. }
  525. public virtual void AnimQTEHurt()
  526. {
  527. Vector3 position = base.transform.position;
  528. position.y = LayerManager.YNum.GetGroundHeight(base.gameObject);
  529. base.transform.position = position;
  530. }
  531. public const int LEFT = -1;
  532. public const int RIGHT = 1;
  533. public const int STOP = 0;
  534. [HideInInspector]
  535. public StateMachine stateMachine;
  536. [SerializeField]
  537. public Transform hurtBox;
  538. [HideInInspector]
  539. public EnemyAttribute eAttr;
  540. [SerializeField]
  541. protected Transform atkBox;
  542. [SerializeField]
  543. protected SkeletonAnimation weakEffect;
  544. [SerializeField]
  545. protected GameObject weakThunder;
  546. [SerializeField]
  547. protected SkeletonAnimation QTE;
  548. [SerializeField]
  549. private TextMesh _qteTextMesh;
  550. protected SpineAnimationController spineAnim;
  551. protected float animSpeed;
  552. protected float startDefenceTime;
  553. protected float defenceTime;
  554. private bool weakEffectShow;
  555. public bool isAutoMoveing;
  556. [SerializeField]
  557. private Transform shadowPrefab;
  558. private Transform shadow;
  559. }