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:
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