Enemy.cs 30 KB

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