PlayerController.cs 37 KB

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