Enemy.cs 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. using Spine.Unity;
  2. using Spine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using Base.Common;
  7. using UnityEditor.SceneManagement;
  8. public enum TargetType
  9. {
  10. None = 0,
  11. Demonic = 1,
  12. Tower = 2,
  13. Player = 3,
  14. Enemy = 4,
  15. EnemyTower = 5,
  16. Boss = 6,
  17. Portal = 7,
  18. }
  19. public enum SearchState
  20. {
  21. NoTarget = 0, //搜索范围内没有目标
  22. InSearchScope = 1, //在搜索范围内发现目标,但不在攻击范围内
  23. InAttackScope = 2, //目标在攻击范围内
  24. }
  25. public class Enemy : MoveCharacter
  26. {
  27. [Header("敌方单位属性")]
  28. public int id;
  29. public int baseSortingOrder;
  30. int sortingOrder = 0;
  31. public bool isBack = false; //往反方向走
  32. public float flyHeight;
  33. public float flyUpSpeed = 10;
  34. public float jumpSpeed = 10;
  35. public float maxMoveSpeed, minMoveSpeed;
  36. public float runSpeed;
  37. [Header("击飞、屏幕反弹")]
  38. public bool isBeBlownUp; //被击飞
  39. public bool isBeReboundedX; //X方向被反弹
  40. public bool isBeReboundedY; //Y方向被反弹
  41. private bool hasBeReboundedX;
  42. private bool hasBeReboundedY;
  43. public float reboundXSpeed;
  44. public float reboundYSpeed;
  45. public int wallDamage;
  46. [Header("落地眩晕")]
  47. public bool willBeComa;
  48. public float willComaTime;
  49. public int comaDamage;
  50. [Header("敌方英灵")]
  51. public Spirits.SpiritType type;
  52. [Header("敌方单位组件")]
  53. public SearchState searchState;
  54. [Header("攻击")]
  55. public float attackDistance;
  56. public float maxAttackDis, minAttackDis;
  57. public bool needToChange;
  58. public float attackRatio;
  59. [Header("掉落魂")]
  60. public int dropSoulMax = 3;
  61. public int dropSoulMin = 1;
  62. public float dropSoulAngle = 60f;
  63. [Header("冲刺攻击")]
  64. public GameObject aimEffect;
  65. public DashEffect dashEffect;
  66. [HideInInspector]
  67. public bool noOnSearchState;
  68. [HideInInspector]
  69. public bool isFindingPlayer;
  70. [HideInInspector]
  71. public bool isFindPlayer;
  72. public float hateDistance;
  73. [HideInInspector]
  74. public float distance;
  75. [HideInInspector]
  76. public Vector3 rushEndPos;
  77. public float aimDistance;
  78. public float rushTime;
  79. public float rushSpeed;
  80. [HideInInspector]
  81. public float time;
  82. public float readyCD;
  83. [HideInInspector]
  84. public Vector3 targetDir;
  85. public bool haveDownRush; //冲刺结束后是否可以接落地斩
  86. public bool rushHaveAttack; //冲刺是否带伤害
  87. public float downRushTime;
  88. public float finishRushTime;
  89. [Header("验证功能开关")]
  90. public bool upFirstAfterWeaknessOrNot; //虚弱结束后是否先升高
  91. private void Awake()
  92. {
  93. aimDistance = rushTime * rushSpeed / 2;
  94. }
  95. private void Start()
  96. {
  97. if (needToChange)
  98. {
  99. attackDistance = Random.Range(minAttackDis, maxAttackDis);
  100. }
  101. }
  102. private void OnEnable()
  103. {
  104. Init();
  105. }
  106. public void OnDisable()
  107. {
  108. EnemyCreater.instance.OnEnemyRecycle(this);
  109. }
  110. public override void Init()
  111. {
  112. base.Init();
  113. moveSpeed = Random.Range(minMoveSpeed, maxMoveSpeed);
  114. ChangeSearchState(SearchState.NoTarget);
  115. }
  116. public override void FixedUpdate()
  117. {
  118. if (!noOnSearchState)
  119. {
  120. OnSearchState();
  121. }
  122. OnState();
  123. }
  124. public override Vector3 GetMoveDir()
  125. {
  126. Vector3 moveDir = Vector3.zero;
  127. if (canMove)
  128. {
  129. switch (searchState)
  130. {
  131. case SearchState.NoTarget:
  132. if (TowerMap.myTowers.Count == 0)
  133. {
  134. moveDir = Vector3.right;
  135. break;
  136. }
  137. float minDistance = Mathf.Infinity;
  138. int id = -1;
  139. for (int i = 0; i < TowerMap.myTowers.Count; i++)
  140. {
  141. Tower myTower = TowerMap.myTowers[i].GetComponent<Tower>();
  142. if (transform.position.y >
  143. myTower.transform.position.y + myTower.height)
  144. {
  145. continue;
  146. }
  147. float distance = Vector3.Distance(transform.position,
  148. TowerMap.myTowers[i].transform.position);
  149. if (distance < minDistance)
  150. {
  151. minDistance = distance;
  152. id = i;
  153. }
  154. }
  155. if (id == -1)
  156. {
  157. moveDir = Vector3.right;
  158. break;
  159. }
  160. if (bodyTrans.position.x > TowerMap.myTowers[id].transform.position.x)
  161. {
  162. moveDir = Vector3.left;
  163. }
  164. else
  165. {
  166. moveDir = Vector3.right;
  167. }
  168. break;
  169. case SearchState.InSearchScope:
  170. if (targetCharacter)
  171. {
  172. if (targetCharacter.transform.position.x - transform.position.x < 0)
  173. {
  174. moveDir = Vector3.left;
  175. }
  176. else
  177. {
  178. moveDir = Vector3.right;
  179. }
  180. }
  181. else
  182. {
  183. moveDir = Vector3.zero;
  184. }
  185. break;
  186. case SearchState.InAttackScope:
  187. if (targetCharacter)
  188. {
  189. if (targetCharacter.transform.position.x - transform.position.x < 0)
  190. {
  191. moveDir = Vector3.left;
  192. }
  193. else
  194. {
  195. moveDir = Vector3.right;
  196. }
  197. }
  198. else
  199. {
  200. moveDir = Vector3.zero;
  201. }
  202. break;
  203. default:
  204. break;
  205. }
  206. }
  207. if (!isBack)
  208. {
  209. return moveDir;
  210. }
  211. return -moveDir;
  212. }
  213. public bool GetAttack()
  214. {
  215. if (searchState == SearchState.InAttackScope)
  216. {
  217. return true;
  218. }
  219. return false;
  220. }
  221. public bool GetJump()
  222. {
  223. return false;
  224. }
  225. public bool AdjustHeight()
  226. {
  227. if (canFly)
  228. {
  229. if (transform.position.y - flyHeight > 0.1f)
  230. {
  231. Vector3 pos = transform.position;
  232. pos.y -= flyUpSpeed * Time.deltaTime;
  233. transform.position = pos;
  234. return false;
  235. }
  236. else if (transform.position.y - flyHeight < -0.1f)
  237. {
  238. Vector3 pos = transform.position;
  239. pos.y += flyUpSpeed * Time.deltaTime;
  240. transform.position = pos;
  241. return false;
  242. }
  243. }
  244. return true;
  245. }
  246. public override void OnState()
  247. {
  248. base.OnState();
  249. //hurtKeepTime -= Time.deltaTime;
  250. dieKeepTime -= Time.deltaTime;
  251. invincibleTime -= Time.deltaTime;
  252. weakTime -= Time.deltaTime;
  253. beRepelValue += Time.deltaTime;
  254. Vector3 leftDir = GetMoveDir();
  255. bool isAttack = GetAttack();
  256. switch (state)
  257. {
  258. case CharacterState.Idle:
  259. if (isAttack)
  260. {
  261. Attack_march();
  262. break;
  263. }
  264. if (!foot.TrigGround && !canFly)
  265. {
  266. if (rb.velocity.y > 0)
  267. {
  268. ChangeState(CharacterState.Rise);
  269. break;
  270. }
  271. else
  272. {
  273. ChangeState(CharacterState.Fall);
  274. break;
  275. }
  276. }
  277. if (leftDir.x > 0.3f || leftDir.x < -0.3f)
  278. {
  279. ChangeState(CharacterState.Run);
  280. break;
  281. }
  282. //rb.velocity = Vector3.zero;
  283. if (!upFirstAfterWeaknessOrNot)
  284. {
  285. if (canFly)
  286. {
  287. rb.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezePositionY;
  288. rb.useGravity = false;
  289. AdjustHeight();
  290. }
  291. }
  292. break;
  293. case CharacterState.Run:
  294. if (isAttack)
  295. {
  296. Attack_march();
  297. break;
  298. }
  299. if (!foot.TrigGround && !canFly)
  300. {
  301. if (rb.velocity.y > 0)
  302. {
  303. ChangeState(CharacterState.Rise);
  304. break;
  305. }
  306. else
  307. {
  308. ChangeState(CharacterState.Fall);
  309. break;
  310. }
  311. }
  312. if (leftDir.x < 0.3f && leftDir.x > -0.3f)
  313. {
  314. ChangeState(CharacterState.Idle);
  315. break;
  316. }
  317. if (leftDir.x > 0.3f)
  318. {
  319. rb.velocity = Vector3.right * moveSpeed;
  320. if (bodyTrans.localScale.x > 0)
  321. {
  322. Turn();
  323. }
  324. }
  325. else if (leftDir.x < -0.3f)
  326. {
  327. rb.velocity = Vector3.left * moveSpeed;
  328. if (bodyTrans.localScale.x < 0)
  329. {
  330. Turn();
  331. }
  332. }
  333. if (!upFirstAfterWeaknessOrNot)
  334. {
  335. if (canFly)
  336. {
  337. rb.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezePositionY;
  338. rb.useGravity = false;
  339. AdjustHeight();
  340. }
  341. }
  342. break;
  343. case CharacterState.Rush:
  344. if (isAttack)
  345. {
  346. Attack_march();
  347. break;
  348. }
  349. if (!foot.TrigGround && !canFly)
  350. {
  351. if (rb.velocity.y > 0)
  352. {
  353. ChangeState(CharacterState.Rise);
  354. break;
  355. }
  356. else
  357. {
  358. ChangeState(CharacterState.Fall);
  359. break;
  360. }
  361. }
  362. if (leftDir.x < 0.3f && leftDir.x > -0.3f)
  363. {
  364. ChangeState(CharacterState.Idle);
  365. break;
  366. }
  367. if (leftDir.x > 0.3f)
  368. {
  369. //rb.velocity += Vector3.right * moveAcc * Time.deltaTime;
  370. rb.velocity = Vector3.right * runSpeed;
  371. //if (rb.velocity.x > maxMoveSpeed)
  372. //{
  373. // rb.velocity = new Vector3(maxMoveSpeed, rb.velocity.y, rb.velocity.z);
  374. //}
  375. if (bodyTrans.localScale.x > 0)
  376. {
  377. Turn();
  378. }
  379. }
  380. else if (leftDir.x < -0.3f)
  381. {
  382. //rb.velocity -= Vector3.right * moveAcc * Time.deltaTime;
  383. rb.velocity = Vector3.left * runSpeed;
  384. //if (rb.velocity.x < -maxMoveSpeed)
  385. //{
  386. // rb.velocity = new Vector3(-maxMoveSpeed, rb.velocity.y, rb.velocity.z);
  387. //}
  388. if (bodyTrans.localScale.x < 0)
  389. {
  390. Turn();
  391. }
  392. }
  393. //AdjustHeight();
  394. break;
  395. case CharacterState.Rise:
  396. if (rb.velocity.y <= 0)
  397. {
  398. ChangeState(CharacterState.Fall);
  399. break;
  400. }
  401. rb.velocity += Vector3.up * extraRiseGravity * Time.deltaTime;
  402. break;
  403. case CharacterState.Fall:
  404. if (foot.TrigGround || canFly)
  405. {
  406. if (isFindingPlayer)
  407. {
  408. ChangeState(CharacterState.FindPlayer);
  409. }
  410. else if (willBeComa)
  411. {
  412. comaTime = willComaTime;
  413. ChangeState(CharacterState.Coma);
  414. }
  415. else
  416. {
  417. ChangeState(CharacterState.Idle);
  418. }
  419. break;
  420. }
  421. Vector3 velocity = rb.velocity;
  422. velocity.y += extraFallGravity * Time.deltaTime;
  423. if (leftDir.x > 0.3f)
  424. {
  425. velocity.x = moveSpeed;
  426. if (bodyTrans.localScale.x > 0)
  427. {
  428. Turn();
  429. }
  430. }
  431. else if (leftDir.x < -0.3f)
  432. {
  433. velocity.x = -moveSpeed;
  434. if (bodyTrans.localScale.x < 0)
  435. {
  436. Turn();
  437. }
  438. }
  439. rb.velocity = velocity;
  440. break;
  441. case CharacterState.Attack:
  442. attackTime -= Time.deltaTime;
  443. attackKeyCount += Time.deltaTime;
  444. if (!isAttackTriggerOn && attackKeyCount >= nextStartKeyTime && attackKeyCount <= nextEndKeyTime)
  445. {
  446. isAttackTriggerOn = true;
  447. foreach (AttackTrigger at in attackTriggers)
  448. {
  449. at.gameObject.SetActive(true);
  450. }
  451. }
  452. else if (isAttackTriggerOn && attackKeyCount >= nextEndKeyTime)
  453. {
  454. isAttackTriggerOn = false;
  455. foreach (AttackTrigger at in attackTriggers)
  456. {
  457. at.gameObject.SetActive(false);
  458. }
  459. SetNextKeyTimes();
  460. }
  461. if (attackTime <= 0)
  462. {
  463. ChangeState(CharacterState.Idle);
  464. break;
  465. }
  466. break;
  467. case CharacterState.Die:
  468. if (dieKeepTime <= 0)
  469. {
  470. gameObject.SetActive(false);
  471. break;
  472. }
  473. break;
  474. case CharacterState.Weak:
  475. if (rb.velocity.magnitude > 1)
  476. {
  477. if (!isBeBlownUp)
  478. {
  479. isBeBlownUp = true;
  480. ani.Play("hitted", 0, 0);
  481. }
  482. Vector3 vel = rb.velocity;
  483. if (foot.TrigGround && vel.y <= 0)
  484. {
  485. vel = Vector3.zero;
  486. }
  487. else
  488. {
  489. if (!hasBeReboundedX)
  490. {
  491. vel.x -= vel.x * decelerationRatio * Time.deltaTime;
  492. if (!hasBeReboundedX && isBeReboundedX)
  493. {
  494. hasBeReboundedX = true;
  495. vel.x = 0;
  496. }
  497. }
  498. if (!hasBeReboundedY)
  499. {
  500. vel.y += extraFallGravity * Time.deltaTime;
  501. if (!hasBeReboundedY && isBeReboundedY)
  502. {
  503. hasBeReboundedY = true;
  504. vel.y = 0;
  505. }
  506. }
  507. }
  508. //vel.y = vel.y * (1 - decelerationRatio * Time.deltaTime);
  509. //vel.x = vel.x * (1 - decelerationRatio * Time.deltaTime);
  510. rb.velocity = vel;
  511. }
  512. else
  513. {
  514. if (isBeBlownUp)
  515. {
  516. isBeBlownUp = false;
  517. isBeReboundedX = false;
  518. isBeReboundedY = false;
  519. hasBeReboundedX = false;
  520. hasBeReboundedY = false;
  521. }
  522. if (weakTime <= -0.1)
  523. {
  524. if (upFirstAfterWeaknessOrNot)
  525. {
  526. if (canFly)
  527. {
  528. rb.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezePositionY;
  529. rb.useGravity = false;
  530. }
  531. if (AdjustHeight())
  532. {
  533. ChangeState(CharacterState.Idle);
  534. }
  535. else
  536. {
  537. if (ani.GetCurrentAnimatorClipInfo(0)[0].clip.name != "walk")
  538. {
  539. ani.Play("walk", 0, 0);
  540. }
  541. }
  542. }
  543. else
  544. {
  545. ChangeState(CharacterState.Idle);
  546. }
  547. break;
  548. }
  549. else
  550. {
  551. if (ani.GetCurrentAnimatorClipInfo(0)[0].clip.name != "weak")
  552. {
  553. ani.Play("weak", 0, 0);
  554. rb.velocity = Vector3.zero;
  555. }
  556. }
  557. }
  558. break;
  559. case CharacterState.Coma:
  560. pastComaTime += Time.deltaTime;
  561. if (pastComaTime >= comaTime)
  562. {
  563. ChangeState(CharacterState.Idle);
  564. }
  565. break;
  566. case CharacterState.FindPlayer:
  567. if (!isFindPlayer)
  568. {
  569. if (!foot.TrigGround && !canFly)
  570. {
  571. if (rb.velocity.y > 0)
  572. {
  573. ChangeState(CharacterState.Rise);
  574. break;
  575. }
  576. else
  577. {
  578. ChangeState(CharacterState.Fall);
  579. break;
  580. }
  581. }
  582. if (targetCharacter == null)
  583. {
  584. ChosePlayer();
  585. if (targetCharacter == null)
  586. {
  587. ChangeState(CharacterState.FinishRush);
  588. time = finishRushTime;
  589. break;
  590. }
  591. }
  592. else
  593. {
  594. if (targetCharacter.isRevive ||
  595. targetCharacter.GetComponent<PlayerController>().isBaseBtnOut)
  596. {
  597. targetCharacter = null;
  598. break;
  599. }
  600. }
  601. if (Mathf.Abs(transform.position.x - targetCharacter.transform.position.x)
  602. < hateDistance)
  603. {
  604. rushEndPos = targetCharacter.transform.position;
  605. isFindPlayer = true;
  606. break;
  607. }
  608. if (targetCharacter.transform.position.x > transform.position.x)
  609. {
  610. rb.velocity = Vector3.right * moveSpeed;
  611. if (bodyTrans.localScale.x > 0)
  612. {
  613. Turn();
  614. }
  615. }
  616. if (targetCharacter.transform.position.x < transform.position.x)
  617. {
  618. rb.velocity = Vector3.left * moveSpeed;
  619. if (bodyTrans.localScale.x < 0)
  620. {
  621. Turn();
  622. }
  623. }
  624. }
  625. break;
  626. case CharacterState.ReadyToRush:
  627. time += Time.deltaTime;
  628. if (time >= readyCD)
  629. {
  630. time = 0;
  631. if (rushHaveAttack)
  632. {
  633. ChangeState(CharacterState.RushAttack);
  634. }
  635. else
  636. {
  637. ChangeState(CharacterState.Rush);
  638. }
  639. }
  640. break;
  641. case CharacterState.RushAttack:
  642. time += Time.deltaTime;
  643. dashEffect.canHit = true;
  644. Rush();
  645. if (time >= rushTime)
  646. {
  647. time = 0;
  648. if (haveDownRush)
  649. {
  650. if (foot.TrigGround)
  651. {
  652. ChangeState(CharacterState.FinishRush);
  653. }
  654. else
  655. {
  656. ChangeState(CharacterState.ReadyToDownRush);
  657. }
  658. }
  659. else
  660. {
  661. ChangeState(CharacterState.FinishRush);
  662. }
  663. }
  664. break;
  665. case CharacterState.ReadyToDownRush:
  666. time += Time.deltaTime;
  667. if (time >= downRushTime)
  668. {
  669. time = 0;
  670. ChangeState(CharacterState.DownRush);
  671. }
  672. break;
  673. case CharacterState.DownRush:
  674. if (transform.position.y > 0)
  675. {
  676. dashEffect.canHit = true;
  677. Rush();
  678. }
  679. if (foot.TrigGround || transform.position.y <= -1)
  680. {
  681. ChangeState(CharacterState.FinishRush);
  682. }
  683. break;
  684. case CharacterState.FinishRush:
  685. time += Time.deltaTime;
  686. if (time > finishRushTime)
  687. {
  688. time = 0;
  689. ChangeState(CharacterState.Idle);
  690. }
  691. break;
  692. default:
  693. break;
  694. }
  695. }
  696. public override void ChangeState(CharacterState newState)
  697. {
  698. if (state == newState)
  699. {
  700. return;
  701. }
  702. switch (state)
  703. {
  704. case CharacterState.Idle:
  705. break;
  706. case CharacterState.Run:
  707. rb.velocity = Vector3.zero;
  708. break;
  709. case CharacterState.Rush:
  710. rb.velocity = Vector3.zero;
  711. break;
  712. case CharacterState.Rise:
  713. if (!canFly)
  714. {
  715. bodyCollider.SetActive(true);
  716. }
  717. break;
  718. case CharacterState.Fall:
  719. rb.velocity = Vector3.zero;
  720. break;
  721. //case CharacterState.Hurt:
  722. // break;
  723. case CharacterState.Attack:
  724. break;
  725. case CharacterState.Die:
  726. isDie = false;
  727. break;
  728. case CharacterState.Weak:
  729. beRepelValue = totalBeRepelValue;
  730. newTotalWeakTime = totalWeakTime;
  731. if (isBeBlownUp)
  732. {
  733. isBeBlownUp = false;
  734. isBeReboundedX = false;
  735. isBeReboundedY = false;
  736. hasBeReboundedX = false;
  737. hasBeReboundedY = false;
  738. }
  739. break;
  740. case CharacterState.Float:
  741. canMove = true;
  742. break;
  743. case CharacterState.Coma:
  744. ani.Play("idle", 0, 0);
  745. if (canFly)
  746. {
  747. rb.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezePositionY;
  748. rb.useGravity = false;
  749. }
  750. canMove = true;
  751. break;
  752. case CharacterState.FindPlayer:
  753. noOnSearchState = false;
  754. rb.velocity = Vector3.zero;
  755. isFindPlayer = false;
  756. break;
  757. case CharacterState.ReadyToRush:
  758. time = 0;
  759. aimEffect.SetActive(false);
  760. aimEffect.transform.localScale = Vector3.zero;
  761. rb.constraints =
  762. RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotation;
  763. break;
  764. case CharacterState.RushAttack:
  765. time = 0;
  766. dashEffect.canHit = false;
  767. rb.velocity = Vector3.zero;
  768. bodyTrans.rotation = Quaternion.Euler(Vector3.zero);
  769. break;
  770. case CharacterState.ReadyToDownRush:
  771. time = 0;
  772. rb.constraints =
  773. RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotation;
  774. break;
  775. case CharacterState.DownRush:
  776. dashEffect.canHit = false;
  777. rb.velocity = Vector3.zero;
  778. bodyTrans.rotation = Quaternion.Euler(Vector3.zero);
  779. if (foot.trigGroundList.Count == 0)
  780. {
  781. transform.position = new Vector3(transform.position.x, -1, 0);
  782. }
  783. else
  784. {
  785. transform.position = new Vector3(transform.position.x,
  786. foot.trigGroundList[0].transform.position.y, 0);
  787. }
  788. break;
  789. case CharacterState.FinishRush:
  790. time = 0;
  791. searchState = SearchState.NoTarget;
  792. noOnSearchState = false;
  793. ani.Play("idle", 0, 0);
  794. break;
  795. default:
  796. break;
  797. }
  798. CharacterState oldState = state;
  799. state = newState;
  800. switch (newState)
  801. {
  802. case CharacterState.Idle:
  803. ani.Play("idle", 0, 0);
  804. rb.velocity = Vector3.zero;
  805. //animalAni.SetInteger("state", (int)PlayerState.Idle);
  806. break;
  807. case CharacterState.Run:
  808. ani.Play("walk", 0, 0);
  809. //animalAni.SetInteger("state", (int)PlayerState.Walk);
  810. break;
  811. case CharacterState.Rush:
  812. ani.Play("rush", 0, 0);
  813. break;
  814. case CharacterState.Float:
  815. canMove = false;
  816. break;
  817. case CharacterState.Coma:
  818. if (willBeComa)
  819. {
  820. BeHit(comaDamage, Vector3.zero, false, 0);
  821. }
  822. //ani.Play("Coma", 0, 0);
  823. ani.Play("weak", 0, 0);
  824. rb.velocity = Vector3.zero;
  825. pastComaTime = 0;
  826. if (canFly)
  827. {
  828. rb.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ;
  829. rb.useGravity = true;
  830. }
  831. break;
  832. case CharacterState.Attack:
  833. break;
  834. case CharacterState.Die:
  835. ani.Play("die", 0, 0);
  836. isDie = true;
  837. dieKeepTime = totalDieKeepTime;
  838. DropSouls();
  839. if (linked)
  840. {
  841. PlayersInput.instance[0].sprintLinkTrigger.linkedEnemy.Remove(this);
  842. PlayersInput.instance[0].playerRope.gameObject.SetActive(false);
  843. }
  844. break;
  845. case CharacterState.Weak:
  846. switch (oldState)
  847. {
  848. case CharacterState.ReadyToRush:
  849. case CharacterState.RushAttack:
  850. case CharacterState.ReadyToDownRush:
  851. case CharacterState.DownRush:
  852. case CharacterState.Rush:
  853. state = oldState;
  854. beRepelValue = totalBeRepelValue;
  855. break;
  856. default:
  857. weakTime = newTotalWeakTime;
  858. rb.AddForce(weakForce);
  859. if (canFly)
  860. {
  861. rb.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ;
  862. rb.useGravity = true;
  863. }
  864. //hurtKeepTime = minHurtKeepTime;
  865. break;
  866. }
  867. break;
  868. case CharacterState.FindPlayer:
  869. isFindPlayer = false;
  870. isFindingPlayer = true;
  871. noOnSearchState = true;
  872. ChosePlayer();
  873. ani.Play("walk", 0, 0);
  874. break;
  875. case CharacterState.ReadyToRush:
  876. time = 0;
  877. canNotChangeHurt = true;
  878. ani.Play("charge", 0, 0);
  879. aimEffect.SetActive(true);
  880. rb.constraints = RigidbodyConstraints.FreezeAll;
  881. ReadyToDash(rushEndPos + Vector3.up, transform.position + Vector3.up);
  882. break;
  883. case CharacterState.RushAttack:
  884. targetDir =
  885. (rushEndPos - transform.position).normalized;
  886. ani.Play("rush_attack", 0, 0);
  887. break;
  888. case CharacterState.ReadyToDownRush:
  889. time = 0;
  890. rb.constraints = RigidbodyConstraints.FreezeAll;
  891. ani.Play("charge", 0, 0);
  892. break;
  893. case CharacterState.DownRush:
  894. time = 0;
  895. targetDir = Vector3.down;
  896. ani.Play("rush_attack", 0, 0);
  897. break;
  898. case CharacterState.FinishRush:
  899. time = 0;
  900. if (oldState == CharacterState.DownRush)
  901. {
  902. ani.Play("fall_end", 0, 0);
  903. }
  904. else
  905. {
  906. ani.Play("idle", 0, 0);
  907. }
  908. canNotChangeHurt = false;
  909. break;
  910. default:
  911. break;
  912. }
  913. }
  914. public void DropSouls()
  915. {
  916. int dropSoulNum = Random.Range(dropSoulMin, dropSoulMax + 1);
  917. if (dropSoulNum > 1)
  918. {
  919. for (int i = 0; i < dropSoulNum; i++)
  920. {
  921. float angleInterval = dropSoulAngle / (float)(dropSoulNum - 1);
  922. float angle = 90 + ((float)i - (float)(dropSoulNum - 1) / 2) * angleInterval;
  923. angle = angle / 180 * Mathf.PI;
  924. GameObject soulObj = PoolManager.Instantiate(soulPrefab, transform.position);
  925. Vector3 dir = new Vector3(Mathf.Cos(angle), Mathf.Sin(angle), 0);
  926. Soul soul = soulObj.GetComponent<Soul>();
  927. soul.Burst(dir * soulStartSpeed);
  928. soul.type = type;
  929. }
  930. }
  931. else
  932. {
  933. GameObject soulObj = PoolManager.Instantiate(soulPrefab, transform.position);
  934. Vector3 dir = Vector3.up;
  935. Soul soul = soulObj.GetComponent<Soul>();
  936. soul.Burst(dir * soulStartSpeed);
  937. soul.type = type;
  938. }
  939. }
  940. public void Jump()
  941. {
  942. SetUpSpeed(jumpSpeed);
  943. ani.Play("jump", 0, 0);
  944. }
  945. public void SetUpSpeed(float speed)
  946. {
  947. ChangeState(CharacterState.Rise);
  948. Vector3 velocity = rb.velocity;
  949. Vector3 leftDir = GetMoveDir();
  950. if (leftDir.x > 0.3f)
  951. {
  952. if (bodyTrans.localScale.x > 0)
  953. {
  954. Turn();
  955. }
  956. }
  957. else if (leftDir.x < -0.3f)
  958. {
  959. if (bodyTrans.localScale.x < 0)
  960. {
  961. Turn();
  962. }
  963. }
  964. velocity.y = speed;
  965. rb.velocity = velocity;
  966. //animalAni.SetInteger("state", (int)PlayerState.Rise);
  967. }
  968. public void ChangeSearchState(SearchState newState)
  969. {
  970. switch (searchState)
  971. {
  972. case SearchState.NoTarget:
  973. break;
  974. case SearchState.InSearchScope:
  975. break;
  976. case SearchState.InAttackScope:
  977. break;
  978. default:
  979. break;
  980. }
  981. searchState = newState;
  982. switch (searchState)
  983. {
  984. case SearchState.NoTarget:
  985. Character character0 = PlayersInput.instance[0];
  986. Character character1 = PlayersInput.instance[1];
  987. if (character0 && character0.beTargetCharacter.Exists(t => t == this))
  988. {
  989. character0.beTargetCharacter.Remove(this);
  990. }
  991. if (character1 && character1.beTargetCharacter.Exists(t => t == this))
  992. {
  993. character1.beTargetCharacter.Remove(this);
  994. }
  995. targetCharacter = null;
  996. break;
  997. case SearchState.InSearchScope:
  998. break;
  999. case SearchState.InAttackScope:
  1000. break;
  1001. default:
  1002. break;
  1003. }
  1004. }
  1005. public bool SearchTarget()
  1006. {
  1007. targetCharacter = searchTrigger.GetMinDisTarget(targetTypes, canHitFly);
  1008. if (targetCharacter != null)
  1009. {
  1010. Character character0 = PlayersInput.instance[0];
  1011. Character character1 = PlayersInput.instance[1];
  1012. if (targetCharacter == character0
  1013. && !character0.beTargetCharacter.Exists(t => t == this))
  1014. {
  1015. character0.beTargetCharacter.Add(this);
  1016. }
  1017. if (targetCharacter == character1
  1018. && !character1.beTargetCharacter.Exists(t => t == this))
  1019. {
  1020. character1.beTargetCharacter.Add(this);
  1021. }
  1022. return true;
  1023. }
  1024. else
  1025. {
  1026. return false;
  1027. }
  1028. }
  1029. public void OnSearchState()
  1030. {
  1031. switch (searchState)
  1032. {
  1033. case SearchState.NoTarget:
  1034. if (SearchTarget())
  1035. {
  1036. ChangeSearchState(SearchState.InSearchScope);
  1037. break;
  1038. }
  1039. //向玩家基地移动
  1040. break;
  1041. case SearchState.InSearchScope:
  1042. if (!SearchTarget())
  1043. {
  1044. targetCharacter = null;
  1045. ChangeSearchState(SearchState.NoTarget);
  1046. break;
  1047. }
  1048. if (targetCharacter != null && Mathf.Abs(targetCharacter.transform.position.x - transform.position.x) <= attackDistance)
  1049. {
  1050. ChangeSearchState(SearchState.InAttackScope);
  1051. break;
  1052. }
  1053. break;
  1054. case SearchState.InAttackScope:
  1055. if (targetCharacter != null)
  1056. {
  1057. if (!targetCharacter.gameObject.activeInHierarchy || targetCharacter.isDie
  1058. || Mathf.Abs(targetCharacter.transform.position.x - transform.position.x) > attackDistance)
  1059. {
  1060. ChangeSearchState(SearchState.NoTarget);
  1061. }
  1062. }
  1063. else
  1064. {
  1065. ChangeSearchState(SearchState.NoTarget);
  1066. }
  1067. break;
  1068. default:
  1069. break;
  1070. }
  1071. }
  1072. public override void Attack_summon()
  1073. {
  1074. base.Attack_summon();
  1075. attackTarget = targetCharacter;
  1076. }
  1077. public override void Attack_march()
  1078. {
  1079. base.Attack_march();
  1080. attackTarget = targetCharacter;
  1081. }
  1082. public void ChosePlayer()
  1083. {
  1084. float distance0 = Mathf.Infinity;
  1085. float distance1 = Mathf.Infinity;
  1086. PlayerController player0 = PlayersInput.instance[0];
  1087. PlayerController player1 = PlayersInput.instance[1];
  1088. if (player0 != null && !player0.isRevive && !player0.isBaseBtnOut)
  1089. {
  1090. distance0 = Mathf.Abs(player0.transform.position.x
  1091. - transform.position.x);
  1092. }
  1093. if (player1 != null && !player1.isRevive && !player1.isBaseBtnOut)
  1094. {
  1095. distance1 = Mathf.Abs(player1.transform.position.x
  1096. - transform.position.x);
  1097. }
  1098. if (distance0 == Mathf.Infinity && distance1 == Mathf.Infinity)
  1099. {
  1100. targetCharacter = null;
  1101. return;
  1102. }
  1103. if (distance0 <= distance1)
  1104. {
  1105. targetCharacter = player0;
  1106. if (!player0.beTargetCharacter.Exists(t => t == this))
  1107. {
  1108. player0.beTargetCharacter.Add(this);
  1109. }
  1110. distance = distance0;
  1111. }
  1112. else
  1113. {
  1114. targetCharacter = player1;
  1115. if (!player1.beTargetCharacter.Exists(t => t == this))
  1116. {
  1117. player1.beTargetCharacter.Add(this);
  1118. }
  1119. distance = distance1;
  1120. }
  1121. }
  1122. public void ReadyToDash(Vector3 pos0, Vector3 pos1)
  1123. {
  1124. Vector3 target = (pos0 - pos1).normalized;
  1125. float distance = aimDistance;
  1126. aimEffect.transform.localScale =
  1127. new Vector3(distance, 1, 1);
  1128. targetDir = pos0 - pos1;
  1129. float k = Mathf.Atan2(targetDir.y, targetDir.x) * Mathf.Rad2Deg;
  1130. if (targetDir.x < 0)
  1131. {
  1132. aimEffect.transform.rotation = Quaternion.Euler(new Vector3(0, 0, k));
  1133. if (bodyTrans.localScale.x < 0)
  1134. {
  1135. bodyTrans.localScale =
  1136. new Vector3(-bodyTrans.localScale.x, bodyTrans.localScale.y, bodyTrans.localScale.z);
  1137. }
  1138. }
  1139. else
  1140. {
  1141. aimEffect.transform.rotation = Quaternion.Euler(new Vector3(0, 0, k));
  1142. if (bodyTrans.localScale.x > 0)
  1143. {
  1144. bodyTrans.localScale =
  1145. new Vector3(-bodyTrans.localScale.x, bodyTrans.localScale.y, bodyTrans.localScale.z);
  1146. }
  1147. }
  1148. }
  1149. private void Rush()
  1150. {
  1151. float k = Mathf.Atan2(targetDir.y, targetDir.x) * Mathf.Rad2Deg;
  1152. if (targetDir.x < 0)
  1153. {
  1154. dashEffect.offset = 1;
  1155. if (bodyTrans.localScale.x < 0)
  1156. {
  1157. bodyTrans.localScale =
  1158. new Vector3(-bodyTrans.localScale.x, bodyTrans.localScale.y, bodyTrans.localScale.z);
  1159. }
  1160. bodyTrans.rotation = Quaternion.Euler(new Vector3(0, 0, k - 180));
  1161. }
  1162. else
  1163. {
  1164. if (bodyTrans.localScale.x > 0)
  1165. {
  1166. bodyTrans.localScale =
  1167. new Vector3(-bodyTrans.localScale.x, bodyTrans.localScale.y, bodyTrans.localScale.z);
  1168. }
  1169. dashEffect.offset = -1;
  1170. bodyTrans.rotation = Quaternion.Euler(new Vector3(0, 0, k));
  1171. }
  1172. rb.velocity = targetDir * rushSpeed;
  1173. }
  1174. }