| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103 |
- using Spine.Unity;
- using Spine;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Base.Common;
- using UnityEditor.SceneManagement;
- public enum TargetType
- {
- None = 0,
- Demonic = 1,
- Tower = 2,
- Player = 3,
- Enemy = 4,
- EnemyTower = 5,
- }
- public enum SearchState
- {
- NoTarget = 0,//搜索范围内没有目标
- InSearchScope = 1,//在搜索范围内发现目标,但不在攻击范围内
- InAttackScope = 2,//目标在攻击范围内
- }
- public class Enemy : MoveCharacter
- {
- public int id;
- public float jumpSpeed = 10;
- public SearchState searchState;
- public float attackDistance;
- public float maxAttackDis, minAttackDis;
- public bool needToChange;
- public bool canFly = false;
- public float flyHeight;
- public float flyUpSpeed = 10;
- public int sortingOrder = 0;
- public float attackRatio;
- public float maxMoveSpeed, minMoveSpeed;
- public float runSpeed;
- public int dropSoul = 1;
-
- public float dropSoulAngle = 60f;
- [HideInInspector]
- public bool noOnSearchState;
- [HideInInspector]
- public bool isFindingPlayer;
- [HideInInspector]
- public bool isFindPlayer;
- public float hateDistance;
- [HideInInspector]
- public float distance;
- [HideInInspector]
- public Vector3 rushEndPos;
- public GameObject aimEffect;
- public float aimDistance;
- public float rushTime;
- public float rushSpeed;
- [HideInInspector]
- public float time;
- public float readyCD;
- public DashEffect dashEffect;
- [HideInInspector]
- public Vector3 targetDir;
- public bool haveDownRush; //冲刺结束后是否可以接落地斩
- public bool rushHaveAttack; //冲刺是否带伤害
- public float downRushTime;
- public float finishRushTime;
- public bool isBack = false; //往反方向走
- private void Awake()
- {
- aimDistance = rushTime * rushSpeed / 2;
- }
- private void Start()
- {
- if (needToChange)
- {
- attackDistance = Random.Range(minAttackDis, maxAttackDis);
- }
- }
- public void OnDisable()
- {
- EnemyCreater.instance.OnEnemyRecycle(this);
- }
- public override void Init()
- {
- base.Init();
- moveSpeed = Random.Range(minMoveSpeed, maxMoveSpeed);
- ChangeSearchState(SearchState.NoTarget);
- }
- public override void FixedUpdate()
- {
- if (!noOnSearchState)
- {
- OnSearchState();
- }
- OnState();
- }
- public override Vector3 GetMoveDir()
- {
- Vector3 moveDir = Vector3.zero;
- if (canMove)
- {
- switch (searchState)
- {
- case SearchState.NoTarget:
- if (TowerMap.myTowers.Count == 0)
- {
- moveDir = Vector3.right;
- break;
- }
- float minDistance =
- Vector3.Distance(transform.position, TowerMap.myTowers[0].transform.position);
- int id = 0;
- for(int i = 1; i < TowerMap.myTowers.Count; i++)
- {
- float distance = Vector3.Distance(transform.position, TowerMap.myTowers[i].transform.position);
- if (distance < minDistance)
- {
- minDistance = distance;
- id = i;
- }
- }
- if(bodyTrans.position.x > TowerMap.myTowers[id].transform.position.x)
- {
- moveDir = Vector3.left;
- }
- else
- {
- moveDir = Vector3.right;
- }
-
- break;
- case SearchState.InSearchScope:
- if (targetCharacter)
- {
- if (targetCharacter.transform.position.x - transform.position.x < 0)
- {
- moveDir = Vector3.left;
- }
- else
- {
- moveDir = Vector3.right;
- }
- }
- else
- {
- moveDir = Vector3.zero;
- }
- break;
- case SearchState.InAttackScope:
- if (targetCharacter)
- {
- if (targetCharacter.transform.position.x - transform.position.x < 0)
- {
- moveDir = Vector3.left;
- }
- else
- {
- moveDir = Vector3.right;
- }
- }
- else
- {
- moveDir = Vector3.zero;
- }
- break;
- default:
- break;
- }
- }
- if (!isBack)
- {
- return moveDir;
- }
- return -moveDir;
- }
- public bool GetAttack()
- {
- if (searchState == SearchState.InAttackScope)
- {
- return true;
- }
- return false;
- }
- public bool GetJump()
- {
- return false;
- }
- public void AdjustHeight()
- {
- if (canFly && !isBeDropped)
- {
- if (transform.position.y - flyHeight > 0.1f)
- {
- Vector3 pos = transform.position;
- pos.y -= flyUpSpeed * Time.deltaTime;
- transform.position = pos;
- }
- else if (transform.position.y - flyHeight < -0.1f)
- {
- Vector3 pos = transform.position;
- pos.y += flyUpSpeed * Time.deltaTime;
- transform.position = pos;
- }
- }
- }
- public override void OnState()
- {
- base.OnState();
- hurtKeepTime -= Time.deltaTime;
- attackTime -= Time.deltaTime;
- dieKeepTime -= Time.deltaTime;
- invincibleTime -= Time.deltaTime;
- weakTime -= Time.deltaTime;
- Vector3 leftDir = GetMoveDir();
- bool isAttack = GetAttack();
- switch (state)
- {
- case CharacterState.Idle:
- if (isAttack)
- {
- Attack2();
- break;
- }
- if (!foot.TrigGround && !canFly)
- {
- if (rb.velocity.y > 0)
- {
- ChangeState(CharacterState.Rise);
- break;
- }
- else
- {
- ChangeState(CharacterState.Fall);
- break;
- }
- }
- if (leftDir.x > 0.3f || leftDir.x < -0.3f)
- {
- ChangeState(CharacterState.Run);
- break;
- }
- //rb.velocity = Vector3.zero;
- AdjustHeight();
- break;
- case CharacterState.Run:
- if (isAttack)
- {
- Attack2();
- break;
- }
- if (!foot.TrigGround && !canFly)
- {
- if (rb.velocity.y > 0)
- {
- ChangeState(CharacterState.Rise);
- break;
- }
- else
- {
- ChangeState(CharacterState.Fall);
- break;
- }
- }
- if (leftDir.x < 0.3f && leftDir.x > -0.3f)
- {
- ChangeState(CharacterState.Idle);
- break;
- }
- if (leftDir.x > 0.3f)
- {
- //rb.velocity += Vector3.right * moveAcc * Time.deltaTime;
- rb.velocity = Vector3.right * moveSpeed;
- //if (rb.velocity.x > maxMoveSpeed)
- //{
- // rb.velocity = new Vector3(maxMoveSpeed, rb.velocity.y, rb.velocity.z);
- //}
- if (bodyTrans.localScale.x > 0)
- {
- Turn();
- }
- }
- else if (leftDir.x < -0.3f)
- {
- //rb.velocity -= Vector3.right * moveAcc * Time.deltaTime;
- rb.velocity = Vector3.left * moveSpeed;
- //if (rb.velocity.x < -maxMoveSpeed)
- //{
- // rb.velocity = new Vector3(-maxMoveSpeed, rb.velocity.y, rb.velocity.z);
- //}
- if (bodyTrans.localScale.x < 0)
- {
- Turn();
- }
- }
- AdjustHeight();
- break;
- case CharacterState.Rush:
- if (isAttack)
- {
- Attack2();
- break;
- }
- if (!foot.TrigGround && !canFly)
- {
- if (rb.velocity.y > 0)
- {
- ChangeState(CharacterState.Rise);
- break;
- }
- else
- {
- ChangeState(CharacterState.Fall);
- break;
- }
- }
- if (leftDir.x < 0.3f && leftDir.x > -0.3f)
- {
- ChangeState(CharacterState.Idle);
- break;
- }
- if (leftDir.x > 0.3f)
- {
- //rb.velocity += Vector3.right * moveAcc * Time.deltaTime;
- rb.velocity = Vector3.right * runSpeed;
- //if (rb.velocity.x > maxMoveSpeed)
- //{
- // rb.velocity = new Vector3(maxMoveSpeed, rb.velocity.y, rb.velocity.z);
- //}
- if (bodyTrans.localScale.x > 0)
- {
- Turn();
- }
- }
- else if (leftDir.x < -0.3f)
- {
- //rb.velocity -= Vector3.right * moveAcc * Time.deltaTime;
- rb.velocity = Vector3.left * runSpeed;
- //if (rb.velocity.x < -maxMoveSpeed)
- //{
- // rb.velocity = new Vector3(-maxMoveSpeed, rb.velocity.y, rb.velocity.z);
- //}
- if (bodyTrans.localScale.x < 0)
- {
- Turn();
- }
- }
- AdjustHeight();
- break;
- case CharacterState.Rise:
- if (rb.velocity.y <= 0)
- {
- ChangeState(CharacterState.Fall);
- break;
- }
- //if (btnJumpPress || cacheJumpTime > 0)
- //{
- // if (!airJumped && rb.velocity.y < airJumpSpeed)
- // {
- // airJumped = true;
- // AirJump();
- // break;
- // }
- //}
- rb.velocity += Vector3.up * extraRiseGravity * Time.deltaTime;
- break;
- case CharacterState.Fall:
- if (foot.TrigGround || canFly)
- {
- if (!isBeDropped)
- {
- if (isFindingPlayer)
- {
- ChangeState(CharacterState.FindPlayer);
- }
- else
- {
- ChangeState(CharacterState.Idle);
- }
- }
- break;
- }
- //if (foot.canStepPlayers.Count > 0)
- //{
- // Jump(jumpSpeed / 2);
- // StepOther();
- // break;
- //}
- //if (foot.canStepEnemyList.Count > 0)
- //{
- // Jump(jumpSpeed / 2);
- // StepEnemy();
- // break;
- //}
- //if (btnJumpPress || cacheJumpTime > 0)
- //{
- // if (!airJumped)
- // {
- // airJumped = true;
- // AirJump();
- // break;
- // }
- // else if (canJumpTick >= runner.Tick)
- // {
- // Jump();
- // break;
- // }
- //}
- rb.velocity += Vector3.up * extraFallGravity * Time.deltaTime;
- break;
- case CharacterState.Hurt:
- if (hurtKeepTime <= 0 && rb.velocity.magnitude < hurtChangeVelocity)
- {
- ChangeState(CharacterState.Idle);
- break;
- }
- if (!foot.TrigGround && !canFly)
- {
- rb.velocity += Vector3.up * extraFallGravity * Time.deltaTime;
- }
- Vector3 vel = rb.velocity;
- vel.x = vel.x * (1 - decelerationRatio * Time.deltaTime);
- rb.velocity = vel;
- break;
- case CharacterState.Attack:
- if (attackTime <= 0)
- {
- ChangeState(CharacterState.Idle);
- break;
- }
- break;
- case CharacterState.Die:
- if (dieKeepTime <= 0)
- {
- gameObject.SetActive(false);
- break;
- }
- break;
- case CharacterState.Weak:
- if (weakTime <= 0)
- {
- ChangeState(CharacterState.Idle);
- break;
- }
- break;
- case CharacterState.Coma:
- if (!isCaughtByCook)
- {
- pastComaTime += Time.deltaTime;
- if (pastComaTime >= comaTime)
- {
- ChangeState(CharacterState.Idle);
- }
- }
- break;
- case CharacterState.FindPlayer:
- if (!isFindPlayer)
- {
- if (!foot.TrigGround && !canFly)
- {
- if (rb.velocity.y > 0)
- {
- ChangeState(CharacterState.Rise);
- break;
- }
- else
- {
- ChangeState(CharacterState.Fall);
- break;
- }
- }
- if(targetCharacter == null)
- {
- ChosePlayer();
- }
- if (Mathf.Abs(transform.position.x-targetCharacter.transform.position.x)
- < hateDistance)
- {
- rushEndPos = targetCharacter.transform.position;
- isFindPlayer = true;
- break;
- }
- if (targetCharacter.transform.position.x > transform.position.x)
- {
- rb.velocity = Vector3.right * moveSpeed;
- if (bodyTrans.localScale.x > 0)
- {
- Turn();
- }
- }
- if (targetCharacter.transform.position.x < transform.position.x)
- {
- rb.velocity = Vector3.left * moveSpeed;
- if (bodyTrans.localScale.x < 0)
- {
- Turn();
- }
- }
- }
- break;
- case CharacterState.ReadyToRush:
- time += Time.deltaTime;
- if (time >= readyCD)
- {
- time = 0;
- if (rushHaveAttack)
- {
- ChangeState(CharacterState.RushAttack);
- }
- else
- {
- ChangeState(CharacterState.Rush);
- }
- }
- break;
- case CharacterState.RushAttack:
- time += Time.deltaTime;
- dashEffect.canHit = true;
- Rush();
- if (time >= rushTime)
- {
- time = 0;
- if (haveDownRush)
- {
- if (foot.TrigGround)
- {
- ChangeState(CharacterState.FinishRush);
- }
- else
- {
- ChangeState(CharacterState.ReadyToDownRush);
- }
- }
- else
- {
- ChangeState(CharacterState.FinishRush);
- }
- }
- break;
- case CharacterState.ReadyToDownRush:
- time += Time.deltaTime;
- if (time >= downRushTime)
- {
- time = 0;
- ChangeState(CharacterState.DownRush);
- }
- break;
- case CharacterState.DownRush:
-
- if (transform.position.y > 0)
- {
- dashEffect.canHit = true;
- Rush();
- }
- if (foot.TrigGround || transform.position.y <= -1)
- {
- ChangeState(CharacterState.FinishRush);
- }
- break;
- case CharacterState.FinishRush:
- time += Time.deltaTime;
- if (time > finishRushTime)
- {
- time = 0;
- ChangeState(CharacterState.Idle);
- }
- break;
- default:
- break;
- }
- }
- public override void ChangeState(CharacterState newState)
- {
- if (state == newState)
- {
- return;
- }
- switch (state)
- {
- case CharacterState.Idle:
- break;
- case CharacterState.Run:
- rb.velocity = Vector3.zero;
- break;
- case CharacterState.Rush:
- rb.velocity = Vector3.zero;
- break;
- case CharacterState.Rise:
- if (!canFly)
- {
- bodyCollider.SetActive(true);
- }
- break;
- case CharacterState.Fall:
- rb.velocity = Vector3.zero;
- break;
- case CharacterState.Hurt:
- break;
- case CharacterState.Attack:
- aniCollider.Play("NotAttack", 1, 0);
- break;
- case CharacterState.Die:
- isDie = false;
- break;
- case CharacterState.Weak:
- break;
- case CharacterState.Float:
- canMove = true;
- break;
- case CharacterState.Coma:
- canMove = true;
- foreach (GameObject i in HitCols)
- {
- i.SetActive(true);
- }
- break;
- case CharacterState.FindPlayer:
- noOnSearchState = false;
- rb.velocity = Vector3.zero;
- isFindPlayer = false;
- break;
- case CharacterState.ReadyToRush:
- time = 0;
- aimEffect.SetActive(false);
- aimEffect.transform.localScale = Vector3.zero;
- rb.constraints =
- RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotation;
- break;
- case CharacterState.RushAttack:
- time = 0;
- dashEffect.canHit = false;
- rb.velocity = Vector3.zero;
- bodyTrans.rotation = Quaternion.Euler(Vector3.zero);
- break;
- case CharacterState.ReadyToDownRush:
- time = 0;
- rb.constraints =
- RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotation;
- break;
- case CharacterState.DownRush:
- dashEffect.canHit = false;
- rb.velocity = Vector3.zero;
- bodyTrans.rotation = Quaternion.Euler(Vector3.zero);
- transform.position = new Vector3(transform.position.x, -1, 0);
- break;
- case CharacterState.FinishRush:
- time = 0;
- canNotChangeHurt = false;
- searchState = SearchState.NoTarget;
- noOnSearchState = false;
- ani.Play("idle", 0, 0);
- aniCollider.Play("Idle", 0, 0);
- break;
- default:
- break;
- }
- CharacterState oldState = state;
- state = newState;
- switch (newState)
- {
- case CharacterState.Idle:
- ani.Play("idle", 0, 0);
- aniCollider.Play("Idle", 0, 0);
- rb.velocity = Vector3.zero;
- //animalAni.SetInteger("state", (int)PlayerState.Idle);
- break;
- case CharacterState.Run:
- ani.Play("walk", 0, 0);
- aniCollider.Play("Walk", 0, 0);
- //animalAni.SetInteger("state", (int)PlayerState.Walk);
- break;
- case CharacterState.Rush:
- ani.Play("rush", 0, 0);
- aniCollider.Play("Rush", 0, 0);
- break;
- case CharacterState.Rise:
- aniCollider.Play("Rise", 0, 0);
- break;
- case CharacterState.Fall:
- aniCollider.Play("Fall", 0, 0);
- //animalAni.SetInteger("state", (int)PlayerState.Fall);
- break;
- case CharacterState.Hurt:
- switch (oldState)
- {
- case CharacterState.ReadyToRush:
- case CharacterState.RushAttack:
- case CharacterState.ReadyToDownRush:
- case CharacterState.DownRush:
- case CharacterState.Rush:
- state = oldState;
- beRepelValue = totalBeRepelValue;
- break;
- default:
- ani.Play("hitted", 0, 0);
- aniCollider.Play("Hurt", 0, 0);
- invincibleTime = totalInvincibleTime;
- hurtKeepTime = minHurtKeepTime;
- break;
- }
- break;
- case CharacterState.Float:
- canMove = false;
- break;
- case CharacterState.Coma:
- //ani.Play("Coma", 0, 0);
- ani.Play("idle", 0, 0);
- aniCollider.Play("Idle", 0, 0);
- rb.velocity = Vector3.zero;
- if (isCaughtByCook)
- {
- foreach (GameObject i in HitCols)
- {
- i.SetActive(false);
- }
- }
- pastComaTime = 0;
- break;
- case CharacterState.Attack:
- break;
- case CharacterState.Die:
- ani.Play("die", 0, 0);
- aniCollider.Play("Die", 0, 0);
- isDie = true;
- dieKeepTime = totalDieKeepTime;
- DropSouls();
- if (linked)
- {
- PlayersInput.instance[0].sprintLinkTrigger.linkedEnemy.Remove(this);
- PlayersInput.instance[0].playerRope.gameObject.SetActive(false);
- }
- break;
- case CharacterState.Weak:
- switch (oldState)
- {
- case CharacterState.ReadyToRush:
- case CharacterState.RushAttack:
- case CharacterState.ReadyToDownRush:
- case CharacterState.DownRush:
- state = oldState;
- beRepelValue = totalBeRepelValue;
- break;
- default:
- aniCollider.Play("Weak", 0, 0);
- ani.Play("weak", 0, 0);
- Vector3 velocity = rb.velocity;
- velocity.y = weakUpSpeed;
- rb.velocity = velocity;
- weakTime = totalWeakTime;
- break;
- }
- break;
- case CharacterState.FindPlayer:
- isFindPlayer = false;
- isFindingPlayer = true;
- noOnSearchState = true;
- ChosePlayer();
- ani.Play("walk", 0, 0);
- aniCollider.Play("Walk", 0, 0);
- break;
- case CharacterState.ReadyToRush:
- time = 0;
- canNotChangeHurt = true;
- ani.Play("charge", 0, 0);
- aimEffect.SetActive(true);
- rb.constraints = RigidbodyConstraints.FreezeAll;
- ReadyToDash(rushEndPos + Vector3.up, transform.position + Vector3.up);
-
- break;
- case CharacterState.RushAttack:
-
- targetDir =
- (rushEndPos - transform.position).normalized;
- ani.Play("rush_attack", 0, 0);
- aniCollider.Play("Attack1", 0, 0);
- break;
- case CharacterState.ReadyToDownRush:
- time = 0;
- rb.constraints = RigidbodyConstraints.FreezeAll;
- ani.Play("charge", 0, 0);
- break;
- case CharacterState.DownRush:
- time = 0;
- targetDir = Vector3.down;
- ani.Play("rush_attack", 0, 0);
- aniCollider.Play("Attack1", 0, 0);
- break;
- case CharacterState.FinishRush:
- time = 0;
- if (oldState == CharacterState.DownRush)
- {
- ani.Play("fall_end", 0, 0);
- }
- else
- {
- ani.Play("idle", 0, 0);
- aniCollider.Play("Idle", 0, 0);
- }
- break;
- default:
- break;
- }
- }
- public void DropSouls()
- {
- if (dropSoul > 1)
- {
- for (int i = 0; i < dropSoul; i++)
- {
- float angleInterval = dropSoulAngle / (float)(dropSoul - 1);
- float angle = 90 + ((float)i - (float)(dropSoul - 1) / 2) * angleInterval;
- angle = angle / 180 * Mathf.PI;
- GameObject soulObj = PoolManager.Instantiate(soulPrefab, transform.position);
- Vector3 dir = new Vector3(Mathf.Cos(angle), Mathf.Sin(angle), 0);
- soulObj.GetComponent<Soul>().Burst(dir * soulStartSpeed);
- }
- }
- else
- {
- GameObject soulObj = PoolManager.Instantiate(soulPrefab, transform.position);
- Vector3 dir = Vector3.up;
- soulObj.GetComponent<Soul>().Burst(dir * soulStartSpeed);
- }
- }
- public void Jump()
- {
- SetUpSpeed(jumpSpeed);
- ani.Play("jump", 0, 0);
- }
- public void SetUpSpeed(float speed)
- {
- ChangeState(CharacterState.Rise);
- Vector3 velocity = rb.velocity;
- Vector3 leftDir = GetMoveDir();
- if (leftDir.x > 0.3f)
- {
- if (bodyTrans.localScale.x > 0)
- {
- Turn();
- }
- }
- else if (leftDir.x < -0.3f)
- {
- if (bodyTrans.localScale.x < 0)
- {
- Turn();
- }
- }
- velocity.y = speed;
- rb.velocity = velocity;
- //animalAni.SetInteger("state", (int)PlayerState.Rise);
- }
- public void ChangeSearchState(SearchState newState)
- {
- switch (searchState)
- {
- case SearchState.NoTarget:
- break;
- case SearchState.InSearchScope:
- break;
- case SearchState.InAttackScope:
- break;
- default:
- break;
- }
- searchState = newState;
- switch (searchState)
- {
- case SearchState.NoTarget:
- Character character0 = PlayersInput.instance[0];
- Character character1 = PlayersInput.instance[1];
- if (character0.beTargetCharacter.Exists(t => t == this))
- {
- character0.beTargetCharacter.Remove(this);
- }
- if (character1.beTargetCharacter.Exists(t => t == this))
- {
- character1.beTargetCharacter.Remove(this);
- }
- targetCharacter = null;
- break;
- case SearchState.InSearchScope:
- break;
- case SearchState.InAttackScope:
- break;
- default:
- break;
- }
- }
- public bool SearchTarget()
- {
- targetCharacter = searchTrigger.GetMinDisTarget(targetTypes, canHitFly);
- if (targetCharacter != null)
- {
- Character character0 = PlayersInput.instance[0];
- Character character1 = PlayersInput.instance[1];
- if (targetCharacter == character0
- && !character0.beTargetCharacter.Exists(t => t == this))
- {
- character0.beTargetCharacter.Add(this);
- }
- if (targetCharacter == character1
- && !character1.beTargetCharacter.Exists(t => t == this))
- {
- character1.beTargetCharacter.Add(this);
- }
- return true;
- }
- else
- {
- return false;
- }
- }
- public void OnSearchState()
- {
- switch (searchState)
- {
- case SearchState.NoTarget:
- if (SearchTarget())
- {
- ChangeSearchState(SearchState.InSearchScope);
- break;
- }
- //向玩家基地移动
- break;
- case SearchState.InSearchScope:
- if (!SearchTarget())
- {
- targetCharacter = null;
- ChangeSearchState(SearchState.NoTarget);
- break;
- }
- if (targetCharacter != null && Mathf.Abs(targetCharacter.transform.position.x - transform.position.x) <= attackDistance)
- {
- ChangeSearchState(SearchState.InAttackScope);
- break;
- }
- break;
- case SearchState.InAttackScope:
- if (targetCharacter != null && !searchTrigger.IsCharacterLeave(targetCharacter, targetTypes, canHitFly))
- {
- if (!targetCharacter.gameObject.activeInHierarchy || targetCharacter.isDie
- || Mathf.Abs(targetCharacter.transform.position.x - transform.position.x) > attackDistance)
- {
- ChangeSearchState(SearchState.NoTarget);
- }
- }
- else
- {
- ChangeSearchState(SearchState.NoTarget);
- }
- break;
- default:
- break;
- }
- }
- public override void Attack1()
- {
- base.Attack1();
- attackTarget = targetCharacter;
- }
- public override void Attack2()
- {
- base.Attack2();
- attackTarget = targetCharacter;
- }
- public void ChosePlayer()
- {
- float distance0 = 1000;
- float distance1 = 1000;
- if (PlayersInput.instance[0])
- {
- distance0 = Mathf.Abs(PlayersInput.instance[0].transform.position.x
- - transform.position.x);
- }
- if (PlayersInput.instance[1])
- {
- distance1 = Mathf.Abs(PlayersInput.instance[1].transform.position.x
- - transform.position.x);
- }
- if (distance0 <= distance1)
- {
- targetCharacter = PlayersInput.instance[0];
- if (!PlayersInput.instance[0].beTargetCharacter.Exists(t => t == this))
- {
- PlayersInput.instance[0].beTargetCharacter.Add(this);
- }
-
- distance = distance0;
- }
- else
- {
- targetCharacter = PlayersInput.instance[1];
- if (!PlayersInput.instance[1].beTargetCharacter.Exists(t => t == this))
- {
- PlayersInput.instance[1].beTargetCharacter.Add(this);
- }
-
- distance = distance1;
- }
- }
- public void ReadyToDash(Vector3 pos0, Vector3 pos1)
- {
- Vector3 target = (pos0 - pos1).normalized;
- float distance = aimDistance;
- aimEffect.transform.localScale =
- new Vector3(distance, 1, 1);
- targetDir = pos0 - pos1;
- float k = Mathf.Atan2(targetDir.y, targetDir.x) * Mathf.Rad2Deg;
- if (targetDir.x < 0)
- {
- aimEffect.transform.rotation = Quaternion.Euler(new Vector3(0, 0, k));
- if (bodyTrans.localScale.x < 0)
- {
- bodyTrans.localScale =
- new Vector3(-bodyTrans.localScale.x, bodyTrans.localScale.y, bodyTrans.localScale.z);
- }
- }
- else
- {
- aimEffect.transform.rotation = Quaternion.Euler(new Vector3(0, 0, k));
- if (bodyTrans.localScale.x > 0)
- {
- bodyTrans.localScale =
- new Vector3(-bodyTrans.localScale.x, bodyTrans.localScale.y, bodyTrans.localScale.z);
- }
- }
- }
- private void Rush()
- {
- float k = Mathf.Atan2(targetDir.y, targetDir.x) * Mathf.Rad2Deg;
- if (targetDir.x < 0)
- {
- dashEffect.offset = 1;
- if (bodyTrans.localScale.x < 0)
- {
- bodyTrans.localScale =
- new Vector3(-bodyTrans.localScale.x, bodyTrans.localScale.y, bodyTrans.localScale.z);
- }
- bodyTrans.rotation = Quaternion.Euler(new Vector3(0, 0, k - 180));
- }
- else
- {
- if (bodyTrans.localScale.x > 0)
- {
- bodyTrans.localScale =
- new Vector3(-bodyTrans.localScale.x, bodyTrans.localScale.y, bodyTrans.localScale.z);
- }
- dashEffect.offset = -1;
- bodyTrans.rotation = Quaternion.Euler(new Vector3(0, 0, k));
- }
- rb.velocity = targetDir * rushSpeed;
- }
- }
|