PlayerController.cs 44 KB

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