Parcourir la source

角色状态相关逻辑整合,修复可以无限二段跳的bug,减少回到Idle状态浪费的一帧,提升玩家操作手感

wulifu il y a 1 an
Parent
commit
d8a870c469
1 fichiers modifiés avec 166 ajouts et 240 suppressions
  1. 166 240
      ActionTowerDefense/Assets/Scripts/PlayerController.cs

+ 166 - 240
ActionTowerDefense/Assets/Scripts/PlayerController.cs

@@ -10,6 +10,7 @@ using Unity.VisualScripting;
 
 public enum PlayerState
 {
+    None = 0,
     Idle = 1,
     Run = 2,
     Rise = 3,//空中上升
@@ -88,6 +89,7 @@ public class PlayerController : MonoBehaviour
             return Input.GetKeyDown(KeyCode.Space) || isClickBtnJump;
         }
     }
+    [HideInInspector]
     public bool isClickBtnJump;
     public bool btnRushPress
     {
@@ -96,6 +98,7 @@ public class PlayerController : MonoBehaviour
             return Input.GetKeyDown(KeyCode.LeftShift) || isClickBtnRush;
         }
     }
+    [HideInInspector]
     public bool isClickBtnRush;
     public bool btnSouthPress
     {
@@ -104,6 +107,7 @@ public class PlayerController : MonoBehaviour
             return Input.GetKeyDown(KeyCode.K) || isClickBtnSouth;
         }
     }
+    [HideInInspector]
     public bool isClickBtnSouth;
     public bool btnEastPress
     {
@@ -112,6 +116,7 @@ public class PlayerController : MonoBehaviour
             return Input.GetKeyDown(KeyCode.L) || isClickBtnEast;
         }
     }
+    [HideInInspector]
     public bool isClickBtnEast;
     public bool btnWestPress
     {
@@ -120,6 +125,7 @@ public class PlayerController : MonoBehaviour
             return Input.GetKeyDown(KeyCode.J) || isClickBtnWest;
         }
     }
+    [HideInInspector]
     public bool isClickBtnWest;
     public bool btnNorthPress
     {
@@ -128,6 +134,7 @@ public class PlayerController : MonoBehaviour
             return Input.GetKeyDown(KeyCode.I) || isClickBtnNorth;
         }
     }
+    [HideInInspector]
     public bool isClickBtnNorth;
 
     public Vector2 leftDir
@@ -237,130 +244,168 @@ public class PlayerController : MonoBehaviour
         transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
     }
 
