Browse Source

multiple input support

master
Josh 2 years ago
parent
commit
26b8cedce9
  1. 113
      unity_project/Assets/BeatEmUp_GameTemplate3D/Resources/InputManager.prefab
  2. 44
      unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/Input/InputManager.cs
  3. 29
      unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/Player/PlayerCombat.cs
  4. 10
      unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/Player/PlayerMovement.cs
  5. 4
      unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/UI/GameOverScrn.cs
  6. 2
      unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/UI/UIButtonEvents.cs
  7. 2
      unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/UI/UICharSelection.cs
  8. 2
      unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/UI/UIInputList.cs

113
unity_project/Assets/BeatEmUp_GameTemplate3D/Resources/InputManager.prefab

@ -43,46 +43,123 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 4c6bbd85ce47f9d46990aab68c46ec0a, type: 3} m_Script: {fileID: 11500000, guid: 4c6bbd85ce47f9d46990aab68c46ec0a, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
inputType: 5 inputType: 0
keyBoardControls: keyBoardControls:
- Action: Left - playerNumber: 0
Action: Left
inputType: 0
key: 97
- playerNumber: 0
Action: Right
inputType: 0
key: 100
- playerNumber: 0
Action: Up
inputType: 0
key: 119
- playerNumber: 0
Action: Down
inputType: 0
key: 115
- playerNumber: 0
Action: Punch
inputType: 0
key: 122
- playerNumber: 0
Action: Jump
inputType: 0
key: 120
- playerNumber: 1
Action: Left
inputType: 0 inputType: 0
key: 276 key: 276
- Action: Right - playerNumber: 1
Action: Right
inputType: 0 inputType: 0
key: 275 key: 275
- Action: Up - playerNumber: 1
Action: Up
inputType: 0 inputType: 0
key: 273 key: 273
- Action: Down - playerNumber: 1
Action: Down
inputType: 0 inputType: 0
key: 274 key: 274
- Action: Punch - playerNumber: 1
Action: Punch
inputType: 0 inputType: 0
key: 122 key: 305
- Action: Kick - playerNumber: 1
Action: Jump
inputType: 0 inputType: 0
key: 120 key: 303
- Action: Defend - playerNumber: 2
Action: Up
inputType: 0
key: 105
- playerNumber: 2
Action: Down
inputType: 0
key: 107
- playerNumber: 2
Action: Left
inputType: 0
key: 106
- playerNumber: 2
Action: Right
inputType: 0
key: 108
- playerNumber: 2
Action: Punch
inputType: 0
key: 91
- playerNumber: 2
Action: Jump
inputType: 0
key: 93
- playerNumber: 3
Action: Up
inputType: 0
key: 116
- playerNumber: 3
Action: Down
inputType: 0
key: 103
- playerNumber: 3
Action: Left
inputType: 0
key: 102
- playerNumber: 3
Action: Right
inputType: 0
key: 104
- playerNumber: 3
Action: Punch
inputType: 0 inputType: 0
key: 99 key: 110
- Action: Jump - playerNumber: 3
Action: Jump
inputType: 0 inputType: 0
key: 32 key: 109
joypadControls: joypadControls:
- Action: Punch - playerNumber: 0
Action: Punch
inputType: 0 inputType: 0
key: 331 key: 331
- Action: - playerNumber: 0
Action:
inputType: 0 inputType: 0
key: 0 key: 0
- Action: Defend - playerNumber: 0
Action: Defend
inputType: 0 inputType: 0
key: 333 key: 333
- Action: Jump - playerNumber: 0
Action: Jump
inputType: 0 inputType: 0
key: 330 key: 330
- Action: Run - playerNumber: 0
Action: Run
inputType: 0 inputType: 0
key: 335 key: 335
doubleTapSpeed: 0.3 doubleTapSpeed: 0.3

44
unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/Input/InputManager.cs

@ -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();

29
unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/Player/PlayerCombat.cs

@ -73,6 +73,8 @@ public class PlayerCombat : MonoBehaviour, IDamagable<DamageObject> {
private string lastAttackInput; private string lastAttackInput;
private DIRECTION lastAttackDirection; private DIRECTION lastAttackDirection;
PlayerMovement _playerMovement;
//a list of states when the player can attack //a list of states when the player can attack
private List<UNITSTATE> AttackStates = new List<UNITSTATE> { private List<UNITSTATE> AttackStates = new List<UNITSTATE> {
UNITSTATE.IDLE, UNITSTATE.IDLE,
@ -135,6 +137,7 @@ public class PlayerCombat : MonoBehaviour, IDamagable<DamageObject> {
animator = GetComponentInChildren<UnitAnimator>(); animator = GetComponentInChildren<UnitAnimator>();
playerState = GetComponent<UnitState>(); playerState = GetComponent<UnitState>();
rb = GetComponent<Rigidbody>(); rb = GetComponent<Rigidbody>();
_playerMovement = GetComponent<PlayerMovement>();
//assign layers and layermasks //assign layers and layermasks
EnemyLayer = LayerMask.NameToLayer("Enemy"); EnemyLayer = LayerMask.NameToLayer("Enemy");
@ -189,15 +192,22 @@ public class PlayerCombat : MonoBehaviour, IDamagable<DamageObject> {
} }
//movement input event //movement input event
void OnDirectionInputEvent(Vector2 inputVector, bool doubleTapActive){ void OnDirectionInputEvent(Vector2 inputVector, bool doubleTapActive, int playerNumber)
if(!MovementStates.Contains(playerState.currentState)) return; {
if (playerNumber != _playerMovement.playerNumber)
return;
if (!MovementStates.Contains(playerState.currentState)) return;
int dir = Mathf.RoundToInt(Mathf.Sign((float)-inputVector.x)); int dir = Mathf.RoundToInt(Mathf.Sign((float)-inputVector.x));
if(Mathf.Abs(inputVector.x)>0) currentDirection = (DIRECTION)dir; if(Mathf.Abs(inputVector.x)>0) currentDirection = (DIRECTION)dir;
} }
#region Combat Input Events #region Combat Input Events
//combat input event //combat input event
private void OnInputEvent(string action, BUTTONSTATE buttonState) { private void OnInputEvent(string action, BUTTONSTATE buttonState, int playerNumber) {
if (playerNumber != _playerMovement.playerNumber)
return;
if (AttackStates.Contains (playerState.currentState) && !isDead) { if (AttackStates.Contains (playerState.currentState) && !isDead) {
//running punch //running punch
@ -270,10 +280,10 @@ public class PlayerCombat : MonoBehaviour, IDamagable<DamageObject> {
//special attack (both) //special attack (both)
if (action == "Punch" && buttonState == BUTTONSTATE.PRESS) { if (action == "Punch" && buttonState == BUTTONSTATE.PRESS) {
var pm = GetComponent<PlayerMovement>();
if (pm != null && (pm.JumpNextFixedUpdate || (pm.jumpInProgress && pm.JumpIsRecent()))) if (_playerMovement != null && (_playerMovement.JumpNextFixedUpdate || (_playerMovement.jumpInProgress && _playerMovement.JumpIsRecent())))
{ {
DoSpecialAttack(pm); DoSpecialAttack(_playerMovement);
Debug.Log("did special attack"); Debug.Log("did special attack");
return; return;
} }
@ -651,10 +661,9 @@ public class PlayerCombat : MonoBehaviour, IDamagable<DamageObject> {
TurnToDir(currentDirection); TurnToDir(currentDirection);
//update playermovement //update playermovement
var pm = GetComponent<PlayerMovement>(); if(_playerMovement != null) {
if(pm != null) { _playerMovement.CancelJump();
pm.CancelJump(); _playerMovement.SetDirection(currentDirection);
pm.SetDirection(currentDirection);
} }
//add knockback force //add knockback force

10
unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/Player/PlayerMovement.cs

@ -212,7 +212,10 @@ public class PlayerMovement : MonoBehaviour {
#region controller input #region controller input
//set current direction to input direction //set current direction to input direction
void OnDirectionInputEvent(Vector2 dir, bool doubleTapActive) { void OnDirectionInputEvent(Vector2 dir, bool doubleTapActive, int playerNum) {
if (playerNum != this.playerNumber)
return;
//ignore input when we are dead or when this state is not active //ignore input when we are dead or when this state is not active
if(!MovementStates.Contains(playerState.currentState) || isDead) return; if(!MovementStates.Contains(playerState.currentState) || isDead) return;
@ -232,7 +235,10 @@ public class PlayerMovement : MonoBehaviour {
} }
//input actions //input actions
void OnInputEvent(string action, BUTTONSTATE buttonState) { void OnInputEvent(string action, BUTTONSTATE buttonState, int playerNum) {
if (playerNum != playerNumber)
return;
//special attack (jump + attack) if jump is hit second //special attack (jump + attack) if jump is hit second
PlayerCombat pc = GetComponent<PlayerCombat>(); PlayerCombat pc = GetComponent<PlayerCombat>();

4
unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/UI/GameOverScrn.cs

@ -30,7 +30,7 @@ public class GameOverScrn : UISceneLoader {
} }
//input event //input event
private void OnInputEvent(string action, BUTTONSTATE buttonState) { private void OnInputEvent(string action, BUTTONSTATE buttonState, int playerNumber) {
if(buttonState != BUTTONSTATE.PRESS) return; if(buttonState != BUTTONSTATE.PRESS) return;
//restart the current level //restart the current level
@ -54,7 +54,7 @@ public class GameOverScrn : UISceneLoader {
//alternative input events //alternative input events
if(Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Return)){ if(Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Return)){
OnInputEvent("AnyKey", BUTTONSTATE.PRESS); OnInputEvent("AnyKey", BUTTONSTATE.PRESS, 0);
} }
} }

2
unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/UI/UIButtonEvents.cs

@ -23,7 +23,7 @@ public class UIButtonEvents : UISceneLoader, IPointerDownHandler, ISelectHandler
InputManager.onInputEvent -= OnInputEvent; InputManager.onInputEvent -= OnInputEvent;
} }
void OnInputEvent(string action, BUTTONSTATE buttonState){ void OnInputEvent(string action, BUTTONSTATE buttonState, int playerNumber){
if(buttonState != BUTTONSTATE.PRESS) return; if(buttonState != BUTTONSTATE.PRESS) return;
//only apply the following actions if this UI gameobject is currently selected //only apply the following actions if this UI gameobject is currently selected

2
unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/UI/UICharSelection.cs

@ -22,7 +22,7 @@ public class UICharSelection : UISceneLoader {
GetComponentInChildren<UICharSelectionPortrait>().OnClick(); GetComponentInChildren<UICharSelectionPortrait>().OnClick();
} }
void OnInputEvent(string action, BUTTONSTATE buttonState){ void OnInputEvent(string action, BUTTONSTATE buttonState, int playerNumber){
//move left //move left
if(action == "Left" && buttonState == BUTTONSTATE.PRESS) OnLeftButtonDown(); if(action == "Left" && buttonState == BUTTONSTATE.PRESS) OnLeftButtonDown();

2
unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/UI/UIInputList.cs

@ -13,7 +13,7 @@ public class UIInputList : MonoBehaviour {
InputManager.onInputEvent -= OnInputEvent; InputManager.onInputEvent -= OnInputEvent;
} }
void OnInputEvent(string action, BUTTONSTATE buttonState){ void OnInputEvent(string action, BUTTONSTATE buttonState, int playerNumber){
if(buttonState != BUTTONSTATE.PRESS) return; //only respond to button press states if(buttonState != BUTTONSTATE.PRESS) return; //only respond to button press states
Sprite icon = Resources.Load<Sprite>("Icons/Icon" + action); Sprite icon = Resources.Load<Sprite>("Icons/Icon" + action);

Loading…
Cancel
Save