입력 시스템 개선

각자의 입력 처리 로직을 갖지 않고 NetworkPlayerController로부터 받아서 쓰도록 함
This commit is contained in:
2026-02-26 01:20:23 +09:00
parent 600f35ae8f
commit 9951aa98b2
4 changed files with 65 additions and 64 deletions

View File

@@ -34,7 +34,6 @@ namespace Northbound
private int currentRotation = 0; // 0-3 private int currentRotation = 0; // 0-3
private Material[] originalMaterials; private Material[] originalMaterials;
private Renderer[] previewRenderers; private Renderer[] previewRenderers;
private PlayerInputActions _inputActions;
// 드래그 건설 관련 // 드래그 건설 관련
private bool isDragging = false; private bool isDragging = false;
@@ -53,35 +52,29 @@ namespace Northbound
{ {
_playerController = GetComponent<NetworkPlayerController>(); _playerController = GetComponent<NetworkPlayerController>();
// 지연 초기화 - 다음 프레임에 체크 if (_playerController != null)
StartCoroutine(InitializeAfterOwnerSet()); {
_playerController.OnInputInitialized += TryInitializeInput;
}
// 이미 로컬 플레이어면 입력 초기화 시도
TryInitializeInput();
} }
private System.Collections.IEnumerator InitializeAfterOwnerSet() private void TryInitializeInput()
{ {
// _ownerPlayerId가 설정될 때까지 대기 (최대 1초) if (_isInitialized) return; // 이미 초기화됨
float timeout = 1f; if (_playerController == null || !_playerController.IsLocalPlayer) return;
while (_playerController != null && _playerController.OwnerPlayerId == ulong.MaxValue && timeout > 0) if (_playerController.InputActions == null) return;
{
yield return null;
timeout -= Time.deltaTime;
}
// 로컬 플레이어인지 확인
if (_playerController == null || !_playerController.IsLocalPlayer)
{
yield break;
}
_isInitialized = true; _isInitialized = true;
_inputActions = new PlayerInputActions(); var inputActions = _playerController.InputActions;
_inputActions.Player.ToggleBuildMode.performed += OnToggleBuildMode; inputActions.Player.ToggleBuildMode.performed += OnToggleBuildMode;
_inputActions.Player.Rotate.performed += OnRotate; inputActions.Player.Rotate.performed += OnRotate;
_inputActions.Player.Build.performed += OnBuildPressed; inputActions.Player.Build.performed += OnBuildPressed;
_inputActions.Player.Build.canceled += OnBuildReleased; inputActions.Player.Build.canceled += OnBuildReleased;
_inputActions.Player.Cancel.performed += OnCancel; inputActions.Player.Cancel.performed += OnCancel;
_inputActions.Enable();
// Create default materials if not assigned // Create default materials if not assigned
if (validMaterial == null) if (validMaterial == null)
@@ -144,15 +137,19 @@ namespace Northbound
public override void OnNetworkDespawn() public override void OnNetworkDespawn()
{ {
if (_isInitialized && _inputActions != null) if (_playerController != null)
{ {
_inputActions.Player.ToggleBuildMode.performed -= OnToggleBuildMode; _playerController.OnInputInitialized -= TryInitializeInput;
_inputActions.Player.Rotate.performed -= OnRotate;
_inputActions.Player.Build.performed -= OnBuildPressed; if (_isInitialized && _playerController.InputActions != null)
_inputActions.Player.Build.canceled -= OnBuildReleased; {
_inputActions.Player.Cancel.performed -= OnCancel; var inputActions = _playerController.InputActions;
_inputActions.Disable(); inputActions.Player.ToggleBuildMode.performed -= OnToggleBuildMode;
_inputActions.Dispose(); inputActions.Player.Rotate.performed -= OnRotate;
inputActions.Player.Build.performed -= OnBuildPressed;
inputActions.Player.Build.canceled -= OnBuildReleased;
inputActions.Player.Cancel.performed -= OnCancel;
}
} }
ClearDragPreviews(); ClearDragPreviews();

View File

@@ -65,9 +65,15 @@ public class NetworkPlayerController : NetworkBehaviour, ITeamMember, IDamageabl
public ulong OwnerPlayerId => _ownerPlayerId.Value; public ulong OwnerPlayerId => _ownerPlayerId.Value;
// 중앙 집중식 입력 관리 - 다른 컴포넌트에서 참조
public PlayerInputActions InputActions => _inputActions;
// 소유자 변경 이벤트 // 소유자 변경 이벤트
public event Action<ulong> OnOwnerChanged; public event Action<ulong> OnOwnerChanged;
// 입력 시스템 초기화 완료 이벤트
public event Action OnInputInitialized;
void Awake() void Awake()
{ {
_controller = GetComponent<CharacterController>(); _controller = GetComponent<CharacterController>();
@@ -141,6 +147,9 @@ public class NetworkPlayerController : NetworkBehaviour, ITeamMember, IDamageabl
_inputActions = new PlayerInputActions(); _inputActions = new PlayerInputActions();
_inputActions.Enable(); _inputActions.Enable();
// 입력 초기화 완료 이벤트 발생
OnInputInitialized?.Invoke();
} }
/// <summary> /// <summary>

View File

@@ -17,11 +17,11 @@ namespace Northbound
[Header("Animation")] [Header("Animation")]
public bool playAnimations = true; public bool playAnimations = true;
private PlayerInputActions _inputActions;
private Dictionary<string, IAction> _actions = new Dictionary<string, IAction>(); private Dictionary<string, IAction> _actions = new Dictionary<string, IAction>();
private Animator _animator; private Animator _animator;
private NetworkAnimator _networkAnimator; private NetworkAnimator _networkAnimator;
private NetworkPlayerController _networkPlayerController; private NetworkPlayerController _networkPlayerController;
private bool _isInputInitialized = false;
// 로컬 플레이어인지 확인 // 로컬 플레이어인지 확인
private bool IsLocalPlayer => _networkPlayerController != null && _networkPlayerController.IsLocalPlayer; private bool IsLocalPlayer => _networkPlayerController != null && _networkPlayerController.IsLocalPlayer;
@@ -49,6 +49,7 @@ namespace Northbound
if (_networkPlayerController != null) if (_networkPlayerController != null)
{ {
_networkPlayerController.OnOwnerChanged += OnOwnerPlayerIdChanged; _networkPlayerController.OnOwnerChanged += OnOwnerPlayerIdChanged;
_networkPlayerController.OnInputInitialized += TryInitializeInput;
} }
// 이미 로컬 플레이어면 입력 초기화 // 이미 로컬 플레이어면 입력 초기화
@@ -63,11 +64,11 @@ namespace Northbound
private void TryInitializeInput() private void TryInitializeInput()
{ {
if (!IsLocalPlayer) return; if (!IsLocalPlayer) return;
if (_inputActions != null) return; if (_isInputInitialized) return; // 이미 초기화됨
if (_networkPlayerController.InputActions == null) return; // 아직 InputActions가 없음
_inputActions = new PlayerInputActions(); _isInputInitialized = true;
_inputActions.Player.Attack.performed += OnAttack; _networkPlayerController.InputActions.Player.Attack.performed += OnAttack;
_inputActions.Enable();
} }
public override void OnNetworkDespawn() public override void OnNetworkDespawn()
@@ -75,13 +76,13 @@ namespace Northbound
if (_networkPlayerController != null) if (_networkPlayerController != null)
{ {
_networkPlayerController.OnOwnerChanged -= OnOwnerPlayerIdChanged; _networkPlayerController.OnOwnerChanged -= OnOwnerPlayerIdChanged;
} _networkPlayerController.OnInputInitialized -= TryInitializeInput;
if (_inputActions != null) // 입력 이벤트 해제
{ if (_networkPlayerController.InputActions != null)
_inputActions.Player.Attack.performed -= OnAttack; {
_inputActions.Disable(); _networkPlayerController.InputActions.Player.Attack.performed -= OnAttack;
_inputActions.Dispose(); }
} }
} }
@@ -126,11 +127,7 @@ namespace Northbound
override public void OnDestroy() override public void OnDestroy()
{ {
if (_inputActions != null) // 입력 정리는 NetworkPlayerController에서 담당
{
_inputActions.Dispose();
}
base.OnDestroy(); base.OnDestroy();
} }
} }