-    public void OnState()
+    //角色处于可自由活动状态时的通用切换状态逻辑,如Idle、Run状态,以及别的状态结束时准备回到Idle状态前
+    public bool CheckPlayerChangeState(PlayerState excludeState)
     {
-        cacheJumpTime -= Time.deltaTime;
-        cacheAttackTime -= Time.deltaTime;
-        cacheSummonTime -= Time.deltaTime;
-        canJumpTime -= Time.deltaTime;
-        invincibleTime -= Time.deltaTime;
-        hurtKeepTime -= Time.deltaTime;
-        attackTime -= Time.deltaTime;
-        summonTime -= Time.deltaTime;
-        rushTime -= Time.deltaTime;
-        cacheRushTime -= Time.deltaTime;
-        switch (state)
+        if (!foot.TrigGround)
         {
-            case PlayerState.Idle:
-                if (btnWestPress || cacheAttackTime > 0)
+            if (rb.velocity.y > 0)
+            {
+                if (excludeState != PlayerState.Rise)
+                {
+                    ChangeState(PlayerState.Rise);
+                    return true;
+                }
+            }
+            else
+            {
+                if (excludeState != PlayerState.Fall)
+                {
+                    ChangeState(PlayerState.Fall);
+                    return true;
+                }
+            }
+        }
+        else
+        {
+            airJumped = false;
+            if (btnWestPress || cacheAttackTime > 0)
+            {
+                if (excludeState != PlayerState.Attack)
                 {
                     ChangeState(PlayerState.Attack);
-                    break;
+                    return true;
                 }
-                if (btnNorthPress)
+            }
+            if (cacheSummonTime > 0)
+            {
+                if (excludeState != PlayerState.Summon)
+                {
+                    Summon(cacheSummonId);
+                    ChangeState(PlayerState.Summon);
+                    return true;
+                }
+            }
+            if (btnNorthPress)
+            {
+                if (excludeState != PlayerState.Summon)
                 {
                     Summon(0);
-                    break;
+                    ChangeState(PlayerState.Summon);
+                    return true;
                 }
-                if (btnSouthPress)
+            }
+            if (btnSouthPress)
+            {
+                if (excludeState != PlayerState.Summon)
                 {
                     Summon(1);
-                    break;
+                    ChangeState(PlayerState.Summon);
+                    return true;
                 }
-                if (btnEastPress)
+            }
+            if (btnEastPress)
+            {
+                if (excludeState != PlayerState.Summon)
                 {
                     Summon(2);
-                    break;
-                }
-                if (cacheSummonTime > 0)
-                {
-                    Summon(cacheSummonId);
-                    break;
+                    ChangeState(PlayerState.Summon);
+                    return true;
                 }
-                if (btnRushPress || cacheRushTime > 0)
+            }
+            if (btnRushPress || cacheRushTime > 0)
+            {
+                if (excludeState != PlayerState.Rush)
                 {
                     ChangeState(PlayerState.Rush);
-                    break;
+                    return true;
                 }
-                if (btnJumpPress || cacheJumpTime > 0)
+            }
+            if (btnJumpPress || cacheJumpTime > 0)
+            {
+                if (excludeState != PlayerState.Rise)
                 {
                     Jump();
                     ChangeState(PlayerState.Rise);
-                    break;
+                    return true;
                 }
-                if (!foot.TrigGround)
+            }
+            if (leftDir.x > 0.3f || leftDir.x < -0.3f)
+            {
+                if (excludeState != PlayerState.Run)
                 {
-                    if (rb.velocity.y > 0)
-                    {
-                        ChangeState(PlayerState.Rise);
-                        break;
-                    }
-                    else
-                    {
-                        ChangeState(PlayerState.Fall);
-                        break;
-                    }
+                    ChangeState(PlayerState.Run);
+                    return true;
                 }
-                if (leftDir.x > 0.3f || leftDir.x < -0.3f)
+            }
+            else
+            {
+                if (excludeState != PlayerState.Idle)
+                {
+                    ChangeState(PlayerState.Idle);
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
+    public void CachedPlayerInput()
+    {
+        if (btnRushPress)
+        {
+            cacheRushTime = totalCacheRushTime;
+        }
+        if (btnJumpPress)
+        {
+            cacheJumpTime = totalCacheJumpTime;
+        }
+        if (btnWestPress)
+        {
+            cacheAttackTime = totalCacheAttackTime;
+        }
+        if (btnNorthPress)
+        {
+            cacheSummonTime = totalCacheSummonTime;
+            cacheSummonId = 0;
+        }
+        if (btnSouthPress)
+        {
+            cacheSummonTime = totalCacheSummonTime;
+            cacheSummonId = 1;
+        }
+        if (btnEastPress)
+        {
+            cacheSummonTime = totalCacheSummonTime;
+            cacheSummonId = 2;
+        }
+    }
+
+    public void OnState()
+    {
+        cacheJumpTime -= Time.deltaTime;
+        cacheAttackTime -= Time.deltaTime;
+        cacheSummonTime -= Time.deltaTime;
+        canJumpTime -= Time.deltaTime;
+        invincibleTime -= Time.deltaTime;
+        hurtKeepTime -= Time.deltaTime;
+        attackTime -= Time.deltaTime;
+        summonTime -= Time.deltaTime;
+        rushTime -= Time.deltaTime;
+        cacheRushTime -= Time.deltaTime;
+        switch (state)
+        {
+            case PlayerState.Idle:
+                if (CheckPlayerChangeState(state))
                 {
-                    ChangeState(PlayerState.Run);
                     break;
                 }
                 canJumpTime = leaveGroundCanJumpTime;
                 //rb.velocity = Vector3.zero;
                 break;
             case PlayerState.Run:
-                if (btnWestPress || cacheAttackTime > 0)
-                {
-                    ChangeState(PlayerState.Attack);
-                    break;
-                }
-                if (btnNorthPress)
+                if (CheckPlayerChangeState(state))
                 {
-                    Summon(0);
-                    break;
-                }
-                if (btnSouthPress)
-                {
-                    Summon(1);
-                    break;
-                }
-                if (btnEastPress)
-                {
-                    Summon(2);
-                    break;
-                }
-                if (cacheSummonTime > 0)
-                {
-                    Summon(cacheSummonId);
-                    break;
-                }
-                if (btnRushPress || cacheRushTime > 0)
-                {
-                    ChangeState(PlayerState.Rush);
-                    break;
-                }
-                if (btnJumpPress || cacheJumpTime > 0)
-                {
-                    Jump();
-                    break;
-                }
-                if (!foot.TrigGround)
-                {
-                    if (rb.velocity.y > 0)
-                    {
-                        ChangeState(PlayerState.Rise);
-                        break;
-                    }
-                    else
-                    {
-                        ChangeState(PlayerState.Fall);
-                        break;
-                    }
-                }
-                if (leftDir.x < 0.3f && leftDir.x > -0.3f)
-                {
-                    ChangeState(PlayerState.Idle);
                     break;
                 }
                 canJumpTime = leaveGroundCanJumpTime;
@@ -411,33 +456,7 @@ public class PlayerController : MonoBehaviour
                         break;
                     }
                 }
-                if (btnRushPress)
-                {
-                    cacheRushTime = totalCacheRushTime;
-                }
-                if (btnJumpPress)
-                {
-                    cacheJumpTime = totalCacheJumpTime;
-                }
-                if (btnWestPress)
-                {
-                    cacheAttackTime = totalCacheAttackTime;
-                }
-                if (btnNorthPress)
-                {
-                    cacheSummonTime = totalCacheSummonTime;
-                    cacheSummonId = 0;
-                }
-                if (btnSouthPress)
-                {
-                    cacheSummonTime = totalCacheSummonTime;
-                    cacheSummonId = 1;
-                }
-                if (btnEastPress)
-                {
-                    cacheSummonTime = totalCacheSummonTime;
-                    cacheSummonId = 2;
-                }
+                CachedPlayerInput();
                 rb.velocity += Vector3.up * extraRiseGravity * Time.deltaTime;
                 AirMove();
                 break;
@@ -449,8 +468,10 @@ public class PlayerController : MonoBehaviour
                 }
                 if (foot.TrigGround)
                 {
-                    ChangeState(PlayerState.Idle);
-                    break;
+                    if (CheckPlayerChangeState(state))
+                    {
+                        break;
+                    }
                 }
                 //if (foot.canStepPlayers.Count > 0)
                 //{
@@ -472,75 +493,25 @@ public class PlayerController : MonoBehaviour
                         AirJump();
                         break;
                     }
-                    else if (cacheJumpTime > 0)
+                    else if (canJumpTime > 0)
                     {
                         Jump();
                         break;
                     }
                 }
-                if (btnRushPress)
-                {
-                    cacheRushTime = totalCacheRushTime;
-                }
-                if (btnJumpPress)
-                {
-                    cacheJumpTime = totalCacheJumpTime;
-                }
-                if (btnWestPress)
-                {
-                    cacheAttackTime = totalCacheAttackTime;
-                }
-                if (btnNorthPress)
-                {
-                    cacheSummonTime = totalCacheSummonTime;
-                    cacheSummonId = 0;
-                }
-                if (btnSouthPress)
-                {
-                    cacheSummonTime = totalCacheSummonTime;
-                    cacheSummonId = 1;
-                }
-                if (btnEastPress)
-                {
-                    cacheSummonTime = totalCacheSummonTime;
-                    cacheSummonId = 2;
-                }
+                CachedPlayerInput();
                 rb.velocity += Vector3.up * extraFallGravity * Time.deltaTime;
                 AirMove();
                 break;
             case PlayerState.Hurt:
                 if (hurtKeepTime <= 0)
                 {
-                    ChangeState(PlayerState.Idle);
-                    break;
-                }
-                if (btnRushPress)
-                {
-                    cacheRushTime = totalCacheRushTime;
-                }
-                if (btnJumpPress)
-                {
-                    cacheJumpTime = totalCacheJumpTime;
-                }
-                if (btnWestPress)
-                {
-                    cacheAttackTime = totalCacheAttackTime;
-                }
-                if (btnNorthPress)
-                {
-                    cacheSummonTime = totalCacheSummonTime;
-                    cacheSummonId = 0;
-                }
-                if (btnSouthPress)
-                {
-                    cacheSummonTime = totalCacheSummonTime;
-                    cacheSummonId = 1;
-                }
-                if (btnEastPress)
-                {
-                    cacheSummonTime = totalCacheSummonTime;
-                    cacheSummonId = 2;
+                    if (CheckPlayerChangeState(state))
+                    {
+                        break;
+                    }
                 }
+                CachedPlayerInput();
                 if (!foot.TrigGround)
                 {
                     rb.velocity += Vector3.up * extraFallGravity * Time.deltaTime;
@@ -550,77 +521,32 @@ public class PlayerController : MonoBehaviour
             case PlayerState.Attack:
                 if (attackTime <= 0)
                 {
-                    ChangeState(PlayerState.Idle);
-                    break;
-                }
-                if (btnRushPress)
-                {
-                    cacheRushTime = totalCacheRushTime;
-                }
-                if (btnJumpPress)
-                {
-                    cacheJumpTime = totalCacheJumpTime;
-                }
-                if (btnWestPress)
-                {
-                    cacheAttackTime = totalCacheAttackTime;
-                }
-                if (btnNorthPress)
-                {
-                    cacheSummonTime = totalCacheSummonTime;
-                    cacheSummonId = 0;
-                }
-                if (btnSouthPress)
-                {
-                    cacheSummonTime = totalCacheSummonTime;
-                    cacheSummonId = 1;
-                }
-                if (btnEastPress)
-                {
-                    cacheSummonTime = totalCacheSummonTime;
-                    cacheSummonId = 2;
+                    if (CheckPlayerChangeState(state))
+                    {
+                        break;
+                    }
                 }
+                CachedPlayerInput();
                 break;
             case PlayerState.Summon:
                 if (summonTime <= 0)
                 {
-                    ChangeState(PlayerState.Idle);
-                    break;
-                }
-                if (btnRushPress)
-                {
-                    cacheRushTime = totalCacheRushTime;
-                }
-                if (btnJumpPress)
-                {
-                    cacheJumpTime = totalCacheJumpTime;
-                }
-                if (btnWestPress)
-                {
-                    cacheAttackTime = totalCacheAttackTime;
-                }
-                if (btnNorthPress)
-                {
-                    cacheSummonTime = totalCacheSummonTime;
-                    cacheSummonId = 0;
-                }
-                if (btnSouthPress)
-                {
-                    cacheSummonTime = totalCacheSummonTime;
-                    cacheSummonId = 1;
-                }
-                if (btnEastPress)
-                {
-                    cacheSummonTime = totalCacheSummonTime;
-                    cacheSummonId = 2;
+                    if (CheckPlayerChangeState(state))
+                    {
+                        break;
+                    }
                 }
+                
                 break;
             case PlayerState.Rush:
                 if (rushTime <= 0)
                 {
-                    ChangeState(PlayerState.Idle);
-                    break;
+                    if (CheckPlayerChangeState(state))
+                    {
+                        break;
+                    }
                 }
+                CachedPlayerInput();
                 if (transform.localScale.x > 0)
                 {
                     rb.velocity = Vector3.left * rushSpeed;
@@ -672,6 +598,7 @@ public class PlayerController : MonoBehaviour
         switch (newState)
         {
             case PlayerState.Idle:
+                airJumped = false;
                 bodyCollider.enabled = true;
                 jumpBodyCollider.enabled = false;
                 if (oldState == PlayerState.Fall)
@@ -776,7 +703,6 @@ public class PlayerController : MonoBehaviour
     public void Summon(int id)
     {
         print("Summon:" + id);
-        ChangeState(PlayerState.Summon);
         if (id >= demonicPrefabs.Count)
         {
             Debug.LogError("未配置" + id + "号使魔");