Enemy.cs 35 KB

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