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

public class Jump : AbstractBehavior {

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

Conner Stern's avatar
Conner Stern committed
14 15 16
	public bool _jumpRequested{
		get{return jumpRequested;}
	}
17
	bool jumpRequested;
Tanner Grehawick's avatar
Tanner Grehawick committed
18
	int airJumpsMade = 0;
Tanner Grehawick's avatar
Tanner Grehawick committed
19

20 21 22 23 24 25
	Walk walk;

	protected override void Awake() {
		base.Awake();
		walk = GetComponent<Walk>();
	}
26

27 28 29
	void Update() {
		if (!jumpRequested && inputState.GetButtonPressed(inputButtons[0])) {
			jumpRequested = true;
30 31
			if(jumpHeight==0)
				animator.SetTrigger("jump failed");
32
		}
33 34 35 36 37 38 39 40
		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);
41
		animator.SetFloat ("jump state", jumpState);
Tanner Grehawick's avatar
Tanner Grehawick committed
42 43 44 45
		// reset air jumps when on ground
		if (collisionState.onGround) {
			airJumpsMade = 0;
		}
46 47
	}

Tanner Grehawick's avatar
Tanner Grehawick committed
48
	void FixedUpdate() {
49
		if (jumpRequested) {
Tanner Grehawick's avatar
Tanner Grehawick committed
50
			Activate();
51
			jumpRequested = false;
Tanner Grehawick's avatar
Tanner Grehawick committed
52 53 54 55
		}
	}

	public void Activate() {
Tanner Grehawick's avatar
Tanner Grehawick committed
56
		if (collisionState.onGround) {
57
			// ground jump
Tanner Grehawick's avatar
Tanner Grehawick committed
58 59 60 61
			Vector2 vel = body.velocity;
			vel.y = GetJumpVelocity(jumpHeight);
			body.velocity = vel;
		}
62
		else if (walk.isWallSliding) {
63
			// wall jump
Tanner Grehawick's avatar
Tanner Grehawick committed
64
			Vector2 vel = Vector2.up * GetJumpVelocity(wallJumpHeight);
65
			if (collisionState.onWallLeft) {
Tanner Grehawick's avatar
Tanner Grehawick committed
66 67 68 69 70 71 72
				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
73 74
			body.velocity = vel;
		}
Tanner Grehawick's avatar
Tanner Grehawick committed
75
		else if (airJumpsMade < airJumpCount) {
76
			// air jump
Tanner Grehawick's avatar
Tanner Grehawick committed
77 78 79 80 81
			Vector2 vel = body.velocity;
			vel.y = GetJumpVelocity(airJumpHeight);
			body.velocity = vel;
			airJumpsMade ++;
		}
Tanner Grehawick's avatar
Tanner Grehawick committed
82 83 84
	}

	float GetJumpVelocity(float height) {
Tanner Grehawick's avatar
Tanner Grehawick committed
85
		float gravity = Physics2D.gravity.y*body.gravityScale;
Tanner Grehawick's avatar
Tanner Grehawick committed
86 87 88
		return Mathf.Sqrt(-2 * gravity * height);
	}

89 90 91 92 93 94
	void OnCollisionEnter2D(Collision2D other)
	{
		airJumpsMade = 0;
	}

}