PlayerController.cs 52 KB

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