Commit 6fcab720 by Philipp Adolf

Store key codes in array indexed by enum

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