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. 27
      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_Name:
m_EditorClassIdentifier:
inputType: 5
inputType: 0
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
key: 276
- Action: Right
- playerNumber: 1
Action: Right
inputType: 0
key: 275
- Action: Up
- playerNumber: 1
Action: Up
inputType: 0
key: 273
- Action: Down
- playerNumber: 1
Action: Down
inputType: 0
key: 274
- Action: Punch
- playerNumber: 1
Action: Punch
inputType: 0
key: 122
- Action: Kick
key: 305
- playerNumber: 1
Action: Jump
inputType: 0
key: 120
- Action: Defend
key: 303
- 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
key: 99
- Action: Jump
key: 110
- playerNumber: 3
Action: Jump
inputType: 0
key: 32
key: 109
joypadControls:
- Action: Punch
- playerNumber: 0
Action: Punch
inputType: 0
key: 331
- Action:
- playerNumber: 0
Action:
inputType: 0
key: 0
- Action: Defend
- playerNumber: 0
Action: Defend
inputType: 0
key: 333
- Action: Jump
- playerNumber: 0
Action: Jump
inputType: 0
key: 330
- Action: Run
- playerNumber: 0
Action: Run
inputType: 0
key: 335
doubleTapSpeed: 0.3

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

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

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

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

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

@ -212,7 +212,10 @@ public class PlayerMovement : MonoBehaviour {
#region controller input
//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
if(!MovementStates.Contains(playerState.currentState) || isDead) return;
@ -232,7 +235,10 @@ public class PlayerMovement : MonoBehaviour {
}
//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
PlayerCombat pc = GetComponent<PlayerCombat>();

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

@ -30,7 +30,7 @@ public class GameOverScrn : UISceneLoader {
}
//input event
private void OnInputEvent(string action, BUTTONSTATE buttonState) {
private void OnInputEvent(string action, BUTTONSTATE buttonState, int playerNumber) {
if(buttonState != BUTTONSTATE.PRESS) return;
//restart the current level
@ -54,7 +54,7 @@ public class GameOverScrn : UISceneLoader {
//alternative input events
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;
}
void OnInputEvent(string action, BUTTONSTATE buttonState){
void OnInputEvent(string action, BUTTONSTATE buttonState, int playerNumber){
if(buttonState != BUTTONSTATE.PRESS) return;
//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();
}
void OnInputEvent(string action, BUTTONSTATE buttonState){
void OnInputEvent(string action, BUTTONSTATE buttonState, int playerNumber){
//move left
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;
}
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
Sprite icon = Resources.Load<Sprite>("Icons/Icon" + action);

Loading…
Cancel
Save