레스토랑 플레이어 FSM 추가

This commit is contained in:
NTG_Lenovo 2025-07-07 12:53:31 +09:00
parent 51e76f7a47
commit ebfec6457d
10 changed files with 132 additions and 17 deletions

View File

@ -1,6 +1,6 @@
namespace DDD namespace DDD
{ {
public interface IPlayerState public interface IStateMachine
{ {
void Enter(); void Enter();
void Update(); void Update();

View File

@ -2,18 +2,18 @@ namespace DDD
{ {
public class PlayerStateMachine public class PlayerStateMachine
{ {
private IPlayerState _currentState; private IStateMachine _currentStateMachine;
public void ChangeState(IPlayerState newState) public void ChangeState(IStateMachine newStateMachine)
{ {
_currentState?.Exit(); _currentStateMachine?.Exit();
_currentState = newState; _currentStateMachine = newStateMachine;
_currentState.Enter(); _currentStateMachine.Enter();
} }
public void Update() public void Update()
{ {
_currentState?.Update(); _currentStateMachine?.Update();
} }
} }
} }

View File

@ -2,6 +2,7 @@
using UnityEngine; using UnityEngine;
using UnityEngine.AddressableAssets; using UnityEngine.AddressableAssets;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
using UnityEngine.Serialization;
namespace DDD namespace DDD
{ {
@ -18,11 +19,14 @@ public class RestaurantPlayer : MonoBehaviour
private Vector3 _inputDirection; private Vector3 _inputDirection;
private Vector3 _currentDirection = Vector3.back; private Vector3 _currentDirection = Vector3.back;
public bool IsMoving;
public bool IsDashing;
public bool IsDashCoolDownActive;
private bool _isMoving;
private bool _isDashing;
private bool _isDashCoolDownActive;
private float _finalSpeed; private float _finalSpeed;
private PlayerStateMachine _stateMachine;
#endregion #endregion
@ -44,11 +48,16 @@ private void Start()
_moveAction.performed += OnMove; _moveAction.performed += OnMove;
_moveAction.canceled += OnMove; _moveAction.canceled += OnMove;
_dashAction.performed += OnDash; _dashAction.performed += OnDash;
_stateMachine = new PlayerStateMachine();
ChangeState(new IdleState(this, _playerView));
} }
//public CellManager cellManager; //public CellManager cellManager;
private void Update() private void Update()
{ {
_stateMachine.Update();
FlipVisualLook(); FlipVisualLook();
//UpdateCell //UpdateCell
@ -101,13 +110,13 @@ public void OnMove(InputAction.CallbackContext context)
public bool CanMove() public bool CanMove()
{ {
return _playerData.IsMoveEnabled && !_isDashing; return _playerData.IsMoveEnabled && !IsDashing;
} }
public void Move() public void Move()
{ {
SetCurrentDirection(_inputDirection); SetCurrentDirection(_inputDirection);
_isMoving = _inputDirection != Vector3.zero; IsMoving = _inputDirection != Vector3.zero;
var finalVelocity = _inputDirection * _playerData.MoveSpeed; var finalVelocity = _inputDirection * _playerData.MoveSpeed;
_playerView.SetVelocity(finalVelocity); _playerView.SetVelocity(finalVelocity);
@ -122,7 +131,7 @@ public void OnDash(InputAction.CallbackContext context)
public bool CanDash() public bool CanDash()
{ {
return _playerData.IsDashEnabled && !_isDashing && !_isDashCoolDownActive; return _playerData.IsDashEnabled && !IsDashing && !IsDashCoolDownActive;
} }
public void Dash() public void Dash()
@ -132,8 +141,8 @@ public void Dash()
private IEnumerator DashCoroutine() private IEnumerator DashCoroutine()
{ {
_isDashing = true; IsDashing = true;
_isDashCoolDownActive = true; IsDashCoolDownActive = true;
_playerView.PlayDashParticle(); _playerView.PlayDashParticle();
AudioManager.Instance.PlaySfx(_playerData.DashSfxName); AudioManager.Instance.PlaySfx(_playerData.DashSfxName);
@ -161,7 +170,7 @@ public void EndDash(float dashCooldown = float.PositiveInfinity)
{ {
Utils.EndUniqueCoroutine(this, ref _dashInstance); Utils.EndUniqueCoroutine(this, ref _dashInstance);
_playerView.SetVelocity(Vector3.zero); _playerView.SetVelocity(Vector3.zero);
_isDashing = false; IsDashing = false;
if (float.IsPositiveInfinity(dashCooldown)) if (float.IsPositiveInfinity(dashCooldown))
{ {
@ -169,7 +178,12 @@ public void EndDash(float dashCooldown = float.PositiveInfinity)
} }
// TODO : ui 연동 // TODO : ui 연동
StartCoroutine(Utils.CoolDownCoroutine(dashCooldown, () => _isDashCoolDownActive = false)); StartCoroutine(Utils.CoolDownCoroutine(dashCooldown, () => IsDashCoolDownActive = false));
}
public void ChangeState(IStateMachine stateMachine)
{
_stateMachine.ChangeState(stateMachine);
} }
#endregion #endregion

View File

@ -1,7 +1,25 @@
using Spine;
using UnityEngine; using UnityEngine;
namespace DDD namespace DDD
{ {
public static class RestaurantSpineAnimation
{
public const string Idle = "Idle";
public const string Walking = "RunFast";
public const string ServingIdle = "Serving/ServingIdle";
public const string Serving = "Serving/ServingFast";
public const string Dash = "Dash";
public const string CleaningFloor = "Cleaning/CleaningFloor";
public const string CleaningTable = "Cleaning/CleaningTable";
public const string MakingCocktail = "BeerMaker";
public const string Pumping = "Attack/AttackWhip";
public const string AttackSlime = "Attack/AttackSlime";
public const string AttackLimeTree = "Attack/AttackBat";
public const string CookingFried = "Cooking/CookingFried";
public const string CookingStew = "Cooking/CookingStew";
}
public class RestaurantPlayerView : MonoBehaviour public class RestaurantPlayerView : MonoBehaviour
{ {
private Rigidbody _rigidbody; private Rigidbody _rigidbody;
@ -26,5 +44,12 @@ public void PlayDashParticle()
_dashParticle.Play(); _dashParticle.Play();
} }
} }
public TrackEntry PlayAnimation(string animationName, bool isLoopActive, float speed = 1f, bool isReverse = false, int trackIndex = 0)
=> _spineController.PlayAnimation(animationName, isLoopActive, speed, isReverse, trackIndex);
public TrackEntry PlayAnimationDuration(string animationName, bool isLoopActive, float duration, bool isReverse = false, int trackIndex = 0) =>
_spineController.PlayAnimationDuration(animationName, isLoopActive, duration, isReverse, trackIndex);
public TrackEntry AddAnimation(string animationName, bool isLoopActive, int trackIndex = 0)
=> _spineController.AddAnimation(animationName, isLoopActive, trackIndex);
} }
} }

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ef03a607bdc3e914aad8e99ab5c6f91b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,32 @@
namespace DDD
{
public class IdleState : IStateMachine
{
private RestaurantPlayer _player;
private RestaurantPlayerView _view;
public IdleState(RestaurantPlayer player, RestaurantPlayerView view)
{
_player = player;
_view = view;
}
public void Enter()
{
_view.PlayAnimation(RestaurantSpineAnimation.Idle, true);
}
public void Update()
{
if (_player.IsMoving)
{
_player.ChangeState(new WalkingState(_player, _view));
}
}
public void Exit()
{
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 8dd7a1f24d102af41848ae82fb8f8ca5

View File

@ -0,0 +1,32 @@
namespace DDD
{
public class WalkingState : IStateMachine
{
private RestaurantPlayer _player;
private RestaurantPlayerView _view;
public WalkingState(RestaurantPlayer player, RestaurantPlayerView view)
{
_player = player;
_view = view;
}
public void Enter()
{
_view.PlayAnimation(RestaurantSpineAnimation.Walking, true);
}
public void Update()
{
if (!_player.IsMoving)
{
_player.ChangeState(new IdleState(_player, _view));
}
}
public void Exit()
{
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 08926e2ade87cab459e625851316b00c