Enemy.cs 33 KB

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