PlayerController.cs 52 KB

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