각 상태를 담을 수 있는 딕셔너리에 상태 저장하도록 구조 수정
This commit is contained in:
@@ -8,13 +8,6 @@ public class PlayerMove : MonoBehaviour
|
|||||||
public SpriteRenderer SpriteRenderer { get; private set; }
|
public SpriteRenderer SpriteRenderer { get; private set; }
|
||||||
public Animator Animator { get; private set; }
|
public Animator Animator { get; private set; }
|
||||||
|
|
||||||
// 다른 클래스에서 접근할 수 있도록 private static 변수와 public 프로퍼티를 분리
|
|
||||||
private static int _isJumping;
|
|
||||||
private static int _isMoving;
|
|
||||||
|
|
||||||
public static int IsJumping => _isJumping;
|
|
||||||
public static int IsMoving => _isMoving;
|
|
||||||
|
|
||||||
public float maxSpeed;
|
public float maxSpeed;
|
||||||
public float jumpPower;
|
public float jumpPower;
|
||||||
|
|
||||||
@@ -41,11 +34,7 @@ public class PlayerMove : MonoBehaviour
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Animator가 초기화된 후에 해시 값 할당
|
_stateMachine = new StateMachine(State.IDLE, this);
|
||||||
_isJumping = Animator.StringToHash("isJumping");
|
|
||||||
_isMoving = Animator.StringToHash("isMoving");
|
|
||||||
|
|
||||||
_stateMachine = new StateMachine(new IdleState(this));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Update()
|
private void Update()
|
||||||
@@ -55,7 +44,17 @@ public class PlayerMove : MonoBehaviour
|
|||||||
|
|
||||||
void FixedUpdate()
|
void FixedUpdate()
|
||||||
{
|
{
|
||||||
_stateMachine.FixedUpdate();
|
float h = Input.GetAxisRaw("Horizontal");
|
||||||
|
RigidBody.AddForce(Vector2.right * h, ForceMode2D.Impulse);
|
||||||
|
|
||||||
|
if (RigidBody.linearVelocity.x > maxSpeed)
|
||||||
|
{
|
||||||
|
RigidBody.linearVelocity = new Vector2(maxSpeed, RigidBody.linearVelocity.y);
|
||||||
|
}
|
||||||
|
else if (RigidBody.linearVelocity.x < -maxSpeed)
|
||||||
|
{
|
||||||
|
RigidBody.linearVelocity = new Vector2(-maxSpeed, RigidBody.linearVelocity.y);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnCollisionEnter2D(Collision2D collision)
|
void OnCollisionEnter2D(Collision2D collision)
|
||||||
|
|||||||
@@ -1,12 +1,28 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
|
public enum State
|
||||||
|
{
|
||||||
|
IDLE, MOVE, JUMP
|
||||||
|
}
|
||||||
|
|
||||||
public class StateMachine
|
public class StateMachine
|
||||||
{
|
{
|
||||||
private IState _state;
|
private IState _state;
|
||||||
|
private State _currentState;
|
||||||
public StateMachine(IState state)
|
|
||||||
|
//장점 : key 값을 바로 불러와서 value를 리턴할 수 있게 된다. (자료구조가 많은 데이터를 담았을 때 리턴하는 경우 속도의 효율)
|
||||||
|
//단점 : 자료구조의 데이터가 적을 때, 100개 혹은 10 이하의 데이터에서 Dictionary를 사용할 경우 속도가 오히려 느릴 수 있다.
|
||||||
|
private Dictionary<State, IState> _states = new Dictionary<State, IState>();
|
||||||
|
|
||||||
|
public StateMachine(State state, PlayerMove player)
|
||||||
{
|
{
|
||||||
_state = state;
|
_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();
|
_state.Enter();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -16,22 +32,18 @@ public class StateMachine
|
|||||||
var newState = _state.CheckTransition();
|
var newState = _state.CheckTransition();
|
||||||
|
|
||||||
// 상태가 변경되었는지 확인 (참조가 다르면)
|
// 상태가 변경되었는지 확인 (참조가 다르면)
|
||||||
if (_state != newState)
|
if (_currentState != newState)
|
||||||
{
|
{
|
||||||
_state.Exit();
|
_state.Exit();
|
||||||
SetTransition(newState);
|
SetTransition(newState);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void FixedUpdate()
|
public void SetTransition(State state)
|
||||||
{
|
|
||||||
_state.FixedUpdate();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void SetTransition(IState state)
|
|
||||||
{
|
{
|
||||||
// 다음음 상태로 전환
|
// 다음음 상태로 전환
|
||||||
_state = state;
|
_currentState = state;
|
||||||
|
_state = _states[state];
|
||||||
_state.Enter();
|
_state.Enter();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -42,19 +54,15 @@ public interface IState
|
|||||||
|
|
||||||
void Update();
|
void Update();
|
||||||
|
|
||||||
void FixedUpdate(); // FixedUpdate 추가
|
|
||||||
|
|
||||||
void Exit();
|
void Exit();
|
||||||
|
|
||||||
// 트리거 조건일 경우 다음 상태로 전환
|
// 트리거 조건일 경우 다음 상태로 전환
|
||||||
IState CheckTransition();
|
State CheckTransition();
|
||||||
}
|
}
|
||||||
|
|
||||||
public class IdleState : IState
|
public class IdleState : IState
|
||||||
{
|
{
|
||||||
private PlayerMove _player; // PlayerMove 객체에 대한 참조
|
private PlayerMove _player; // PlayerMove 객체에 대한 참조
|
||||||
private MoveState _moveState;
|
|
||||||
private JumpState _jumpState;
|
|
||||||
|
|
||||||
public IdleState(PlayerMove player)
|
public IdleState(PlayerMove player)
|
||||||
{
|
{
|
||||||
@@ -63,7 +71,7 @@ public class IdleState : IState
|
|||||||
|
|
||||||
public void Enter()
|
public void Enter()
|
||||||
{
|
{
|
||||||
_player.Animator.SetBool(PlayerMove.IsMoving, false);
|
// _player.Animator.SetBool(PlayerMove.IsMoving, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update()
|
public void Update()
|
||||||
@@ -71,35 +79,30 @@ public class IdleState : IState
|
|||||||
// 상태별 로직은 여기서는 필요 없음
|
// 상태별 로직은 여기서는 필요 없음
|
||||||
}
|
}
|
||||||
|
|
||||||
public void FixedUpdate()
|
|
||||||
{
|
|
||||||
// 물리적인 로직이 없으므로 비워둠
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Exit()
|
public void Exit()
|
||||||
{
|
{
|
||||||
// Idle 상태에서 벗어날 때의 로직
|
// Idle 상태에서 벗어날 때의 로직
|
||||||
}
|
}
|
||||||
|
|
||||||
public IState CheckTransition()
|
public State CheckTransition()
|
||||||
{
|
{
|
||||||
if (Input.GetButtonDown("Jump"))
|
if (Input.GetButtonDown("Jump"))
|
||||||
{
|
{
|
||||||
return new JumpState(_player); // Create new JumpState object
|
return State.JUMP;
|
||||||
}
|
}
|
||||||
if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0)
|
if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) > 0)
|
||||||
{
|
{
|
||||||
return new MoveState(_player); // Create new MoveState object
|
return State.MOVE;
|
||||||
}
|
}
|
||||||
return this;
|
return State.IDLE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MoveState : IState
|
public class MoveState : IState
|
||||||
{
|
{
|
||||||
|
private static int _isMoving = Animator.StringToHash("isMoving");
|
||||||
|
|
||||||
private PlayerMove _player;
|
private PlayerMove _player;
|
||||||
private IdleState _idleState;
|
|
||||||
private JumpState _jumpState;
|
|
||||||
|
|
||||||
public MoveState(PlayerMove player)
|
public MoveState(PlayerMove player)
|
||||||
{
|
{
|
||||||
@@ -108,7 +111,7 @@ public class MoveState : IState
|
|||||||
|
|
||||||
public void Enter()
|
public void Enter()
|
||||||
{
|
{
|
||||||
_player.Animator.SetBool(PlayerMove.IsMoving, true);
|
_player.Animator.SetBool(_isMoving, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update()
|
public void Update()
|
||||||
@@ -116,46 +119,32 @@ public class MoveState : IState
|
|||||||
// 상태별 로직
|
// 상태별 로직
|
||||||
_player.SpriteRenderer.flipX = Input.GetAxisRaw("Horizontal") > 0;
|
_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()
|
public void Exit()
|
||||||
{
|
{
|
||||||
// 이동 상태에서 벗어날 때 필요한 로직
|
// 이동 상태에서 벗어날 때 필요한 로직
|
||||||
_player.Animator.SetBool(PlayerMove.IsMoving, false);
|
_player.Animator.SetBool(_isMoving, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IState CheckTransition()
|
public State CheckTransition()
|
||||||
{
|
{
|
||||||
if (Input.GetButtonDown("Jump"))
|
if (Input.GetButtonDown("Jump"))
|
||||||
{
|
{
|
||||||
return new JumpState(_player); // Create new JumpState object
|
return State.JUMP;
|
||||||
}
|
}
|
||||||
if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) < 0.1f)
|
if (Mathf.Abs(Input.GetAxisRaw("Horizontal")) < 0.1f)
|
||||||
{
|
{
|
||||||
return new IdleState(_player); // Create new IdleState object
|
return State.IDLE; // Create new IdleState object
|
||||||
}
|
}
|
||||||
return this;
|
return State.MOVE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class JumpState : IState
|
public class JumpState : IState
|
||||||
{
|
{
|
||||||
|
private static int _isJumping = Animator.StringToHash("isJumping");
|
||||||
|
|
||||||
private PlayerMove _player;
|
private PlayerMove _player;
|
||||||
private IdleState _idleState;
|
|
||||||
|
|
||||||
public JumpState(PlayerMove player)
|
public JumpState(PlayerMove player)
|
||||||
{
|
{
|
||||||
@@ -165,7 +154,7 @@ public class JumpState : IState
|
|||||||
public void Enter()
|
public void Enter()
|
||||||
{
|
{
|
||||||
_player.RigidBody.AddForce(Vector2.up * _player.jumpPower, ForceMode2D.Impulse);
|
_player.RigidBody.AddForce(Vector2.up * _player.jumpPower, ForceMode2D.Impulse);
|
||||||
_player.Animator.SetBool(PlayerMove.IsJumping, true);
|
_player.Animator.SetBool(_isJumping, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update()
|
public void Update()
|
||||||
@@ -177,17 +166,12 @@ public class JumpState : IState
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void FixedUpdate()
|
|
||||||
{
|
|
||||||
// FixedUpdate에 물리 로직이 없으므로 비워둠
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Exit()
|
public void Exit()
|
||||||
{
|
{
|
||||||
_player.Animator.SetBool(PlayerMove.IsJumping, false);
|
_player.Animator.SetBool(_isJumping, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IState CheckTransition()
|
public State CheckTransition()
|
||||||
{
|
{
|
||||||
if (_player.RigidBody.linearVelocity.y < 0)
|
if (_player.RigidBody.linearVelocity.y < 0)
|
||||||
{
|
{
|
||||||
@@ -195,10 +179,10 @@ public class JumpState : IState
|
|||||||
|
|
||||||
if (rayHit.collider != null && rayHit.distance < 0.7f)
|
if (rayHit.collider != null && rayHit.distance < 0.7f)
|
||||||
{
|
{
|
||||||
return new IdleState(_player); // Create new IdleState object
|
return State.IDLE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this;
|
return State.JUMP;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -232,10 +216,10 @@ public class AttackState : IState
|
|||||||
// _player.Animator.SetBool(PlayerMove.IsAttacking, false);
|
// _player.Animator.SetBool(PlayerMove.IsAttacking, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IState CheckTransition()
|
public State CheckTransition()
|
||||||
{
|
{
|
||||||
// 공격이 종료됐을 때
|
// 공격이 종료됐을 때
|
||||||
// idle
|
// idle
|
||||||
return this;
|
return State.IDLE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user