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