Commit 10dc8fd6 by Tim Reiter
parents 39efc318 0b9dca7e
......@@ -5,6 +5,8 @@ using System.Collections;
public class PlayerControl : MonoBehaviour
{
private enum Keys : int { Left = 0, Right, Up, Down, Jump, Dash, Shoot };
// movement config
public float pushForce = 20f;
public float gravity = -15f;
......@@ -14,9 +16,9 @@ public class PlayerControl : MonoBehaviour
public float targetJumpHeight = 2f;
public float lastDashStart = float.NegativeInfinity;
[HideInInspector]
public float currentDashDirection = 0.0f;
public Vector2 dashDirection = Vector2.right;
[HideInInspector]
public float dashDirection = 1.0f;
public Vector2 currentDashDirection = Vector2.right;
public float dashCompletionTime = 0.25f;
public float dashStartSpeed = 20.0f;
public float dashEndSpeed = 8.0f;
......@@ -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,31 @@ public class PlayerControl : MonoBehaviour
switch (playerNumber)
{
case 1:
setKeyCodes(KeyCode.D, KeyCode.A, KeyCode.W, KeyCode.S, KeyCode.LeftShift);
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:
setKeyCodes(KeyCode.RightArrow, KeyCode.LeftArrow, KeyCode.UpArrow, KeyCode.DownArrow, KeyCode.RightShift);
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;
//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>();
......@@ -93,16 +95,9 @@ public class PlayerControl : MonoBehaviour
if (_controller.isGrounded)
velocity.y = 0;
if (Input.GetKey (goRightKeyCode))
{
dashDirection = 1.0f;
}
else if (Input.GetKey (goLeftKeyCode))
{
dashDirection = -1.0f;
}
updateDashDirection();
if (canDash () && Input.GetKey (dashKeyCode))
if (canDash () && Input.GetKey (keyCodes[(int)Keys.Dash]))
{
lastDashStart = Time.time;
currentDashDirection = dashDirection;
......@@ -111,12 +106,11 @@ public class PlayerControl : MonoBehaviour
if (isDashing ())
{
velocity.x = Mathf.Lerp(currentDashDirection * dashStartSpeed, currentDashDirection * dashEndSpeed, getDashTime() / dashCompletionTime);
velocity.y = 0;
velocity = currentDashDirection * Mathf.Lerp(dashStartSpeed, dashEndSpeed, getDashTime() / dashCompletionTime);
}
else
{
if (Input.GetKey(goRightKeyCode))
if (Input.GetKey(keyCodes[(int)Keys.Right]))
{
normalizedHorizontalSpeed = 1;
if (transform.localScale.x < 0f)
......@@ -124,7 +118,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)
......@@ -139,7 +133,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)
......@@ -157,7 +151,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())
......@@ -171,6 +165,32 @@ public class PlayerControl : MonoBehaviour
_controller.move(velocity * Time.deltaTime);
}
private void updateDashDirection()
{
float horizontalDashDirection = 0.0f;
if (Input.GetKey (keyCodes[(int)Keys.Right]))
{
horizontalDashDirection = 1.0f;
}
else if (Input.GetKey (keyCodes[(int)Keys.Left]))
{
horizontalDashDirection = -1.0f;
}
float verticalDashDirection = 0.0f;
if (Input.GetKey (keyCodes[(int)Keys.Up]))
{
verticalDashDirection = 1.0f;
}
else if (Input.GetKey (keyCodes[(int)Keys.Down]))
{
verticalDashDirection = -1.0f;
}
if (horizontalDashDirection != 0.0f || verticalDashDirection != 0.0f)
dashDirection = new Vector2(horizontalDashDirection, verticalDashDirection);
}
private float getDashTime ()
{
return Time.time - lastDashStart;
......
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