Commit 0b9dca7e by Philipp Adolf

Merge branch 'arbitrary-dash'

parents d18a8444 9c625f66
...@@ -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, Up, Down, Jump, Dash, Shoot };
// movement config // movement config
public float pushForce = 20f; public float pushForce = 20f;
public float gravity = -15f; public float gravity = -15f;
...@@ -14,9 +16,9 @@ public class PlayerControl : MonoBehaviour ...@@ -14,9 +16,9 @@ public class PlayerControl : MonoBehaviour
public float targetJumpHeight = 2f; public float targetJumpHeight = 2f;
public float lastDashStart = float.NegativeInfinity; public float lastDashStart = float.NegativeInfinity;
[HideInInspector] [HideInInspector]
public float currentDashDirection = 0.0f; public Vector2 dashDirection = Vector2.right;
[HideInInspector] [HideInInspector]
public float dashDirection = 1.0f; public Vector2 currentDashDirection = Vector2.right;
public float dashCompletionTime = 0.25f; public float dashCompletionTime = 0.25f;
public float dashStartSpeed = 20.0f; public float dashStartSpeed = 20.0f;
public float dashEndSpeed = 8.0f; public float dashEndSpeed = 8.0f;
...@@ -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,31 @@ public class PlayerControl : MonoBehaviour ...@@ -48,31 +50,31 @@ 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.S, KeyCode.LeftShift); 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; 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; 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>();
...@@ -93,16 +95,9 @@ public class PlayerControl : MonoBehaviour ...@@ -93,16 +95,9 @@ public class PlayerControl : MonoBehaviour
if (_controller.isGrounded) if (_controller.isGrounded)
velocity.y = 0; velocity.y = 0;
if (Input.GetKey (goRightKeyCode)) updateDashDirection();
{
dashDirection = 1.0f;
}
else if (Input.GetKey (goLeftKeyCode))
{
dashDirection = -1.0f;
}
if (canDash () && Input.GetKey (dashKeyCode)) if (canDash () && Input.GetKey (keyCodes[(int)Keys.Dash]))
{ {
lastDashStart = Time.time; lastDashStart = Time.time;
currentDashDirection = dashDirection; currentDashDirection = dashDirection;
...@@ -111,12 +106,11 @@ public class PlayerControl : MonoBehaviour ...@@ -111,12 +106,11 @@ public class PlayerControl : MonoBehaviour
if (isDashing ()) if (isDashing ())
{ {
velocity.x = Mathf.Lerp(currentDashDirection * dashStartSpeed, currentDashDirection * dashEndSpeed, getDashTime() / dashCompletionTime); velocity = currentDashDirection * Mathf.Lerp(dashStartSpeed, dashEndSpeed, getDashTime() / dashCompletionTime);
velocity.y = 0;
} }
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)
...@@ -124,7 +118,7 @@ public class PlayerControl : MonoBehaviour ...@@ -124,7 +118,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)
...@@ -139,7 +133,7 @@ public class PlayerControl : MonoBehaviour ...@@ -139,7 +133,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)
...@@ -157,7 +151,7 @@ public class PlayerControl : MonoBehaviour ...@@ -157,7 +151,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())
...@@ -171,6 +165,32 @@ public class PlayerControl : MonoBehaviour ...@@ -171,6 +165,32 @@ public class PlayerControl : MonoBehaviour
_controller.move(velocity * Time.deltaTime); _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 () private float getDashTime ()
{ {
return Time.time - lastDashStart; 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