Prechádzať zdrojové kódy

敌方刺客英灵状态写入enemy脚本

LAPTOP-OM1V99U2\永远de小亡灵 1 rok pred
rodič
commit
32c2099f9b

+ 16 - 16
ActionTowerDefense/Assets/Resources/Prefab/ESpirits_Assassin.prefab

@@ -183,23 +183,8 @@ MonoBehaviour:
   m_EditorClassIdentifier: 
   time: 0
   attackCD: 5
-  readyCD: 1.5
-  hateDistance: 10
   state: 0
   enemy: {fileID: -677743004047987606}
-  rb: {fileID: 2437299196472462355}
-  distance: 0
-  dashEffect: {fileID: 2254593709981819441}
-  dashSpeed: 30
-  offset: 2
-  targetDir: {x: 0, y: 0, z: 0}
-  dashTime: 0.8
-  downTime: 0.8
-  finishTime: 0.8
-  body: {fileID: 5216717017252562314}
-  aim: {fileID: 6373916053725309541}
-  aimDistance: 20
-  character: {x: 0, y: 0, z: 0}
 --- !u!54 &2437299196472462355
 Rigidbody:
   m_ObjectHideFlags: 0
@@ -316,7 +301,22 @@ MonoBehaviour:
   soulStartSpeed: 30
   dropSoulAngle: 0
   noOnSearchState: 0
-  noOnState: 0
+  isFindingPlayer: 0
+  isFindPlayer: 0
+  hateDistance: 10
+  distance: 0
+  rushEndPos: {x: 0, y: 0, z: 0}
+  aimEffect: {fileID: 6373916053725309541}
+  aimDistance: 20
+  rushTime: 0.8
+  rushSpeed: 30
+  time: 0
+  readyCD: 1.5
+  dashEffect: {fileID: 2254593709981819441}
+  targetDir: {x: 0, y: 0, z: 0}
+  haveDownRush: 1
+  downRushTime: 0.8
+  finishRushTime: 0.8
 --- !u!1 &2437299196663985757
 GameObject:
   m_ObjectHideFlags: 0

+ 5 - 0
ActionTowerDefense/Assets/Scripts/Character.cs

@@ -22,6 +22,11 @@ public enum CharacterState
     PullRope = 13,
     Spirits = 14,   //召唤英灵
     Float = 15,   //空中漂浮
+    FindPlayer = 16,    //寻找玩家
+    ReadyToRush = 17,   //瞄准准备冲刺
+    ReadyToDownRush = 18,   //准备落地冲刺
+    DownRush = 19,  //落地冲刺
+    FinishRush = 20,    //结束冲刺
 }
 
 public enum AttackType

+ 260 - 7
ActionTowerDefense/Assets/Scripts/Enemy.cs

@@ -41,8 +41,30 @@ public class Enemy : MoveCharacter
     public float soulStartSpeed = 5f;
     public float dropSoulAngle = 60f;
 
+    [HideInInspector]
     public bool noOnSearchState;
-    public bool noOnState;
+    [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 float downRushTime;
+    public float finishRushTime;
 
     private void Awake()
     {
@@ -68,11 +90,10 @@ public class Enemy : MoveCharacter
             OnSearchState();
             
         }
-        if (!noOnState)
-        {
-            OnState();
-        }
-        
+        OnState();
+
+
+
     }
 
     public override Vector3 GetMoveDir()
@@ -269,7 +290,15 @@ public class Enemy : MoveCharacter
             case CharacterState.Fall:
                 if (foot.TrigGround || canFly)
                 {
-                    ChangeState(CharacterState.Idle);
+                    if (isFindPlayer)
+                    {
+                        ChangeState(CharacterState.FindPlayer);
+                    }
+                    else
+                    {
+                        ChangeState(CharacterState.Idle);
+                    }
+                    
                     break;
                 }
                 //if (foot.canStepPlayers.Count > 0)
@@ -335,6 +364,105 @@ public class Enemy : MoveCharacter
                     break;
                 }
                 break;
