FSM 구현중... 실행하면 유니티가 종료됨

This commit is contained in:
Mingu Kim
2025-08-15 20:05:29 +09:00
parent 0ef120a766
commit 89a2bfa424
2 changed files with 202 additions and 141 deletions

View File

@@ -2,16 +2,10 @@ using UnityEngine;
public class StateMachine
{
private Animator animator;
private IState _state;
private PlayerMove _playerBase;
// private TextMeshProUGUI _textState;
private IState _state;
public StateMachine(IState state, PlayerMove playerMove)
public StateMachine(IState state)
{
// 초기 상태 객체 생성
_playerBase = playerMove;;
_state = state;
_state.Enter();
}
@@ -19,9 +13,9 @@ public class StateMachine
public void Update()
{
_state.Update();
var newState = _state.CheckTransition();
// 상태가 변경되었는지 확인 (참조가 다르면)
if (_state != newState)
{
_state.Exit();
@@ -29,6 +23,11 @@ public class StateMachine
}
}
public void FixedUpdate()
{
_state.FixedUpdate();
}
public void SetTransition(IState state)
{
// 다음음 상태로 전환
@@ -43,8 +42,170 @@ public interface IState
void Update();
void FixedUpdate(); // FixedUpdate 추가
void Exit();
// 트리거 조건일 경우 다음 상태로 전환
IState CheckTransition();
}
public class IdleState : IState
{
private PlayerMove _player; // PlayerMove 객체에 대한 참조
private MoveState _moveState;
private JumpState _jumpState;
public IdleState(PlayerMove player)
{
_player = player;
_moveState = new MoveState(_player);
_jumpState = new JumpState(_player);
}
public void Enter()
{
_player.Animator.SetBool(PlayerMove.IsMoving, false);
}
public void Update()
{
// 상태별 로직은 여기서는 필요 없음
}
public void FixedUpdate()
{
// 물리적인 로직이 없으므로 비워둠
}
public void Exit()
{
// Idle 상태에서 벗어날 때의 로직
}
public IState CheckTransition()
{
// 우선 순위: Jump -> Move
if (Input.GetButtonDown("Jump"))
{
return _jumpState;
}
if (Input.GetAxisRaw("Horizontal") != 0)
{
return _moveState;
}
return this;
}
}
public class MoveState : IState
{
private PlayerMove _player;
private IdleState _idleState;
private JumpState _jumpState;
public MoveState(PlayerMove player)
{
_player = player;
_idleState = new IdleState(_player);
_jumpState = new JumpState(_player);
}
public void Enter()
{
_player.Animator.SetBool(PlayerMove.IsMoving, true);
}
public void Update()
{
// 상태별 로직
_player.SpriteRenderer.flipX = Input.GetAxisRaw("Horizontal") < 0;
}
public void FixedUpdate()
{
float h = Input.GetAxisRaw("Horizontal");
_player.RigidBody.AddForce(Vector2.right * h, ForceMode2D.Impulse);
if (_player.RigidBody.linearVelocity.x > _player.maxSpeed)
{
_player.RigidBody.linearVelocity = new Vector2(_player.maxSpeed, _player.RigidBody.linearVelocity.y);
}
else if (_player.RigidBody.linearVelocity.x < -_player.maxSpeed)
{
_player.RigidBody.linearVelocity = new Vector2(-_player.maxSpeed, _player.RigidBody.linearVelocity.y);
}
}
public void Exit()
{
// 이동 상태에서 벗어날 때 필요한 로직
_player.Animator.SetBool(PlayerMove.IsMoving, false);
}
public IState CheckTransition()
{
if (Input.GetButtonDown("Jump"))
{
return _jumpState;
}
if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) < 0.1f) // 키 입력이 없을 때
{
return _idleState;
}
return this;
}
}
public class JumpState : IState
{
private PlayerMove _player;
private IdleState _idleState;
public JumpState(PlayerMove player)
{
_player = player;
_idleState = new IdleState(_player);
}
public void Enter()
{
_player.RigidBody.AddForce(Vector2.up * _player.jumpPower, ForceMode2D.Impulse);
_player.Animator.SetBool(PlayerMove.IsJumping, true);
}
public void Update()
{
// 점프 중 스프라이트 방향 변경
if (Input.GetButton("Horizontal"))
{
_player.SpriteRenderer.flipX = Input.GetAxisRaw("Horizontal") < 0;
}
}
public void FixedUpdate()
{
// FixedUpdate에 물리 로직이 없으므로 비워둠
}
public void Exit()
{
_player.Animator.SetBool(PlayerMove.IsJumping, false);
}
public IState CheckTransition()
{
// 바닥에 닿았는지 확인 -> IdleState로 전환
if (_player.RigidBody.linearVelocity.y < 0)
{
Debug.DrawRay(_player.RigidBody.position, Vector3.down, Color.green);
RaycastHit2D rayHit = Physics2D.Raycast(_player.RigidBody.position, Vector3.down, 1, LayerMask.GetMask("Platform"));
if (rayHit.collider != null && rayHit.distance < 0.7f)
{
return _idleState;
}
}
return this;
}
}