Newer
Older
TheVengeance-Project-IADE-Unity2D / Assets / Scripts / Managers / InputManager.cs
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public enum InputAction
  6. {
  7. Interact,
  8. Attack,
  9. Defend,
  10. Run,
  11. PauseGame,
  12. Inventory,
  13. SelectItem,
  14. SplitItemStack,
  15. use1,
  16. use2,
  17. use3,
  18. use4,
  19. use5,
  20. }
  21. public static class InputManager
  22. {
  23. //Input Dictionary
  24. public static Dictionary<InputAction, KeyCode> keyMaps = new Dictionary<InputAction, KeyCode>()
  25. {
  26. //Movement
  27. //Interaction
  28. {InputAction.Interact, KeyCode.E },
  29. //Combat
  30. {InputAction.Attack, KeyCode.Mouse0 },
  31. {InputAction.Defend, KeyCode.Space },
  32. //Movement
  33. { InputAction.Run, KeyCode.LeftShift},
  34. //Menu
  35. {InputAction.PauseGame, KeyCode.Escape },
  36. {InputAction.Inventory, KeyCode.I },
  37. //Inventory Actions
  38. {InputAction.SelectItem, KeyCode.Mouse0 },
  39. {InputAction.SplitItemStack, KeyCode.Mouse1 },
  40. //Quick Inventory Actions
  41. {InputAction.use1, KeyCode.Alpha1 },
  42. {InputAction.use2, KeyCode.Alpha2 },
  43. {InputAction.use3, KeyCode.Alpha3 },
  44. {InputAction.use4, KeyCode.Alpha4 },
  45. {InputAction.use5, KeyCode.Alpha5 }
  46. };
  47. //Change the key buttons
  48. public static void ChangeKey(InputAction action, KeyCode code)
  49. {
  50. //Check if the dictionary contains the action input
  51. if (keyMaps.ContainsKey(action))
  52. {
  53. //Sets the button value of the action input
  54. keyMaps[action] = code;
  55. }
  56. else
  57. {
  58. Debug.LogError($"The action '{action}' doesn't exist in the key map.");
  59. }
  60. }
  61. //Get key buttons
  62. public static KeyCode GetKey(InputAction action)
  63. {
  64. //Check if the dictionary contains the action input
  65. if (keyMaps.ContainsKey(action))
  66. {
  67. //Return the button of the action input
  68. return keyMaps[action];
  69. }
  70. else
  71. {
  72. Debug.LogError($"The action '{action}' doesn't exist in the key map.");
  73. return KeyCode.None;
  74. }
  75. }
  76. // Saves key bindings to PlayerPrefs
  77. public static void SaveKeyBinds()
  78. {
  79. foreach (KeyValuePair<InputAction, KeyCode> pair in keyMaps)
  80. {
  81. // Store each action's key binding as a string in PlayerPrefs
  82. PlayerPrefs.SetString(pair.Key.ToString(), pair.Value.ToString());
  83. }
  84. // Ensure PlayerPrefs saves all changes
  85. PlayerPrefs.Save();
  86. }
  87. // Loads the key bindings from PlayerPrefs
  88. public static void LoadKeyBinds()
  89. {
  90. foreach(InputAction action in Enum.GetValues(typeof(InputAction)))
  91. {
  92. // Check if a key binding for this action exists in PlayerPrefs
  93. if (PlayerPrefs.HasKey(action.ToString()))
  94. {
  95. //Retrieve the saved key binding for this action
  96. string savedKey = PlayerPrefs.GetString(action.ToString());
  97. // Try to convert the saved string back into a KeyCode
  98. if (Enum.TryParse(savedKey, out KeyCode keyCode))
  99. {
  100. // If successful, update the key binding in the dictionary
  101. keyMaps[action] = keyCode;
  102. }
  103. }
  104. }
  105. }
  106. }