+            case CharacterState.FindPlayer:
+                if (!foot.TrigGround && !canFly)
+                {
+                    if (rb.velocity.y > 0)
+                    {
+                        ChangeState(CharacterState.Rise);
+                        break;
+                    }
+                    else
+                    {
+                        ChangeState(CharacterState.Fall);
+                        break;
+                    }
+                }
+                if (Vector3.Distance(transform.position, targetCharacter.transform.position) 
+                    < hateDistance)
+                {
+                    rushEndPos = targetCharacter.transform.position;
+                    isFindPlayer = true;
+                }
+                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;
+                ReadyToDash(rushEndPos + Vector3.up, transform.position + Vector3.up);
+                if (time >= readyCD)
+                {
+                    time = 0;
+                    
+                    ChangeState(CharacterState.Rush);
+                }
+                break;
+            case CharacterState.Rush:
+                time += Time.deltaTime;
+                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)
+                {
+                    ani.Play("fall_end", 0, 0);
+                }
+                else
+                {
+                    Rush();
+                }
+                if (foot.TrigGround || transform.position.y <= -1)
+                {
+                    ChangeState(CharacterState.FinishRush);
+                }
+                break;
+            case CharacterState.FinishRush:
+                time += Time.deltaTime;
+                if (time > finishRushTime)
+                {
+                    ChangeState(CharacterState.Idle);
+                }
+                break;
             default:
                 break;
         }
@@ -364,6 +492,37 @@ public class Enemy : MoveCharacter
                 break;
             case CharacterState.Weak:
                 break;
+            case CharacterState.FindPlayer:
+                noOnSearchState = false;
+                rb.velocity = Vector3.zero;
+                isFindPlayer = false;
+                break;
+            case CharacterState.ReadyToRush:
+                aimEffect.SetActive(false);
+                aimEffect.transform.localScale = Vector3.zero;
+                rb.constraints = 
+                    RigidbodyConstraints.FreezePositionZ|RigidbodyConstraints.FreezeRotation;
+                break;
+            case CharacterState.Rush:
+                dashEffect.canHit = false;
+                rb.velocity = Vector3.zero;
+                bodyTrans.rotation = Quaternion.Euler(Vector3.zero);
+                break;
+            case CharacterState.ReadyToDownRush:
+                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:
+                searchState = SearchState.NoTarget;
+                noOnSearchState = false;
+                break;
             default:
                 break;
         }
@@ -418,6 +577,34 @@ public class Enemy : MoveCharacter
                 rb.velocity = velocity;
                 weakTime = totalWeakTime;
                 break;
+            case CharacterState.FindPlayer:
+                isFindPlayer = true;
+                noOnSearchState = true;
+                ChosePlayer();
+                ani.Play("walk", 0, 0);
+                aniCollider.Play("Walk", 0, 0);
+                break;
+            case CharacterState.ReadyToRush:
+                ani.Play("charge", 0, 0);
+                aimEffect.SetActive(true);
+                aimDistance = rushTime * rushSpeed / 2;
+                rb.constraints = RigidbodyConstraints.FreezeAll;
+                break;
+            case CharacterState.Rush:
+                dashEffect.canHit = true;
+                targetDir =
+                        (rushEndPos - transform.position).normalized;
+                ani.Play("attack_summon", 0, 0);
+                break;
+            case CharacterState.ReadyToDownRush:
+                rb.constraints = RigidbodyConstraints.FreezeAll;
+                ani.Play("charge", 0, 0);
+                break;
+            case CharacterState.DownRush:
+                targetDir = Vector3.down;
+                ani.Play("attack_summon", 0, 0);
+                dashEffect.canHit = true;
+                break;
             default:
                 break;
         }
@@ -571,4 +758,70 @@ public class Enemy : MoveCharacter
         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];
+            distance = distance0;
+
+        }
+        else
+        {
+            targetCharacter = PlayersInput.instance[1];
+            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));
+            bodyTrans.localScale = new Vector3(1, 1, 1);
+        }
+        else
+        {
+            aimEffect.transform.rotation = Quaternion.Euler(new Vector3(0, 0, k));
+            bodyTrans.localScale = new Vector3(-1, 1, 1);
+        }
+    }
+
+    private void Rush()
+    {
+        float k = Mathf.Atan2(targetDir.y, targetDir.x) * Mathf.Rad2Deg;
+        if (targetDir.x < 0)
+        {
+            dashEffect.offset = 1;
+            bodyTrans.localScale = new Vector3(1, 1, 1);
+            bodyTrans.rotation = Quaternion.Euler(new Vector3(0, 0, k - 180));
+        }
+        else
+        {
+            dashEffect.offset = -1;
+            bodyTrans.localScale = new Vector3(-1, 1, 1);
+            bodyTrans.rotation = Quaternion.Euler(new Vector3(0, 0, k));
+        }
+
+
+        rb.velocity = targetDir * rushSpeed;
+    }
 }

