using System.Collections.Generic; using UnityEngine; public enum State { IDLE, MOVE, JUMP, Attack } public class StateMachine { private IState _state; private State _currentState; //장점 : key 값을 바로 불러와서 value를 리턴할 수 있게 된다. (자료구조가 많은 데이터를 담았을 때 리턴하는 경우 속도의 효율) //단점 : 자료구조의 데이터가 적을 때, 100개 혹은 10 이하의 데이터에서 Dictionary를 사용할 경우 속도가 오히려 느릴 수 있다. private Dictionary _states = new Dictionary(); public StateMachine(State state, PlayerMove player) { _states.Add(State.IDLE, new IdleState(player)); _states.Add(State.MOVE, new MoveState(player)); _states.Add(State.JUMP, new JumpState(player)); _currentState = state; _state = _states[state]; _state.Enter(); } public void Update() { _state.Update(); var newState = _state.CheckTransition(); // 상태가 변경되었는지 확인 (참조가 다르면) if (_currentState != newState) { _state.Exit(); SetTransition(newState); } } public void SetTransition(State state) { // 다음음 상태로 전환 _currentState = state; _state = _states[state]; _state.Enter(); } } public interface IState { void Enter(); void Update(); void Exit(); // 트리거 조건일 경우 다음 상태로 전환 State CheckTransition(); } public class IdleState : IState { private PlayerMove _player; // PlayerMove 객체에 대한 참조 public IdleState(PlayerMove player) { _player = player; } public void Enter() { // _player.Animator.SetBool(PlayerMove.IsMoving, false); } public void Update() { // 상태별 로직은 여기서는 필요 없음 } public void Exit() { // Idle 상태에서 벗어날 때의 로직 } public State CheckTransition() { if (Input.GetButtonDown("Jump")) { return State.JUMP; } if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0) { return State.MOVE; } return State.IDLE; } } public class MoveState : IState { private static int _isMoving = Animator.StringToHash("isMoving"); private PlayerMove _player; public MoveState(PlayerMove player) { _player = player; } public void Enter() { _player.Animator.SetBool(_isMoving, true); } public void Update() { float horizontalInput = Input.GetAxisRaw("Horizontal"); if (horizontalInput != 0) { // 입력 방향에 따라 스프라이트를 뒤집음. _player.SpriteRenderer.flipX = horizontalInput > 0; } } public void Exit() { // 이동 상태에서 벗어날 때 필요한 로직 _player.Animator.SetBool(_isMoving, false); } public State CheckTransition() { if (Input.GetButtonDown("Jump")) { return State.JUMP; } if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) < 0.1f) { return State.IDLE; // Create new IdleState object } return State.MOVE; } } public class JumpState : IState { private static int _isJumping = Animator.StringToHash("isJumping"); private PlayerMove _player; public JumpState(PlayerMove player) { _player = player; } public void Enter() { _player.RigidBody.AddForce(Vector2.up * _player.jumpPower, ForceMode2D.Impulse); _player.Animator.SetBool(_isJumping, true); } public void Update() { // 점프 중 스프라이트 방향 변경 if (Input.GetButton("Horizontal")) { _player.SpriteRenderer.flipX = Input.GetAxisRaw("Horizontal") > 0; } } public void Exit() { _player.Animator.SetBool(_isJumping, false); } public State CheckTransition() { if (_player.RigidBody.linearVelocity.y < 0) { RaycastHit2D rayHit = Physics2D.Raycast(_player.RigidBody.position, Vector3.down, 1, LayerMask.GetMask("Platform")); if (rayHit.collider != null && rayHit.distance < 0.7f) { return State.IDLE; } } return State.JUMP; } } public class AttackState : IState { private PlayerMove _player; public AttackState(PlayerMove player) { _player = player; } public void Enter() { // 공격 애니메이션 시작 _player.Animator.SetTrigger("Attack"); } public void Update() { // 공격 상태 로직 (예: 애니메이션 재생 중 다른 입력 무시) } public void FixedUpdate() { } public void Exit() { // 공격 상태 종료 } public State CheckTransition() { // 공격 애니메이션이 끝나면 Idle 상태로 전환 // Animator의 현재 상태를 확인하여 transition을 처리합니다. // 예를 들어, GetCurrentAnimatorStateInfo(0).IsName("Attack")이 false가 되면 전환 // 또는 애니메이션 이벤트로 상태 전환을 직접 호출할 수도 있습니다. AnimatorStateInfo stateInfo = _player.Animator.GetCurrentAnimatorStateInfo(0); if (stateInfo.normalizedTime >= 1.0f && !stateInfo.IsTag("Attack")) { return State.IDLE; } return State.Attack; } }