PlayerController.cs 40 KB

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