Advertisement
schooliedee

quest 64 input manager

Apr 5th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.67 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. /// <summary>
  4. /// Simple enum to represent input types.
  5. /// </summary>
  6. public enum InputType
  7. {
  8.     Keyboard = 0,
  9.     XBox360 = 1,
  10. }
  11.  
  12. /// <summary>
  13. /// This is the singleton manager for handling player input.  It will be set
  14. /// to run before any other scripts that might utilize player input to make sure
  15. /// that input is available and up-to-date when queried.
  16. /// </summary>
  17. public class InputManager : MonoBehaviour
  18. {
  19.     // Key states.  These are intended to be used for inequalities with Down and Pressed.
  20.     //
  21.     // If a button is down, it should also be pressed.
  22.     // If a button is pressed, it may not necessarily be down this frame, but cannot be up
  23.     // If a button is up, it should not be down or pressed
  24.     // If a button is at rest / default, it should be none of these.
  25.     //
  26.     // By this, if a key state is >= Down, and similarly for >= Pressed and Up
  27.     //
  28.     private const int KEY_DEFAULT = 0;
  29.     private const int KEY_DOWN = 3;
  30.     private const int KEY_PRESSED = 2;
  31.     private const int KEY_UP = 1;
  32.  
  33.     // Deadzones for the axes.  These are a bit repetitive as the Axis definitions within Unity's InputManager editor
  34.     // have their own sensitivities and deadzones
  35.     private const float DEADZONE_LEFT = 0.2f;
  36.     private const float DEADZONE_RIGHT = 0.02f;
  37.  
  38.     // We'll keep track of which directions are being held
  39.     private static Vector2 movementInput = Vector2.zero;
  40.     private static Vector2 cameraInput = Vector2.zero;
  41.  
  42.     // We might be curious if movement was detected at all
  43.     private static bool movementDetected = false;
  44.     private static bool panningDetected = false;
  45.  
  46.     // Rather than mapping to a specific controller layout, the variables
  47.     // here will be named after the intended functionality we intend to have
  48.     // for whichever controller scheme the player is using.
  49.     private static int stateSubmit = 0;
  50.     private static int stateBack = 0;
  51.     private static int stateCheck = 0;
  52.  
  53.     // Middle buttons on most controllers
  54.     private static int stateStart = 0;
  55.     private static int stateSelect = 0;
  56.  
  57.     // Active input
  58.     private static InputType activeInput = InputType.Keyboard;
  59.  
  60.     /// <summary>
  61.     /// The update for our InputManager is set to run before any other scripts
  62.     /// via the project settings / Script Execution Order.
  63.     /// </summary>
  64.     void Update()
  65.     {
  66.         // Check if there's a controller active
  67.         InputManager.ActiveInput = InputManager.CheckInputType();
  68.  
  69.         // We'll need to capture our input here
  70.         switch (InputManager.ActiveInput)
  71.         {
  72.             default:
  73.             case InputType.Keyboard:
  74.                 InputManager.UseKeyboardInput();
  75.                 break;
  76.  
  77.             case InputType.XBox360:
  78.                 InputManager.UseXBoxInput();
  79.                 break;
  80.         }
  81.  
  82.         // Update our flags that will be used across platforms
  83.         InputManager.movementDetected = (InputManager.movementInput.sqrMagnitude >= InputManager.DEADZONE_LEFT);
  84.         InputManager.panningDetected = (InputManager.cameraInput.sqrMagnitude >= InputManager.DEADZONE_RIGHT);
  85.     }
  86.  
  87.     /// <summary>
  88.     /// Check what sort of input we're using.  Assume that a 360 controller is the intended
  89.     /// way of playing.
  90.     /// </summary>
  91.     /// <returns></returns>
  92.     private static InputType CheckInputType()
  93.     {
  94.         // This returns an array of active joystick names, but will always have
  95.         // a first element -- even if there isn't a controller plugged in.  
  96.         //
  97.         string[] joysticks = Input.GetJoystickNames();
  98.  
  99.         // We'll check if "360" is here to indicate an xbox controller
  100.         for (int k = 0; k < joysticks.Length; k++)
  101.         {
  102.             if (joysticks[k].Contains("360"))
  103.                 return InputType.XBox360;
  104.         }
  105.  
  106.         // We'll fix this later
  107.         return InputType.Keyboard;
  108.     }
  109.  
  110.     /// <summary>
  111.     /// Updates the input values with the keyboard layout.
  112.     /// </summary>
  113.     private static void UseKeyboardInput()
  114.     {
  115.         // Movement
  116.         InputManager.movementInput.x = InputManager.KeyAxis(KeyCode.D, KeyCode.A);
  117.         InputManager.movementInput.y = InputManager.KeyAxis(KeyCode.W, KeyCode.S);
  118.         InputManager.movementInput.Normalize();
  119.  
  120.         // Camera Input
  121.         //
  122.         // For the keyboard layout, I don't want the mouse to always move the camera as
  123.         // that will get annoying quickly as we learned with Dark Souls.  The camera doesn't
  124.         // always need to move, but we want it to behave similarly to map UIs.
  125.         //
  126.         if (Input.GetMouseButton(ControlCodes.MOUSE_RIGHT))
  127.         {
  128.             InputManager.cameraInput.x = Input.GetAxis("Mouse X");
  129.             InputManager.cameraInput.y = Input.GetAxis("Mouse Y");
  130.         }
  131.         else
  132.         {
  133.             InputManager.cameraInput = Vector2.zero;
  134.         }
  135.  
  136.         // Button actions
  137.         InputManager.stateBack = InputManager.KeyState(KeyCode.Escape);
  138.         InputManager.stateSubmit = InputManager.KeyState(KeyCode.E);
  139.         InputManager.stateStart = InputManager.KeyState(KeyCode.Escape);
  140.         InputManager.stateSelect = InputManager.KeyState(KeyCode.Tab);
  141.     }
  142.  
  143.     /// <summary>
  144.     /// Updates input values with the XBox 360 controller layout.
  145.     /// </summary>
  146.     private static void UseXBoxInput()
  147.     {
  148.         // Since the XBox 360 buttons are bound to different keycodes across platforms,
  149.         // we need to check compile directives when mapping these.
  150.         InputManager.movementInput.x = Input.GetAxis(InputNames.LEFT_AXIS_X);
  151.         InputManager.movementInput.y = Input.GetAxis(InputNames.LEFT_AXIS_Y);
  152.  
  153. #if UNITY_STANDALONE_WIN
  154.  
  155.         InputManager.cameraInput.x = Input.GetAxis(InputNames.RIGHT_AXIS_X_WINDOWS);
  156.         InputManager.cameraInput.y = Input.GetAxis(InputNames.RIGHT_AXIS_Y_WINDOWS);
  157.  
  158.         InputManager.stateBack = InputManager.KeyState(ControlCodes.XBOX_WIN_B);
  159.         InputManager.stateSubmit = InputManager.KeyState(ControlCodes.XBOX_WIN_A);
  160.         InputManager.stateStart = InputManager.KeyState(ControlCodes.XBOX_WIN_START);
  161.         InputManager.stateSelect = InputManager.KeyState(ControlCodes.XBOX_WIN_BACK);
  162. #endif
  163.  
  164. #if UNITY_STANDALONE_OSX
  165.  
  166.         InputManager.cameraInput.x = Input.GetAxis(InputNames.RIGHT_AXIS_X_OSX);
  167.         InputManager.cameraInput.y = Input.GetAxis(InputNames.RIGHT_AXIS_Y_OSX);
  168.  
  169.         InputManager.stateBack = InputManager.KeyState(ControlCodes.XBOX_OSX_B);
  170.         InputManager.stateSubmit = InputManager.KeyState(ControlCodes.XBOX_OSX_A);
  171.         InputManager.stateStart = InputManager.KeyState(ControlCodes.XBOX_OSX_START);
  172.         InputManager.stateSelect = InputManager.KeyState(ControlCodes.XBOX_OSX_BACK);
  173. #endif
  174.  
  175. #if UNITY_STANDALONE_LINUX
  176.        
  177.         InputManager.cameraInput.x = Input.GetAxis(InputNames.RIGHT_AXIS_X_LINUX);
  178.         InputManager.cameraInput.y = Input.GetAxis(InputNames.RIGHT_AXIS_Y_LINUX);
  179.  
  180.         InputManager.stateBack = InputManager.KeyState(ControlCodes.XBOX_LINUX_B);
  181.         InputManager.stateSubmit = InputManager.KeyState(ControlCodes.XBOX_LINUX_A);
  182.         InputManager.stateStart = InputManager.KeyState(ControlCodes.XBOX_LINUX_START);
  183.         InputManager.stateSelect = InputManager.KeyState(ControlCodes.XBOX_LINUX_BACK);
  184. #endif
  185.     }
  186.  
  187.     /// <summary>
  188.     /// Simulates a float-style axis using button inputs.
  189.     /// </summary>
  190.     /// <param name="positive"></param>
  191.     /// <param name="negative"></param>
  192.     /// <returns></returns>
  193.     public static float KeyAxis(KeyCode positive, KeyCode negative)
  194.     {
  195.         int p = Input.GetKey(positive) ? 1 : 0;
  196.         int b = Input.GetKey(negative) ? 1 : 0;
  197.         return p - b;
  198.     }
  199.  
  200.     /// <summary>
  201.     /// Returns an integer indicating the state of this key.  This can also be used to get controller
  202.     /// inputs, provided the correct enumerated joystick button is used (i.e. KeyCode.Joystick1Button8)
  203.     ///
  204.     /// 0 == Not Pressed or UP this frame
  205.     /// 1 == PRESSED this frame
  206.     /// 2 == PRESSED
  207.     /// 3 == UP this frame
  208.     ///
  209.     /// </summary>
  210.     /// <param name="key"></param>
  211.     /// <returns></returns>
  212.     private static int KeyState(KeyCode key)
  213.     {
  214.         if (Input.GetKeyDown(key))
  215.             return InputManager.KEY_DOWN;
  216.         else if (Input.GetKey(key))
  217.             return InputManager.KEY_PRESSED;
  218.         else if (Input.GetKeyUp(key))
  219.             return InputManager.KEY_UP;
  220.         else
  221.             return InputManager.KEY_DEFAULT;
  222.     }
  223.  
  224.     /// <summary>
  225.     /// Returns the normalized Vector3 of player movement.  This will return the zero vector
  226.     /// if no movement was detected.
  227.     /// </summary>
  228.     public static Vector3 MovementInput
  229.     {
  230.         get { return movementInput; }
  231.     }
  232.  
  233.     /// <summary>
  234.     /// Returns whether or not the player is using the movement axis.
  235.     /// </summary>
  236.     public static bool MovementDetected
  237.     {
  238.         get { return movementDetected; }
  239.     }
  240.  
  241.     /// <summary>
  242.     /// Returns the normalized Vector3 of player movement.  This will return the zero vector
  243.     /// if no movement was detected.
  244.     /// </summary>
  245.     public static Vector3 CameraInput
  246.     {
  247.         get { return cameraInput; }
  248.     }
  249.  
  250.     /// <summary>
  251.     /// Returns whether or not the player attempted to pan the camera.
  252.     /// </summary>
  253.     public static bool PanningDetected
  254.     {
  255.         get { return panningDetected; }
  256.     }
  257.  
  258.     /// <summary>
  259.     /// Gets the active input method currently detected.
  260.     /// </summary>
  261.     public static InputType ActiveInput
  262.     {
  263.         get { return InputManager.activeInput; }
  264.         private set
  265.         {
  266.             if (InputManager.activeInput != value)
  267.             {
  268.                 InputManager.activeInput = value;
  269.                 InputManager.OnActiveInputChanged();
  270.             }
  271.         }
  272.     }
  273.  
  274.     /// <summary>
  275.     /// Called when the user's input method has changed.
  276.     /// </summary>
  277.     private static void OnActiveInputChanged()
  278.     {
  279.         // Just call it for now, eventually we'll change the scriptable objects
  280.         // determining their control scheme.
  281.     }
  282.  
  283.     /// <summary>
  284.     /// Helper values to keep information about how Unity maps the various controller
  285.     /// inputs to its Input system.
  286.     /// </summary>
  287.     public static class ControlCodes
  288.     {
  289.         // Mouse
  290.         public const int MOUSE_LEFT = 0;
  291.         public const int MOUSE_RIGHT = 1;
  292.         public const int MOUSE_MIDDLE = 2;
  293.  
  294.         // Windows
  295.         public const KeyCode XBOX_WIN_A = KeyCode.Joystick1Button0;
  296.         public const KeyCode XBOX_WIN_B = KeyCode.Joystick1Button1;
  297.         public const KeyCode XBOX_WIN_X = KeyCode.Joystick1Button2;
  298.         public const KeyCode XBOX_WIN_Y = KeyCode.Joystick1Button3;
  299.         public const KeyCode XBOX_WIN_LB = KeyCode.Joystick1Button4;
  300.         public const KeyCode XBOX_WIN_RB = KeyCode.Joystick1Button5;
  301.         public const KeyCode XBOX_WIN_BACK = KeyCode.Joystick1Button6;
  302.         public const KeyCode XBOX_WIN_START = KeyCode.Joystick1Button7;
  303.         public const KeyCode XBOX_WIN_LEFT_STICK_CLICK = KeyCode.Joystick1Button8;
  304.         public const KeyCode XBOX_WIN_RIGHT_STICK_CLICK = KeyCode.Joystick1Button9;
  305.  
  306.         // OSX
  307.         public const KeyCode XBOX_OSX_A = KeyCode.Joystick1Button16;
  308.         public const KeyCode XBOX_OSX_B = KeyCode.Joystick1Button17;
  309.         public const KeyCode XBOX_OSX_X = KeyCode.Joystick1Button18;
  310.         public const KeyCode XBOX_OSX_Y = KeyCode.Joystick1Button19;
  311.         public const KeyCode XBOX_OSX_LB = KeyCode.Joystick1Button13;
  312.         public const KeyCode XBOX_OSX_RB = KeyCode.Joystick1Button14;
  313.         public const KeyCode XBOX_OSX_BACK = KeyCode.Joystick1Button10;
  314.         public const KeyCode XBOX_OSX_START = KeyCode.Joystick1Button9;
  315.         public const KeyCode XBOX_OSX_LEFT_STICK_CLICK = KeyCode.Joystick1Button11;
  316.         public const KeyCode XBOX_OSX_RIGHT_STICK_CLICK = KeyCode.Joystick1Button12;
  317.  
  318.         // Linux
  319.         public const KeyCode XBOX_LINUX_A = KeyCode.Joystick1Button0;
  320.         public const KeyCode XBOX_LINUX_B = KeyCode.Joystick1Button1;
  321.         public const KeyCode XBOX_LINUX_X = KeyCode.Joystick1Button2;
  322.         public const KeyCode XBOX_LINUX_Y = KeyCode.Joystick1Button3;
  323.         public const KeyCode XBOX_LINUX_LB = KeyCode.Joystick1Button4;
  324.         public const KeyCode XBOX_LINUX_RB = KeyCode.Joystick1Button5;
  325.         public const KeyCode XBOX_LINUX_BACK = KeyCode.Joystick1Button6;
  326.         public const KeyCode XBOX_LINUX_START = KeyCode.Joystick1Button7;
  327.         public const KeyCode XBOX_LINUX_LEFT_STICK_CLICK = KeyCode.Joystick1Button9;
  328.         public const KeyCode XBOX_LINUX_RIGHT_STICK_CLICK = KeyCode.Joystick1Button10;
  329.     }
  330.  
  331.     /// <summary>
  332.     /// Helper values to standardize what we've named certain things in the editor's InputManager.
  333.     ///
  334.     /// As Axes are the only thing that still need manual configuration, most of these values will
  335.     /// be names of the axes.
  336.     /// </summary>
  337.     public static class InputNames
  338.     {
  339.         // Axes
  340.         public const string RIGHT_AXIS_X_WINDOWS = "Right X Windows";
  341.         public const string RIGHT_AXIS_Y_WINDOWS = "Right Y Windows";
  342.         public const string RIGHT_AXIS_X_OSX = "Right X OSX";
  343.         public const string RIGHT_AXIS_Y_OSX = "Right Y OSX";
  344.         public const string RIGHT_AXIS_X_LINUX = "Right X Linux";
  345.         public const string RIGHT_AXIS_Y_LINUX = "Right Y Linux";
  346.  
  347.         // The left stick is always the same, so it only needs one entry in the InputManager.asset
  348.         public const string LEFT_AXIS_X = "Left X";
  349.         public const string LEFT_AXIS_Y = "Left Y";
  350.     }
  351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement