Walk.cs 3.65 KB
Newer Older
Jordan B Mckenney's avatar
Jordan B Mckenney committed
1 2 3 4 5
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Walk : AbstractBehavior {
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	public float speed;
	private float airSpeed;
	public float airAcceleration;
	public float friction;
	public float wallClingTime; //time you will 'stick' to the wall after pressing the opposite direction
	public float clingSpeed = 0.1f;

	public float realSpeed;
	public bool wallSliding = false;

	private int rightInt = 0;
	private int leftInt = 0;

	private float rightTime = 0;
	private float leftTime = 0;

	// Use this for initialization
	void Start() {
	}

	// Update is called once per frame
	void Update() {
		Buttons left = inputButtons[0];
		Buttons right = inputButtons[1];

		rightInt = 0;
		leftInt = 0;


		if (inputState.GetButtonValue(right))
			rightInt = 1;
		else
			rightInt = 0;

		if (inputState.GetButtonValue(left))
			leftInt = -1;
		else
			leftInt = 0;

		wallSliding = false;

		if (collisionState.onGround) //movement on ground
		{

			realSpeed = body.velocity.x;
			if (inputState.GetButtonValue(right) || inputState.GetButtonValue(left))
			{

				realSpeed = Mathf.Clamp(realSpeed + speed * (rightInt + leftInt), -speed, speed);
			}
			else
			{
				if (realSpeed != 0)
				{
					realSpeed += (-1) * Mathf.Sign(realSpeed) * friction;
					if (Mathf.Abs(realSpeed) < friction)
						realSpeed = 0;
				}
			}
				body.velocity = new Vector2(realSpeed, body.velocity.y);
		} else if (!collisionState.onWall || body.velocity.y > 0) //in air and not sliding
		{

			wallSliding = false;

			airSpeed = body.velocity.x;
			if (inputState.GetButtonValue(right) || inputState.GetButtonValue(left))
			{
				if(Mathf.Abs(airSpeed) <= speed)
					airSpeed = Mathf.Clamp(airSpeed + airAcceleration * (rightInt + leftInt), -speed, speed);
				else
				{
					if (airSpeed != 0)
					{
						airSpeed += (-1) * Mathf.Sign(airSpeed) * airAcceleration;
						if (Mathf.Abs(airSpeed) < airAcceleration)
							airSpeed = 0;
					}
				}
			}
			/*else //air deceleration while pressing nothing, I don't like this - may be removed
			{
				if (airSpeed != 0)
				{
					airSpeed += (-1) * Mathf.Sign(airSpeed) * airAcceleration;
					if (Mathf.Abs(airSpeed) < airAcceleration)
						airSpeed = 0;
				}
			}*/
				body.velocity = new Vector2(airSpeed, body.velocity.y);
		} else //handles wall sliding
		{
			wallSliding = true;
			rightTime = inputState.GetButtonHoldTime(right);
			leftTime = inputState.GetButtonHoldTime(left);

			airSpeed = body.velocity.x;

			//only sets a change if the button has been held for a set time
			if (rightTime >= wallClingTime)
			{
				wallSliding = false;
				airSpeed = Mathf.Clamp(airSpeed + airAcceleration * (rightInt + leftInt), -speed, speed);
			}
			if (leftTime >= wallClingTime)
			{
				wallSliding = false;
				airSpeed = Mathf.Clamp(airSpeed + airAcceleration * (rightInt + leftInt), -speed, speed);
			}

			//same as if not on wall and not pressing the buttons
			if (!inputState.GetButtonValue(right) && !inputState.GetButtonValue(left))
			{
				if (airSpeed != 0)
				{
					airSpeed += (-1) * Mathf.Sign(airSpeed) * airAcceleration;
					if (Mathf.Abs(airSpeed) < airAcceleration)
						airSpeed = 0;
				}
			}
			body.velocity = new Vector2(airSpeed, body.velocity.y);

			//slow down y velocity
			if(body.velocity.y < clingSpeed)
			{
				body.velocity = new Vector2(body.velocity.x, -clingSpeed);
			}

		}
Jordan B Mckenney's avatar
Jordan B Mckenney committed
135 136

		//change localScale depending on direction
137
		if (inputState.GetButtonValue (right) && inputState.GetButtonValue (left))
Jordan B Mckenney's avatar
Jordan B Mckenney committed
138
		{
139 140

		} else if (inputState.GetButtonValue (right))
Jordan B Mckenney's avatar
Jordan B Mckenney committed
141 142 143 144 145 146
		{
			inputState.direction = Directions.Right;
		} else if (inputState.GetButtonValue (left))
		{
			inputState.direction = Directions.Left;
		}
147
	}
Jordan B Mckenney's avatar
Jordan B Mckenney committed
148
}