Enemy.cs 38 KB

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