Last active
October 12, 2019 00:34
-
-
Save BreadFish64/80187fb4f9eee21d49f8fc617a819df0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class InputManager : MonoBehaviour | |
{ | |
static readonly Vector2 x1 = new Vector2(1f, 0f); | |
public abstract class InputBase | |
{ | |
public abstract Vector2 Get(); | |
static public implicit operator Vector2(InputBase inputTypeBase){ | |
return inputTypeBase.Get(); | |
} | |
static public implicit operator float(InputBase inputTypeBase) | |
{ | |
return inputTypeBase.Get().magnitude; | |
} | |
static public implicit operator bool(InputBase inputTypeBase) | |
{ | |
return inputTypeBase.Get().magnitude != 0; | |
} | |
} | |
class KeyInput : InputBase | |
{ | |
readonly KeyCode key; | |
readonly Vector2 direction; | |
public KeyInput(KeyCode key) : this(key, x1) { } | |
public KeyInput(KeyCode key, Vector2 direction) { | |
this.key = key; | |
this.direction = direction; | |
} | |
public override Vector2 Get() | |
{ | |
return Input.GetKey(key) ? direction : Vector2.zero; | |
} | |
} | |
class Button : InputBase | |
{ | |
InputBase[] mappings; | |
public Button(params InputBase[] mappings) | |
{ | |
this.mappings = mappings; | |
} | |
public override Vector2 Get() | |
{ | |
Vector2 v; | |
foreach(var mapping in mappings) | |
{ | |
v = mapping.Get(); | |
if (v != Vector2.zero) | |
return x1; | |
} | |
return Vector2.zero; | |
} | |
} | |
public enum Inputs {Primary, Secondary, Look, Move, End} | |
InputBase[] inputMap = new InputBase[(int)Inputs.End]; | |
public InputBase GetInput(Inputs input) { return inputMap[(int)input]; } | |
public InputBase this[Inputs input] { get { return GetInput(input); } } | |
// Start is called before the first frame update | |
void Start() | |
{ | |
inputMap[(int)Inputs.Primary] = new Button(new KeyInput(KeyCode.E), new KeyInput(KeyCode.Underscore)); | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
bool E_ = this[Inputs.Primary]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment