Enemy.cs 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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. if(targetCharacter == null)
  472. {
  473. ChangeState(CharacterState.FinishRush);
  474. break;
  475. }
  476. }
  477. else
  478. {
  479. if (targetCharacter.isRevive &&
  480. targetCharacter.GetComponent<PlayerController>().isBaseBtnOut)
  481. {
  482. targetCharacter = null;
  483. break;
  484. }
  485. }
  486. if (Mathf.Abs(transform.position.x-targetCharacter.transform.position.x)
  487. < hateDistance)
  488. {
  489. rushEndPos = targetCharacter.transform.position;
  490. isFindPlayer = true;
  491. break;
  492. }
  493. if (targetCharacter.transform.position.x > transform.position.x)
  494. {
  495. rb.velocity = Vector3.right * moveSpeed;
  496. if (bodyTrans.localScale.x > 0)
  497. {
  498. Turn();
  499. }
  500. }
  501. if (targetCharacter.transform.position.x < transform.position.x)
  502. {
  503. rb.velocity = Vector3.left * moveSpeed;
  504. if (bodyTrans.localScale.x < 0)
  505. {
  506. Turn();
  507. }
  508. }
  509. }
  510. break;
  511. case CharacterState.ReadyToRush:
  512. time += Time.deltaTime;
  513. if (time >= readyCD)
  514. {
  515. time = 0;
  516. if (rushHaveAttack)
  517. {
  518. ChangeState(CharacterState.RushAttack);
  519. }
  520. else
  521. {
  522. ChangeState(CharacterState.Rush);
  523. }
  524. }
  525. break;
  526. case CharacterState.RushAttack:
  527. time += Time.deltaTime;
  528. dashEffect.canHit = true;
  529. Rush();
  530. if (time >= rushTime)
  531. {
  532. time = 0;
  533. if (haveDownRush)
  534. {
  535. if (foot.TrigGround)
  536. {
  537. ChangeState(CharacterState.FinishRush);
  538. }
  539. else
  540. {
  541. ChangeState(CharacterState.ReadyToDownRush);
  542. }
  543. }
  544. else
  545. {
  546. ChangeState(CharacterState.FinishRush);
  547. }
  548. }
  549. break;
  550. case CharacterState.ReadyToDownRush:
  551. time += Time.deltaTime;
  552. if (time >= downRushTime)
  553. {
  554. time = 0;
  555. ChangeState(CharacterState.DownRush);
  556. }
  557. break;
  558. case CharacterState.DownRush:
  559. if (transform.position.y > 0)
  560. {
  561. dashEffect.canHit = true;
  562. Rush();
  563. }
  564. if (foot.TrigGround || transform.position.y <= -1)
  565. {
  566. ChangeState(CharacterState.FinishRush);
  567. }
  568. break;
  569. case CharacterState.FinishRush:
  570. time += Time.deltaTime;
  571. if (time > finishRushTime)
  572. {
  573. time = 0;
  574. ChangeState(CharacterState.Idle);
  575. }
  576. break;
  577. default:
  578. break;
  579. }
  580. }
  581. public override void ChangeState(CharacterState newState)
  582. {
  583. if (state == newState)
  584. {
  585. return;
  586. }
  587. switch (state)
  588. {
  589. case CharacterState.Idle:
  590. break;
  591. case CharacterState.Run:
  592. rb.velocity = Vector3.zero;
  593. break;
  594. case CharacterState.Rush:
  595. rb.velocity = Vector3.zero;
  596. break;
  597. case CharacterState.Rise:
  598. if (!canFly)
  599. {
  600. bodyCollider.SetActive(true);
  601. }
  602. break;
  603. case CharacterState.Fall:
  604. rb.velocity = Vector3.zero;
  605. break;
  606. case CharacterState.Hurt:
  607. break;
  608. case CharacterState.Attack:
  609. aniCollider.Play("NotAttack", 1, 0);
  610. break;
  611. case CharacterState.Die:
  612. isDie = false;
  613. break;
  614. case CharacterState.Weak:
  615. break;
  616. case CharacterState.Float:
  617. canMove = true;
  618. break;
  619. case CharacterState.Coma:
  620. canMove = true;
  621. foreach (GameObject i in HitCols)
  622. {
  623. i.SetActive(true);
  624. }
  625. break;
  626. case CharacterState.FindPlayer:
  627. noOnSearchState = false;
  628. rb.velocity = Vector3.zero;
  629. isFindPlayer = false;
  630. break;
  631. case CharacterState.ReadyToRush:
  632. time = 0;
  633. aimEffect.SetActive(false);
  634. aimEffect.transform.localScale = Vector3.zero;
  635. rb.constraints =
  636. RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotation;
  637. break;
  638. case CharacterState.RushAttack:
  639. time = 0;
  640. dashEffect.canHit = false;
  641. rb.velocity = Vector3.zero;
  642. bodyTrans.rotation = Quaternion.Euler(Vector3.zero);
  643. break;
  644. case CharacterState.ReadyToDownRush:
  645. time = 0;
  646. rb.constraints =
  647. RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotation;
  648. break;
  649. case CharacterState.DownRush:
  650. dashEffect.canHit = false;
  651. rb.velocity = Vector3.zero;
  652. bodyTrans.rotation = Quaternion.Euler(Vector3.zero);
  653. transform.position = new Vector3(transform.position.x, -1, 0);
  654. break;
  655. case CharacterState.FinishRush:
  656. time = 0;
  657. canNotChangeHurt = false;
  658. searchState = SearchState.NoTarget;
  659. noOnSearchState = false;
  660. ani.Play("idle", 0, 0);
  661. aniCollider.Play("Idle", 0, 0);
  662. break;
  663. default:
  664. break;
  665. }
  666. CharacterState oldState = state;
  667. state = newState;
  668. switch (newState)
  669. {
  670. case CharacterState.Idle:
  671. ani.Play("idle", 0, 0);
  672. aniCollider.Play("Idle", 0, 0);
  673. rb.velocity = Vector3.zero;
  674. //animalAni.SetInteger("state", (int)PlayerState.Idle);
  675. break;
  676. case CharacterState.Run:
  677. ani.Play("walk", 0, 0);
  678. aniCollider.Play("Walk", 0, 0);
  679. //animalAni.SetInteger("state", (int)PlayerState.Walk);
  680. break;
  681. case CharacterState.Rush:
  682. ani.Play("rush", 0, 0);
  683. aniCollider.Play("Rush", 0, 0);
  684. break;
  685. case CharacterState.Rise:
  686. aniCollider.Play("Rise", 0, 0);
  687. break;
  688. case CharacterState.Fall:
  689. aniCollider.Play("Fall", 0, 0);
  690. //animalAni.SetInteger("state", (int)PlayerState.Fall);
  691. break;
  692. case CharacterState.Hurt:
  693. switch (oldState)
  694. {
  695. case CharacterState.ReadyToRush:
  696. case CharacterState.RushAttack:
  697. case CharacterState.ReadyToDownRush:
  698. case CharacterState.DownRush:
  699. case CharacterState.Rush:
  700. state = oldState;
  701. beRepelValue = totalBeRepelValue;
  702. break;
  703. default:
  704. ani.Play("hitted", 0, 0);
  705. aniCollider.Play("Hurt", 0, 0);
  706. invincibleTime = totalInvincibleTime;
  707. hurtKeepTime = minHurtKeepTime;
  708. break;
  709. }
  710. break;
  711. case CharacterState.Float:
  712. canMove = false;
  713. break;
  714. case CharacterState.Coma:
  715. //ani.Play("Coma", 0, 0);
  716. ani.Play("idle", 0, 0);
  717. aniCollider.Play("Idle", 0, 0);
  718. rb.velocity = Vector3.zero;
  719. if (isCaughtByCook)
  720. {
  721. foreach (GameObject i in HitCols)
  722. {
  723. i.SetActive(false);
  724. }
  725. }
  726. pastComaTime = 0;
  727. break;
  728. case CharacterState.Attack:
  729. break;
  730. case CharacterState.Die:
  731. ani.Play("die", 0, 0);
  732. aniCollider.Play("Die", 0, 0);
  733. isDie = true;
  734. dieKeepTime = totalDieKeepTime;
  735. DropSouls();
  736. if (linked)
  737. {
  738. PlayersInput.instance[0].sprintLinkTrigger.linkedEnemy.Remove(this);
  739. PlayersInput.instance[0].playerRope.gameObject.SetActive(false);
  740. }
  741. break;
  742. case CharacterState.Weak:
  743. switch (oldState)
  744. {
  745. case CharacterState.ReadyToRush:
  746. case CharacterState.RushAttack:
  747. case CharacterState.ReadyToDownRush:
  748. case CharacterState.DownRush:
  749. state = oldState;
  750. beRepelValue = totalBeRepelValue;
  751. break;
  752. default:
  753. aniCollider.Play("Weak", 0, 0);
  754. ani.Play("weak", 0, 0);
  755. Vector3 velocity = rb.velocity;
  756. velocity.y = weakUpSpeed;
  757. rb.velocity = velocity;
  758. weakTime = totalWeakTime;
  759. break;
  760. }
  761. break;
  762. case CharacterState.FindPlayer:
  763. isFindPlayer = false;
  764. isFindingPlayer = true;
  765. noOnSearchState = true;
  766. ChosePlayer();
  767. ani.Play("walk", 0, 0);
  768. aniCollider.Play("Walk", 0, 0);
  769. break;
  770. case CharacterState.ReadyToRush:
  771. time = 0;
  772. canNotChangeHurt = true;
  773. ani.Play("charge", 0, 0);
  774. aimEffect.SetActive(true);
  775. rb.constraints = RigidbodyConstraints.FreezeAll;
  776. ReadyToDash(rushEndPos + Vector3.up, transform.position + Vector3.up);
  777. break;
  778. case CharacterState.RushAttack:
  779. targetDir =
  780. (rushEndPos - transform.position).normalized;
  781. ani.Play("rush_attack", 0, 0);
  782. aniCollider.Play("Attack1", 0, 0);
  783. break;
  784. case CharacterState.ReadyToDownRush:
  785. time = 0;
  786. rb.constraints = RigidbodyConstraints.FreezeAll;
  787. ani.Play("charge", 0, 0);
  788. break;
  789. case CharacterState.DownRush:
  790. time = 0;
  791. targetDir = Vector3.down;
  792. ani.Play("rush_attack", 0, 0);
  793. aniCollider.Play("Attack1", 0, 0);
  794. break;
  795. case CharacterState.FinishRush:
  796. time = 0;
  797. if (oldState == CharacterState.DownRush)
  798. {
  799. ani.Play("fall_end", 0, 0);
  800. }
  801. else
  802. {
  803. ani.Play("idle", 0, 0);
  804. aniCollider.Play("Idle", 0, 0);
  805. }
  806. break;
  807. default:
  808. break;
  809. }
  810. }
  811. public void DropSouls()
  812. {
  813. if (dropSoul > 1)
  814. {
  815. for (int i = 0; i < dropSoul; i++)
  816. {
  817. float angleInterval = dropSoulAngle / (float)(dropSoul - 1);
  818. float angle = 90 + ((float)i - (float)(dropSoul - 1) / 2) * angleInterval;
  819. angle = angle / 180 * Mathf.PI;
  820. GameObject soulObj = PoolManager.Instantiate(soulPrefab, transform.position);
  821. Vector3 dir = new Vector3(Mathf.Cos(angle), Mathf.Sin(angle), 0);
  822. soulObj.GetComponent<Soul>().Burst(dir * soulStartSpeed);
  823. }
  824. }
  825. else
  826. {
  827. GameObject soulObj = PoolManager.Instantiate(soulPrefab, transform.position);
  828. Vector3 dir = Vector3.up;
  829. soulObj.GetComponent<Soul>().Burst(dir * soulStartSpeed);
  830. }
  831. }
  832. public void Jump()
  833. {
  834. SetUpSpeed(jumpSpeed);
  835. ani.Play("jump", 0, 0);
  836. }
  837. public void SetUpSpeed(float speed)
  838. {
  839. ChangeState(CharacterState.Rise);
  840. Vector3 velocity = rb.velocity;
  841. Vector3 leftDir = GetMoveDir();
  842. if (leftDir.x > 0.3f)
  843. {
  844. if (bodyTrans.localScale.x > 0)
  845. {
  846. Turn();
  847. }
  848. }
  849. else if (leftDir.x < -0.3f)
  850. {
  851. if (bodyTrans.localScale.x < 0)
  852. {
  853. Turn();
  854. }
  855. }
  856. velocity.y = speed;
  857. rb.velocity = velocity;
  858. //animalAni.SetInteger("state", (int)PlayerState.Rise);
  859. }
  860. public void ChangeSearchState(SearchState newState)
  861. {
  862. switch (searchState)
  863. {
  864. case SearchState.NoTarget:
  865. break;
  866. case SearchState.InSearchScope:
  867. break;
  868. case SearchState.InAttackScope:
  869. break;
  870. default:
  871. break;
  872. }
  873. searchState = newState;
  874. switch (searchState)
  875. {
  876. case SearchState.NoTarget:
  877. Character character0 = PlayersInput.instance[0];
  878. Character character1 = PlayersInput.instance[1];
  879. if (character0.beTargetCharacter.Exists(t => t == this))
  880. {
  881. character0.beTargetCharacter.Remove(this);
  882. }
  883. if (character1.beTargetCharacter.Exists(t => t == this))
  884. {
  885. character1.beTargetCharacter.Remove(this);
  886. }
  887. targetCharacter = null;
  888. break;
  889. case SearchState.InSearchScope:
  890. break;
  891. case SearchState.InAttackScope:
  892. break;
  893. default:
  894. break;
  895. }
  896. }
  897. public bool SearchTarget()
  898. {
  899. targetCharacter = searchTrigger.GetMinDisTarget(targetTypes, canHitFly);
  900. if (targetCharacter != null)
  901. {
  902. Character character0 = PlayersInput.instance[0];
  903. Character character1 = PlayersInput.instance[1];
  904. if (targetCharacter == character0
  905. && !character0.beTargetCharacter.Exists(t => t == this))
  906. {
  907. character0.beTargetCharacter.Add(this);
  908. }
  909. if (targetCharacter == character1
  910. && !character1.beTargetCharacter.Exists(t => t == this))
  911. {
  912. character1.beTargetCharacter.Add(this);
  913. }
  914. return true;
  915. }
  916. else
  917. {
  918. return false;
  919. }
  920. }
  921. public void OnSearchState()
  922. {
  923. switch (searchState)
  924. {
  925. case SearchState.NoTarget:
  926. if (SearchTarget())
  927. {
  928. ChangeSearchState(SearchState.InSearchScope);
  929. break;
  930. }
  931. //向玩家基地移动
  932. break;
  933. case SearchState.InSearchScope:
  934. if (!SearchTarget())
  935. {
  936. targetCharacter = null;
  937. ChangeSearchState(SearchState.NoTarget);
  938. break;
  939. }
  940. if (targetCharacter != null && Mathf.Abs(targetCharacter.transform.position.x - transform.position.x) <= attackDistance)
  941. {
  942. ChangeSearchState(SearchState.InAttackScope);
  943. break;
  944. }
  945. break;
  946. case SearchState.InAttackScope:
  947. if (targetCharacter != null && !searchTrigger.IsCharacterLeave(targetCharacter, targetTypes, canHitFly))
  948. {
  949. if (!targetCharacter.gameObject.activeInHierarchy || targetCharacter.isDie
  950. || Mathf.Abs(targetCharacter.transform.position.x - transform.position.x) > attackDistance)
  951. {
  952. ChangeSearchState(SearchState.NoTarget);
  953. }
  954. }
  955. else
  956. {
  957. ChangeSearchState(SearchState.NoTarget);
  958. }
  959. break;
  960. default:
  961. break;
  962. }
  963. }
  964. public override void Attack1()
  965. {
  966. base.Attack1();
  967. attackTarget = targetCharacter;
  968. }
  969. public override void Attack2()
  970. {
  971. base.Attack2();
  972. attackTarget = targetCharacter;
  973. }
  974. public void ChosePlayer()
  975. {
  976. float distance0 = Mathf.Infinity;
  977. float distance1 = Mathf.Infinity;
  978. PlayerController player0 = PlayersInput.instance[0];
  979. PlayerController player1 = PlayersInput.instance[1];
  980. if (player0!=null && !player0.isRevive && !player0.isBaseBtnOut)
  981. {
  982. distance0 = Mathf.Abs(player0.transform.position.x
  983. - transform.position.x);
  984. }
  985. if (player1!=null && !player1.isRevive && !player1.isBaseBtnOut)
  986. {
  987. distance1 = Mathf.Abs(player1.transform.position.x
  988. - transform.position.x);
  989. }
  990. if(distance0 == Mathf.Infinity && distance1 == Mathf.Infinity)
  991. {
  992. targetCharacter = null;
  993. return;
  994. }
  995. if (distance0 <= distance1)
  996. {
  997. targetCharacter = player0;
  998. if (!player0.beTargetCharacter.Exists(t => t == this))
  999. {
  1000. player0.beTargetCharacter.Add(this);
  1001. }
  1002. distance = distance0;
  1003. }
  1004. else
  1005. {
  1006. targetCharacter = player1;
  1007. if (!player1.beTargetCharacter.Exists(t => t == this))
  1008. {
  1009. player1.beTargetCharacter.Add(this);
  1010. }
  1011. distance = distance1;
  1012. }
  1013. }
  1014. public void ReadyToDash(Vector3 pos0, Vector3 pos1)
  1015. {
  1016. Vector3 target = (pos0 - pos1).normalized;
  1017. float distance = aimDistance;
  1018. aimEffect.transform.localScale =
  1019. new Vector3(distance, 1, 1);
  1020. targetDir = pos0 - pos1;
  1021. float k = Mathf.Atan2(targetDir.y, targetDir.x) * Mathf.Rad2Deg;
  1022. if (targetDir.x < 0)
  1023. {
  1024. aimEffect.transform.rotation = Quaternion.Euler(new Vector3(0, 0, k));
  1025. if (bodyTrans.localScale.x < 0)
  1026. {
  1027. bodyTrans.localScale =
  1028. new Vector3(-bodyTrans.localScale.x, bodyTrans.localScale.y, bodyTrans.localScale.z);
  1029. }
  1030. }
  1031. else
  1032. {
  1033. aimEffect.transform.rotation = Quaternion.Euler(new Vector3(0, 0, k));
  1034. if (bodyTrans.localScale.x > 0)
  1035. {
  1036. bodyTrans.localScale =
  1037. new Vector3(-bodyTrans.localScale.x, bodyTrans.localScale.y, bodyTrans.localScale.z);
  1038. }
  1039. }
  1040. }
  1041. private void Rush()
  1042. {
  1043. float k = Mathf.Atan2(targetDir.y, targetDir.x) * Mathf.Rad2Deg;
  1044. if (targetDir.x < 0)
  1045. {
  1046. dashEffect.offset = 1;
  1047. if (bodyTrans.localScale.x < 0)
  1048. {
  1049. bodyTrans.localScale =
  1050. new Vector3(-bodyTrans.localScale.x, bodyTrans.localScale.y, bodyTrans.localScale.z);
  1051. }
  1052. bodyTrans.rotation = Quaternion.Euler(new Vector3(0, 0, k - 180));
  1053. }
  1054. else
  1055. {
  1056. if (bodyTrans.localScale.x > 0)
  1057. {
  1058. bodyTrans.localScale =
  1059. new Vector3(-bodyTrans.localScale.x, bodyTrans.localScale.y, bodyTrans.localScale.z);
  1060. }
  1061. dashEffect.offset = -1;
  1062. bodyTrans.rotation = Quaternion.Euler(new Vector3(0, 0, k));
  1063. }
  1064. rb.velocity = targetDir * rushSpeed;
  1065. }
  1066. }