|
|
@ -18,9 +18,9 @@ public class InputManager : MonoBehaviour { |
|
|
|
private string lastInputAction = ""; |
|
|
|
|
|
|
|
//delegates
|
|
|
|
public delegate void DirectionInputEventHandler(Vector2 dir, bool doubleTapActive); |
|
|
|
public delegate void DirectionInputEventHandler(Vector2 dir, bool doubleTapActive, int playerNumber); |
|
|
|
public static event DirectionInputEventHandler onDirectionInputEvent; |
|
|
|
public delegate void InputEventHandler(string action, BUTTONSTATE buttonState); |
|
|
|
public delegate void InputEventHandler(string action, BUTTONSTATE buttonState, int playerNumber); |
|
|
|
public static event InputEventHandler onInputEvent; |
|
|
|
|
|
|
|
[Space(15)] |
|
|
@ -35,8 +35,8 @@ public class InputManager : MonoBehaviour { |
|
|
|
#endif
|
|
|
|
} |
|
|
|
|
|
|
|
public static void DirectionEvent(Vector2 dir, bool doubleTapActive){ |
|
|
|
if( onDirectionInputEvent != null) onDirectionInputEvent(dir, doubleTapActive); |
|
|
|
public static void DirectionEvent(Vector2 dir, bool doubleTapActive, int playerNumber){ |
|
|
|
if( onDirectionInputEvent != null) onDirectionInputEvent(dir, doubleTapActive, playerNumber); |
|
|
|
} |
|
|
|
|
|
|
|
void Update(){ |
|
|
@ -50,30 +50,29 @@ public class InputManager : MonoBehaviour { |
|
|
|
} |
|
|
|
|
|
|
|
void KeyboardControls(){ |
|
|
|
float x = 0; |
|
|
|
float y = 0; |
|
|
|
bool doubleTapState = false; |
|
|
|
bool[] doubleTapState = new bool[4]; |
|
|
|
Vector2[] playerMovement = new Vector2[4]; |
|
|
|
|
|
|
|
foreach(InputControl inputControl in keyBoardControls){ |
|
|
|
if(onInputEvent == null) return; |
|
|
|
|
|
|
|
//on keyboard key down
|
|
|
|
if(Input.GetKeyDown(inputControl.key)){ |
|
|
|
doubleTapState = DetectDoubleTap(inputControl.Action); |
|
|
|
onInputEvent(inputControl.Action, BUTTONSTATE.PRESS); |
|
|
|
doubleTapState[inputControl.playerNumber] = DetectDoubleTap(inputControl.Action); |
|
|
|
onInputEvent(inputControl.Action, BUTTONSTATE.PRESS, inputControl.playerNumber); |
|
|
|
} |
|
|
|
|
|
|
|
//on keyboard key up
|
|
|
|
if(Input.GetKeyUp(inputControl.key)){ |
|
|
|
onInputEvent(inputControl.Action, BUTTONSTATE.RELEASE); |
|
|
|
onInputEvent(inputControl.Action, BUTTONSTATE.RELEASE, inputControl.playerNumber); |
|
|
|
} |
|
|
|
|
|
|
|
//convert keyboard direction keys to x,y values (every frame)
|
|
|
|
if(Input.GetKey(inputControl.key)){ |
|
|
|
if(inputControl.Action == "Left") x = -1f; |
|
|
|
else if(inputControl.Action == "Right") x = 1f; |
|
|
|
else if(inputControl.Action == "Up") y = 1; |
|
|
|
else if(inputControl.Action == "Down") y = -1; |
|
|
|
if(inputControl.Action == "Left") playerMovement[inputControl.playerNumber].x = -1f; |
|
|
|
else if(inputControl.Action == "Right") playerMovement[inputControl.playerNumber].x = 1f; |
|
|
|
else if(inputControl.Action == "Up") playerMovement[inputControl.playerNumber].y = 1; |
|
|
|
else if(inputControl.Action == "Down") playerMovement[inputControl.playerNumber].y = -1; |
|
|
|
} |
|
|
|
|
|
|
|
//defend key exception (checks the defend state every frame)
|
|
|
@ -81,7 +80,11 @@ public class InputManager : MonoBehaviour { |
|
|
|
} |
|
|
|
|
|
|
|
//send a direction event
|
|
|
|
DirectionEvent(new Vector2(x,y), doubleTapState); |
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++) |
|
|
|
{ |
|
|
|
DirectionEvent(playerMovement[i], doubleTapState[i], i); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
void JoyPadControls(){ |
|
|
@ -89,7 +92,7 @@ public class InputManager : MonoBehaviour { |
|
|
|
|
|
|
|
//on Joypad button press
|
|
|
|
foreach(InputControl inputControl in joypadControls){ |
|
|
|
if(Input.GetKeyDown(inputControl.key)) onInputEvent(inputControl.Action,BUTTONSTATE.PRESS); |
|
|
|
if(Input.GetKeyDown(inputControl.key)) onInputEvent(inputControl.Action,BUTTONSTATE.PRESS, 0); |
|
|
|
|
|
|
|
//defend key exception (checks the defend state every frame)
|
|
|
|
if(inputControl.Action == "Defend") defendKeyDown = Input.GetKey(inputControl.key); |
|
|
@ -100,12 +103,12 @@ public class InputManager : MonoBehaviour { |
|
|
|
float y = Input.GetAxis("Joypad Up-Down"); |
|
|
|
|
|
|
|
//send a direction event
|
|
|
|
DirectionEvent(new Vector2(x,y).normalized, false); |
|
|
|
DirectionEvent(new Vector2(x,y).normalized, false, 0); |
|
|
|
} |
|
|
|
|
|
|
|
//this function is called when a touch screen button is pressed
|
|
|
|
public void OnTouchScreenInputEvent(string action, BUTTONSTATE buttonState){ |
|
|
|
onInputEvent(action, buttonState); |
|
|
|
onInputEvent(action, buttonState, 0); |
|
|
|
|
|
|
|
//defend exception
|
|
|
|
if(action == "Defend") defendKeyDown = (buttonState == BUTTONSTATE.PRESS); |
|
|
@ -113,7 +116,7 @@ public class InputManager : MonoBehaviour { |
|
|
|
|
|
|
|
//this function is used for the touch screen thumb-stick
|
|
|
|
public void OnTouchScreenJoystickEvent(Vector2 joystickDir){ |
|
|
|
DirectionEvent(joystickDir.normalized, false); |
|
|
|
DirectionEvent(joystickDir.normalized, false, 0); |
|
|
|
} |
|
|
|
|
|
|
|
//returns true if a key double tap is detected
|
|
|
@ -130,6 +133,7 @@ public class InputManager : MonoBehaviour { |
|
|
|
//---------------
|
|
|
|
[System.Serializable] |
|
|
|
public class InputControl { |
|
|
|
public int playerNumber; |
|
|
|
public string Action; |
|
|
|
public INPUTTYPE inputType; |
|
|
|
public KeyCode key; |
|
|
@ -170,6 +174,8 @@ public class InputManagerEditor : Editor { |
|
|
|
EditorGUILayout.LabelField("Keyboard Keys", EditorStyles.boldLabel); |
|
|
|
foreach(InputControl inputControl in inputManager.keyBoardControls){ |
|
|
|
GUILayout.BeginHorizontal(); |
|
|
|
|
|
|
|
inputControl.playerNumber = EditorGUILayout.IntField("Player:", inputControl.playerNumber); |
|
|
|
inputControl.Action = EditorGUILayout.TextField("Action:", inputControl.Action); |
|
|
|
inputControl.key = (KeyCode)EditorGUILayout.EnumPopup("Key:", inputControl.key, GUILayout.Width(350)); |
|
|
|
GUILayout.EndHorizontal(); |
|
|
|