292 lines
9.1 KiB
C#
292 lines
9.1 KiB
C#
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
namespace BlueWater
|
|
{
|
|
public enum InputActionMaps
|
|
{
|
|
None = 0,
|
|
UI = 1,
|
|
Tycoon = 2,
|
|
TycoonUi = 3,
|
|
CombatTitle = 4,
|
|
Combat = 5,
|
|
CombatUi = 6
|
|
}
|
|
|
|
public static class UiActions
|
|
{
|
|
public const string Navigate = "Navigate";
|
|
}
|
|
|
|
public static class TycoonActions
|
|
{
|
|
public const string Move = "Move";
|
|
public const string Dash = "Dash";
|
|
public const string Interaction = "Interaction";
|
|
public const string DevelopKey01 = "DevelopKey01";
|
|
public const string OpenManualBook = "OpenManualBook";
|
|
public const string ZoomOut = "ZoomOut";
|
|
public const string ZoomIn = "ZoomIn";
|
|
public const string Options = "Options";
|
|
}
|
|
|
|
public static class TycoonUiActions
|
|
{
|
|
public const string Move = "Move";
|
|
public const string Cancel = "Cancel";
|
|
public const string PressQ = "PressQ";
|
|
public const string PressAnyKey = "PressAnyKey";
|
|
public const string InteractionE = "InteractionE";
|
|
}
|
|
|
|
public class PlayerInputKeyManager : Singleton<PlayerInputKeyManager>
|
|
{
|
|
[SerializeField]
|
|
private PlayerInput _currentPlayerInput;
|
|
|
|
protected override void OnAwake()
|
|
{
|
|
base.OnAwake();
|
|
|
|
_currentPlayerInput = GetComponent<PlayerInput>();
|
|
}
|
|
|
|
private async void Start()
|
|
{
|
|
await Initialize();
|
|
|
|
SceneManager.sceneLoaded += OnSceneLoaded;
|
|
}
|
|
|
|
private async void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
|
{
|
|
await Initialize();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
SceneManager.sceneLoaded -= OnSceneLoaded;
|
|
}
|
|
|
|
private async Task Initialize()
|
|
{
|
|
await Task.Delay(1000);
|
|
|
|
DisableAllActionMaps();
|
|
string currentSceneName = SceneManager.GetActiveScene().name;
|
|
|
|
if (currentSceneName == SceneName.TycoonTile)
|
|
{
|
|
SwitchCurrentActionMap(InputActionMaps.TycoonUi);
|
|
}
|
|
else if (currentSceneName == SceneName.Tycoon)
|
|
{
|
|
SwitchCurrentActionMap(InputActionMaps.Tycoon);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 현재 실행되고 있는 PlayerInput을 관리할 수 있게
|
|
/// PlayerInput 컴포넌트를 받아와서 사용하는 경우에 필수로 호출
|
|
/// </summary>
|
|
/// <param name="playerInput"></param>
|
|
public void SetCurrentPlayerInput(PlayerInput playerInput) => _currentPlayerInput = playerInput;
|
|
|
|
private bool IsNullCurrentPlayerInput()
|
|
{
|
|
if (_currentPlayerInput && _currentPlayerInput.enabled) return false;
|
|
|
|
Debug.Log("CurrentPlayerInput가 할당되지 않았습니다.");
|
|
return true;
|
|
}
|
|
|
|
public InputAction GetAction(InputActionMaps actionMapName, string actionName)
|
|
{
|
|
if (IsNullCurrentPlayerInput()) return null;
|
|
|
|
var actionMap = _currentPlayerInput.actions.FindActionMap(actionMapName.ToString(), true);
|
|
if (actionMap == null)
|
|
{
|
|
Debug.LogError($"Action Map '{actionMapName}' not found!");
|
|
return null;
|
|
}
|
|
|
|
var action = actionMap.FindAction(actionName, true);
|
|
if (action == null)
|
|
{
|
|
Debug.LogError($"Action '{actionName}' not found in Action Map '{actionMapName}'!");
|
|
}
|
|
return action;
|
|
}
|
|
|
|
public string GetBoundKey(InputActionMaps actionMapName, string actionName)
|
|
{
|
|
if (IsNullCurrentPlayerInput()) return null;
|
|
|
|
var actionMap = _currentPlayerInput.actions.FindActionMap(actionMapName.ToString(), true);
|
|
if (actionMap == null)
|
|
{
|
|
Debug.LogError($"Action Map '{actionMapName}' not found!");
|
|
return null;
|
|
}
|
|
|
|
var action = actionMap.FindAction(actionName, true);
|
|
if (action == null)
|
|
{
|
|
Debug.LogError($"Action '{actionName}' not found in Action Map '{actionMapName}'!");
|
|
return null;
|
|
}
|
|
|
|
// 첫 번째 바인딩에서 키 이름 가져오기
|
|
foreach (var binding in action.bindings)
|
|
{
|
|
if (!string.IsNullOrEmpty(binding.path))
|
|
{
|
|
// 키 이름만 추출
|
|
var key = InputControlPath.ToHumanReadableString(binding.path, InputControlPath.HumanReadableStringOptions.OmitDevice);
|
|
return key;
|
|
}
|
|
}
|
|
|
|
Debug.LogWarning($"No bindings found for action '{actionName}' in Action Map '{actionMapName}'.");
|
|
return null;
|
|
}
|
|
|
|
public string GetBoundKey(InputAction inputAction)
|
|
{
|
|
if (IsNullCurrentPlayerInput()) return null;
|
|
|
|
if (inputAction == null)
|
|
{
|
|
Debug.LogError($"Action not found'!");
|
|
return null;
|
|
}
|
|
|
|
// 첫 번째 바인딩에서 키 이름 가져오기
|
|
foreach (var binding in inputAction.bindings)
|
|
{
|
|
if (!string.IsNullOrEmpty(binding.path))
|
|
{
|
|
// 키 이름만 추출
|
|
var key = InputControlPath.ToHumanReadableString(binding.path, InputControlPath.HumanReadableStringOptions.OmitDevice);
|
|
return key;
|
|
}
|
|
}
|
|
|
|
Debug.LogWarning($"No bindings found for action '{inputAction}'");
|
|
return null;
|
|
}
|
|
|
|
public bool IsCurrentActionMap(InputActionMaps inputActionMaps)
|
|
{
|
|
if (IsNullCurrentPlayerInput()) return false;
|
|
|
|
return _currentPlayerInput.currentActionMap.ToString() == inputActionMaps.ToString();
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
} |