+ 11 - 1
ActionTowerDefense/Assets/Scripts/Spirits/DashEffect.cs

@@ -59,7 +59,7 @@ public class DashEffect : MonoBehaviour
     }
     private void OnTriggerExit(Collider other)
     {
-        if (other.gameObject.layer == 8 && other.name == "BodyCollider")
+        if (!isEnemy && other.gameObject.layer == 8 && other.name == "BodyCollider")
         {
             Character character = other.GetComponentInParent<Character>();
             if (beHitTriggers.Exists(t => t == character))
@@ -68,6 +68,16 @@ public class DashEffect : MonoBehaviour
             }
             
         }
+        if (isEnemy && (other.gameObject.layer == 6 || other.gameObject.layer == 7)
+            && other.name == "BodyCollider")
+        {
+            Character character = other.GetComponentInParent<Character>();
+            if (beHitTriggers.Exists(t => t == character))
+            {
+                beHitTriggers.Remove(character);
+            }
+
+        }
     }
     public void DashAttack(Character character)
     {

+ 10 - 193
ActionTowerDefense/Assets/Scripts/Spirits/ESpirits_Assassin.cs

@@ -6,36 +6,15 @@ public enum AssassinState
     None = -1,
     Normal = 0, //正常状态
     FindPlayer = 1, //寻找主角位置
-    ReadyToDash = 2,  //准备攻击主角
-    Dash = 3,   //向主角方向冲刺
-    ReadyToDown = 4,    //准备落地斩
-    Down = 5,   //落地斩
-    Finish = 6, //结束冲刺
+    Rushing = 2,   //向主角方向冲刺中
 }
 public class ESpirits_Assassin : MonoBehaviour
 {
     [HideInInspector] 
     public float time;
     public float attackCD;
-    public float readyCD;
-    public float hateDistance;
     public AssassinState state;
     public Enemy enemy;
-    public Rigidbody rb;
-    [HideInInspector]
-    public float distance;
-    public DashEffect dashEffect;
-    public float dashSpeed;
-    public float offset;
-    [HideInInspector]
-    public Vector3 targetDir;
-    public float dashTime;
-    public float downTime;
-    public float finishTime;
-    public GameObject body;
-    public GameObject aim;
-    public float aimDistance;
-    public Vector3 character;
     
     private void Update()
     {
@@ -45,28 +24,6 @@ public class ESpirits_Assassin : MonoBehaviour
         }
         
     }
