Commit 6fcab720 by Philipp Adolf

Store key codes in array indexed by enum

parent 1733117d
......@@ -5,6 +5,8 @@ using System.Collections;
public class PlayerControl : MonoBehaviour
{
private enum Keys : int { Left = 0, Right, Jump, Dash, Shoot };
// movement config
public float pushForce = 20f;
public float gravity = -15f;
......@@ -38,7 +40,7 @@ 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;
KeyCode goRightKeyCode, goLeftKeyCode, jumpKeyCode, dashKeyCode, shootKeyCode;
KeyCode[] keyCodes = new KeyCode[Keys.GetNames(typeof(Keys)).Length];
void Start()
{
......@@ -48,31 +50,27 @@ public class PlayerControl : MonoBehaviour
switch (playerNumber)
{
case 1:
setKeyCodes(KeyCode.D, KeyCode.A, KeyCode.W, KeyCode.Space, KeyCode.LeftShift);
keyCodes[(int)Keys.Right] = KeyCode.D;
keyCodes[(int)Keys.Left] = KeyCode.A;
keyCodes[(int)Keys.Jump] = KeyCode.W;
keyCodes[(int)Keys.Dash] = KeyCode.Space;
keyCodes[(int)Keys.Shoot] = KeyCode.LeftShift;
break;
case 2:
setKeyCodes(KeyCode.RightArrow, KeyCode.LeftArrow, KeyCode.UpArrow, KeyCode.RightControl, KeyCode.RightShift);
case 2:
keyCodes[(int)Keys.Right] = KeyCode.RightArrow;
keyCodes[(int)Keys.Left] = KeyCode.LeftArrow;
keyCodes[(int)Keys.Jump] = KeyCode.UpArrow;
keyCodes[(int)Keys.Dash] = KeyCode.RightControl;
keyCodes[(int)Keys.Shoot] = KeyCode.RightShift;
break;
//TODO player 3 and 4
default:
Debug.LogError ("No keys defined for player number " + playerNumber);
break;
}
}
private void setKeyCodes(KeyCode right, KeyCode left, KeyCode up, KeyCode dash, KeyCode shoot)
{
goRightKeyCode = right;
goLeftKeyCode = left;
jumpKeyCode = up;
dashKeyCode = dash;
shootKeyCode = shoot;
}
void onControllerCollider(RaycastHit2D hit)
{
Rigidbody2D body = hit.collider.gameObject.GetComponent<Rigidbody2D>();
......@@ -95,7 +93,7 @@ public class PlayerControl : MonoBehaviour
updateDashDirection();
if (canDash () && Input.GetKey (dashKeyCode))
if (canDash () && Input.GetKey (keyCodes[(int)Keys.Dash]))
{
lastDashStart = Time.time;
currentDashDirection = dashDirection;
......@@ -108,7 +106,7 @@ public class PlayerControl : MonoBehaviour
}
else
{
if (Input.GetKey(goRightKeyCode))
if (Input.GetKey(keyCodes[(int)Keys.Right]))
{
normalizedHorizontalSpeed = 1;
if (transform.localScale.x < 0f)
......@@ -116,7 +114,7 @@ public class PlayerControl : MonoBehaviour
//if (_controller.isGrounded)
}
else if (Input.GetKey(goLeftKeyCode))
else if (Input.GetKey(keyCodes[(int)Keys.Left]))
{
normalizedHorizontalSpeed = -1;
if (transform.localScale.x > 0f)
......@@ -131,7 +129,7 @@ public class PlayerControl : MonoBehaviour
//if (_controller.isGrounded)
}
if (Input.GetKey (jumpKeyCode))
if (Input.GetKey (keyCodes[(int)Keys.Jump]))
{
//to avoid DOUBLE JUMP
if (_controller.isGrounded)
......@@ -149,7 +147,7 @@ public class PlayerControl : MonoBehaviour
velocity.y += gravity * Time.deltaTime;
/*==========Power Ups // Bullet management ===*/
if (Input.GetKeyDown(shootKeyCode))
if (Input.GetKeyDown(keyCodes[(int)Keys.Shoot]))
{
Debug.Log("Shoot pressed. Can shoot: " + canShoot());
if (canShoot())
......@@ -166,11 +164,11 @@ public class PlayerControl : MonoBehaviour
private void updateDashDirection()
{
float horizontalDashDirection = 0.0f;
if (Input.GetKey (goRightKeyCode))
if (Input.GetKey (keyCodes[(int)Keys.Right]))
{
horizontalDashDirection = 1.0f;
}
else if (Input.GetKey (goLeftKeyCode))
else if (Input.GetKey (keyCodes[(int)Keys.Left]))
{
horizontalDashDirection = -1.0f;
}
......
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