PlayerController.cs 37 KB

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