Enemy.cs 29 KB

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