Demonic.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. [Header("友方单位属性")]
  9. public PlayerController player; //召唤师
  10. public int playerID;
  11. public int id;
  12. public float costMp = 10;
  13. public bool isBack = false; //往反方向走
  14. public int baseSortingOrder;
  15. public float runSpeed;
  16. public float summonTime;
  17. [Header("锁魂塔")]
  18. public bool isReturnSoulTower;
  19. public Vector3 origSoulPos;
  20. public bool isRecorded;
  21. [Header("友方单位组件")]
  22. public SearchState searchState;
  23. public Collider soulCollector;
  24. [Header("攻击")]
  25. [SerializeField]
  26. private int curAttackID;
  27. private AttackController.AttackMethod[] am;
  28. private int len;
  29. [Header("八卦")]
  30. public float adsorbSpeed; //八卦吸附的速度
  31. [HideInInspector] public Vector3 adsorbTarget; //八卦胖子吸附时目标位置的X
  32. private float adsorbTime; //八卦吸附中的时间
  33. public GameObject effectPrefab; //八卦卦象效果
  34. [Header("掉落魂")]
  35. public int dropSoulMax = 3;
  36. public int dropSoulMin = 1;
  37. public float dropSoulAngle = 60f;
  38. private void Start()
  39. {
  40. am = attackController.attackMethod;
  41. len = am.Length;
  42. }
  43. private void OnDisable()
  44. {
  45. PlayersInput.instance[playerID].OnDemonicRecycle(this);
  46. curAttackID = 0;
  47. }
  48. public override void FixedUpdate()
  49. {
  50. OnSearchState();
  51. OnState();
  52. }
  53. public bool SearchTarget()
  54. {
  55. targetCharacter = searchTrigger.GetMinDisTarget(attackController.targetTypes, attackController.canHitFly);
  56. if (targetCharacter != null)
  57. {
  58. return true;
  59. }
  60. else
  61. {
  62. return false;
  63. }
  64. }
  65. public void ChangeSearchState(SearchState newState)
  66. {
  67. switch (searchState)
  68. {
  69. case SearchState.NoTarget:
  70. break;
  71. case SearchState.InSearchScope:
  72. break;
  73. case SearchState.InAttackScope:
  74. break;
  75. default:
  76. break;
  77. }
  78. searchState = newState;
  79. switch (searchState)
  80. {
  81. case SearchState.NoTarget:
  82. targetCharacter = null;
  83. break;
  84. case SearchState.InSearchScope:
  85. break;
  86. case SearchState.InAttackScope:
  87. break;
  88. default:
  89. break;
  90. }
  91. }
  92. public void OnSearchState()
  93. {
  94. switch (searchState)
  95. {
  96. case SearchState.NoTarget:
  97. if (SearchTarget())
  98. {
  99. ChangeSearchState(SearchState.InSearchScope);
  100. break;
  101. }
  102. //向玩家基地移动
  103. break;
  104. case SearchState.InSearchScope:
  105. if (!SearchTarget())
  106. {
  107. targetCharacter = null;
  108. ChangeSearchState(SearchState.NoTarget);
  109. break;
  110. }
  111. attackDis = attackController.attackDistance + targetCharacter.beHitDistance;
  112. if (targetCharacter != null && Mathf.Abs(targetCharacter.transform.position.x - transform.position.x) <= attackDis)
  113. {
  114. ChangeSearchState(SearchState.InAttackScope);
  115. break;
  116. }
  117. break;
  118. case SearchState.InAttackScope:
  119. if (targetCharacter != null)
  120. {
  121. attackDis = attackController.attackDistance + targetCharacter.beHitDistance;
  122. if (!targetCharacter.gameObject.activeInHierarchy || targetCharacter.isDie
  123. || Mathf.Abs(targetCharacter.transform.position.x - transform.position.x) > attackDis)
  124. {
  125. ChangeSearchState(SearchState.NoTarget);
  126. }
  127. }
  128. else
  129. {
  130. ChangeSearchState(SearchState.NoTarget);
  131. }
  132. break;
  133. default:
  134. break;
  135. }
  136. }
  137. public override Vector3 GetMoveDir()
  138. {
  139. Vector3 moveDir = Vector3.zero;
  140. switch (searchState)
  141. {
  142. case SearchState.NoTarget:
  143. if (TowerMap.enemyTowers.Count == 0)
  144. {
  145. moveDir = Vector3.left;
  146. break;
  147. }
  148. float minDistance = Mathf.Infinity;
  149. int id = -1;
  150. for (int i = 0; i < TowerMap.enemyTowers.Count; i++)
  151. {
  152. EnemyTower enemyTower = TowerMap.enemyTowers[i].GetComponent<EnemyTower>();
  153. if (transform.position.y >
  154. enemyTower.transform.position.y + enemyTower.height)
  155. {
  156. continue;
  157. }
  158. float distance = Vector3.Distance(transform.position,
  159. TowerMap.enemyTowers[i].transform.position);
  160. if (distance < minDistance)
  161. {
  162. minDistance = distance;
  163. id = i;
  164. }
  165. }
  166. if (id == -1)
  167. {
  168. moveDir = Vector3.left;
  169. break;
  170. }
  171. if (bodyTrans.position.x > TowerMap.enemyTowers[id].transform.position.x)
  172. {
  173. moveDir = Vector3.left;
  174. }
  175. else
  176. {
  177. moveDir = Vector3.right;
  178. }
  179. break;
  180. case SearchState.InSearchScope:
  181. if (targetCharacter)
  182. {
  183. if (targetCharacter.transform.position.x - transform.position.x < 0)
  184. {
  185. moveDir = Vector3.left;
  186. }
  187. else
  188. {
  189. moveDir = Vector3.right;
  190. }
  191. }
  192. else
  193. {
  194. moveDir = Vector3.zero;
  195. }
  196. break;
  197. case SearchState.InAttackScope:
  198. if (targetCharacter)
  199. {
  200. if (targetCharacter.transform.position.x - transform.position.x < 0)
  201. {
  202. moveDir = Vector3.left;
  203. }
  204. else
  205. {
  206. moveDir = Vector3.right;
  207. }
  208. }
  209. else
  210. {
  211. moveDir = Vector3.zero;
  212. }
  213. break;
  214. default:
  215. break;
  216. }
  217. if (!isBack)
  218. {
  219. return moveDir;
  220. }
  221. return -moveDir;
  222. }
  223. public bool GetAttack()
  224. {
  225. if (searchState == SearchState.InAttackScope)
  226. {
  227. return true;
  228. }
  229. return false;
  230. }
  231. public override void OnState()
  232. {
  233. base.OnState();
  234. //hurtKeepTime -= Time.deltaTime;
  235. dieKeepTime -= Time.deltaTime;
  236. invincibleTime -= Time.deltaTime;
  237. Vector3 leftDir = GetMoveDir();
  238. bool isAttack = GetAttack();
  239. switch (state)
  240. {
  241. case CharacterState.Idle:
  242. if (isAdjustHeight == 1)
  243. {
  244. ChangeState(CharacterState.Rise);
  245. break;
  246. }
  247. if (isAttack)
  248. {
  249. Attack_march();
  250. break;
  251. }
  252. if (!foot.TrigGround && !canFly)
  253. {
  254. if (rb.velocity.y > 0)
  255. {
  256. ChangeState(CharacterState.Rise);
  257. break;
  258. }
  259. else
  260. {
  261. ChangeState(CharacterState.Fall);
  262. break;
  263. }
  264. }
  265. if (leftDir.x > 0.3f || leftDir.x < -0.3f)
  266. {
  267. ChangeState(CharacterState.Run);
  268. break;
  269. }
  270. rb.velocity = Vector3.right * velocityAddition;
  271. break;
  272. case CharacterState.Run:
  273. if (isAttack)
  274. {
  275. Attack_march();
  276. break;
  277. }
  278. if (!foot.TrigGround && !canFly)
  279. {
  280. if (rb.velocity.y > 0)
  281. {
  282. ChangeState(CharacterState.Rise);
  283. break;
  284. }
  285. else
  286. {
  287. ChangeState(CharacterState.Fall);
  288. break;
  289. }
  290. }
  291. if (leftDir.x < 0.3f && leftDir.x > -0.3f)
  292. {
  293. ChangeState(CharacterState.Idle);
  294. break;
  295. }
  296. if (leftDir.x > 0.3f)
  297. {
  298. //rb.velocity += Vector3.right * moveAcc * Time.deltaTime;
  299. rb.velocity = Vector3.right * (moveSpeed + velocityAddition);
  300. //if (rb.velocity.x > maxMoveSpeed)
  301. //{
  302. // rb.velocity = new Vector3(maxMoveSpeed, rb.velocity.y, rb.velocity.z);
  303. //}
  304. if (bodyTrans.localScale.x > 0)
  305. {
  306. Turn();
  307. }
  308. }
  309. else if (leftDir.x < -0.3f)
  310. {
  311. //rb.velocity -= Vector3.right * moveAcc * Time.deltaTime;
  312. rb.velocity = Vector3.right * (-moveSpeed + velocityAddition);
  313. //if (rb.velocity.x < -maxMoveSpeed)
  314. //{
  315. // rb.velocity = new Vector3(-maxMoveSpeed, rb.velocity.y, rb.velocity.z);
  316. //}
  317. if (bodyTrans.localScale.x < 0)
  318. {
  319. Turn();
  320. }
  321. }
  322. AdjustHeight();
  323. break;
  324. case CharacterState.Rush:
  325. if (isAttack)
  326. {
  327. Attack_march();
  328. break;
  329. }
  330. if (!foot.TrigGround && !canFly)
  331. {
  332. if (rb.velocity.y > 0)
  333. {
  334. ChangeState(CharacterState.Rise);
  335. break;
  336. }
  337. else
  338. {
  339. ChangeState(CharacterState.Fall);
  340. break;
  341. }
  342. }
  343. if (leftDir.x < 0.3f && leftDir.x > -0.3f)
  344. {
  345. ChangeState(CharacterState.Idle);
  346. break;
  347. }
  348. if (leftDir.x > 0.3f)
  349. {
  350. //rb.velocity += Vector3.right * moveAcc * Time.deltaTime;
  351. rb.velocity = Vector3.right * runSpeed;
  352. //if (rb.velocity.x > maxMoveSpeed)
  353. //{
  354. // rb.velocity = new Vector3(maxMoveSpeed, rb.velocity.y, rb.velocity.z);
  355. //}
  356. if (bodyTrans.localScale.x > 0)
  357. {
  358. Turn();
  359. }
  360. }
  361. else if (leftDir.x < -0.3f)
  362. {
  363. //rb.velocity -= Vector3.right * moveAcc * Time.deltaTime;
  364. rb.velocity = Vector3.left * runSpeed;
  365. //if (rb.velocity.x < -maxMoveSpeed)
  366. //{
  367. // rb.velocity = new Vector3(-maxMoveSpeed, rb.velocity.y, rb.velocity.z);
  368. //}
  369. if (bodyTrans.localScale.x < 0)
  370. {
  371. Turn();
  372. }
  373. }
  374. break;
  375. case CharacterState.Rise:
  376. if (isAdjustHeight == 1)
  377. {
  378. AdjustHeight();
  379. }
  380. else if(isAdjustHeight == 2)
  381. {
  382. ChangeState(CharacterState.Idle);
  383. isAdjustHeight = 0;
  384. }
  385. else
  386. {
  387. if (rb.velocity.y <= 0)
  388. {
  389. ChangeState(CharacterState.Fall);
  390. break;
  391. }
  392. rb.velocity += Vector3.up * extraRiseGravity * Time.deltaTime;
  393. }
  394. break;
  395. case CharacterState.Fall:
  396. if (foot.TrigGround || canFly)
  397. {
  398. ChangeState(CharacterState.Idle);
  399. break;
  400. }
  401. Vector3 velocity = rb.velocity;
  402. velocity.y += extraFallGravity * Time.deltaTime;
  403. if (leftDir.x > 0.3f)
  404. {
  405. velocity.x = moveSpeed;
  406. if (bodyTrans.localScale.x > 0)
  407. {
  408. Turn();
  409. }
  410. }
  411. else if (leftDir.x < -0.3f)
  412. {
  413. velocity.x = -moveSpeed;
  414. if (bodyTrans.localScale.x < 0)
  415. {
  416. Turn();
  417. }
  418. }
  419. rb.velocity = velocity;
  420. break;
  421. case CharacterState.Attack:
  422. attackController.JudgeTriggerOnOff();
  423. if (attackController.attackTime <= 0)
  424. {
  425. if (isInSoulTower)
  426. {
  427. ChangeState(CharacterState.LockSoul);
  428. }
  429. else
  430. {
  431. ChangeState(CharacterState.Idle);
  432. }
  433. break;
  434. }
  435. rb.velocity = new Vector3(velocityAddition, rb.velocity.y, rb.velocity.z);
  436. break;
  437. case CharacterState.Die:
  438. if (dieKeepTime <= 0)
  439. {
  440. gameObject.SetActive(false);
  441. break;
  442. }
  443. break;
  444. case CharacterState.LockSoul:
  445. if (!isReturnSoulTower)
  446. {
  447. if (targetCharacter != null && targetCharacter.gameObject.layer == 8 && targetCharacter.isInSoulTower)
  448. {
  449. ChangeState(CharacterState.Idle);
  450. }
  451. else
  452. {
  453. isReturnSoulTower = true;
  454. }
  455. }
  456. else
  457. {
  458. if (transform.position.x - origSoulPos.x >= 0.2f)
  459. {
  460. rb.velocity = Vector3.right * (-moveSpeed + velocityAddition);
  461. if (bodyTrans.localScale.x < 0)
  462. {
  463. Turn();
  464. }
  465. }
  466. else if (origSoulPos.x - transform.position.x >= 0.2f)
  467. {
  468. rb.velocity = Vector3.right * (moveSpeed + velocityAddition);
  469. if (bodyTrans.localScale.x > 0)
  470. {
  471. Turn();
  472. }
  473. }
  474. else
  475. {
  476. ani.Play("idle", 0, 0);
  477. rb.velocity = Vector3.zero;
  478. transform.position = origSoulPos;
  479. isReturnSoulTower = false;
  480. targetCharacter = null;
  481. FaceToEneTower();
  482. }
  483. //锁魂塔内使魔返回原地过程中扫描到敌人时,停止返回并索敌攻击
  484. if (targetCharacter != null && targetCharacter.gameObject.layer == 8 && targetCharacter.isInSoulTower)
  485. {
  486. ChangeState(CharacterState.Idle);
  487. }
  488. }
  489. break;
  490. case CharacterState.SpecialStatus_Float:
  491. attributeStatus.SpecialStateEffect(SpecialState.FloatState);
  492. break;
  493. case CharacterState.SpecialStatus_BlowUp:
  494. attributeStatus.SpecialStateEffect(SpecialState.BlownUp);
  495. break;
  496. case CharacterState.SpecialStatus_ShotDown:
  497. attributeStatus.SpecialStateEffect(SpecialState.ShotDown);
  498. break;
  499. case CharacterState.SpecialStatus_Weak:
  500. attributeStatus.SpecialStateEffect(SpecialState.Weak);
  501. break;
  502. default:
  503. break;
  504. }
  505. }
  506. private void FaceToEneTower()
  507. {
  508. float dis = 1000000;
  509. GameObject to = null;
  510. foreach (GameObject g in TowerMap.enemyTowers)
  511. {
  512. float k = Vector3.Distance(g.transform.position, transform.position);
  513. if (k < dis)
  514. {
  515. dis = k;
  516. to = g;
  517. }
  518. }
  519. if (to != null && to.transform.position.x < transform.position.x)
  520. {
  521. if (bodyTrans.localScale.x < 0)
  522. {
  523. Turn();
  524. }
  525. }
  526. else
  527. {
  528. if (bodyTrans.localScale.x > 0)
  529. {
  530. Turn();
  531. }
  532. }
  533. }
  534. public override void ChangeState(CharacterState newState)
  535. {
  536. if (state == newState)
  537. {
  538. return;
  539. }
  540. switch (state)
  541. {
  542. case CharacterState.Idle:
  543. break;
  544. case CharacterState.Run:
  545. rb.velocity = Vector3.zero;
  546. break;
  547. case CharacterState.Rush:
  548. rb.velocity = Vector3.zero;
  549. break;
  550. case CharacterState.Rise:
  551. if (!canFly)
  552. {
  553. bodyCollider.SetActive(true);
  554. }
  555. break;
  556. case CharacterState.Fall:
  557. rb.velocity = Vector3.zero;
  558. break;
  559. case CharacterState.Attack:
  560. if (attackController.effect)
  561. {
  562. attackController.effect.SetActive(false);
  563. }
  564. attackController.ChooseAttack(curAttackID);
  565. break;
  566. case CharacterState.Die:
  567. isDie = false;
  568. break;
  569. case CharacterState.LockSoul:
  570. //isReturnSoulTower = true;
  571. break;
  572. default:
  573. break;
  574. }
  575. state = newState;
  576. switch (newState)
  577. {
  578. case CharacterState.Idle:
  579. ani.Play("idle", 0, 0);
  580. rb.velocity = Vector3.zero;
  581. //animalAni.SetInteger("state", (int)PlayerState.Idle);
  582. break;
  583. case CharacterState.Run:
  584. ani.Play("walk", 0, 0);
  585. //animalAni.SetInteger("state", (int)PlayerState.Walk);
  586. break;
  587. case CharacterState.Rush:
  588. ani.Play("rush", 0, 0);
  589. break;
  590. case CharacterState.Fall:
  591. ani.Play("fall", 0, 0);
  592. //animalAni.SetInteger("state", (int)PlayerState.Fall);
  593. break;
  594. case CharacterState.Attack:
  595. break;
  596. case CharacterState.Die:
  597. ani.Play("die", 0, 0);
  598. isDie = true;
  599. dieKeepTime = totalDieKeepTime;
  600. break;
  601. case CharacterState.LockSoul:
  602. rb.velocity = Vector3.zero;
  603. ani.Play("walk", 0, 0);
  604. //FaceToEneTower();
  605. break;
  606. default:
  607. break;
  608. }
  609. }
  610. public void Attack_summon()
  611. {
  612. if (needToAdjustFlyHeight && canFly)
  613. {
  614. flyHeight = Random.Range(minFlyHeight, maxFlyHeight);
  615. isAdjustHeight = 1;
  616. }
  617. attackController.Attack_summon();
  618. Vector3 moveDir;
  619. if (PlayersInput.instance[playerID].bodyTrans.localScale.x > 0)
  620. {
  621. moveDir = Vector3.left;
  622. }
  623. else
  624. {
  625. moveDir = Vector3.right;
  626. }
  627. if (moveDir.x > 0.3f)
  628. {
  629. if (bodyTrans.localScale.x > 0)
  630. {
  631. Turn();
  632. }
  633. }
  634. else if (moveDir.x < -0.3f)
  635. {
  636. if (bodyTrans.localScale.x < 0)
  637. {
  638. Turn();
  639. }
  640. }
  641. invincibleTime = totalAttack_summonTime;
  642. attackTarget = targetCharacter;
  643. curAttackID = 1;
  644. }
  645. public virtual void Attack_march()
  646. {
  647. attackController.Attack_march(curAttackID);
  648. if (curAttackID + 1 < len)
  649. {
  650. curAttackID += 1;
  651. }
  652. else if (am[0].id == 0)
  653. {
  654. curAttackID = 1;
  655. }
  656. else
  657. {
  658. curAttackID = 0;
  659. }
  660. attackTarget = targetCharacter;
  661. }
  662. public void DropSouls()
  663. {
  664. int dropSoulNum = Random.Range(dropSoulMin, dropSoulMax + 1);
  665. if (dropSoulNum > 1)
  666. {
  667. for (int i = 0; i < dropSoulNum; i++)
  668. {
  669. float angleInterval = dropSoulAngle / (float)(dropSoulNum - 1);
  670. float angle = 90 + ((float)i - (float)(dropSoulNum - 1) / 2) * angleInterval;
  671. angle = angle / 180 * Mathf.PI;
  672. GameObject soulObj = PoolManager.Instantiate(soulPrefab, transform.position);
  673. Vector3 dir = new Vector3(Mathf.Cos(angle), Mathf.Sin(angle), 0);
  674. Soul soul = soulObj.GetComponent<Soul>();
  675. soul.Burst(dir * soulStartSpeed);
  676. }
  677. }
  678. else
  679. {
  680. GameObject soulObj = PoolManager.Instantiate(soulPrefab, transform.position);
  681. Vector3 dir = Vector3.up;
  682. Soul soul = soulObj.GetComponent<Soul>();
  683. soul.Burst(dir * soulStartSpeed);
  684. }
  685. }
  686. }