diff --git a/Assets/Scripts/Enemies/BossEnemyMove.cs b/Assets/Scripts/Enemies/BossEnemyMove.cs index a8f2b8af..d9aa2724 100644 --- a/Assets/Scripts/Enemies/BossEnemyMove.cs +++ b/Assets/Scripts/Enemies/BossEnemyMove.cs @@ -1,5 +1,15 @@ using UnityEngine; +// public class AIInputController +// { +// public override void UpdateInput(ref Inputs inputs) +// { +// // Monsters 배열 정보 받아 +// // 주변 환경 정보 받아오기 +// // 환경 정보에 따라서 inputs 변경 +// } +// } + public class BossEnemyMove : MonoBehaviour { // Start is called once before the first execution of Update after the MonoBehaviour is created @@ -36,7 +46,8 @@ public class BossEnemyMove : MonoBehaviour { _rigidbody = GetComponent(); - Invoke("Think", 5f); // 5초 후에 다시 생각 시작 + // GC Alloc 최소화 + Invoke(nameof(Think), 5f); // 5초 후에 다시 생각 시작 } private void FixedUpdate() diff --git a/Assets/Scripts/Player/PlayerMove.cs b/Assets/Scripts/Player/PlayerMove.cs index 687e7b17..323eb6ab 100644 --- a/Assets/Scripts/Player/PlayerMove.cs +++ b/Assets/Scripts/Player/PlayerMove.cs @@ -1,5 +1,24 @@ using System; using UnityEngine; +using UnityEngine.PlayerLoop; + +public struct Inputs +{ + public bool Jump; + public Vector2 Move; + public bool Attack; +} + +public class InputController +{ + public static void UpdateInput(ref Inputs inputs) + { + inputs.Jump = Input.GetButtonDown("Jump"); + inputs.Move.x = Input.GetAxisRaw("Horizontal"); + inputs.Attack = Input.GetMouseButtonDown(0); + } +} + public class PlayerMove : MonoBehaviour { // 컴포넌트 프로퍼티 @@ -10,6 +29,10 @@ public class PlayerMove : MonoBehaviour public float maxSpeed; public float jumpPower; + private Inputs _inputs; + + public Inputs Inputs => _inputs; + // 변수 [SerializeField] public int hp = 5; @@ -30,7 +53,7 @@ public class PlayerMove : MonoBehaviour // Null 체크 추가 if (RigidBody == null || SpriteRenderer == null || Animator == null) { - Debug.LogError("PlayerMove script requires Rigidbody2D, SpriteRenderer, and Animator components on the same GameObject."); + Debug.LogError("PlayerMove 스크립트에는 동일한 GameObject에 Rigidbody2D, SpriteRenderer 및 Animator 구성 요소가 필요합니다."); // 게임 오브젝트 비활성화 또는 스크립트 비활성화 enabled = false; return; @@ -52,18 +75,20 @@ public class PlayerMove : MonoBehaviour if (attackCollider == null) { - Debug.LogError("AttackArea child object or Collider2D component not found!"); + Debug.LogError("AttackArea 자식 객체나 Collider2D 구성 요소를 찾을 수 없습니다!"); } } - - void Start() - { - - } private void Update() { + // 모든 입력값을 매 프레임 업데이트 + InputController.UpdateInput(ref _inputs); + // 상태 머신 업데이트 _stateMachine.Update(); + + // 프레임이 끝난 후, 다음 프레임을 위해 "GetButtonDown"으로 받는 입력들은 리셋( Jump와 Attack ) + _inputs.Jump = false; + _inputs.Attack = false; } void OnCollisionEnter2D(Collision2D collision) diff --git a/Assets/Scripts/Player/StateMachine.cs b/Assets/Scripts/Player/StateMachine.cs index 48b33586..57a7f096 100644 --- a/Assets/Scripts/Player/StateMachine.cs +++ b/Assets/Scripts/Player/StateMachine.cs @@ -67,6 +67,8 @@ public interface IState public class IdleState : IState { + private static readonly int IsMoving = Animator.StringToHash("isMoving"); + private static readonly int IsJumping = Animator.StringToHash("isJumping"); private PlayerMove _player; public IdleState(PlayerMove player) @@ -76,8 +78,8 @@ public class IdleState : IState public void Enter() { - _player.Animator.SetBool("isMoving", false); - _player.Animator.SetBool("isJumping", false); + _player.Animator.SetBool(IsMoving, false); + _player.Animator.SetBool(IsJumping, false); } public void Update() @@ -90,15 +92,16 @@ public class IdleState : IState public State CheckTransition() { - if (Input.GetButtonDown("Jump")) + if (_player.Inputs.Jump) { return State.JUMP; } - if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0) + // 수정: 좌우 어느 방향으로든 입력이 있을 때 MOVE 상태로 전환 + if (_player.Inputs.Move != Vector2.zero) { return State.MOVE; } - if (Input.GetMouseButtonDown(0)) + if (_player.Inputs.Attack) { return State.Attack; } @@ -108,6 +111,7 @@ public class IdleState : IState public class MoveState : IState { + private static readonly int IsMoving = Animator.StringToHash("isMoving"); private PlayerMove _player; public MoveState(PlayerMove player) @@ -117,23 +121,21 @@ public class MoveState : IState public void Enter() { - _player.Animator.SetBool("isMoving", true); + _player.Animator.SetBool(IsMoving, true); } public void Update() { - float horizontalInput = Input.GetAxisRaw("Horizontal"); + float horizontalInput = _player.Inputs.Move.x; if (horizontalInput != 0) { - // transform.localScale.x를 사용하여 캐릭터와 자식 오브젝트를 함께 뒤집습니다. - // 기본 스프라이트가 왼쪽을 바라보고 있으므로, 오른쪽 이동 시 x 스케일을 뒤집어야 합니다. Vector3 newScale = _player.transform.localScale; newScale.x = horizontalInput > 0 ? -1f : 1f; _player.transform.localScale = newScale; } _player.RigidBody.AddForce(Vector2.right * horizontalInput * _player.maxSpeed, ForceMode2D.Force); - + if (Mathf.Abs(_player.RigidBody.linearVelocity.x) > _player.maxSpeed) { _player.RigidBody.linearVelocity = new Vector2(Mathf.Sign(_player.RigidBody.linearVelocity.x) * _player.maxSpeed, _player.RigidBody.linearVelocity.y); @@ -142,20 +144,20 @@ public class MoveState : IState public void Exit() { - _player.Animator.SetBool("isMoving", false); + _player.Animator.SetBool(IsMoving, false); } public State CheckTransition() { - if (Input.GetButtonDown("Jump")) + if (_player.Inputs.Jump) { return State.JUMP; } - if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) < 0.1f) + if (_player.Inputs.Move == Vector2.zero) { return State.IDLE; } - if (Input.GetMouseButtonDown(0)) + if (_player.Inputs.Attack) { return State.Attack; } @@ -165,6 +167,7 @@ public class MoveState : IState public class JumpState : IState { + private static readonly int IsJumping = Animator.StringToHash("isJumping"); private PlayerMove _player; public JumpState(PlayerMove player) @@ -175,27 +178,28 @@ public class JumpState : IState public void Enter() { _player.RigidBody.AddForce(Vector2.up * _player.jumpPower, ForceMode2D.Impulse); - _player.Animator.SetBool("isJumping", true); + _player.Animator.SetBool(IsJumping, true); } public void Update() { - if (Input.GetButton("Horizontal")) + float horizontalInput = _player.Inputs.Move.x; + if (horizontalInput != 0) { Vector3 newScale = _player.transform.localScale; - newScale.x = Input.GetAxisRaw("Horizontal") > 0 ? -1f : 1f; + newScale.x = horizontalInput > 0 ? -1f : 1f; _player.transform.localScale = newScale; } } public void Exit() { - _player.Animator.SetBool("isJumping", false); + _player.Animator.SetBool(IsJumping, false); } public State CheckTransition() { - if (Input.GetMouseButtonDown(0)) + if (_player.Inputs.Attack) { return State.Attack; }