CapersProject/Assets/02.Scripts/PlayerInputKeyManager.cs

299 lines
9.4 KiB
C#
Raw Normal View History

2024-11-17 04:29:57 +00:00
using System.Threading.Tasks;
2025-02-10 02:13:46 +00:00
using DDD;
2024-06-03 18:26:03 +00:00
using UnityEngine;
using UnityEngine.InputSystem;
2024-11-17 04:29:57 +00:00
using UnityEngine.SceneManagement;
2024-06-03 18:26:03 +00:00
2025-02-10 02:13:46 +00:00
public enum InputActionMaps
2024-06-03 18:26:03 +00:00
{
None = 0,
2024-11-30 11:53:38 +00:00
UI = 1,
Tycoon = 2,
TycoonUi = 3,
CombatTitle = 4,
Combat = 5,
CombatUi = 6
}
public static class UiActions
{
public const string Navigate = "Navigate";
2024-06-03 18:26:03 +00:00
}
2024-11-18 10:07:19 +00:00
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
{
2024-11-26 12:24:01 +00:00
public const string Move = "Move";
2024-11-18 10:07:19 +00:00
public const string Cancel = "Cancel";
public const string PressQ = "PressQ";
2024-12-17 07:42:47 +00:00
public const string PressR = "PressR";
public const string PressA = "PressA";
public const string PressD = "PressD";
2024-11-18 10:07:19 +00:00
public const string PressAnyKey = "PressAnyKey";
public const string InteractionE = "InteractionE";
2025-02-14 03:17:41 +00:00
public const string PressF = "PressF";
2024-11-18 10:07:19 +00:00
}
2024-06-03 18:26:03 +00:00
public class PlayerInputKeyManager : Singleton<PlayerInputKeyManager>
{
[SerializeField]
private PlayerInput _currentPlayerInput;
2024-12-03 12:30:09 +00:00
public bool IsInitialized { get; private set; }
2024-10-31 12:17:10 +00:00
2024-11-17 04:29:57 +00:00
protected override void OnAwake()
2024-10-31 12:17:10 +00:00
{
2024-11-17 04:29:57 +00:00
base.OnAwake();
_currentPlayerInput = GetComponent<PlayerInput>();
2024-10-31 12:17:10 +00:00
}
2024-11-17 04:29:57 +00:00
private async void Start()
2024-10-31 12:17:10 +00:00
{
2024-11-17 04:29:57 +00:00
await Initialize();
2024-11-28 23:07:50 +00:00
SceneManager.sceneLoaded += OnSceneLoaded;
2024-10-31 12:17:10 +00:00
}
2024-11-17 04:29:57 +00:00
2024-11-28 23:07:50 +00:00
private async void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
await Initialize();
}
private void OnDestroy()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
2024-11-17 04:29:57 +00:00
private async Task Initialize()
{
2024-12-03 12:30:09 +00:00
IsInitialized = false;
2024-11-17 04:29:57 +00:00
await Task.Delay(1000);
DisableAllActionMaps();
2024-11-29 08:34:20 +00:00
string currentSceneName = SceneManager.GetActiveScene().name;
if (currentSceneName == SceneName.TycoonTile)
{
SwitchCurrentActionMap(InputActionMaps.TycoonUi);
}
2025-01-21 08:58:43 +00:00
else
2024-11-17 04:29:57 +00:00
{
2024-11-29 08:34:20 +00:00
SwitchCurrentActionMap(InputActionMaps.Tycoon);
2024-11-17 04:29:57 +00:00
}
2024-12-03 12:30:09 +00:00
IsInitialized = true;
2024-11-17 04:29:57 +00:00
}
2024-10-31 12:17:10 +00:00
2024-06-03 18:26:03 +00:00
/// <summary>
/// 현재 실행되고 있는 PlayerInput을 관리할 수 있게
/// PlayerInput 컴포넌트를 받아와서 사용하는 경우에 필수로 호출
/// </summary>
/// <param name="playerInput"></param>
public void SetCurrentPlayerInput(PlayerInput playerInput) => _currentPlayerInput = playerInput;
private bool IsNullCurrentPlayerInput()
{
if (_currentPlayerInput && _currentPlayerInput.enabled) return false;
2024-06-03 18:26:03 +00:00
Debug.Log("CurrentPlayerInput가 할당되지 않았습니다.");
2024-06-03 18:26:03 +00:00
return true;
}
2024-11-17 04:29:57 +00:00
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;
}
2024-11-19 06:03:43 +00:00
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;
}
2024-11-17 04:29:57 +00:00
public bool IsCurrentActionMap(InputActionMaps inputActionMaps)
{
if (IsNullCurrentPlayerInput()) return false;
return _currentPlayerInput.currentActionMap.ToString() == inputActionMaps.ToString();
}
2024-06-03 18:26:03 +00:00
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;
2024-06-03 18:26:03 +00:00
_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();
}
}
2024-06-03 18:26:03 +00:00
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()
2024-06-03 18:26:03 +00:00
{
if (IsNullCurrentPlayerInput()) return;
foreach (var action in _currentPlayerInput.actions)
{
action.Enable();
}
}
2024-10-31 05:41:13 +00:00
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();
}
2025-02-10 02:13:46 +00:00
}