Enemy.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. using OfficeOpenXml.FormulaParsing.Excel.Functions.Logical;
  2. using Sirenix.OdinInspector;
  3. using Spine;
  4. using Spine.Unity;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using TMPro;
  8. using Unity.VisualScripting;
  9. using UnityEngine;
  10. public enum EnemyTag
  11. {
  12. Common,
  13. Backline,
  14. Tank
  15. }
  16. public enum SearchState
  17. {
  18. NoTarget = 0, //搜索范围内没有目标
  19. InSearchScope = 1, //在搜索范围内发现目标,但不在攻击范围内
  20. InAttackScope = 2, //目标在攻击范围内
  21. }
  22. public class Enemy : MoveCharacter
  23. {
  24. [Space(30)]
  25. [Title("Enemy属性")]
  26. [LabelText("击杀提供的经验值")]
  27. public int exp;
  28. public EnemyTag tag;
  29. [LabelText("死亡特效")]
  30. public GameObject dieEffect;
  31. public string name;
  32. public int baseSortingOrder;
  33. int sortingOrder = 0;
  34. public bool isBack = false; //往反方向走
  35. public float jumpSpeed = 10;
  36. public float maxMoveSpeed, minMoveSpeed;
  37. public float runSpeed;
  38. [Header("击飞、屏幕反弹")]
  39. public bool isBeBlownUp; //被击飞
  40. public bool isBeReboundedX; //X方向被反弹
  41. public bool isBeReboundedY; //Y方向被反弹
  42. private bool hasBeReboundedX;
  43. private bool hasBeReboundedY;
  44. public float reboundXSpeed;
  45. public float reboundYSpeed;
  46. public int wallDamage;
  47. [Header("敌方英灵")]
  48. public Spirits.SpiritType type;
  49. [Header("敌方单位组件")]
  50. public SearchState searchState;
  51. [Header("攻击")]
  52. public float attackRatio;
  53. protected int curAttackID;
  54. public int len;
  55. protected float pastAttackTime;
  56. protected bool isConAttack; //连续攻击,不切idle动画
  57. [Header("掉落魂")]
  58. public int dropSoulMax = 3;
  59. public int dropSoulMin = 1;
  60. public int dropProbability = 100;
  61. public float dropSoulAngle = 60f;
  62. public override void Awake()
  63. {
  64. base.Awake();
  65. }
  66. protected virtual void Start()
  67. {
  68. len = attackController.attackMethod_march.Length;
  69. }
  70. protected virtual void OnEnable()
  71. {
  72. Init();
  73. }
  74. protected override void OnDisable()
  75. {
  76. base.OnDisable();
  77. EnemyCreater.instance.OnEnemyRecycle(this);
  78. }
  79. public override void Init()
  80. {
  81. base.Init();
  82. moveSpeed = Random.Range(minMoveSpeed, maxMoveSpeed);
  83. spinee.transform.rotation = Quaternion.identity;
  84. ChangeSearchState(SearchState.NoTarget);
  85. attributeStatus.curSpecialStates = SpecialState.Null;
  86. attributeStatus.attributeTime = 0;
  87. curAttackID = 0;
  88. attackController.curAttackMethod = attackController.attackMethod_march[0];
  89. }
  90. public override void FixedUpdate()
  91. {
  92. OnSearchState();
  93. OnState();
  94. }
  95. public override Vector3 GetMoveDir()
  96. {
  97. Vector3 moveDir = Vector3.zero;
  98. switch (searchState)
  99. {
  100. case SearchState.NoTarget:
  101. if (TowerMap.myTowers.Count == 0)
  102. {
  103. moveDir = Vector3.right;
  104. break;
  105. }
  106. float minDistance = Mathf.Infinity;
  107. int id = -1;
  108. for (int i = 0; i < TowerMap.myTowers.Count; i++)
  109. {
  110. Tower myTower = TowerMap.myTowers[i].GetComponent<Tower>();
  111. if (transform.position.y >
  112. myTower.transform.position.y + myTower.height)
  113. {
  114. continue;
  115. }
  116. float distance = Vector3.Distance(transform.position,
  117. TowerMap.myTowers[i].transform.position);
  118. if (distance < minDistance)
  119. {
  120. minDistance = distance;
  121. id = i;
  122. }
  123. }
  124. if (id == -1)
  125. {
  126. moveDir = Vector3.right;
  127. break;
  128. }
  129. if (bodyTrans.position.x > TowerMap.myTowers[id].transform.position.x)
  130. {
  131. moveDir = Vector3.left;
  132. }
  133. else
  134. {
  135. moveDir = Vector3.right;
  136. }
  137. break;
  138. case SearchState.InSearchScope:
  139. if (targetCharacter)
  140. {
  141. if (targetCharacter.transform.position.x - transform.position.x < -1)
  142. {
  143. moveDir = Vector3.left;
  144. }
  145. else if(targetCharacter.transform.position.x - transform.position.x >1)
  146. {
  147. moveDir = Vector3.right;
  148. }
  149. else
  150. {
  151. moveDir = bodyTrans.localScale.x > 0 ? Vector3.left : Vector3.right;
  152. }
  153. }
  154. else
  155. {
  156. moveDir = Vector3.zero;
  157. }
  158. break;
  159. case SearchState.InAttackScope:
  160. if (targetCharacter)
  161. {
  162. if (targetCharacter.transform.position.x - transform.position.x < 0)
  163. {
  164. moveDir = Vector3.left;
  165. }
  166. else
  167. {
  168. moveDir = Vector3.right;
  169. }
  170. }
  171. else
  172. {
  173. moveDir = Vector3.zero;
  174. }
  175. break;
  176. default:
  177. break;
  178. }
  179. if (!isBack)
  180. {
  181. return moveDir;
  182. }
  183. return -moveDir;
  184. }
  185. public virtual bool GetAttack()
  186. {
  187. if (searchState == SearchState.InAttackScope)
  188. {
  189. return true;
  190. }
  191. return false;
  192. }
  193. public bool GetJump()
  194. {
  195. return false;
  196. }
  197. public override void OnState()
  198. {
  199. base.OnState();
  200. if (state == CharacterState.FramePause)
  201. {
  202. return;
  203. }
  204. //hurtKeepTime -= Time.deltaTime;
  205. invincibleTime -= Time.deltaTime;
  206. pastAttackTime += Time.deltaTime;
  207. Vector3 leftDir = GetMoveDir();
  208. bool isAttack = GetAttack();
  209. Vector3 velocity = rb.velocity;
  210. Quaternion targetQt = Quaternion.Euler(Vector3.zero);
  211. switch (state)
  212. {
  213. case CharacterState.Idle:
  214. if (isAdjustHeight == 1)
  215. {
  216. ChangeState(CharacterState.Rise);
  217. break;
  218. }
  219. else if (isAttack)
  220. {
  221. if (pastAttackTime >= attackController.attackInterval)
  222. {
  223. Attack_march();
  224. }
  225. }
  226. else
  227. {
  228. if (!foot.TrigGround && !canFly)
  229. {
  230. if (rb.velocity.y > 0)
  231. {
  232. ChangeState(CharacterState.Rise);
  233. break;
  234. }
  235. else
  236. {
  237. ChangeState(CharacterState.Fall);
  238. break;
  239. }
  240. }
  241. if (leftDir.x > 0.3f || leftDir.x < -0.3f)
  242. {
  243. ChangeState(CharacterState.Run);
  244. break;
  245. }
  246. velocity.y = 0;
  247. if (!foot.haveGravity)
  248. {
  249. transform.position = new Vector3(transform.position.x, platformPosY, transform.position.z);
  250. targetQt = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, platformRotZ);
  251. }
  252. if (RotLerpTime < 1)
  253. {
  254. RotLerpTime += RotLerpSpeed * Time.deltaTime;
  255. transform.rotation = Quaternion.Lerp(transform.rotation, targetQt, RotLerpTime);
  256. }
  257. else
  258. {
  259. transform.rotation = targetQt;
  260. }
  261. rb.velocity = velocity * moveSpeedScale;
  262. }
  263. break;
  264. case CharacterState.Run:
  265. if (isAttack)
  266. {
  267. if (pastAttackTime >= attackController.attackInterval)
  268. {
  269. Attack_march();
  270. }
  271. else
  272. {
  273. ChangeState(CharacterState.Idle);
  274. }
  275. }
  276. else
  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. velocity.x = moveSpeed;
  299. if (bodyTrans.localScale.x > 0)
  300. {
  301. Turn();
  302. }
  303. }
  304. else if (leftDir.x < -0.3f)
  305. {
  306. velocity.x = -moveSpeed;
  307. if (bodyTrans.localScale.x < 0)
  308. {
  309. Turn();
  310. }
  311. }
  312. velocity.y = 0;
  313. if (!foot.haveGravity)
  314. {
  315. transform.position = new Vector3(transform.position.x, platformPosY, transform.position.z);
  316. targetQt = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, platformRotZ);
  317. }
  318. if (RotLerpTime < 1)
  319. {
  320. RotLerpTime += RotLerpSpeed * Time.deltaTime;
  321. transform.rotation = Quaternion.Lerp(transform.rotation, targetQt, RotLerpTime);
  322. }
  323. else
  324. {
  325. transform.rotation = targetQt;
  326. }
  327. rb.velocity = velocity * moveSpeedScale;
  328. AdjustHeight();
  329. }
  330. break;
  331. case CharacterState.Rush:
  332. if (isAttack)
  333. {
  334. if (pastAttackTime >= attackController.attackInterval)
  335. {
  336. Attack_march();
  337. }
  338. else
  339. {
  340. ChangeState(CharacterState.Idle);
  341. }
  342. }
  343. else
  344. {
  345. if (!foot.TrigGround && !canFly)
  346. {
  347. if (rb.velocity.y > 0)
  348. {
  349. ChangeState(CharacterState.Rise);
  350. break;
  351. }
  352. else
  353. {
  354. ChangeState(CharacterState.Attack);
  355. break;
  356. }
  357. }
  358. if (leftDir.x < 0.3f && leftDir.x > -0.3f)
  359. {
  360. ChangeState(CharacterState.Idle);
  361. break;
  362. }
  363. if (leftDir.x > 0.3f)
  364. {
  365. //rb.velocity += Vector3.right * moveAcc * Time.deltaTime;
  366. rb.velocity = Vector3.right * runSpeed * moveSpeedScale;
  367. //if (rb.velocity.x > maxMoveSpeed)
  368. //{
  369. // rb.velocity = new Vector3(maxMoveSpeed, rb.velocity.y, rb.velocity.z);
  370. //}
  371. if (bodyTrans.localScale.x > 0)
  372. {
  373. Turn();
  374. }
  375. }
  376. else if (leftDir.x < -0.3f)
  377. {
  378. //rb.velocity -= Vector3.right * moveAcc * Time.deltaTime;
  379. rb.velocity = Vector3.left * runSpeed * moveSpeedScale;
  380. //if (rb.velocity.x < -maxMoveSpeed)
  381. //{
  382. // rb.velocity = new Vector3(-maxMoveSpeed, rb.velocity.y, rb.velocity.z);
  383. //}
  384. if (bodyTrans.localScale.x < 0)
  385. {
  386. Turn();
  387. }
  388. }
  389. //AdjustHeight();
  390. }
  391. break;
  392. case CharacterState.Rise:
  393. if (isAdjustHeight == 1)
  394. {
  395. AdjustHeight();
  396. if (!foot.haveGravity)
  397. {
  398. targetQt = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, platformRotZ);
  399. }
  400. if (RotLerpTime < 1)
  401. {
  402. RotLerpTime += RotLerpSpeed * Time.deltaTime;
  403. transform.rotation = Quaternion.Lerp(transform.rotation, targetQt, RotLerpTime);
  404. }
  405. else
  406. {
  407. transform.rotation = targetQt;
  408. }
  409. }
  410. else if (isAdjustHeight == 2)
  411. {
  412. ChangeState(CharacterState.Idle);
  413. isAdjustHeight = 0;
  414. }
  415. else
  416. {
  417. if (rb.velocity.y <= 0)
  418. {
  419. ChangeState(CharacterState.Fall);
  420. break;
  421. }
  422. rb.velocity += Vector3.up * extraRiseGravity * Time.deltaTime * moveSpeedScale;
  423. }
  424. break;
  425. case CharacterState.Fall:
  426. if (foot.TrigGround || canFly)
  427. {
  428. ChangeState(CharacterState.Idle);
  429. break;
  430. }
  431. velocity.y += extraFallGravity * Time.deltaTime;
  432. if (leftDir.x > 0.3f)
  433. {
  434. velocity.x = moveSpeed;
  435. if (bodyTrans.localScale.x > 0)
  436. {
  437. Turn();
  438. }
  439. }
  440. else if (leftDir.x < -0.3f)
  441. {
  442. velocity.x = -moveSpeed;
  443. if (bodyTrans.localScale.x < 0)
  444. {
  445. Turn();
  446. }
  447. }
  448. rb.velocity = velocity * moveSpeedScale;
  449. break;
  450. case CharacterState.Attack:
  451. attackController.JudgeTriggerOnOff();
  452. if (attackController.attackTime <= 0)
  453. {
  454. isAttack = GetAttack();
  455. if (isAttack)
  456. {
  457. isConAttack = true;
  458. }
  459. ChangeState(CharacterState.Idle);
  460. break;
  461. }
  462. if (!foot.haveGravity)
  463. {
  464. targetQt = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, platformRotZ);
  465. }
  466. if (RotLerpTime < 1)
  467. {
  468. RotLerpTime += RotLerpSpeed * Time.deltaTime;
  469. transform.rotation = Quaternion.Lerp(transform.rotation, targetQt, RotLerpTime);
  470. }
  471. else
  472. {
  473. transform.rotation = targetQt;
  474. }
  475. break;
  476. case CharacterState.Die:
  477. //if (killer != null && killer.GetComponent<Demonic>())
  478. //{
  479. // SoldierType st = killer.GetComponent<Demonic>().soldierType;
  480. // SoldierEXP.expInstance.AddEXP(st, (int)(exp * LevelSelect.EXPRatio + 0.5f));
  481. //}
  482. //if (dieEffect)
  483. //{
  484. // PoolManager.Instantiate(dieEffect, transform.position);
  485. //}
  486. //GameManager gameManager = GameManager.instance;
  487. //if (gameManager.gameType == GameType.GameEnd)
  488. //{
  489. // GameObject fx = PoolManager.Instantiate(gameManager.dropGoldFX);
  490. // fx.transform.position = transform.position;
  491. // GameObject injuryNum = PoolManager.Instantiate(gameManager.dropGoldText);
  492. // injuryNum.transform.position = new Vector3(
  493. // transform.position.x + injuryNumPos_march.x + Random.Range(-injuryNumRandom_march.x / 2f, injuryNumRandom_march.x / 2f),
  494. // transform.position.y + injuryNumPos_march.y + Random.Range(-injuryNumRandom_march.y / 2f, injuryNumRandom_march.y / 2f),
  495. // transform.position.z);
  496. // TextMeshProUGUI text = injuryNum.GetComponentInChildren<TextMeshProUGUI>();
  497. // text.text = $"+{gameManager.enemyGoldDrop}";
  498. //}
  499. gameObject.SetActive(false);
  500. //dieKeepTime -= Time.deltaTime;
  501. //if (dieKeepTime <= 0)
  502. //{
  503. // if (killer!=null && killer.GetComponent<Demonic>())
  504. // {
  505. // SoldierType st = killer.GetComponent<Demonic>().soldierType;
  506. // SoldierEXP.expInstance.AddEXP(st, (int)(exp * LevelSelect.EXPRatio + 0.5f));
  507. // }
  508. // if (dieEffect)
  509. // {
  510. // PoolManager.Instantiate(dieEffect, transform.position);
  511. // }
  512. // GameManager gameManager = GameManager.instance;
  513. // if (gameManager.gameType == GameType.GameEnd)
  514. // {
  515. // GameObject fx = PoolManager.Instantiate(gameManager.dropGoldFX);
  516. // fx.transform.position = transform.position;
  517. // GameObject injuryNum = PoolManager.Instantiate(gameManager.dropGoldText);
  518. // injuryNum.transform.position = new Vector3(
  519. // transform.position.x + injuryNumPos_march.x + Random.Range(-injuryNumRandom_march.x / 2f, injuryNumRandom_march.x / 2f),
  520. // transform.position.y + injuryNumPos_march.y + Random.Range(-injuryNumRandom_march.y / 2f, injuryNumRandom_march.y / 2f),
  521. // transform.position.z);
  522. // TextMeshProUGUI text = injuryNum.GetComponentInChildren<TextMeshProUGUI>();
  523. // text.text = $"+{gameManager.enemyGoldDrop}";
  524. // }
  525. // gameObject.SetActive(false);
  526. // break;
  527. //}
  528. break;
  529. case CharacterState.HitStun:
  530. hitFeedbackSystem.HitStunUpdate();
  531. break;
  532. case CharacterState.SpecialStatus_Float:
  533. attributeStatus.SpecialStateEffect(SpecialState.FloatState);
  534. break;
  535. case CharacterState.SpecialStatus_BlowUp:
  536. attributeStatus.SpecialStateEffect(SpecialState.BlownUp);
  537. break;
  538. case CharacterState.SpecialStatus_ShotDown:
  539. attributeStatus.SpecialStateEffect(SpecialState.ShotDown);
  540. break;
  541. case CharacterState.SpecialStatus_Weak:
  542. attributeStatus.SpecialStateEffect(SpecialState.Weak);
  543. break;
  544. default:
  545. break;
  546. }
  547. }
  548. public override void ChangeState(CharacterState newState)
  549. {
  550. if (state == newState || newState == CharacterState.FramePause)
  551. {
  552. state = newState;
  553. return;
  554. }
  555. //Debug.Log("从" + state + "转向" + newState);
  556. switch (state)
  557. {
  558. case CharacterState.Idle:
  559. //transform.rotation = Quaternion.Euler(Vector3.zero);
  560. break;
  561. case CharacterState.Run:
  562. rb.velocity = Vector3.zero;
  563. //transform.rotation = Quaternion.Euler(Vector3.zero);
  564. break;
  565. case CharacterState.Rush:
  566. rb.velocity = Vector3.zero;
  567. break;
  568. case CharacterState.Rise:
  569. if (!canFly)
  570. {
  571. bodyCollider.SetActive(true);
  572. }
  573. break;
  574. case CharacterState.Fall:
  575. rb.velocity = Vector3.zero;
  576. break;
  577. //case CharacterState.Hurt:
  578. // break;
  579. case CharacterState.Attack:
  580. attackController.isAttackTriggerOn = false;
  581. attackController.curAttackMethod.attackTrigger.gameObject.SetActive(false);
  582. break;
  583. case CharacterState.Die:
  584. isDie = false;
  585. break;
  586. case CharacterState.FramePause:
  587. state = hitFeedbackSystem.curCharacterState;
  588. ChangeState(newState);
  589. return;
  590. default:
  591. break;
  592. }
  593. CharacterState oldState = state;
  594. state = newState;
  595. switch (newState)
  596. {
  597. case CharacterState.Idle:
  598. if (!isConAttack || attackController.attackInterval > 0)
  599. {
  600. ani.Play(AnimatorHash.ANIMATOR_idle, 0, 0);
  601. }
  602. rb.velocity = Vector3.zero;
  603. break;
  604. case CharacterState.Run:
  605. ani.Play(AnimatorHash.ANIMATOR_walk, 0, 0);
  606. break;
  607. case CharacterState.Rush:
  608. ani.Play(AnimatorHash.ANIMATOR_rush, 0, 0);
  609. break;
  610. case CharacterState.Fall:
  611. ani.Play(AnimatorHash.ANIMATOR_fall, 0, 0);
  612. break;
  613. case CharacterState.Attack:
  614. break;
  615. case CharacterState.Rise:
  616. nowCanFly = canFly;
  617. break;
  618. case CharacterState.Die:
  619. ani.Play(AnimatorHash.ANIMATOR_die, 0, 0);
  620. isDie = true;
  621. dieKeepTime = totalDieKeepTime;
  622. if(GameManager.instance.gameType != GameType.GameEnd)
  623. {
  624. DropSouls();
  625. }
  626. if (killer != null && killer.GetComponent<Demonic>())
  627. {
  628. SoldierType st = killer.GetComponent<Demonic>().soldierType;
  629. SoldierEXP.expInstance.AddEXP(st, (int)(exp * LevelSelect.EXPRatio + 0.5f));
  630. }
  631. if (dieEffect)
  632. {
  633. PoolManager.Instantiate(dieEffect, transform.position);
  634. }
  635. GameManager gameManager = GameManager.instance;
  636. if (gameManager.gameType == GameType.GameEnd)
  637. {
  638. GameObject fx = PoolManager.Instantiate(gameManager.dropGoldFX);
  639. fx.transform.position = transform.position;
  640. GameObject injuryNum = PoolManager.Instantiate(gameManager.dropGoldText);
  641. injuryNum.transform.position = new Vector3(
  642. transform.position.x + injuryNumPos_march.x + Random.Range(-injuryNumRandom_march.x / 2f, injuryNumRandom_march.x / 2f),
  643. transform.position.y + injuryNumPos_march.y + Random.Range(-injuryNumRandom_march.y / 2f, injuryNumRandom_march.y / 2f),
  644. transform.position.z);
  645. TextMeshProUGUI text = injuryNum.GetComponentInChildren<TextMeshProUGUI>();
  646. text.text = $"+{gameManager.enemyGoldDrop}";
  647. }
  648. if (GameManager.instance.isRockEnable)
  649. {
  650. int randomInt = Random.Range(0, 100);
  651. if (randomInt < GameManager.instance.rockProbability)
  652. {
  653. PoolManager.Instantiate(Resources.Load<GameObject>("Prefab/StoneStatue"), transform.position);
  654. }
  655. }
  656. break;
  657. default:
  658. break;
  659. }
  660. }
  661. public void DropSouls()
  662. {
  663. int randomInt;
  664. if (GameManager.instance.isWoodEnable)
  665. {
  666. randomInt = Random.Range(0, 100);
  667. if (randomInt < GameManager.instance.woodProbability) PoolManager.Instantiate(Resources.Load<GameObject>("Prefab/SoulFlower"), transform.position);
  668. }
  669. int dropSoulNum = Random.Range(dropSoulMin, dropSoulMax + 1);
  670. if (dropSoulNum > 1)
  671. {
  672. for (int i = 0; i < dropSoulNum; i++)
  673. {
  674. float angleInterval = dropSoulAngle / (float)(dropSoulNum - 1);
  675. float angle = 90 + ((float)i - (float)(dropSoulNum - 1) / 2) * angleInterval;
  676. angle = angle / 180 * Mathf.PI;
  677. GameObject soulObj = PoolManager.Instantiate(soulPrefab, transform.position);
  678. Vector3 dir = new Vector3(Mathf.Cos(angle), Mathf.Sin(angle), 0);
  679. Soul soul = soulObj.GetComponent<Soul>();
  680. soul.Burst(dir * soulStartSpeed);
  681. soul.type = type;
  682. }
  683. }
  684. else
  685. {
  686. randomInt = Random.Range(0, 100);
  687. if (randomInt >= dropProbability)
  688. {
  689. return;
  690. }
  691. GameObject soulObj = PoolManager.Instantiate(soulPrefab, transform.position);
  692. Vector3 dir = Vector3.up;
  693. Soul soul = soulObj.GetComponent<Soul>();
  694. soul.Burst(dir * soulStartSpeed);
  695. soul.type = type;
  696. }
  697. }
  698. public void Jump()
  699. {
  700. SetUpSpeed(jumpSpeed);
  701. ani.Play(AnimatorHash.ANIMATOR_jump, 0, 0);
  702. }
  703. public void SetUpSpeed(float speed)
  704. {
  705. ChangeState(CharacterState.Rise);
  706. Vector3 velocity = rb.velocity;
  707. Vector3 leftDir = GetMoveDir();
  708. if (leftDir.x > 0.3f)
  709. {
  710. if (bodyTrans.localScale.x > 0)
  711. {
  712. Turn();
  713. }
  714. }
  715. else if (leftDir.x < -0.3f)
  716. {
  717. if (bodyTrans.localScale.x < 0)
  718. {
  719. Turn();
  720. }
  721. }
  722. velocity.y = speed;
  723. rb.velocity = velocity * moveSpeedScale;
  724. //animalAni.SetInteger("state", (int)PlayerState.Rise);
  725. }
  726. public void ChangeSearchState(SearchState newState)
  727. {
  728. switch (searchState)
  729. {
  730. case SearchState.NoTarget:
  731. break;
  732. case SearchState.InSearchScope:
  733. break;
  734. case SearchState.InAttackScope:
  735. break;
  736. default:
  737. break;
  738. }
  739. searchState = newState;
  740. switch (searchState)
  741. {
  742. case SearchState.NoTarget:
  743. Character character0 = PlayersInput.instance[0];
  744. if (character0 && character0.attackController.beTargetCharacter.Exists(t => t == this))
  745. {
  746. character0.attackController.beTargetCharacter.Remove(this);
  747. }
  748. targetCharacter = null;
  749. break;
  750. case SearchState.InSearchScope:
  751. break;
  752. case SearchState.InAttackScope:
  753. break;
  754. default:
  755. break;
  756. }
  757. }
  758. public bool SearchTarget()
  759. {
  760. targetCharacter = searchTrigger.GetMinDisTarget(attackController.targetTypes, attackController.curAttackMethod.canHitFly, attackController.curAttackMethod.searchMode);
  761. if (targetCharacter != null)
  762. {
  763. Character character0 = PlayersInput.instance[0];
  764. Character character1 = PlayersInput.instance[1];
  765. if (targetCharacter == character0
  766. && !character0.attackController.beTargetCharacter.Exists(t => t == this))
  767. {
  768. character0.attackController.beTargetCharacter.Add(this);
  769. }
  770. if (targetCharacter == character1
  771. && !character1.attackController.beTargetCharacter.Exists(t => t == this))
  772. {
  773. character1.attackController.beTargetCharacter.Add(this);
  774. }
  775. return true;
  776. }
  777. else
  778. {
  779. return false;
  780. }
  781. }
  782. public void OnSearchState()
  783. {
  784. switch (searchState)
  785. {
  786. case SearchState.NoTarget:
  787. if (SearchTarget())
  788. {
  789. ChangeSearchState(SearchState.InSearchScope);
  790. break;
  791. }
  792. //向玩家基地移动
  793. break;
  794. case SearchState.InSearchScope:
  795. if (!SearchTarget())
  796. {
  797. targetCharacter = null;
  798. ChangeSearchState(SearchState.NoTarget);
  799. break;
  800. }
  801. attackDis = attackController.curAttackMethod.attackDistance + targetCharacter.beHitDistance;
  802. if (targetCharacter != null && Mathf.Abs(targetCharacter.transform.position.x - transform.position.x) <= attackDis)
  803. {
  804. ChangeSearchState(SearchState.InAttackScope);
  805. break;
  806. }
  807. break;
  808. case SearchState.InAttackScope:
  809. if (targetCharacter != null)
  810. {
  811. //判断是否在射程夹角内
  812. AttackController.AttackMethod am = attackController.curAttackMethod;
  813. if (am.attackType == AttackController.AttackType.Shoot
  814. && attackController.curAttackMethod.attackInfo.attackMethod_Type == AttackMethod_Type.Attack_March)
  815. {
  816. Vector3 dir = targetCharacter.beSearchTrigger.transform.position - transform.position;
  817. float angle = Vector3.Angle(dir, Vector3.left * bodyTrans.localScale.x);
  818. if ((dir.y > 0 && angle > am.maxUpAngle) || (dir.y < 0 && angle > am.maxDownAngle))
  819. {
  820. ChangeSearchState(SearchState.NoTarget);
  821. }
  822. }
  823. attackDis = attackController.curAttackMethod.attackDistance + targetCharacter.beHitDistance;
  824. if (!targetCharacter.gameObject.activeInHierarchy || targetCharacter.isDie
  825. || Mathf.Abs(targetCharacter.transform.position.x - transform.position.x) > attackDis)
  826. {
  827. ChangeSearchState(SearchState.NoTarget);
  828. }
  829. }
  830. else
  831. {
  832. ChangeSearchState(SearchState.NoTarget);
  833. }
  834. break;
  835. default:
  836. break;
  837. }
  838. }
  839. public void Attack_summon()
  840. {
  841. CheckTurn(GetMoveDir().x);
  842. attackController.Attack_summon();
  843. attackTarget = targetCharacter;
  844. }
  845. public virtual void Attack_march()
  846. {
  847. CheckTurn(GetMoveDir().x);
  848. attackController.Attack_march();
  849. if (curAttackID + 1 < len)
  850. {
  851. curAttackID += 1;
  852. }
  853. else if (attackController.curAttackMethod.attackInfo.attackMethod_Type == AttackMethod_Type.Attack_Summon)
  854. {
  855. curAttackID = 1;
  856. }
  857. else
  858. {
  859. curAttackID = 0;
  860. }
  861. attackTarget = targetCharacter;
  862. pastAttackTime = 0;
  863. }
  864. public void ChosePlayer()
  865. {
  866. float distance0 = Mathf.Infinity;
  867. float distance1 = Mathf.Infinity;
  868. PlayerController player0 = PlayersInput.instance[0];
  869. PlayerController player1 = PlayersInput.instance[1];
  870. if (player0 != null && !player0.isRevive)
  871. {
  872. distance0 = Mathf.Abs(player0.transform.position.x
  873. - transform.position.x);
  874. }
  875. if (player1 != null && !player1.isRevive)
  876. {
  877. distance1 = Mathf.Abs(player1.transform.position.x
  878. - transform.position.x);
  879. }
  880. if (distance0 == Mathf.Infinity && distance1 == Mathf.Infinity)
  881. {
  882. targetCharacter = null;
  883. return;
  884. }
  885. if (distance0 <= distance1)
  886. {
  887. targetCharacter = player0;
  888. if (!player0.attackController.beTargetCharacter.Exists(t => t == this))
  889. {
  890. player0.attackController.beTargetCharacter.Add(this);
  891. }
  892. }
  893. else
  894. {
  895. targetCharacter = player1;
  896. if (!player1.attackController.beTargetCharacter.Exists(t => t == this))
  897. {
  898. player1.attackController.beTargetCharacter.Add(this);
  899. }
  900. }
  901. }
  902. public override void BeHit(int damage)
  903. {
  904. base.BeHit(damage);
  905. }
  906. public override void BeHit(AttackController.AttackMethod attackMethod, Character attackFrom, int damage = -1)
  907. {
  908. base.BeHit(attackMethod, attackFrom, damage);
  909. }
  910. }