diff --git a/unity_project/Assets/BeatEmUp_GameTemplate3D/Resources/InputManager.prefab b/unity_project/Assets/BeatEmUp_GameTemplate3D/Resources/InputManager.prefab index 2b319ec..366b9d9 100644 --- a/unity_project/Assets/BeatEmUp_GameTemplate3D/Resources/InputManager.prefab +++ b/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 diff --git a/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/Input/InputManager.cs b/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/Input/InputManager.cs index 43a7c77..8d2f3a3 100644 --- a/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/Input/InputManager.cs +++ b/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,38 +50,41 @@ 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) if(inputControl.Action == "Defend") defendKeyDown = Input.GetKey(inputControl.key); } - //send a direction event - DirectionEvent(new Vector2(x,y), doubleTapState); + //send a direction event + + 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,7 +133,8 @@ public class InputManager : MonoBehaviour { //--------------- [System.Serializable] public class InputControl { - public string Action; + public int playerNumber; + public string Action; public INPUTTYPE inputType; public KeyCode key; } @@ -170,7 +174,9 @@ public class InputManagerEditor : Editor { EditorGUILayout.LabelField("Keyboard Keys", EditorStyles.boldLabel); foreach(InputControl inputControl in inputManager.keyBoardControls){ GUILayout.BeginHorizontal(); - inputControl.Action = EditorGUILayout.TextField("Action:", inputControl.Action); + + 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(); } diff --git a/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/Player/PlayerCombat.cs b/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/Player/PlayerCombat.cs index c19ec4d..01f2f4b 100644 --- a/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/Player/PlayerCombat.cs +++ b/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/Player/PlayerCombat.cs @@ -73,8 +73,10 @@ public class PlayerCombat : MonoBehaviour, IDamagable { private string lastAttackInput; private DIRECTION lastAttackDirection; - //a list of states when the player can attack - private List AttackStates = new List { + PlayerMovement _playerMovement; + + //a list of states when the player can attack + private List AttackStates = new List { UNITSTATE.IDLE, UNITSTATE.WALK, UNITSTATE.RUN, @@ -135,9 +137,10 @@ public class PlayerCombat : MonoBehaviour, IDamagable { animator = GetComponentInChildren(); playerState = GetComponent(); rb = GetComponent(); + _playerMovement = GetComponent(); - //assign layers and layermasks - EnemyLayer = LayerMask.NameToLayer("Enemy"); + //assign layers and layermasks + EnemyLayer = LayerMask.NameToLayer("Enemy"); DestroyableObjectLayer = LayerMask.NameToLayer("DestroyableObject"); EnvironmentLayer = LayerMask.NameToLayer("Environment"); HitLayerMask = (1 << EnemyLayer) | (1 << DestroyableObjectLayer); @@ -189,15 +192,22 @@ public class PlayerCombat : MonoBehaviour, IDamagable { } //movement input event - void OnDirectionInputEvent(Vector2 inputVector, bool doubleTapActive){ - if(!MovementStates.Contains(playerState.currentState)) return; + 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; } #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 { //special attack (both) if (action == "Punch" && buttonState == BUTTONSTATE.PRESS) { - var pm = GetComponent(); - 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 { TurnToDir(currentDirection); //update playermovement - var pm = GetComponent(); - if(pm != null) { - pm.CancelJump(); - pm.SetDirection(currentDirection); + if(_playerMovement != null) { + _playerMovement.CancelJump(); + _playerMovement.SetDirection(currentDirection); } //add knockback force diff --git a/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/Player/PlayerMovement.cs b/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/Player/PlayerMovement.cs index 40ce2c1..fb2f8cd 100644 --- a/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/Player/PlayerMovement.cs +++ b/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(); diff --git a/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/UI/GameOverScrn.cs b/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/UI/GameOverScrn.cs index 0459bd3..5cf1058 100644 --- a/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/UI/GameOverScrn.cs +++ b/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); } } diff --git a/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/UI/UIButtonEvents.cs b/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/UI/UIButtonEvents.cs index f402c24..d35b835 100644 --- a/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/UI/UIButtonEvents.cs +++ b/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 diff --git a/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/UI/UICharSelection.cs b/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/UI/UICharSelection.cs index 468195f..cdb8e43 100644 --- a/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/UI/UICharSelection.cs +++ b/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/UI/UICharSelection.cs @@ -22,7 +22,7 @@ public class UICharSelection : UISceneLoader { GetComponentInChildren().OnClick(); } - void OnInputEvent(string action, BUTTONSTATE buttonState){ + void OnInputEvent(string action, BUTTONSTATE buttonState, int playerNumber){ //move left if(action == "Left" && buttonState == BUTTONSTATE.PRESS) OnLeftButtonDown(); diff --git a/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/UI/UIInputList.cs b/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/UI/UIInputList.cs index 9c9930e..436b591 100644 --- a/unity_project/Assets/BeatEmUp_GameTemplate3D/Scripts/UI/UIInputList.cs +++ b/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("Icons/Icon" + action);