-    public void ReadyToDash(Vector3 pos0, Vector3 pos1)
-    {
-        Vector3 target = (pos0 - pos1).normalized;
-        //float distance = time / readyCD * aimDistance;
-        float distance = aimDistance;
-        //aim.transform.position = pos1 + target * distance / 2;
-        aim.transform.localScale =
-            new Vector3(distance, 1, 1);
-
-        Vector3 targetDir = pos0 - pos1;
-        float k = Mathf.Atan2(targetDir.y, targetDir.x) * Mathf.Rad2Deg;
-        if (targetDir.x < 0)
-        {
-            aim.transform.rotation = Quaternion.Euler(new Vector3(0, 0, k));
-            body.transform.localScale = new Vector3(1, 1, 1);
-        }
-        else
-        {
-            aim.transform.rotation = Quaternion.Euler(new Vector3(0, 0, k));
-            body.transform.localScale = new Vector3(-1, 1, 1);
-        }
-    }
     public void OnState()
     {
         switch (state)
@@ -75,167 +32,27 @@ public class ESpirits_Assassin : MonoBehaviour
                 time += Time.deltaTime;
                 if (time > attackCD)
                 {
-                    enemy.noOnSearchState = true;
                     time = 0;
-                    
-                    ChosePlayer();
-                    enemy.searchState = SearchState.InSearchScope;
-                    enemy.ChangeState(CharacterState.Run);
-                    
-                    
+                    enemy.ChangeState(CharacterState.FindPlayer);
                     state = AssassinState.FindPlayer;
                 }
                 break;
             case AssassinState.FindPlayer:
-                if (Vector3.Distance(transform.position, enemy.targetCharacter.transform.position)
-                    < hateDistance)
-                {
-                    enemy.noOnState = true;
-                    character = enemy.targetCharacter.transform.position;
-                    enemy.ChangeState(CharacterState.Rush);
-                    rb.velocity = Vector3.zero;
-                    enemy.ani.Play("charge", 0, 0);
-                    aim.SetActive(true);
-                    aimDistance = dashTime * dashSpeed/2;
-                    rb.constraints = RigidbodyConstraints.FreezeAll;
-                    state = AssassinState.ReadyToDash;
-                }
-                break;
-            case AssassinState.ReadyToDash:
-                time += Time.deltaTime;
-                ReadyToDash(character + Vector3.up,
-                    transform.position + Vector3.up);
-                if (time >= readyCD)
-                {
-
-                    dashEffect.canHit = true;
-                    targetDir =
-                        (character - transform.position).normalized;
-                    enemy.ani.Play("attack_summon", 0, 0);
-                    aim.SetActive(false);
-                    aim.transform.localScale = Vector3.one;
-                    time = 0;
-                    rb.constraints =
-                        RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezeRotation;
-                    state = AssassinState.Dash;
-                }
-                break;
-            case AssassinState.Dash:
-                time += Time.deltaTime;
-                Dash();
-                if (time >= dashTime)
-                {
-                    rb.velocity = Vector3.zero;
-                    time = 0;
-                    dashEffect.canHit = false;
-                    if (enemy.foot.TrigGround)
-                    {
-
-                        enemy.ani.Play("idle", 0, 0);
-                        body.transform.rotation = Quaternion.Euler(Vector3.zero);
-                        state = AssassinState.Finish;
-                    }
-                    else
-                    {
-                        rb.constraints = RigidbodyConstraints.FreezeAll;
-                        enemy.ani.Play("charge", 0, 0);
-                        state = AssassinState.ReadyToDown;
-                    }
-
-
-                }
-                break;
-            case AssassinState.ReadyToDown:
-                time += Time.deltaTime;
-                rb.velocity = Vector3.zero;
-                if(time >= downTime)
-                {
-                    time = 0;
-                    rb.constraints = 
-                        RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotation;
-                    targetDir = Vector3.down;
-                    enemy.ani.Play("attack_summon", 0, 0);
-                    dashEffect.canHit = true;
-                    state = AssassinState.Down;
-                }
-                break;
-            case AssassinState.Down:
-               
-                if (transform.position.y < 0)
-                {
-                    enemy.ani.Play("fall_end", 0, 0);
-
-                }
-                else
+                if (enemy.isFindPlayer)
                 {
-                    Dash();
-                }
-                if (enemy.foot.TrigGround|| transform.position.y < -1)
-                {
-                    transform.position = new Vector3(transform.position.x, -1, 0);
-                    body.transform.rotation = Quaternion.Euler(Vector3.zero);
-                    dashEffect.canHit = false;
-                    state = AssassinState.Finish;
-
+                    enemy.ChangeState(CharacterState.ReadyToRush);
+                    state = AssassinState.Rushing;
                 }
                 break;
-            case AssassinState.Finish:
-                time += Time.deltaTime;
-                if(time > finishTime)
+            case AssassinState.Rushing:
+                if(enemy.state == CharacterState.FinishRush)
                 {
-                    enemy.searchState = SearchState.NoTarget;
-                    enemy.ChangeState(CharacterState.Idle);
-                    enemy.noOnSearchState = false;
-                    enemy.noOnState = false;
-                    
                     state = AssassinState.Normal;
                 }
                 break;
+            default:
+                break;
         }
     }
-    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)
-        {
-            enemy.targetCharacter = PlayersInput.instance[0];
-            distance = distance0;
-           
-        }
-        else
-        {
-            enemy.targetCharacter = PlayersInput.instance[1];
-            distance = distance1;
-        }
-    }
-    private void Dash()
-    {
-        float k = Mathf.Atan2(targetDir.y, targetDir.x)*Mathf.Rad2Deg;
-        if (targetDir.x < 0)
-        {
-            dashEffect.offset = offset;
-            body.transform.localScale = new Vector3(1, 1, 1);
-            body.transform.rotation = Quaternion.Euler(new Vector3(0, 0, k-180));
-        }
-        else
-        {
-            dashEffect.offset = -offset;
-            body.transform.localScale = new Vector3(-1, 1, 1);
-            body.transform.rotation = Quaternion.Euler(new Vector3(0, 0, k));
-        }
-        
-        
-        rb.velocity = targetDir * dashSpeed;
-    }
+
 }