Demonic.cs 25 KB

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