Advertisement
Jon50

School Invaders Clone KeyBinds

Jan 7th, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.74 KB | None | 0 0
  1. //There are four classes:
  2. // -OptionsManager
  3. // -SavingSystem
  4. // -ControlsData    - This is where the player is getting the KeyCodes.
  5. // -KeyID           - This is an id for the buttons on the options menu.
  6.  
  7. //-----OptionsManager-----//
  8. using System;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using TMPro;
  12. using UnityEngine;
  13. using UnityEngine.UI;
  14. using Invaders.Controls;
  15. using Invaders.Enums;
  16. using Invaders.MainMenu;
  17. using Invaders.Save;
  18. using System.IO;
  19.  
  20. namespace Invaders.Managers
  21. {
  22.     public class OptionsManager : MonoBehaviour
  23.     {
  24.         private bool _canEdit = true;
  25.         private Button _selectedButton = null;
  26.         private Navigation _navigation = new Navigation();
  27.         private Dictionary<KeyIDEnum, KeyCode> _controls = new Dictionary<KeyIDEnum, KeyCode>();
  28.  
  29.         private const string ControlsSaveFile = "/Controls.sav";
  30.  
  31.         private void Awake()
  32.         {
  33.             InitializeControls(ControlsSaveFile);
  34.         }
  35.  
  36.         private void InitializeControls(string saveFile)
  37.         {
  38.             if (File.Exists(SavingSystem.GetSavePath(saveFile)))
  39.             {
  40.                 _controls = SavingSystem.Load(saveFile) as Dictionary<KeyIDEnum, KeyCode>;
  41.             }
  42.             else
  43.             {
  44.                 CreateDefaultControls(saveFile);
  45.             }
  46.  
  47.             ControlsData.SetControls(_controls);
  48.         }
  49.  
  50.         private void CreateDefaultControls(string saveFile)
  51.         {
  52.             _controls.Clear();
  53.  
  54.             _controls.Add(KeyIDEnum.Left, KeyCode.LeftArrow);
  55.             _controls.Add(KeyIDEnum.Right, KeyCode.RightArrow);
  56.             _controls.Add(KeyIDEnum.Shoot, KeyCode.Space);
  57.  
  58.             SavingSystem.Save(saveFile, _controls);
  59.         }
  60.  
  61.         //ONCLICK EVENT OPTIONS BUTTON
  62.         public void LoadControlsUI()
  63.         {
  64.             ReloadControlsUI(_controls);
  65.         }
  66.  
  67.         private void ReloadControlsUI(Dictionary<KeyIDEnum, KeyCode> controls)
  68.         {
  69.             foreach(Transform t in KeyID.GetButtonsList)
  70.             {
  71.                 var buttonKeyID = t.GetComponent<KeyID>().GetKeyEnumID;
  72.                 var fieldTextUI = t.GetChild(0).GetComponent<TextMeshProUGUI>();
  73.  
  74.                 foreach(KeyIDEnum controlKeyPair in controls.Keys)
  75.                 {
  76.                     if(buttonKeyID == controlKeyPair)
  77.                     {
  78.                         fieldTextUI.text = controls[controlKeyPair].ToString().ToUpper();
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.  
  84.         //ONCLICK EVENT DEFAULT BUTTON
  85.         public void ResetDefaults()
  86.         {
  87.             CreateDefaultControls(ControlsSaveFile);
  88.             ReloadControlsUI(_controls);
  89.         }
  90.  
  91.         //ONCLICK EVENT BINDABLE KEYS BUTTONS
  92.         public void StartKeybindRoutine()
  93.         {
  94.             if (_canEdit)
  95.             {
  96.                 _canEdit = false;
  97.  
  98.                 _selectedButton = MenuManager.CurrentSelectedButton;
  99.  
  100.                 SetButtonNavigationMode(Navigation.Mode.None);
  101.  
  102.                 StartCoroutine(KeyListener());
  103.             }
  104.         }
  105.  
  106.         private IEnumerator KeyListener()
  107.         {
  108.             yield return new WaitUntil(() => Input.anyKeyDown);
  109.             yield return new WaitUntil(() => Input.anyKeyDown);
  110.  
  111.             if (Input.GetKeyDown(KeyCode.Escape))
  112.             {
  113.                 CancelEdit();
  114.                 yield break;
  115.             }
  116.  
  117.             foreach (KeyCode vKey in Enum.GetValues(typeof(KeyCode)))
  118.             {
  119.                 if (Input.GetKey(vKey))
  120.                 {
  121.                     if(_controls.ContainsValue(vKey))
  122.                     {
  123.                         CancelEdit();
  124.                         yield break;
  125.                     }
  126.  
  127.                     SetNewControls(vKey);
  128.                 }
  129.             }
  130.         }
  131.  
  132.         private void CancelEdit()
  133.         {
  134.             _canEdit = true;
  135.  
  136.             SetButtonNavigationMode(Navigation.Mode.Automatic);
  137.  
  138.             ReloadControlsUI(_controls);
  139.         }
  140.  
  141.         private void SetNewControls(KeyCode vKey)
  142.         {
  143.             var keyID = _selectedButton.GetComponent<KeyID>().GetKeyEnumID;
  144.  
  145.             _controls[keyID] = vKey;
  146.             SavingSystem.Save(ControlsSaveFile, _controls);
  147.  
  148.             _canEdit = true;
  149.  
  150.             SetButtonNavigationMode(Navigation.Mode.Automatic);
  151.  
  152.             ControlsData.SetControls(_controls);
  153.             ReloadControlsUI(_controls);
  154.         }
  155.  
  156.         private void SetButtonNavigationMode(Navigation.Mode mode)
  157.         {
  158.             _navigation.mode = mode;
  159.             _selectedButton.navigation = _navigation;
  160.         }
  161.     }
  162. }
  163.  
  164.  
  165. //-----SavingSystem-----//
  166. using UnityEngine;
  167. using System.IO;
  168. using System.Runtime.Serialization.Formatters.Binary;
  169.  
  170. namespace Invaders.Save
  171. {
  172.     public static class SavingSystem
  173.     {
  174.         public static void Save(string saveFile, object data)
  175.         {
  176.             using (FileStream stream = File.Open(GetSavePath(saveFile), FileMode.Create))
  177.             {
  178.                 BinaryFormatter formatter = new BinaryFormatter();
  179.                 formatter.Serialize(stream, data);
  180.             }
  181.         }
  182.  
  183.         public static object Load(string saveFile)
  184.         {
  185.             using (FileStream stream = File.Open(GetSavePath(saveFile), FileMode.Open))
  186.             {
  187.                 BinaryFormatter formatter = new BinaryFormatter();
  188.                 return formatter.Deserialize(stream);
  189.             }
  190.         }
  191.  
  192.         public static string GetSavePath(string saveFile)
  193.         {
  194.             return Path.Combine(Application.persistentDataPath + saveFile);
  195.         }
  196.     }
  197. }
  198.  
  199.  
  200. //-----ControlsData-----//
  201. using UnityEngine;
  202. using System.Collections.Generic;
  203. using Invaders.Enums;
  204.  
  205. namespace Invaders.Controls
  206. {
  207.     public class ControlsData
  208.     {
  209.         public static Dictionary<KeyIDEnum, KeyCode> CurrentControlsMap { get; private set; }
  210.  
  211.         public static void SetControls(Dictionary<KeyIDEnum, KeyCode> controls)
  212.         {
  213.             CurrentControlsMap = controls;
  214.         }
  215.     }
  216. }
  217.  
  218.  
  219. //-----KeyID-----//
  220. using UnityEngine;
  221. using System.Collections.Generic;
  222. using Invaders.Enums;
  223.  
  224. namespace Invaders.MainMenu
  225. {
  226.     public enum KeyIDEnum
  227.     {
  228.         None,
  229.         Left,
  230.         Right,
  231.         Shoot
  232.     }
  233.  
  234.     public class KeyID : MonoBehaviour
  235.     {
  236.         [SerializeField] private KeyIDEnum _key = KeyIDEnum.None;
  237.         public KeyIDEnum GetKeyEnumID => _key;
  238.  
  239.         private static readonly List<Transform> _buttons = new List<Transform>();
  240.         public static List<Transform> GetButtonsList { get; private set; }
  241.  
  242.         private void Awake()
  243.         {
  244.             _buttons.Add(transform);
  245.             GetButtonsList = _buttons;
  246.         }
  247.     }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement