225 lines
4.9 KiB
C#
225 lines
4.9 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public enum State
|
|
{
|
|
IDLE, MOVE, JUMP
|
|
}
|
|
|
|
public class StateMachine
|
|
{
|
|
private IState _state;
|
|
private State _currentState;
|
|
|
|
//장점 : key 값을 바로 불러와서 value를 리턴할 수 있게 된다. (자료구조가 많은 데이터를 담았을 때 리턴하는 경우 속도의 효율)
|
|
//단점 : 자료구조의 데이터가 적을 때, 100개 혹은 10 이하의 데이터에서 Dictionary를 사용할 경우 속도가 오히려 느릴 수 있다.
|
|
private Dictionary<State, IState> _states = new Dictionary<State, IState>();
|
|
|
|
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()
|
|
{
|
|
// 상태별 로직
|
|
_player.SpriteRenderer.flipX = Input.GetAxisRaw("Horizontal") > 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;
|
|
private IdleState _idleState;
|
|
|
|
public AttackState(PlayerMove player)
|
|
{
|
|
_player = player;
|
|
}
|
|
|
|
public void Enter()
|
|
{
|
|
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
|
|
}
|
|
|
|
public void FixedUpdate()
|
|
{
|
|
|
|
}
|
|
|
|
public void Exit()
|
|
{
|
|
// _player.Animator.SetBool(PlayerMove.IsAttacking, false);
|
|
}
|
|
|
|
public State CheckTransition()
|
|
{
|
|
// 공격이 종료됐을 때
|
|
// idle
|
|
return State.IDLE;
|
|
}
|
|
} |