PlayerController.cs 54 KB

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