Enemy.cs 28 KB

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