View File

@@ -33,7 +33,6 @@ namespace Northbound
[Header("Worker")] [Header("Worker")]
public Worker assignedWorker; public Worker assignedWorker;
private PlayerInputActions _inputActions;
private IInteractable _currentInteractable; private IInteractable _currentInteractable;
private IInteractable _unavailableInteractable; private IInteractable _unavailableInteractable;
private Camera _mainCamera; private Camera _mainCamera;
@@ -48,6 +47,7 @@ namespace Northbound
private NetworkPlayerController _networkPlayerController; private NetworkPlayerController _networkPlayerController;
private PlayerStats _playerStats; private PlayerStats _playerStats;
private bool _isInputInitialized = false;
public bool IsInteracting => _isInteracting; public bool IsInteracting => _isInteracting;
public float WorkPower => _playerStats?.GetManpower() ?? 10f; public float WorkPower => _playerStats?.GetManpower() ?? 10f;
@@ -69,7 +69,7 @@ namespace Northbound
{ {
_animator = GetComponent<Animator>(); _animator = GetComponent<Animator>();
_equipmentSocket = GetComponent<EquipmentSocket>(); _equipmentSocket = GetComponent<EquipmentSocket>();
if (rayOrigin == null) if (rayOrigin == null)
rayOrigin = transform; rayOrigin = transform;
@@ -77,6 +77,7 @@ namespace Northbound
if (_networkPlayerController != null) if (_networkPlayerController != null)
{ {
_networkPlayerController.OnOwnerChanged += OnOwnerPlayerIdChanged; _networkPlayerController.OnOwnerChanged += OnOwnerPlayerIdChanged;
_networkPlayerController.OnInputInitialized += TryInitializeInput;
} }
// 이미 로컬 플레이어면 입력 초기화 // 이미 로컬 플레이어면 입력 초기화
@@ -91,12 +92,12 @@ namespace Northbound
private void TryInitializeInput() private void TryInitializeInput()
{ {
if (!IsLocalPlayer) return; if (!IsLocalPlayer) return;
if (_inputActions != null) return; if (_isInputInitialized) return; // 이미 초기화됨
if (_networkPlayerController.InputActions == null) return; // 아직 InputActions가 없음
_isInputInitialized = true;
_mainCamera = Camera.main; _mainCamera = Camera.main;
_inputActions = new PlayerInputActions(); _networkPlayerController.InputActions.Player.Interact.performed += OnInteract;
_inputActions.Player.Interact.performed += OnInteract;
_inputActions.Enable();
} }
public override void OnNetworkDespawn() public override void OnNetworkDespawn()
@@ -104,13 +105,13 @@ namespace Northbound
if (_networkPlayerController != null) if (_networkPlayerController != null)
{ {
_networkPlayerController.OnOwnerChanged -= OnOwnerPlayerIdChanged; _networkPlayerController.OnOwnerChanged -= OnOwnerPlayerIdChanged;
} _networkPlayerController.OnInputInitialized -= TryInitializeInput;
if (_inputActions != null) // 입력 이벤트 해제
{ if (_networkPlayerController.InputActions != null)
_inputActions.Player.Interact.performed -= OnInteract; {
_inputActions.Disable(); _networkPlayerController.InputActions.Player.Interact.performed -= OnInteract;
_inputActions.Dispose(); }
} }
} }
@@ -402,10 +403,7 @@ namespace Northbound
public override void OnDestroy() public override void OnDestroy()
{ {
if (_inputActions != null) // 입력 정리는 NetworkPlayerController에서 담당
{
_inputActions.Dispose();
}
} }
} }
} }