PlayerController.cs 33 KB

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