Demonic.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. using Spine.Unity;
  2. using Spine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public class Demonic : MoveCharacter
  7. {
  8. public PlayerController player; //召唤师
  9. public int id;
  10. public float costMp = 10;
  11. public float totalSummonTime = 0.5f;
  12. public SearchState searchState;
  13. public float attackDistance;
  14. public float maxAttackDis, minAttackDis;
  15. public bool needToChange;
  16. public bool canFly = false;
  17. public float flyHeight;
  18. public float flyUpSpeed = 10;
  19. public int sortingOrder = 0;
  20. public int playerID;
  21. public bool hasEffect = false;
  22. public Collider soulCollector;
  23. public bool isBack = false; //往反方向走
  24. private void Awake()
  25. {
  26. }
  27. private void Start()
  28. {
  29. if (needToChange)
  30. {
  31. attackDistance = Random.Range(minAttackDis, maxAttackDis);
  32. }
  33. }
  34. private void OnDisable()
  35. {
  36. PlayersInput.instance[playerID].OnDemonicRecycle(this);
  37. }
  38. public override void FixedUpdate()
  39. {
  40. if (!isNonAttack)
  41. {
  42. OnSearchState();
  43. }
  44. OnState();
  45. }
  46. public bool SearchTarget()
  47. {
  48. targetCharacter = searchTrigger.GetMinDisTarget(targetTypes, canHitFly);
  49. if (targetCharacter != null)
  50. {
  51. return true;
  52. }
  53. else
  54. {
  55. return false;
  56. }
  57. }
  58. public void ChangeSearchState(SearchState newState)
  59. {
  60. switch (searchState)
  61. {
  62. case SearchState.NoTarget:
  63. break;
  64. case SearchState.InSearchScope:
  65. break;
  66. case SearchState.InAttackScope:
  67. break;
  68. default:
  69. break;
  70. }
  71. searchState = newState;
  72. switch (searchState)
  73. {
  74. case SearchState.NoTarget:
  75. targetCharacter = null;
  76. break;
  77. case SearchState.InSearchScope:
  78. break;
  79. case SearchState.InAttackScope:
  80. break;
  81. default:
  82. break;
  83. }
  84. }
  85. public void OnSearchState()
  86. {
  87. switch (searchState)
  88. {
  89. case SearchState.NoTarget:
  90. if (SearchTarget())
  91. {
  92. ChangeSearchState(SearchState.InSearchScope);
  93. break;
  94. }
  95. //向玩家基地移动
  96. break;
  97. case SearchState.InSearchScope:
  98. if (!SearchTarget())
  99. {
  100. targetCharacter = null;
  101. ChangeSearchState(SearchState.NoTarget);
  102. break;
  103. }
  104. if (targetCharacter != null && Mathf.Abs(targetCharacter.transform.position.x - transform.position.x) <= attackDistance)
  105. {
  106. ChangeSearchState(SearchState.InAttackScope);
  107. break;
  108. }
  109. break;
  110. case SearchState.InAttackScope:
  111. if (targetCharacter != null && !searchTrigger.IsCharacterLeave(targetCharacter, targetTypes, canHitFly))
  112. {
  113. if (!targetCharacter.gameObject.activeInHierarchy || targetCharacter.isDie
  114. || Mathf.Abs(targetCharacter.transform.position.x - transform.position.x) > attackDistance)
  115. {
  116. ChangeSearchState(SearchState.NoTarget);
  117. }
  118. }
  119. else
  120. {
  121. ChangeSearchState(SearchState.NoTarget);
  122. }
  123. break;
  124. default:
  125. break;
  126. }
  127. }
  128. public override Vector3 GetMoveDir()
  129. {
  130. Vector3 moveDir = Vector3.zero;
  131. if (canMove)
  132. {
  133. if (!isTran)
  134. {
  135. switch (searchState)
  136. {
  137. case SearchState.NoTarget:
  138. //if (PlayerController.instance.bodyTrans.localScale.x > 0)
  139. //{
  140. // moveDir = Vector3.left;
  141. //}
  142. //else
  143. //{
  144. // moveDir = Vector3.right;
  145. //}
  146. moveDir = Vector3.left;
  147. break;
  148. case SearchState.InSearchScope:
  149. if (targetCharacter)
  150. {
  151. if (targetCharacter.transform.position.x - transform.position.x < 0)
  152. {
  153. moveDir = Vector3.left;
  154. }
  155. else
  156. {
  157. moveDir = Vector3.right;
  158. }
  159. }
  160. else
  161. {
  162. moveDir = Vector3.zero;
  163. }
  164. break;
  165. case SearchState.InAttackScope:
  166. if (targetCharacter)
  167. {
  168. if (targetCharacter.transform.position.x - transform.position.x < 0)
  169. {
  170. moveDir = Vector3.left;
  171. }
  172. else
  173. {
  174. moveDir = Vector3.right;
  175. }
  176. }
  177. else
  178. {
  179. moveDir = Vector3.zero;
  180. }
  181. break;
  182. default:
  183. break;
  184. }
  185. }
  186. else
  187. {
  188. if (pc == null)
  189. {
  190. pc = GetComponentInParent<PlayerController>();
  191. }
  192. moveDir = pc.GetMoveDir();
  193. }
  194. }
  195. if (!isBack)
  196. {
  197. return moveDir;
  198. }
  199. return -moveDir;
  200. }
  201. public bool GetAttack()
  202. {
  203. if (searchState == SearchState.InAttackScope)
  204. {
  205. return true;
  206. }
  207. return false;
  208. }
  209. public void AdjustHeight()
  210. {
  211. if (canFly && !isBeDropped)
  212. {
  213. if (transform.position.y - flyHeight > 0.1f)
  214. {
  215. Vector3 pos = transform.position;
  216. pos.y -= flyUpSpeed * Time.deltaTime;
  217. transform.position = pos;
  218. }
  219. else if (transform.position.y - flyHeight < -0.1f)
  220. {
  221. Vector3 pos = transform.position;
  222. pos.y += flyUpSpeed * Time.deltaTime;
  223. transform.position = pos;
  224. }
  225. }
  226. }
  227. public override void OnState()
  228. {
  229. base.OnState();
  230. hurtKeepTime -= Time.deltaTime;
  231. attackTime -= Time.deltaTime;
  232. dieKeepTime -= Time.deltaTime;
  233. invincibleTime -= Time.deltaTime;
  234. weakTime -= Time.deltaTime;
  235. Vector3 leftDir = GetMoveDir();
  236. bool isAttack = GetAttack();
  237. switch (state)
  238. {
  239. case CharacterState.Idle:
  240. if (isAttack)
  241. {
  242. Attack2();
  243. break;
  244. }
  245. if (!foot.TrigGround && !canFly)
  246. {
  247. if (rb.velocity.y > 0)
  248. {
  249. ChangeState(CharacterState.Rise);
  250. break;
  251. }
  252. else
  253. {
  254. ChangeState(CharacterState.Fall);
  255. break;
  256. }
  257. }
  258. if (leftDir.x > 0.3f || leftDir.x < -0.3f)
  259. {
  260. ChangeState(CharacterState.Run);
  261. break;
  262. }
  263. AdjustHeight();
  264. //rb.velocity = Vector3.zero;
  265. break;
  266. case CharacterState.Run:
  267. if (isAttack)
  268. {
  269. Attack2();
  270. break;
  271. }
  272. if (!foot.TrigGround && !canFly)
  273. {
  274. if (rb.velocity.y > 0)
  275. {
  276. ChangeState(CharacterState.Rise);
  277. break;
  278. }
  279. else
  280. {
  281. ChangeState(CharacterState.Fall);
  282. break;
  283. }
  284. }
  285. if (leftDir.x < 0.3f && leftDir.x > -0.3f)
  286. {
  287. ChangeState(CharacterState.Idle);
  288. break;
  289. }
  290. if (leftDir.x > 0.3f)
  291. {
  292. //rb.velocity += Vector3.right * moveAcc * Time.deltaTime;
  293. rb.velocity = Vector3.right * moveSpeed;
  294. //if (rb.velocity.x > maxMoveSpeed)
  295. //{
  296. // rb.velocity = new Vector3(maxMoveSpeed, rb.velocity.y, rb.velocity.z);
  297. //}
  298. if (bodyTrans.localScale.x > 0)
  299. {
  300. Turn();
  301. }
  302. }
  303. else if (leftDir.x < -0.3f)
  304. {
  305. //rb.velocity -= Vector3.right * moveAcc * Time.deltaTime;
  306. rb.velocity = Vector3.left * moveSpeed;
  307. //if (rb.velocity.x < -maxMoveSpeed)
  308. //{
  309. // rb.velocity = new Vector3(-maxMoveSpeed, rb.velocity.y, rb.velocity.z);
  310. //}
  311. if (bodyTrans.localScale.x < 0)
  312. {
  313. Turn();
  314. }
  315. }
  316. AdjustHeight();
  317. break;
  318. case CharacterState.Rise:
  319. if (rb.velocity.y <= 0)
  320. {
  321. ChangeState(CharacterState.Fall);
  322. break;
  323. }
  324. //if (btnJumpPress || cacheJumpTime > 0)
  325. //{
  326. // if (!airJumped && rb.velocity.y < airJumpSpeed)
  327. // {
  328. // airJumped = true;
  329. // AirJump();
  330. // break;
  331. // }
  332. //}
  333. rb.velocity += Vector3.up * extraRiseGravity * Time.deltaTime;
  334. break;
  335. case CharacterState.Fall:
  336. if (foot.TrigGround || canFly)
  337. {
  338. if (!isBeDropped)
  339. {
  340. ChangeState(CharacterState.Idle);
  341. }
  342. break;
  343. }
  344. //if (foot.canStepPlayers.Count > 0)
  345. //{
  346. // Jump(jumpSpeed / 2);
  347. // StepOther();
  348. // break;
  349. //}
  350. //if (foot.canStepEnemyList.Count > 0)
  351. //{
  352. // Jump(jumpSpeed / 2);
  353. // StepEnemy();
  354. // break;
  355. //}
  356. //if (btnJumpPress || cacheJumpTime > 0)
  357. //{
  358. // if (!airJumped)
  359. // {
  360. // airJumped = true;
  361. // AirJump();
  362. // break;
  363. // }
  364. // else if (canJumpTick >= runner.Tick)
  365. // {
  366. // Jump();
  367. // break;
  368. // }
  369. //}
  370. rb.velocity += Vector3.up * extraFallGravity * Time.deltaTime;
  371. break;
  372. case CharacterState.Hurt:
  373. if (hurtKeepTime <= 0 && rb.velocity.magnitude < hurtChangeVelocity)
  374. {
  375. ChangeState(CharacterState.Idle);
  376. break;
  377. }
  378. if (!foot.TrigGround)
  379. {
  380. rb.velocity += Vector3.up * extraFallGravity * Time.deltaTime;
  381. }
  382. Vector3 vel = rb.velocity;
  383. vel.x = vel.x * (1 - decelerationRatio * Time.deltaTime);
  384. rb.velocity = vel;
  385. break;
  386. case CharacterState.Coma:
  387. if (!isCaughtByCook)
  388. {
  389. pastComaTime += Time.deltaTime;
  390. if (pastComaTime >= comaTime)
  391. {
  392. ChangeState(CharacterState.Idle);
  393. }
  394. }
  395. break;
  396. case CharacterState.Attack:
  397. if (attackTime <= 0)
  398. {
  399. ChangeState(CharacterState.Idle);
  400. break;
  401. }
  402. break;
  403. case CharacterState.Die:
  404. if (dieKeepTime <= 0)
  405. {
  406. print("Die:" + gameObject.name);
  407. player.corpses += 1;
  408. player.ShowCorpse();
  409. gameObject.SetActive(false);
  410. break;
  411. }
  412. break;
  413. case CharacterState.Weak:
  414. if (weakTime <= 0)
  415. {
  416. ChangeState(CharacterState.Idle);
  417. break;
  418. }
  419. break;
  420. default:
  421. break;
  422. }
  423. }
  424. public override void ChangeState(CharacterState newState)
  425. {
  426. if(state == newState)
  427. {
  428. return;
  429. }
  430. switch (state)
  431. {
  432. case CharacterState.Idle:
  433. break;
  434. case CharacterState.Run:
  435. rb.velocity = Vector3.zero;
  436. break;
  437. case CharacterState.Rise:
  438. break;
  439. case CharacterState.Fall:
  440. rb.velocity = Vector3.zero;
  441. break;
  442. case CharacterState.Hurt:
  443. break;
  444. case CharacterState.Coma:
  445. canMove = true;
  446. foreach(GameObject i in HitCols)
  447. {
  448. i.SetActive(true);
  449. }
  450. break;
  451. case CharacterState.Attack:
  452. aniCollider.Play("NotAttack", 1, 0);
  453. break;
  454. case CharacterState.Die:
  455. isDie = false;
  456. break;
  457. case CharacterState.Weak:
  458. if (canFly)
  459. {
  460. rb.constraints += 4; //RigidbodyConstraints.FreezePositionY = 4,不能直接加
  461. }
  462. break;
  463. case CharacterState.Float:
  464. canMove = true;
  465. break;
  466. default:
  467. break;
  468. }
  469. CharacterState oldState = state;
  470. state = newState;
  471. switch (newState)
  472. {
  473. case CharacterState.Idle:
  474. ani.Play("idle", 0, 0);
  475. aniCollider.Play("Idle", 0, 0);
  476. rb.velocity = Vector3.zero;
  477. //animalAni.SetInteger("state", (int)PlayerState.Idle);
  478. break;
  479. case CharacterState.Run:
  480. ani.Play("walk", 0, 0);
  481. aniCollider.Play("Walk", 0, 0);
  482. //animalAni.SetInteger("state", (int)PlayerState.Walk);
  483. break;
  484. case CharacterState.Rise:
  485. aniCollider.Play("Rise", 0, 0);
  486. break;
  487. case CharacterState.Fall:
  488. ani.Play("fall", 0, 0);
  489. aniCollider.Play("Fall", 0, 0);
  490. //animalAni.SetInteger("state", (int)PlayerState.Fall);
  491. break;
  492. case CharacterState.Hurt:
  493. ani.Play("hitted", 0, 0);
  494. aniCollider.Play("Hurt", 0, 0);
  495. invincibleTime = totalInvincibleTime;
  496. hurtKeepTime = minHurtKeepTime;
  497. //ani.Play("Invincible", 2, 0);
  498. break;
  499. case CharacterState.Coma:
  500. //ani.Play("Coma", 0, 0);
  501. ani.Play("idle", 0, 0);
  502. aniCollider.Play("Idle", 0, 0);
  503. if (isCaughtByCook)
  504. {
  505. foreach (GameObject i in HitCols)
  506. {
  507. i.SetActive(false);
  508. }
  509. }
  510. pastComaTime = 0;
  511. rb.velocity = Vector3.zero;
  512. break;
  513. case CharacterState.Attack:
  514. break;
  515. case CharacterState.Die:
  516. ani.Play("die", 0, 0);
  517. aniCollider.Play("Die", 0, 0);
  518. isDie = true;
  519. dieKeepTime = totalDieKeepTime;
  520. //接尸体存入动效
  521. //友方死亡后计入尸体
  522. PlayersInput.instance[playerID].corpses++;
  523. //print(PlayersInput.instance[0].corpses);
  524. break;
  525. case CharacterState.Weak:
  526. aniCollider.Play("Weak", 0, 0);
  527. ani.Play("weak", 0, 0);
  528. Vector3 velocity = rb.velocity;
  529. velocity.y = weakUpSpeed;
  530. rb.velocity = velocity;
  531. weakTime = totalWeakTime;
  532. if (canFly)
  533. {
  534. rb.constraints -= RigidbodyConstraints.FreezePositionY;
  535. }
  536. break;
  537. case CharacterState.Float:
  538. canMove = false;
  539. break;
  540. default:
  541. break;
  542. }
  543. }
  544. public override void Attack1()
  545. {
  546. base.Attack1();
  547. Vector3 moveDir;
  548. if (PlayersInput.instance[playerID].bodyTrans.localScale.x > 0)
  549. {
  550. moveDir = Vector3.left;
  551. }
  552. else
  553. {
  554. moveDir = Vector3.right;
  555. }
  556. if (moveDir.x > 0.3f)
  557. {
  558. if (bodyTrans.localScale.x > 0)
  559. {
  560. Turn();
  561. }
  562. }
  563. else if (moveDir.x < -0.3f)
  564. {
  565. if (bodyTrans.localScale.x < 0)
  566. {
  567. Turn();
  568. }
  569. }
  570. invincibleTime = totalAttack1Time;
  571. attackTarget = targetCharacter;
  572. }
  573. public override void Attack2()
  574. {
  575. base.Attack2();
  576. attackTarget = targetCharacter;
  577. }
  578. private int[] origDamages;
  579. private float origAttackTime;
  580. private int count;
  581. public GameObject attributeUpEffect;
  582. public void AttackAttributesUp(float upDamageDegree, float upAttackSpeedDegree) //提升攻击和攻速
  583. {
  584. attributeUpEffect.SetActive(true);
  585. count = attack2Infos.Count;
  586. origDamages = new int[count];
  587. for (int i = 0; i < count; i++)
  588. {
  589. int d = attack2Infos[i].damage;
  590. origDamages[i] = d;
  591. attack2Infos[i].setDamage((int)(d * (1 + upDamageDegree)));
  592. }
  593. origAttackTime = totalAttack2Time;
  594. totalAttack2Time = totalAttack2Time / (1 + upAttackSpeedDegree);
  595. }
  596. public void AttackAttributesBack()
  597. {
  598. attributeUpEffect.SetActive(false);
  599. for (int i = 0; i < count; i++)
  600. {
  601. attack2Infos[i].setDamage(origDamages[i]);
  602. }
  603. totalAttack2Time = origAttackTime;
  604. }
  605. }