PlayerController.cs 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Spine;
  5. using Spine.Unity;
  6. using UnityEngine.LowLevel;
  7. using UnityEngine.Playables;
  8. using System.ComponentModel;
  9. using Unity.VisualScripting;
  10. using System;
  11. using Base.Common;
  12. using cfg;
  13. using static UnityEngine.EventSystems.EventTrigger;
  14. [Serializable]
  15. public struct AttackInfo
  16. {
  17. public int damage;
  18. public Vector3 attackDir;
  19. public float force;
  20. public bool changeHurt;
  21. public float hurtTime;
  22. public float repelValue;
  23. }
  24. public enum PlayerAttackState
  25. {
  26. Idle = 0,
  27. WalkForward = 1,
  28. WalkBack = 2,
  29. }
  30. public class PlayerController : MoveCharacter
  31. {
  32. public static PlayerController instance;
  33. public List<GameObject> demonicPrefabs;
  34. public List<Vector3> demonicSummonPos;
  35. public Dictionary<int, List<Demonic>> demonicDic;
  36. public UIHP uiMp;
  37. public float jumpSpeed = 10;
  38. public float airJumpSpeed = 10;
  39. //public float moveAcc = 5f;
  40. //public float airMoveAcc = 3f;
  41. public float rushSpeed = 100;
  42. public float mp;
  43. public float totalMp;
  44. public float mpReplySpeed = 1;
  45. public float rushCostMp = 5;
  46. public float sprintCostMp = 5;
  47. public float rushInvincibleTime = 0.2f;
  48. [HideInInspector]
  49. public float canJumpTime; //离开平台后仍然可以跳跃的时间,用于提升手感
  50. public float leaveGroundCanJumpTime = 0.1f;
  51. [HideInInspector]
  52. public float cacheJumpTime; //即将落地时按下跳跃键不会跳跃,手感不好,缓存几帧,在这几帧内落地会立即跳跃;
  53. public float totalCacheJumpTime = 0.1f;
  54. [HideInInspector]
  55. public float summonTime;
  56. public float totalSummonTime = 0.5f;
  57. [HideInInspector]
  58. public float cacheAttackTime; //无法攻击时按下攻击键不会攻击,手感不好,缓存几帧,在这几帧内落地会立即攻击;
  59. public float totalCacheAttackTime = 0.1f;
  60. [HideInInspector]
  61. public float cacheSummonTime; //无法召唤时按下召唤键不会召唤,手感不好,缓存几帧,在这几帧内落地会立即召唤;
  62. public float totalCacheSummonTime = 0.1f;
  63. [HideInInspector]
  64. public int cacheSummonId;
  65. [HideInInspector]
  66. public float rushTime;
  67. public float totalRushTime = 0.5f;
  68. [HideInInspector]
  69. public float cacheRushTime; //无法Rush时按下Rush键不会Rush,手感不好,缓存几帧,在这几帧内落地会立即Rush;
  70. public float totalCacheRushTime = 0.1f;
  71. [HideInInspector]
  72. public bool airJumped;
  73. public PlayerAttackState attackState;
  74. public float attackMoveSpeed = 5f;
  75. public bool btnJumpPress
  76. {
  77. get
  78. {
  79. return Input.GetKeyDown(KeyCode.Space) || isClickBtnJump;
  80. }
  81. }
  82. [HideInInspector]
  83. public bool isClickBtnJump;
  84. public bool btnRushPress
  85. {
  86. get
  87. {
  88. return Input.GetKeyDown(KeyCode.LeftShift) || isClickBtnRush;
  89. }
  90. }
  91. [HideInInspector]
  92. public bool isClickBtnRush;
  93. public bool btnRushKeep
  94. {
  95. get
  96. {
  97. return Input.GetKey(KeyCode.LeftShift) || isKeepBtnRush;
  98. }
  99. }
  100. [HideInInspector]
  101. public bool isKeepBtnRush;
  102. public bool btnSouthPress
  103. {
  104. get
  105. {
  106. return Input.GetKeyDown(KeyCode.K) || isClickBtnSouth;
  107. }
  108. }
  109. [HideInInspector]
  110. public bool isClickBtnSouth;
  111. public bool btnEastPress
  112. {
  113. get
  114. {
  115. return Input.GetKeyDown(KeyCode.L) || isClickBtnEast;
  116. }
  117. }
  118. [HideInInspector]
  119. public bool isClickBtnEast;
  120. public bool btnWestPress
  121. {
  122. get
  123. {
  124. return Input.GetKeyDown(KeyCode.J) || isClickBtnWest;
  125. }
  126. }
  127. [HideInInspector]
  128. public bool isClickBtnWest;
  129. public bool btnNorthPress
  130. {
  131. get
  132. {
  133. return Input.GetKeyDown(KeyCode.I) || isClickBtnNorth;
  134. }
  135. }
  136. [HideInInspector]
  137. public bool isClickBtnNorth;
  138. public bool btnNorthKeep
  139. {
  140. get
  141. {
  142. return Input.GetKey(KeyCode.I) || isKeepBtnNorth;
  143. }
  144. }
  145. [HideInInspector]
  146. public bool isKeepBtnNorth;
  147. public Vector2 leftDir
  148. {
  149. get
  150. {
  151. int x = 0;
  152. int y = 0;
  153. if (Input.GetKey(KeyCode.A))
  154. {
  155. x--;
  156. }
  157. if (Input.GetKey(KeyCode.D))
  158. {
  159. x++;
  160. }
  161. if (Input.GetKey(KeyCode.S))
  162. {
  163. y--;
  164. }
  165. if (Input.GetKey(KeyCode.W))
  166. {
  167. y++;
  168. }
  169. return new Vector2(x, y);
  170. }
  171. }
  172. public override void Init()
  173. {
  174. base.Init();
  175. mp = totalMp;
  176. uiMp.Show(mp, totalMp);
  177. }
  178. private void Awake()
  179. {
  180. if (!instance)
  181. {
  182. instance = this;
  183. }
  184. else
  185. {
  186. DestroyImmediate(gameObject);
  187. return;
  188. }
  189. demonicDic = new Dictionary<int, List<Demonic>>();
  190. Init();
  191. }
  192. private void Update()
  193. {
  194. if (Input.GetKeyDown(KeyCode.LeftShift))
  195. {
  196. isClickBtnRush = true;
  197. }
  198. if (Input.GetKey(KeyCode.LeftShift))
  199. {
  200. isKeepBtnRush = true;
  201. }
  202. if (Input.GetKeyDown(KeyCode.Space))
  203. {
  204. isClickBtnJump = true;
  205. }
  206. if (Input.GetKeyDown(KeyCode.J))
  207. {
  208. isClickBtnWest = true;
  209. }
  210. if (Input.GetKeyDown(KeyCode.K))
  211. {
  212. isClickBtnSouth = true;
  213. }
  214. if (Input.GetKeyDown(KeyCode.L))
  215. {
  216. isClickBtnEast = true;
  217. }
  218. if (Input.GetKeyDown(KeyCode.I))
  219. {
  220. isClickBtnNorth = true;
  221. }
  222. }
  223. public void Jump()
  224. {
  225. SetUpSpeed(jumpSpeed);
  226. ani.Play("jump", 0, 0);
  227. }
  228. public void AirJump()
  229. {
  230. SetUpSpeed(airJumpSpeed);
  231. ani.Play("jump", 0, 0);
  232. }
  233. public void SetUpSpeed(float speed)
  234. {
  235. ChangeState(CharacterState.Rise);
  236. Vector3 velocity = rb.velocity;
  237. CheckTurn();
  238. velocity.y = speed;
  239. rb.velocity = velocity;
  240. //animalAni.SetInteger("state", (int)PlayerState.Rise);
  241. }
  242. public bool CheckSummon()
  243. {
  244. if (cacheSummonTime > 0)
  245. {
  246. Summon(cacheSummonId);
  247. return true;
  248. }
  249. if (btnWestPress)
  250. {
  251. Summon(0);
  252. return true;
  253. }
  254. if (btnSouthPress)
  255. {
  256. Summon(1);
  257. return true;
  258. }
  259. if (btnEastPress)
  260. {
  261. Summon(2);
  262. return true;
  263. }
  264. return false;
  265. }
  266. //角色处于可自由活动状态时的通用切换状态逻辑,如Idle、Run状态,以及别的状态结束时准备回到Idle状态前
  267. public bool CheckPlayerChangeState(CharacterState excludeState = CharacterState.None)
  268. {
  269. if (!foot.TrigGround)
  270. {
  271. if ((btnRushPress || cacheRushTime > 0) && mp >= rushCostMp)
  272. {
  273. if (excludeState != CharacterState.Rush)
  274. {
  275. ChangeState(CharacterState.Rush);
  276. return true;
  277. }
  278. }
  279. if (rb.velocity.y > 0)
  280. {
  281. if (excludeState != CharacterState.Rise)
  282. {
  283. ChangeState(CharacterState.Rise);
  284. return true;
  285. }
  286. }
  287. else
  288. {
  289. if (excludeState != CharacterState.Fall)
  290. {
  291. ChangeState(CharacterState.Fall);
  292. return true;
  293. }
  294. }
  295. }
  296. else
  297. {
  298. airJumped = false;
  299. if (btnNorthPress || cacheAttackTime > 0)
  300. {
  301. if (excludeState != CharacterState.Attack)
  302. {
  303. Attack1();
  304. return true;
  305. }
  306. }
  307. if (excludeState != CharacterState.Summon)
  308. {
  309. if (CheckSummon())
  310. {
  311. return true;
  312. }
  313. }
  314. if ((btnRushPress || cacheRushTime > 0) && mp >= rushCostMp)
  315. {
  316. if (excludeState != CharacterState.Rush)
  317. {
  318. ChangeState(CharacterState.Rush);
  319. return true;
  320. }
  321. }
  322. if (btnJumpPress || cacheJumpTime > 0)
  323. {
  324. if (excludeState != CharacterState.Rise)
  325. {
  326. Jump();
  327. ChangeState(CharacterState.Rise);
  328. return true;
  329. }
  330. }
  331. if (leftDir.x > 0.3f || leftDir.x < -0.3f)
  332. {
  333. if (excludeState != CharacterState.Run)
  334. {
  335. ChangeState(CharacterState.Run);
  336. return true;
  337. }
  338. }
  339. else
  340. {
  341. if (excludeState != CharacterState.Idle)
  342. {
  343. ChangeState(CharacterState.Idle);
  344. return true;
  345. }
  346. }
  347. }
  348. return false;
  349. }
  350. public override Vector3 GetMoveDir()
  351. {
  352. return leftDir;
  353. }
  354. public void CachedPlayerInput()
  355. {
  356. if (btnRushPress)
  357. {
  358. cacheRushTime = totalCacheRushTime;
  359. }
  360. if (btnJumpPress)
  361. {
  362. cacheJumpTime = totalCacheJumpTime;
  363. }
  364. if (btnNorthPress)
  365. {
  366. cacheAttackTime = totalCacheAttackTime;
  367. }
  368. if (btnWestPress)
  369. {
  370. cacheSummonTime = totalCacheSummonTime;
  371. cacheSummonId = 0;
  372. }
  373. if (btnSouthPress)
  374. {
  375. cacheSummonTime = totalCacheSummonTime;
  376. cacheSummonId = 1;
  377. }
  378. if (btnEastPress)
  379. {
  380. cacheSummonTime = totalCacheSummonTime;
  381. cacheSummonId = 2;
  382. }
  383. }
  384. public override void OnState()
  385. {
  386. base.OnState();
  387. cacheJumpTime -= Time.deltaTime;
  388. cacheAttackTime -= Time.deltaTime;
  389. cacheSummonTime -= Time.deltaTime;
  390. canJumpTime -= Time.deltaTime;
  391. invincibleTime -= Time.deltaTime;
  392. hurtKeepTime -= Time.deltaTime;
  393. attackTime -= Time.deltaTime;
  394. summonTime -= Time.deltaTime;
  395. rushTime -= Time.deltaTime;
  396. cacheRushTime -= Time.deltaTime;
  397. dieKeepTime -= Time.deltaTime;
  398. weakTime -= Time.deltaTime;
  399. Vector3 velocity = rb.velocity;
  400. switch (state)
  401. {
  402. case CharacterState.Idle:
  403. if (CheckPlayerChangeState(CharacterState.Idle))
  404. {
  405. break;
  406. }
  407. break;
  408. case CharacterState.Run:
  409. if (CheckPlayerChangeState(CharacterState.Run))
  410. {
  411. break;
  412. }
  413. CheckTurn();
  414. if (leftDir.x > 0.3f)
  415. {
  416. rb.velocity = Vector3.right * moveSpeed;
  417. }
  418. else if (leftDir.x < -0.3f)
  419. {
  420. rb.velocity = Vector3.left * moveSpeed;
  421. }
  422. break;
  423. case CharacterState.Rise:
  424. if (CheckSummon())
  425. {
  426. break;
  427. }
  428. if ((btnRushPress || cacheRushTime > 0) && mp >= rushCostMp)
  429. {
  430. ChangeState(CharacterState.Rush);
  431. break;
  432. }
  433. if (rb.velocity.y <= 0)
  434. {
  435. ChangeState(CharacterState.Fall);
  436. break;
  437. }
  438. if (btnJumpPress || cacheJumpTime > 0)
  439. {
  440. if (!airJumped && rb.velocity.y < airJumpSpeed)
  441. {
  442. airJumped = true;
  443. AirJump();
  444. break;
  445. }
  446. }
  447. CachedPlayerInput();
  448. rb.velocity = AirMove(rb.velocity);
  449. break;
  450. case CharacterState.Fall:
  451. if (CheckSummon())
  452. {
  453. break;
  454. }
  455. if ((btnRushPress || cacheRushTime > 0) && mp >= rushCostMp)
  456. {
  457. ChangeState(CharacterState.Rush);
  458. break;
  459. }
  460. if (foot.TrigGround)
  461. {
  462. if (CheckPlayerChangeState())
  463. {
  464. break;
  465. }
  466. }
  467. //if (foot.canStepPlayers.Count > 0)
  468. //{
  469. // Jump(jumpSpeed / 2);
  470. // StepOther();
  471. // break;
  472. //}
  473. //if (foot.canStepEnemyList.Count > 0)
  474. //{
  475. // Jump(jumpSpeed / 2);
  476. // StepEnemy();
  477. // break;
  478. //}
  479. if (btnJumpPress || cacheJumpTime > 0)
  480. {
  481. if (canJumpTime > 0)
  482. {
  483. Jump();
  484. break;
  485. }
  486. else if (!airJumped)
  487. {
  488. airJumped = true;
  489. AirJump();
  490. break;
  491. }
  492. }
  493. CachedPlayerInput();
  494. rb.velocity = AirMove(rb.velocity);
  495. break;
  496. case CharacterState.Hurt:
  497. if (hurtKeepTime <= 0)
  498. {
  499. if (CheckPlayerChangeState())
  500. {
  501. break;
  502. }
  503. }
  504. CachedPlayerInput();
  505. break;
  506. case CharacterState.Attack:
  507. if (attackTime <= 0)
  508. {
  509. if (btnNorthKeep)
  510. {
  511. ChangeState(CharacterState.KeepAttack);
  512. break;
  513. }
  514. if (CheckPlayerChangeState())
  515. {
  516. break;
  517. }
  518. }
  519. CachedPlayerInput();
  520. break;
  521. case CharacterState.KeepAttack:
  522. if ((btnRushPress) && mp >= rushCostMp)
  523. {
  524. ChangeState(CharacterState.Rush);
  525. break;
  526. }
  527. if (btnJumpPress && canJumpTime > 0)
  528. {
  529. Jump();
  530. break;
  531. }
  532. if (!btnNorthKeep)
  533. {
  534. if (CheckPlayerChangeState(CharacterState.Attack))
  535. {
  536. break;
  537. }
  538. }
  539. switch (attackState)
  540. {
  541. case PlayerAttackState.Idle:
  542. if (bodyTrans.localScale.x > 0)
  543. {
  544. if (leftDir.x > 0.3f)
  545. {
  546. SetAttackState(PlayerAttackState.WalkBack);
  547. velocity.x = attackMoveSpeed;
  548. rb.velocity = velocity;
  549. break;
  550. }
  551. else if (leftDir.x < -0.3f)
  552. {
  553. SetAttackState(PlayerAttackState.WalkForward);
  554. velocity.x = -attackMoveSpeed;
  555. rb.velocity = velocity;
  556. break;
  557. }
  558. }
  559. else
  560. {
  561. if (leftDir.x > 0.3f)
  562. {
  563. SetAttackState(PlayerAttackState.WalkForward);
  564. velocity.x = attackMoveSpeed;
  565. rb.velocity = velocity;
  566. break;
  567. }
  568. else if (leftDir.x < -0.3f)
  569. {
  570. SetAttackState(PlayerAttackState.WalkBack);
  571. velocity.x = -attackMoveSpeed;
  572. rb.velocity = velocity;
  573. break;
  574. }
  575. }
  576. velocity.x = 0;
  577. rb.velocity = velocity;
  578. break;
  579. case PlayerAttackState.WalkForward:
  580. if (bodyTrans.localScale.x > 0)
  581. {
  582. if (leftDir.x > 0.3f)
  583. {
  584. SetAttackState(PlayerAttackState.WalkBack);
  585. velocity.x = attackMoveSpeed;
  586. rb.velocity = velocity;
  587. break;
  588. }
  589. else if (leftDir.x > -0.3f && leftDir.x < 0.3f)
  590. {
  591. SetAttackState(PlayerAttackState.Idle);
  592. velocity.x = 0;
  593. rb.velocity = velocity;
  594. break;
  595. }
  596. else
  597. {
  598. velocity.x = -attackMoveSpeed;
  599. rb.velocity = velocity;
  600. }
  601. }
  602. else
  603. {
  604. if (leftDir.x < -0.3f)
  605. {
  606. SetAttackState(PlayerAttackState.WalkBack);
  607. velocity.x = -attackMoveSpeed;
  608. rb.velocity = velocity;
  609. break;
  610. }
  611. else if (leftDir.x > -0.3f && leftDir.x < 0.3f)
  612. {
  613. SetAttackState(PlayerAttackState.Idle);
  614. velocity.x = 0;
  615. rb.velocity = velocity;
  616. break;
  617. }
  618. else
  619. {
  620. velocity.x = attackMoveSpeed;
  621. rb.velocity = velocity;
  622. }
  623. }
  624. break;
  625. case PlayerAttackState.WalkBack:
  626. if (bodyTrans.localScale.x > 0)
  627. {
  628. if (leftDir.x < -0.3f)
  629. {
  630. SetAttackState(PlayerAttackState.WalkForward);
  631. velocity.x = -attackMoveSpeed;
  632. rb.velocity = velocity;
  633. break;
  634. }
  635. else if (leftDir.x > -0.3f && leftDir.x < 0.3f)
  636. {
  637. SetAttackState(PlayerAttackState.Idle);
  638. velocity.x = 0;
  639. rb.velocity = velocity;
  640. break;
  641. }
  642. else
  643. {
  644. velocity.x = attackMoveSpeed;
  645. rb.velocity = velocity;
  646. }
  647. }
  648. else
  649. {
  650. if (leftDir.x > 0.3f)
  651. {
  652. SetAttackState(PlayerAttackState.WalkForward);
  653. velocity.x = attackMoveSpeed;
  654. rb.velocity = velocity;
  655. break;
  656. }
  657. else if (leftDir.x > -0.3f && leftDir.x > 0.3f)
  658. {
  659. SetAttackState(PlayerAttackState.Idle);
  660. velocity.x = 0;
  661. rb.velocity = velocity;
  662. break;
  663. }
  664. else
  665. {
  666. velocity.x = -attackMoveSpeed;
  667. rb.velocity = velocity;
  668. }
  669. }
  670. break;
  671. default:
  672. break;
  673. }
  674. break;
  675. case CharacterState.Summon:
  676. if (summonTime <= 0)
  677. {
  678. if (CheckPlayerChangeState())
  679. {
  680. break;
  681. }
  682. }
  683. break;
  684. case CharacterState.Rush:
  685. if (rushTime <= 0)
  686. {
  687. if (btnRushKeep)
  688. {
  689. ChangeState(CharacterState.Sprint);
  690. break;
  691. }
  692. if (CheckPlayerChangeState())
  693. {
  694. break;
  695. }
  696. }
  697. CachedPlayerInput();
  698. if (bodyTrans.localScale.x > 0)
  699. {
  700. rb.velocity = Vector3.left * rushSpeed;
  701. }
  702. else
  703. {
  704. rb.velocity = Vector3.right * rushSpeed;
  705. }
  706. break;
  707. case CharacterState.Sprint:
  708. if (!btnRushKeep)
  709. {
  710. if (CheckPlayerChangeState(CharacterState.Rush))
  711. {
  712. break;
  713. }
  714. }
  715. if (mp < sprintCostMp * Time.deltaTime)
  716. {
  717. if (CheckPlayerChangeState(CharacterState.Rush))
  718. {
  719. break;
  720. }
  721. }
  722. mp -= sprintCostMp * Time.deltaTime;
  723. uiMp.Show(mp, totalMp);
  724. CachedPlayerInput();
  725. CheckTurn();
  726. if (bodyTrans.localScale.x > 0)
  727. {
  728. rb.velocity = Vector3.left * rushSpeed;
  729. }
  730. else
  731. {
  732. rb.velocity = Vector3.right * rushSpeed;
  733. }
  734. break;
  735. case CharacterState.Die:
  736. if (dieKeepTime <= 0)
  737. {
  738. gameObject.SetActive(false);
  739. break;
  740. }
  741. break;
  742. case CharacterState.Weak:
  743. if (weakTime <= 0)
  744. {
  745. ChangeState(CharacterState.Idle);
  746. break;
  747. }
  748. break;
  749. default:
  750. break;
  751. }
  752. if (!foot.TrigGround)
  753. {
  754. if (rb.velocity.y > 0)
  755. {
  756. rb.velocity += Vector3.up * extraRiseGravity * Time.deltaTime;
  757. }
  758. else
  759. {
  760. rb.velocity += Vector3.up * extraFallGravity * Time.deltaTime;
  761. }
  762. }
  763. isClickBtnRush = false;
  764. isKeepBtnRush = false;
  765. isClickBtnJump = false;
  766. isClickBtnSouth = false;
  767. isClickBtnEast = false;
  768. isClickBtnNorth = false;
  769. isClickBtnWest = false;
  770. if (foot.TrigGround)
  771. {
  772. canJumpTime = leaveGroundCanJumpTime;
  773. }
  774. SearchTarget();
  775. attackTarget = targetCharacter;
  776. if (mp < totalMp)
  777. {
  778. mp += mpReplySpeed * Time.deltaTime;
  779. }
  780. if (mp > totalMp)
  781. {
  782. mp = totalMp;
  783. }
  784. uiMp.Show(mp, totalMp);
  785. }
  786. public override void ChangeState(CharacterState newState)
  787. {
  788. Vector3 velocity = rb.velocity;
  789. switch (state)
  790. {
  791. case CharacterState.Idle:
  792. break;
  793. case CharacterState.Run:
  794. velocity.x = 0;
  795. break;
  796. case CharacterState.Rise:
  797. break;
  798. case CharacterState.Fall:
  799. break;
  800. case CharacterState.Hurt:
  801. break;
  802. case CharacterState.Attack:
  803. aniCollider.Play("NotAttack", 1, 0);
  804. break;
  805. case CharacterState.KeepAttack:
  806. aniCollider.Play("NotAttack", 1, 0);
  807. break;
  808. case CharacterState.Summon:
  809. break;
  810. case CharacterState.Rush:
  811. velocity = Vector3.zero;
  812. break;
  813. case CharacterState.Sprint:
  814. velocity = Vector3.zero;
  815. break;
  816. case CharacterState.Die:
  817. isDie = false;
  818. break;
  819. case CharacterState.Weak:
  820. break;
  821. default:
  822. break;
  823. }
  824. CharacterState oldState = state;
  825. state = newState;
  826. switch (newState)
  827. {
  828. case CharacterState.Idle:
  829. aniCollider.Play("Idle", 0, 0);
  830. if (oldState == CharacterState.Fall)
  831. {
  832. ani.Play("fall_end", 0, 0);
  833. }
  834. else
  835. {
  836. ani.Play("idle", 0, 0);
  837. }
  838. velocity = Vector3.zero;
  839. //animalAni.SetInteger("state", (int)PlayerState.Idle);
  840. break;
  841. case CharacterState.Run:
  842. aniCollider.Play("Run", 0, 0);
  843. ani.Play("run_start", 0, 0);
  844. //animalAni.SetInteger("state", (int)PlayerState.Walk);
  845. break;
  846. case CharacterState.Rise:
  847. aniCollider.Play("Rise", 0, 0);
  848. canJumpTime = 0;
  849. break;
  850. case CharacterState.Fall:
  851. aniCollider.Play("Fall", 0, 0);
  852. ani.Play("fall", 0, 0);
  853. //animalAni.SetInteger("state", (int)PlayerState.Fall);
  854. break;
  855. case CharacterState.Hurt:
  856. aniCollider.Play("Hurt", 0, 0);
  857. ani.Play("hitted", 0, 0);
  858. invincibleTime = totalInvincibleTime;
  859. //ani.Play("Invincible", 2, 0);
  860. break;
  861. case CharacterState.Attack:
  862. attackTime = totalAttack1Time;
  863. break;
  864. case CharacterState.KeepAttack:
  865. aniCollider.Play("Attack1Keep", 1, 0);
  866. break;
  867. case CharacterState.Summon:
  868. aniCollider.Play("Summon", 0, 0);
  869. ani.Play("summon", 0, 0);
  870. summonTime = totalSummonTime;
  871. break;
  872. case CharacterState.Rush:
  873. aniCollider.Play("Rush", 0, 0);
  874. ani.Play("rush_loop", 0, 0);
  875. rushTime = totalRushTime;
  876. invincibleTime = rushInvincibleTime;
  877. if (bodyTrans.localScale.x > 0)
  878. {
  879. velocity = Vector3.left * rushSpeed;
  880. }
  881. else
  882. {
  883. velocity = Vector3.right * rushSpeed;
  884. }
  885. mp -= rushCostMp;
  886. uiMp.Show(mp, totalMp);
  887. break;
  888. case CharacterState.Sprint:
  889. aniCollider.Play("Sprint", 0, 0);
  890. ani.Play("rush_loop", 0, 0);
  891. if (bodyTrans.localScale.x > 0)
  892. {
  893. velocity = Vector3.left * rushSpeed;
  894. }
  895. else
  896. {
  897. velocity = Vector3.right * rushSpeed;
  898. }
  899. break;
  900. case CharacterState.Die:
  901. aniCollider.Play("Die", 0, 0);
  902. ani.Play("die", 0, 0);
  903. isDie = true;
  904. dieKeepTime = totalDieKeepTime;
  905. break;
  906. case CharacterState.Weak:
  907. aniCollider.Play("Weak", 0, 0);
  908. ani.Play("weak", 0, 0);
  909. velocity.y = weakUpSpeed;
  910. weakTime = totalWeakTime;
  911. break;
  912. default:
  913. break;
  914. }
  915. rb.velocity = velocity;
  916. }
  917. public void CheckTurn()
  918. {
  919. if (leftDir.x > 0.3f && bodyTrans.localScale.x > 0)
  920. {
  921. Turn();
  922. }
  923. else if (leftDir.x < -0.3f && bodyTrans.localScale.x < 0)
  924. {
  925. Turn();
  926. }
  927. }
  928. public Vector3 AirMove(Vector3 velocity)
  929. {
  930. CheckTurn();
  931. if (leftDir.x > 0.3f)
  932. {
  933. velocity = new Vector3(moveSpeed, velocity.y, velocity.z);
  934. }
  935. else if (leftDir.x < -0.3f)
  936. {
  937. velocity = new Vector3(-moveSpeed, velocity.y, velocity.z);
  938. }
  939. else
  940. {
  941. velocity = new Vector3(0, velocity.y, velocity.z);
  942. }
  943. return velocity;
  944. }
  945. public void Summon(int id)
  946. {
  947. if (id >= demonicPrefabs.Count)
  948. {
  949. Debug.LogError("未配置" + id + "号使魔");
  950. return;
  951. }
  952. if (id >= demonicSummonPos.Count)
  953. {
  954. Debug.LogError("未配置" + id + "号使魔召唤位置");
  955. return;
  956. }
  957. GameObject prefab = demonicPrefabs[id];
  958. if (!CheckCanSummon(id))
  959. {
  960. return;
  961. }
  962. ChangeState(CharacterState.Summon);
  963. float costMp = prefab.GetComponent<Demonic>().costMp;
  964. mp -= costMp;
  965. uiMp.Show(mp, totalMp);
  966. GameObject demonicObj = PoolManager.Instantiate(prefab);
  967. Demonic demonic = demonicObj.GetComponent<Demonic>();
  968. demonic.id = id;
  969. if (!demonicDic.ContainsKey(id))
  970. {
  971. demonicDic.Add(id, new List<Demonic>());
  972. }
  973. demonicDic[id].Add(demonic);
  974. demonicObj.transform.parent = null;
  975. demonicObj.transform.localEulerAngles = Vector3.zero;
  976. demonicObj.transform.localScale = new Vector3(1, 1, 1);
  977. Vector3 offset = demonicSummonPos[id];
  978. if (bodyTrans.localScale.x > 0)
  979. {
  980. demonicObj.transform.position = transform.position + offset;
  981. if (demonic.bodyTrans.localScale.x < 0)
  982. {
  983. demonic.Turn();
  984. }
  985. }
  986. else
  987. {
  988. demonicObj.transform.position = transform.position + new Vector3(-offset.x, offset.y, offset.z);
  989. if (demonic.bodyTrans.localScale.x > 0)
  990. {
  991. demonic.Turn();
  992. }
  993. }
  994. demonic.totalHp = 100;
  995. demonic.Init();
  996. demonic.SetSortingOrder(id * 1000 + demonicDic[id].Count);
  997. demonic.Attack1();
  998. }
  999. public void OnDemonicRecycle(Demonic demonic)
  1000. {
  1001. if (!demonicDic.ContainsKey(demonic.id))
  1002. {
  1003. return;
  1004. }
  1005. demonicDic[demonic.id].Remove(demonic);
  1006. for (int i = 0; i < demonicDic[demonic.id].Count; i++)
  1007. {
  1008. demonicDic[demonic.id][i].SetSortingOrder(demonic.id * 1000 + i);
  1009. }
  1010. }
  1011. public bool CheckCanSummon(int id)
  1012. {
  1013. GameObject prefab = demonicPrefabs[id];
  1014. float costMp = prefab.GetComponent<Demonic>().costMp;
  1015. if (mp < costMp)
  1016. {
  1017. Debug.Log("mp不足召唤失败, 还得加个动画或者音效啥的");
  1018. return false;
  1019. }
  1020. return true;
  1021. }
  1022. public override void Attack1()
  1023. {
  1024. base.Attack1();
  1025. if (leftDir.x > 0.3f)
  1026. {
  1027. if (bodyTrans.localScale.x > 0)
  1028. {
  1029. Turn();
  1030. }
  1031. SetAttackState(PlayerAttackState.WalkForward);
  1032. }
  1033. else if (leftDir.x < -0.3f)
  1034. {
  1035. if (bodyTrans.localScale.x < 0)
  1036. {
  1037. Turn();
  1038. }
  1039. SetAttackState(PlayerAttackState.WalkForward);
  1040. }
  1041. else
  1042. {
  1043. SetAttackState(PlayerAttackState.Idle);
  1044. }
  1045. }
  1046. public void SetAttackState(PlayerAttackState value)
  1047. {
  1048. attackState = value;
  1049. ani.SetInteger("attackState", (int)value);
  1050. aniCollider.Play("Attack1Keep", 1, 0);
  1051. }
  1052. public void SearchTarget()
  1053. {
  1054. targetCharacter = searchTrigger.GetMinDisTarget(targetTypes, canHitFly);
  1055. }
  1056. }