Jump.cs 1.74 KB
Newer Older
Tanner Grehawick's avatar
Tanner Grehawick committed
1 2 3 4 5 6 7
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Jump : AbstractBehavior {

	public float jumpHeight = 5;
Tanner Grehawick's avatar
Tanner Grehawick committed
8 9
	public float airJumpHeight = 5;
	public int airJumpCount = 1;
Tanner Grehawick's avatar
Tanner Grehawick committed
10 11
	public float wallJumpHeight = 3;
	public float wallJumpHorizontalSpeed = 5;
Tanner Grehawick's avatar
Tanner Grehawick committed
12

13 14
	bool jumpRequested;

Tanner Grehawick's avatar
Tanner Grehawick committed
15
	int airJumpsMade = 0;
Tanner Grehawick's avatar
Tanner Grehawick committed
16

17

18 19 20 21
	void Update() {
		if (!jumpRequested && inputState.GetButtonPressed(inputButtons[0])) {
			jumpRequested = true;
		}
22 23 24 25 26 27 28 29 30
		float jumpState;
		Vector2 vel = body.velocity;
		jumpState = Mathf.Abs(vel.y) / GetJumpVelocity(jumpHeight);
		jumpState = 1 - Mathf.Clamp01(jumpState);
		if (jumpState == 1) {
			jumpState = 0;
		}
		jumpState = Mathf.Sqrt(jumpState);
		animator.SetFloat("jump state", jumpState);
31 32
	}

Tanner Grehawick's avatar
Tanner Grehawick committed
33
	void FixedUpdate() {
34
		if (jumpRequested) {
Tanner Grehawick's avatar
Tanner Grehawick committed
35
			Activate();
36
			jumpRequested = false;
Tanner Grehawick's avatar
Tanner Grehawick committed
37 38 39 40
		}
	}

	public void Activate() {
Tanner Grehawick's avatar
Tanner Grehawick committed
41
		if (collisionState.onGround) {
42
			// ground jump
Tanner Grehawick's avatar
Tanner Grehawick committed
43 44 45 46 47
			airJumpsMade = 0;
			Vector2 vel = body.velocity;
			vel.y = GetJumpVelocity(jumpHeight);
			body.velocity = vel;
		}
Tanner Grehawick's avatar
Tanner Grehawick committed
48
		else if (collisionState.onWall) {
49
			// wall jump
Tanner Grehawick's avatar
Tanner Grehawick committed
50
			Vector2 vel = Vector2.up * GetJumpVelocity(wallJumpHeight);
51
			if (collisionState.onWallLeft) {
Tanner Grehawick's avatar
Tanner Grehawick committed
52 53 54 55 56 57 58
				vel += Vector2.right * wallJumpHorizontalSpeed;
				inputState.direction = Directions.Right;
			}
			if (collisionState.onWallRight) {
				vel -= Vector2.right * wallJumpHorizontalSpeed;
				inputState.direction = Directions.Left;
			}
Tanner Grehawick's avatar
Tanner Grehawick committed
59 60
			body.velocity = vel;
		}
Tanner Grehawick's avatar
Tanner Grehawick committed
61
		else if (airJumpsMade < airJumpCount) {
62
			// air jump
Tanner Grehawick's avatar
Tanner Grehawick committed
63 64 65 66 67
			Vector2 vel = body.velocity;
			vel.y = GetJumpVelocity(airJumpHeight);
			body.velocity = vel;
			airJumpsMade ++;
		}
Tanner Grehawick's avatar
Tanner Grehawick committed
68 69 70
	}

	float GetJumpVelocity(float height) {
Tanner Grehawick's avatar
Tanner Grehawick committed
71
		float gravity = Physics2D.gravity.y*body.gravityScale;
Tanner Grehawick's avatar
Tanner Grehawick committed
72 73 74 75
		return Mathf.Sqrt(-2 * gravity * height);
	}

}