using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.InputSystem; namespace BlueWater { public enum InputActionMaps { None = 0, Tycoon, TycoonUi, CombatTitle, Combat, CombatUi, Bar } public class PlayerInputKeyManager : Singleton { [SerializeField] private PlayerInput _currentPlayerInput; private Vector3 _lastMousePosition; //마우스 이동 감지용 private bool _isKey = false; //키보드 입력상태 == true / 마우스 입력상태 == false (중복 방직용) private readonly List _onActionKeyboard = new List(); private readonly List _onActionMouse = new List(); public void AddOnActionKeyboard(Action action) { _onActionKeyboard.Add(action); } public void AddOnActionMouse(Action action) { _onActionMouse.Add(action); } public void Update() { bool CheckMouse(){return Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1)||Input.GetMouseButtonDown(2);} if(!_isKey && Input.anyKeyDown && !CheckMouse()) //키보드 감지 (최초) { foreach (var element in _onActionKeyboard) { element?.Invoke(); } _lastMousePosition = Input.mousePosition; _isKey = true; } else if (_isKey && (Input.anyKeyDown && CheckMouse() || (_isKey && Input.mousePosition != _lastMousePosition))) //마우스 감지 (최초) { foreach (var element in _onActionMouse) { element?.Invoke(); } _lastMousePosition = Input.mousePosition; _isKey = false; } } /// /// 현재 실행되고 있는 PlayerInput을 관리할 수 있게 /// PlayerInput 컴포넌트를 받아와서 사용하는 경우에 필수로 호출 /// /// public void SetCurrentPlayerInput(PlayerInput playerInput) => _currentPlayerInput = playerInput; private bool IsNullCurrentPlayerInput() { if (_currentPlayerInput && _currentPlayerInput.enabled) return false; Debug.Log("CurrentPlayerInput가 할당되지 않았습니다."); return true; } public void SwitchCurrentActionMap(string inputActionMaps) { if (IsNullCurrentPlayerInput()) return; _currentPlayerInput.SwitchCurrentActionMap(inputActionMaps); } public void SwitchCurrentActionMap(InputActionMaps inputActionMaps) { if (IsNullCurrentPlayerInput()) return; _currentPlayerInput.SwitchCurrentActionMap(inputActionMaps.ToString()); } public InputActionMap GetCurrentInputActionMap() { if (IsNullCurrentPlayerInput()) return null; return _currentPlayerInput.currentActionMap; } public void EnableCurrentPlayerInput() { if (!_currentPlayerInput) return; _currentPlayerInput.enabled = true; } public void DisableCurrentPlayerInput() { if (IsNullCurrentPlayerInput()) return; _currentPlayerInput.enabled = false; } public void DisableAllActionMaps() { if (IsNullCurrentPlayerInput()) return; foreach (var element in _currentPlayerInput.actions.actionMaps) { element.Disable(); } } public void DisableAllActionsExcept(string exceptActionName) { if (IsNullCurrentPlayerInput()) return; var exceptAction = _currentPlayerInput.currentActionMap.FindAction(exceptActionName); foreach (var action in _currentPlayerInput.currentActionMap.actions) { if (action != exceptAction) { action.Disable(); } else { action.Enable(); } } } public void EnableAllActionsMaps() { if (IsNullCurrentPlayerInput()) return; foreach (var action in _currentPlayerInput.actions) { action.Enable(); } } public void EnableAction(string actionName) { if (IsNullCurrentPlayerInput()) return; var action = _currentPlayerInput.currentActionMap.FindAction(actionName); if (action == null) { Debug.Log($"현재 Action Map인 {_currentPlayerInput.currentActionMap}에는 {actionName} Action이 존재하지 않습니다"); return; } action.Enable(); } public void DisableAction(string actionName) { if (IsNullCurrentPlayerInput()) return; var action = _currentPlayerInput.currentActionMap.FindAction(actionName); if (action == null) { Debug.Log($"현재 Action Map인 {_currentPlayerInput.currentActionMap}에는 {actionName} Action이 존재하지 않습니다"); return; } action.Disable(); } } }