using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public enum InputAction { Interact, Attack, Defend, Run, PauseGame, Inventory, SelectItem, SplitItemStack, use1, use2, use3, use4, use5, } public static class InputManager { //Input Dictionary public static Dictionary<InputAction, KeyCode> keyMaps = new Dictionary<InputAction, KeyCode>() { //Movement //Interaction {InputAction.Interact, KeyCode.E }, //Combat {InputAction.Attack, KeyCode.Mouse0 }, {InputAction.Defend, KeyCode.Space }, //Movement { InputAction.Run, KeyCode.LeftShift}, //Menu {InputAction.PauseGame, KeyCode.Escape }, {InputAction.Inventory, KeyCode.I }, //Inventory Actions {InputAction.SelectItem, KeyCode.Mouse0 }, {InputAction.SplitItemStack, KeyCode.Mouse1 }, //Quick Inventory Actions {InputAction.use1, KeyCode.Alpha1 }, {InputAction.use2, KeyCode.Alpha2 }, {InputAction.use3, KeyCode.Alpha3 }, {InputAction.use4, KeyCode.Alpha4 }, {InputAction.use5, KeyCode.Alpha5 } }; //Change the key buttons public static void ChangeKey(InputAction action, KeyCode code) { //Check if the dictionary contains the action input if (keyMaps.ContainsKey(action)) { //Sets the button value of the action input keyMaps[action] = code; } else { Debug.LogError($"The action '{action}' doesn't exist in the key map."); } } //Get key buttons public static KeyCode GetKey(InputAction action) { //Check if the dictionary contains the action input if (keyMaps.ContainsKey(action)) { //Return the button of the action input return keyMaps[action]; } else { Debug.LogError($"The action '{action}' doesn't exist in the key map."); return KeyCode.None; } } // Saves key bindings to PlayerPrefs public static void SaveKeyBinds() { foreach (KeyValuePair<InputAction, KeyCode> pair in keyMaps) { // Store each action's key binding as a string in PlayerPrefs PlayerPrefs.SetString(pair.Key.ToString(), pair.Value.ToString()); } // Ensure PlayerPrefs saves all changes PlayerPrefs.Save(); } // Loads the key bindings from PlayerPrefs public static void LoadKeyBinds() { foreach(InputAction action in Enum.GetValues(typeof(InputAction))) { // Check if a key binding for this action exists in PlayerPrefs if (PlayerPrefs.HasKey(action.ToString())) { //Retrieve the saved key binding for this action string savedKey = PlayerPrefs.GetString(action.ToString()); // Try to convert the saved string back into a KeyCode if (Enum.TryParse(savedKey, out KeyCode keyCode)) { // If successful, update the key binding in the dictionary keyMaps[action] = keyCode; } } } } }