Commit e860e653 by Tim Reiter

Added Startmenu, added GamepadInput.

parent c70aa22f
fileFormatVersion: 2
guid: 1f23681ab1d1026439c0c97388d33fcd
folderAsset: yes
timeCreated: 1433510547
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
//Author: Richard Pieterse
//Date: 16 May 2013
//Email: Merrik44@live.com
using UnityEngine;
using System.Collections;
namespace GamepadInput
{
public static class GamePad
{
public enum Button { A, B, Y, X, RightShoulder, LeftShoulder, RightStick, LeftStick, Back, Start }
public enum Trigger { LeftTrigger, RightTrigger }
public enum Axis { LeftStick, RightStick, Dpad }
public enum Index { Any, One, Two, Three, Four }
public static bool GetButtonDown(Button button, Index controlIndex)
{
KeyCode code = GetKeycode(button, controlIndex);
return Input.GetKeyDown(code);
}
public static bool GetButtonUp(Button button, Index controlIndex)
{
KeyCode code = GetKeycode(button, controlIndex);
return Input.GetKeyUp(code);
}
public static bool GetButton(Button button, Index controlIndex)
{
KeyCode code = GetKeycode(button, controlIndex);
return Input.GetKey(code);
}
/// <summary>
/// returns a specified axis
/// </summary>
/// <param name="axis">One of the analogue sticks, or the dpad</param>
/// <param name="controlIndex">The controller number</param>
/// <param name="raw">if raw is false then the controlIndex will be returned with a deadspot</param>
/// <returns></returns>
public static Vector2 GetAxis(Axis axis, Index controlIndex, bool raw = false)
{
string xName = "", yName = "";
switch (axis)
{
case Axis.Dpad:
xName = "DPad_XAxis_" + (int)controlIndex;
yName = "DPad_YAxis_" + (int)controlIndex;
break;
case Axis.LeftStick:
xName = "L_XAxis_" + (int)controlIndex;
yName = "L_YAxis_" + (int)controlIndex;
break;
case Axis.RightStick:
xName = "R_XAxis_" + (int)controlIndex;
yName = "R_YAxis_" + (int)controlIndex;
break;
}
Vector2 axisXY = Vector3.zero;
try
{
if (raw == false)
{
axisXY.x = Input.GetAxis(xName);
axisXY.y = -Input.GetAxis(yName);
}
else
{
axisXY.x = Input.GetAxisRaw(xName);
axisXY.y = -Input.GetAxisRaw(yName);
}
}
catch (System.Exception e)
{
Debug.LogError(e);
Debug.LogWarning("Have you set up all axes correctly? \nThe easiest solution is to replace the InputManager.asset with version located in the GamepadInput package. \nWarning: do so will overwrite any existing input");
}
return axisXY;
}
public static float GetTrigger(Trigger trigger, Index controlIndex, bool raw = false)
{
//
string name = "";
if (trigger == Trigger.LeftTrigger)
name = "TriggersL_" + (int)controlIndex;
else if (trigger == Trigger.RightTrigger)
name = "TriggersR_" + (int)controlIndex;
//
float axis = 0;
try
{
if (raw == false)
axis = Input.GetAxis(name);
else
axis = Input.GetAxisRaw(name);
}
catch (System.Exception e)
{
Debug.LogError(e);
Debug.LogWarning("Have you set up all axes correctly? \nThe easiest solution is to replace the InputManager.asset with version located in the GamepadInput package. \nWarning: do so will overwrite any existing input");
}
return axis;
}
static KeyCode GetKeycode(Button button, Index controlIndex)
{
switch (controlIndex)
{
case Index.One:
switch (button)
{
case Button.A: return KeyCode.Joystick1Button0;
case Button.B: return KeyCode.Joystick1Button1;
case Button.X: return KeyCode.Joystick1Button2;
case Button.Y: return KeyCode.Joystick1Button3;
case Button.RightShoulder: return KeyCode.Joystick1Button5;
case Button.LeftShoulder: return KeyCode.Joystick1Button4;
case Button.Back: return KeyCode.Joystick1Button6;
case Button.Start: return KeyCode.Joystick1Button7;
case Button.LeftStick: return KeyCode.Joystick1Button8;
case Button.RightStick: return KeyCode.Joystick1Button9;
}
break;
case Index.Two:
switch (button)
{
case Button.A: return KeyCode.Joystick2Button0;
case Button.B: return KeyCode.Joystick2Button1;
case Button.X: return KeyCode.Joystick2Button2;
case Button.Y: return KeyCode.Joystick2Button3;
case Button.RightShoulder: return KeyCode.Joystick2Button5;
case Button.LeftShoulder: return KeyCode.Joystick2Button4;
case Button.Back: return KeyCode.Joystick2Button6;
case Button.Start: return KeyCode.Joystick2Button7;
case Button.LeftStick: return KeyCode.Joystick2Button8;
case Button.RightStick: return KeyCode.Joystick2Button9;
}
break;
case Index.Three:
switch (button)
{
case Button.A: return KeyCode.Joystick3Button0;
case Button.B: return KeyCode.Joystick3Button1;
case Button.X: return KeyCode.Joystick3Button2;
case Button.Y: return KeyCode.Joystick3Button3;
case Button.RightShoulder: return KeyCode.Joystick3Button5;
case Button.LeftShoulder: return KeyCode.Joystick3Button4;
case Button.Back: return KeyCode.Joystick3Button6;
case Button.Start: return KeyCode.Joystick3Button7;
case Button.LeftStick: return KeyCode.Joystick3Button8;
case Button.RightStick: return KeyCode.Joystick3Button9;
}
break;
case Index.Four:
switch (button)
{
case Button.A: return KeyCode.Joystick4Button0;
case Button.B: return KeyCode.Joystick4Button1;
case Button.X: return KeyCode.Joystick4Button2;
case Button.Y: return KeyCode.Joystick4Button3;
case Button.RightShoulder: return KeyCode.Joystick4Button5;
case Button.LeftShoulder: return KeyCode.Joystick4Button4;
case Button.Back: return KeyCode.Joystick4Button6;
case Button.Start: return KeyCode.Joystick4Button7;
case Button.LeftStick: return KeyCode.Joystick4Button8;
case Button.RightStick: return KeyCode.Joystick4Button9;
}
break;
case Index.Any:
switch (button)
{
case Button.A: return KeyCode.JoystickButton0;
case Button.B: return KeyCode.JoystickButton1;
case Button.X: return KeyCode.JoystickButton2;
case Button.Y: return KeyCode.JoystickButton3;
case Button.RightShoulder: return KeyCode.JoystickButton5;
case Button.LeftShoulder: return KeyCode.JoystickButton4;
case Button.Back: return KeyCode.JoystickButton6;
case Button.Start: return KeyCode.JoystickButton7;
case Button.LeftStick: return KeyCode.JoystickButton8;
case Button.RightStick: return KeyCode.JoystickButton9;
}
break;
}
return KeyCode.None;
}
public static GamepadState GetState(Index controlIndex, bool raw = false)
{
GamepadState state = new GamepadState();
state.A = GetButton(Button.A, controlIndex);
state.B = GetButton(Button.B, controlIndex);
state.Y = GetButton(Button.Y, controlIndex);
state.X = GetButton(Button.X, controlIndex);
state.RightShoulder = GetButton(Button.RightShoulder, controlIndex);
state.LeftShoulder = GetButton(Button.LeftShoulder, controlIndex);
state.RightStick = GetButton(Button.RightStick, controlIndex);
state.LeftStick = GetButton(Button.LeftStick, controlIndex);
state.Start = GetButton(Button.Start, controlIndex);
state.Back = GetButton(Button.Back, controlIndex);
state.LeftStickAxis = GetAxis(Axis.LeftStick, controlIndex, raw);
state.rightStickAxis = GetAxis(Axis.RightStick, controlIndex, raw);
state.dPadAxis = GetAxis(Axis.Dpad, controlIndex, raw);
state.Left = (state.dPadAxis.x < 0);
state.Right = (state.dPadAxis.x > 0);
state.Up = (state.dPadAxis.y > 0);
state.Down = (state.dPadAxis.y < 0);
state.LeftTrigger = GetTrigger(Trigger.LeftTrigger, controlIndex, raw);
state.RightTrigger = GetTrigger(Trigger.RightTrigger, controlIndex, raw);
return state;
}
}
public class GamepadState
{
public bool A = false;
public bool B = false;
public bool X = false;
public bool Y = false;
public bool Start = false;
public bool Back = false;
public bool Left = false;
public bool Right = false;
public bool Up = false;
public bool Down = false;
public bool LeftStick = false;
public bool RightStick = false;
public bool RightShoulder = false;
public bool LeftShoulder = false;
public Vector2 LeftStickAxis = Vector2.zero;
public Vector2 rightStickAxis = Vector2.zero;
public Vector2 dPadAxis = Vector2.zero;
public float LeftTrigger = 0;
public float RightTrigger = 0;
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: e586ff2a35082fc4a8e925f9f885e4a9
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 4eef7aa2d9929164f90205736c8f038e
timeCreated: 1433516677
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 35b793df4d97b407088a52900b7b2e30
TextScriptImporter:
userData:
......@@ -12,6 +12,10 @@ public class GameMaster : MonoBehaviour {
// Use this for initialization
void Start () {
levelLoader = new DeserializedLevelsLoader();
}
public void loadLevel()
{
levelLoader.loadLevel();
}
......
......@@ -27,14 +27,17 @@ public class GameStateTracker : MonoBehaviour {
void Start () {
gameMaster = GetComponent<GameMaster>();
}
public void reset()
{
ranking.Clear();
}
public void setNumberOfPlayers(int players)
{
numberOfPlayers = players;
}
public void playerDied(string name)
{
if (!ranking.Contains(name))
......
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using GamepadInput;
using UnityEngine.UI;
public class Menu : MonoBehaviour
{
public GameStateTracker gameStateTracker;
public GameMaster gameMaster;
public GameObject menuPanel;
public GameObject p1Image, p2Image, p3Image, p4Image;
public List<PlayerInputMapping> inputMappings;
private int numberOfRegisteredPlayers;
private bool WASDAdded, ArrowAdded, GP1Added, GP2Added, GP3Added, GP4Added;
void Start()
{
#region init bools
WASDAdded = false;
ArrowAdded = false;
GP1Added = false;
GP2Added = false;
GP3Added = false;
GP4Added = false;
#endregion
numberOfRegisteredPlayers = 0;
inputMappings = new List<PlayerInputMapping>();
for (int i = 1; i < 5; i++)
{
inputMappings.Add(GameObject.Find("InputMapping" + i).GetComponent<PlayerInputMapping>());
}
}
//returns true if game was started;
private bool startGameIfPossible()
{
if (numberOfRegisteredPlayers > 1 || (numberOfRegisteredPlayers > 0 && Debug.isDebugBuild))
{
gameStateTracker.setNumberOfPlayers(numberOfRegisteredPlayers);
gameStateTracker.reset();
gameMaster.loadLevel();
gameObject.SetActive(false);
return true;
}
else return false;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (WASDAdded)
{
if (startGameIfPossible())
{
return;
}
}
else
{
int playerNumber = addPlayer();
if (playerNumber > 0)
{
inputMappings[playerNumber-1].set(controlType.WASD);
WASDAdded = true;
}
}
}
if (Input.GetKeyDown(KeyCode.RightControl))
{
if (ArrowAdded)
{
if (startGameIfPossible())
{
return;
}
}
else
{
int playerNumber = addPlayer();
if (playerNumber > 0)
{
ArrowAdded = true;
inputMappings[playerNumber-1].set(controlType.ARROWS);
}
}
}
if (GamePad.GetButton(GamePad.Button.A, GamePad.Index.One))
{
if (GP1Added)
{
if (startGameIfPossible())
{
return;
}
}
else
{
int playerNumber = addPlayer();
if (playerNumber > 0)
{
GP1Added = true;
inputMappings[playerNumber-1].set(controlType.GAMEPAD1);
}
}
}
if (GamePad.GetButton(GamePad.Button.A, GamePad.Index.Two))
{
if (GP2Added)
{
if (startGameIfPossible())
{
return;
}
}
else
{
int playerNumber = addPlayer();
if (playerNumber > 0)
{
GP2Added = true;
inputMappings[playerNumber-1].set(controlType.GAMEPAD2);
}
}
}
if (GamePad.GetButton(GamePad.Button.A, GamePad.Index.Three))
{
if (GP3Added)
{
if (startGameIfPossible())
{
return;
}
}
else
{
int playerNumber = addPlayer();
if (playerNumber > 0)
{
GP3Added = true;
inputMappings[playerNumber-1].set(controlType.GAMEPAD3);
}
}
}
if (GamePad.GetButton(GamePad.Button.A, GamePad.Index.Four))
{
if (GP4Added)
{
if (startGameIfPossible())
{
return;
}
}
else
{
int playerNumber = addPlayer();
if (playerNumber > 0)
{
GP4Added = true;
inputMappings[playerNumber-1].set(controlType.GAMEPAD4);
}
}
}
}
//returns the player number (3 for player three). returns -1 when there is no free player slot.
private int addPlayer()
{
if (!p1Image.activeSelf)
{
p1Image.SetActive(true);
numberOfRegisteredPlayers = 1;
return 1;
}
else if (!p2Image.activeSelf)
{
p2Image.SetActive(true);
numberOfRegisteredPlayers = 2;
return 2;
}
else if (!p3Image.activeSelf)
{
p3Image.SetActive(true);
numberOfRegisteredPlayers = 3;
return 3;
}
else if (!p4Image.activeSelf)
{
p4Image.SetActive(true);
numberOfRegisteredPlayers = 4;
return 4;
}
else return -1;
}
}
fileFormatVersion: 2
guid: 776fe13068328144dabcce46566255c3
timeCreated: 1433510398
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

using UnityEngine;
using System.Collections;
using GamepadInput;
public class PlayerControl : MonoBehaviour
{
private enum Keys : int { Left = 0, Right, Up, Down, Jump, Dash, Shoot };
private Rigidbody2D body2D;
private Vector3 lastPos; // we make sure the last Pos is never the curPos.
private Vector2 moveDirection;
......@@ -46,40 +46,19 @@ public class PlayerControl : MonoBehaviour
public Vector3 velocity;
public int playerNumber = 1; //gibt an, ob es sich um player one, player two, etc. handelt. sollte nicht 0 sein;
public PlayerInputMapping inputMapping;
private controlType controls;
KeyCode[] keyCodes = new KeyCode[Keys.GetNames(typeof(Keys)).Length];
void Start()
{
body2D = GetComponent<Rigidbody2D>();
switch (playerNumber)
{
case 1:
keyCodes[(int)Keys.Right] = KeyCode.D;
keyCodes[(int)Keys.Left] = KeyCode.A;
keyCodes[(int)Keys.Up] = KeyCode.W;
keyCodes[(int)Keys.Jump] = keyCodes[(int)Keys.Up];
keyCodes[(int)Keys.Down] = KeyCode.S;
keyCodes[(int)Keys.Dash] = KeyCode.Space;
keyCodes[(int)Keys.Shoot] = KeyCode.LeftShift;
break;
case 2:
keyCodes[(int)Keys.Right] = KeyCode.RightArrow;
keyCodes[(int)Keys.Left] = KeyCode.LeftArrow;
keyCodes[(int)Keys.Up] = KeyCode.UpArrow;
keyCodes[(int)Keys.Jump] = keyCodes[(int)Keys.Up];
keyCodes[(int)Keys.Down] = KeyCode.DownArrow;
keyCodes[(int)Keys.Dash] = KeyCode.RightControl;
keyCodes[(int)Keys.Shoot] = KeyCode.RightShift;
break;
default:
Debug.LogError ("No keys defined for player number " + playerNumber);
break;
}
//in der szene muss fuer jeden spieer so ein InputMapping"nr" sein:
inputMapping = GameObject.Find("InputMapping" + playerNumber).GetComponent<PlayerInputMapping>();
}
void onControllerCollider(RaycastHit2D hit)
{
Rigidbody2D body = hit.collider.gameObject.GetComponent<Rigidbody2D>();
......@@ -103,7 +82,7 @@ public class PlayerControl : MonoBehaviour
updateDashDirection();
if (canDash () && Input.GetKey (keyCodes[(int)Keys.Dash]))
if (canDash() && inputMapping.isDashPressed())
{
lastDashStart = Time.time;
currentDashDirection = dashDirection;
......@@ -117,7 +96,11 @@ public class PlayerControl : MonoBehaviour
else
{
velocity = body2D.velocity;
if (Input.GetKey(keyCodes[(int)Keys.Right]))
if (playerNumber == 1)
{
Debug.Log("is right pressed p1: "+inputMapping.isRightPressed());
}
if (inputMapping.isRightPressed())
{
//normalizedHorizontalSpeed = 1;
//if (transform.localScale.x < 0f)
......@@ -129,7 +112,7 @@ public class PlayerControl : MonoBehaviour
//if (_controller.isGrounded)
}
else if (Input.GetKey(keyCodes[(int)Keys.Left]))
else if (inputMapping.isLeftPressed())
{
//normalizedHorizontalSpeed = -1;
//if (transform.localScale.x > 0f)
......@@ -139,7 +122,7 @@ public class PlayerControl : MonoBehaviour
//if (_controller.isGrounded)
}
if (Input.GetKey (keyCodes[(int)Keys.Jump]))
if (inputMapping.isJumpPressed())
{
//to avoid DOUBLE JUMP
if (isGrounded())
......@@ -148,7 +131,6 @@ public class PlayerControl : MonoBehaviour
}
}
// apply horizontal speed smoothing it
var smoothedMovementFactor = isGrounded() ? groundDamping : inAirDamping; // how fast do we change direction?
velocity.x = Mathf.Lerp(velocity.x,0, Time.deltaTime * smoothedMovementFactor);
......@@ -157,7 +139,7 @@ public class PlayerControl : MonoBehaviour
if (velocity.y>0.0f) velocity.y = Mathf.Lerp(velocity.y, 0, Time.deltaTime * verticalSmoothedFactor);
/*==========Power Ups // Bullet management ===*/
if (Input.GetKeyDown(keyCodes[(int)Keys.Shoot]))
if (inputMapping.isShootPressed())
{
Debug.Log("Shoot pressed. Can shoot: " + canShoot());
if (canShoot())
......
using UnityEngine;
using System.Collections;
using GamepadInput;
public enum controlType : int { WASD = 0, ARROWS = 1, GAMEPAD1 = 2, GAMEPAD2 = 3, GAMEPAD3=4, GAMEPAD4=5}
public class PlayerInputMapping : MonoBehaviour {
private enum Keys : int { Left = 0, Right, Up, Down, Jump, Dash, Shoot };
KeyCode[] keyCodes = new KeyCode[Keys.GetNames(typeof(Keys)).Length];
bool usesGamepad;
GamePad.Index gamepadIndex;
public void set(controlType controls)
{
usesGamepad = true;
#region switch case controls
switch (controls)
{
case controlType.WASD:
usesGamepad = false;
keyCodes[(int)Keys.Right] = KeyCode.D;
keyCodes[(int)Keys.Left] = KeyCode.A;
keyCodes[(int)Keys.Up] = KeyCode.W;
keyCodes[(int)Keys.Jump] = keyCodes[(int)Keys.Up];
keyCodes[(int)Keys.Down] = KeyCode.S;
keyCodes[(int)Keys.Dash] = KeyCode.Space;
keyCodes[(int)Keys.Shoot] = KeyCode.LeftShift;
break;
case controlType.ARROWS:
usesGamepad = false;
keyCodes[(int)Keys.Right] = KeyCode.RightArrow;
keyCodes[(int)Keys.Left] = KeyCode.LeftArrow;
keyCodes[(int)Keys.Up] = KeyCode.UpArrow;
keyCodes[(int)Keys.Jump] = keyCodes[(int)Keys.Up];
keyCodes[(int)Keys.Down] = KeyCode.DownArrow;
keyCodes[(int)Keys.Dash] = KeyCode.RightControl;
keyCodes[(int)Keys.Shoot] = KeyCode.RightShift;
break;
case controlType.GAMEPAD1:
gamepadIndex = GamePad.Index.One;
break;
case controlType.GAMEPAD2:
gamepadIndex = GamePad.Index.Two;
break;
case controlType.GAMEPAD3:
gamepadIndex = GamePad.Index.Three;
break;
case controlType.GAMEPAD4:
gamepadIndex = GamePad.Index.Four;
break;
default:
break;
#endregion
}
}
public bool isRightPressed()
{
if (usesGamepad)
{
Vector2 leftStick = GamePad.GetAxis(GamePad.Axis.LeftStick, gamepadIndex);
Vector2 dPad = GamePad.GetAxis(GamePad.Axis.Dpad, gamepadIndex);
return leftStick.x > 0.0f || dPad.x > 0.0f;
}
else return Input.GetKey(keyCodes[(int)Keys.Right]);
}
public bool isLeftPressed()
{
if (usesGamepad)
{
Vector2 leftStick = GamePad.GetAxis(GamePad.Axis.LeftStick, gamepadIndex);
Vector2 dPad = GamePad.GetAxis(GamePad.Axis.Dpad, gamepadIndex);
return leftStick.x < 0.0f || dPad.x < 0.0f;
}
else return Input.GetKey(keyCodes[(int)Keys.Left]);
}
public bool isJumpPressed()
{
if (usesGamepad) return GamePad.GetButton(GamePad.Button.A, gamepadIndex);
else return Input.GetKey(keyCodes[(int)Keys.Jump]);
}
public bool isUpPressed()
{
if (usesGamepad)
{
Vector2 leftStick = GamePad.GetAxis(GamePad.Axis.LeftStick, gamepadIndex);
Vector2 dPad = GamePad.GetAxis(GamePad.Axis.Dpad, gamepadIndex);
return leftStick.y > 0.0f || dPad.y > 0.0f;
}
else return Input.GetKey(keyCodes[(int)Keys.Up]);
}
public bool isDownPressed()
{
if (usesGamepad)
{
Vector2 leftStick = GamePad.GetAxis(GamePad.Axis.LeftStick, gamepadIndex);
Vector2 dPad = GamePad.GetAxis(GamePad.Axis.Dpad, gamepadIndex);
return leftStick.y < 0.0f || dPad.y < 0.0f;
}
else return Input.GetKey(keyCodes[(int)Keys.Down]);
}
public bool isDashPressed()
{
if (usesGamepad)
{
return (GamePad.GetTrigger(GamePad.Trigger.LeftTrigger, gamepadIndex) > 0.01f) || (GamePad.GetTrigger(GamePad.Trigger.RightTrigger, gamepadIndex) > 0.01f)
|| GamePad.GetButton(GamePad.Button.LeftShoulder, gamepadIndex) || GamePad.GetButton(GamePad.Button.RightShoulder, gamepadIndex);
}
else return Input.GetKey(keyCodes[(int)Keys.Dash]);
}
public bool isShootPressed()
{
if (usesGamepad)
{
return GamePad.GetButton(GamePad.Button.X, gamepadIndex) || GamePad.GetButton(GamePad.Button.Y, gamepadIndex);
}
else return Input.GetKey(keyCodes[(int)Keys.Shoot]);
}
}
fileFormatVersion: 2
guid: dbaefa6d68c0e4f48a677093cb7c263f
timeCreated: 1433511988
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment