Commit 14dbaa34 authored by Tanner Grehawick's avatar Tanner Grehawick

working CollisionStateRefactor

parent 8c74d0fa
No preview for this file type
...@@ -33,32 +33,37 @@ public class CollisionState : MonoBehaviour { ...@@ -33,32 +33,37 @@ public class CollisionState : MonoBehaviour {
enemyHit = Physics2D.OverlapCircle(body.position + bottomPosition, collisionRadius, enemyLayer); enemyHit = Physics2D.OverlapCircle(body.position + bottomPosition, collisionRadius, enemyLayer);
} }
void OnDrawGizmosSelected() { // void OnDrawGizmosSelected() {
Gizmos.color = Color.red; // Gizmos.color = Color.red;
Vector2 pos = bottomPosition; // Vector2 pos = bottomPosition;
pos.x += transform.position.x; // pos.x += transform.position.x;
pos.y += transform.position.y; // pos.y += transform.position.y;
Gizmos.DrawWireSphere(pos, collisionRadius); // Gizmos.DrawWireSphere(pos, collisionRadius);
pos = rightPosition; // pos = rightPosition;
pos.x += transform.position.x; // pos.x += transform.position.x;
pos.y += transform.position.y; // pos.y += transform.position.y;
Gizmos.DrawWireSphere(pos, collisionRadius); // Gizmos.DrawWireSphere(pos, collisionRadius);
pos = leftPosition; // pos = leftPosition;
pos.x += transform.position.x; // pos.x += transform.position.x;
pos.y += transform.position.y; // pos.y += transform.position.y;
Gizmos.DrawWireSphere(pos, collisionRadius); // Gizmos.DrawWireSphere(pos, collisionRadius);
} // }
float lastFixedTime;
void OnCollisionStay2D(Collision2D collision) { void OnCollisionStay2D(Collision2D collision) {
onGround = false; if (Time.fixedTime != lastFixedTime) {
onWallLeft = false; onGround = false;
onWallRight = false; onWallLeft = false;
onWallRight = false;
lastFixedTime = Time.fixedTime;
}
foreach (ContactPoint2D contact in collision.contacts) { foreach (ContactPoint2D contact in collision.contacts) {
Color color = Color.white; Color color = Color.white;
Vector2 normal = contact.normal; Vector2 normal = contact.normal;
......
...@@ -3,135 +3,135 @@ using System.Collections.Generic; ...@@ -3,135 +3,135 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class Walk : AbstractBehavior { public class Walk : AbstractBehavior {
public float speed; public float speed;
private float airSpeed; private float airSpeed;
public float airAcceleration; public float airAcceleration;
public float friction; public float friction;
public float wallClingTime; //time you will 'stick' to the wall after pressing the opposite direction public float wallClingTime; //time you will 'stick' to the wall after pressing the opposite direction
public float clingSpeed = 0.1f; public float clingSpeed = 0.1f;
public float realSpeed; public float realSpeed;
public bool wallSliding = false; public bool wallSliding = false;
private int rightInt = 0; private int rightInt = 0;
private int leftInt = 0; private int leftInt = 0;
private float rightTime = 0; private float rightTime = 0;
private float leftTime = 0; private float leftTime = 0;
// Use this for initialization // Use this for initialization
void Start() { void Start() {
} }
// Update is called once per frame // Update is called once per frame
void Update() { void Update() {
Buttons left = inputButtons[0]; Buttons left = inputButtons[0];
Buttons right = inputButtons[1]; Buttons right = inputButtons[1];
rightInt = 0; rightInt = 0;
leftInt = 0; leftInt = 0;
if (inputState.GetButtonValue(right)) if (inputState.GetButtonValue(right))
rightInt = 1; rightInt = 1;
else else
rightInt = 0; rightInt = 0;
if (inputState.GetButtonValue(left)) if (inputState.GetButtonValue(left))
leftInt = -1; leftInt = -1;
else else
leftInt = 0; leftInt = 0;
wallSliding = false; wallSliding = false;
if (collisionState.onGround) //movement on ground if (collisionState.onGround) //movement on ground
{ {
realSpeed = body.velocity.x; realSpeed = body.velocity.x;
if (inputState.GetButtonValue(right) || inputState.GetButtonValue(left)) if (inputState.GetButtonValue(right) || inputState.GetButtonValue(left))
{ {
realSpeed = Mathf.Clamp(realSpeed + speed * (rightInt + leftInt), -speed, speed); realSpeed = Mathf.Clamp(realSpeed + speed * (rightInt + leftInt), -speed, speed);
} }
else else
{ {
if (realSpeed != 0) if (realSpeed != 0)
{ {
realSpeed += (-1) * Mathf.Sign(realSpeed) * friction; realSpeed += (-1) * Mathf.Sign(realSpeed) * friction;
if (Mathf.Abs(realSpeed) < friction) if (Mathf.Abs(realSpeed) < friction)
realSpeed = 0; realSpeed = 0;
} }
} }
body.velocity = new Vector2(realSpeed, body.velocity.y); body.velocity = new Vector2(realSpeed, body.velocity.y);
} else if (!collisionState.onWall || body.velocity.y > 0) //in air and not sliding } else if (!collisionState.onWall || body.velocity.y > 0) //in air and not sliding
{ {
wallSliding = false; wallSliding = false;
airSpeed = body.velocity.x; airSpeed = body.velocity.x;
if (inputState.GetButtonValue(right) || inputState.GetButtonValue(left)) if (inputState.GetButtonValue(right) || inputState.GetButtonValue(left))
{ {
if(Mathf.Abs(airSpeed) <= speed) if(Mathf.Abs(airSpeed) <= speed)
airSpeed = Mathf.Clamp(airSpeed + airAcceleration * (rightInt + leftInt), -speed, speed); airSpeed = Mathf.Clamp(airSpeed + airAcceleration * (rightInt + leftInt), -speed, speed);
else else
{ {
if (airSpeed != 0) if (airSpeed != 0)
{ {
airSpeed += (-1) * Mathf.Sign(airSpeed) * airAcceleration; airSpeed += (-1) * Mathf.Sign(airSpeed) * airAcceleration;
if (Mathf.Abs(airSpeed) < airAcceleration) if (Mathf.Abs(airSpeed) < airAcceleration)
airSpeed = 0; airSpeed = 0;
} }
} }
} }
/*else //air deceleration while pressing nothing, I don't like this - may be removed /*else //air deceleration while pressing nothing, I don't like this - may be removed
{ {
if (airSpeed != 0) if (airSpeed != 0)
{ {
airSpeed += (-1) * Mathf.Sign(airSpeed) * airAcceleration; airSpeed += (-1) * Mathf.Sign(airSpeed) * airAcceleration;
if (Mathf.Abs(airSpeed) < airAcceleration) if (Mathf.Abs(airSpeed) < airAcceleration)
airSpeed = 0; airSpeed = 0;
} }
}*/ }*/
body.velocity = new Vector2(airSpeed, body.velocity.y); body.velocity = new Vector2(airSpeed, body.velocity.y);
} else //handles wall sliding } else //handles wall sliding
{ {
wallSliding = true; wallSliding = true;
rightTime = inputState.GetButtonHoldTime(right); rightTime = inputState.GetButtonHoldTime(right);
leftTime = inputState.GetButtonHoldTime(left); leftTime = inputState.GetButtonHoldTime(left);
airSpeed = body.velocity.x; airSpeed = body.velocity.x;
//only sets a change if the button has been held for a set time //only sets a change if the button has been held for a set time
if (rightTime >= wallClingTime) if (rightTime >= wallClingTime)
{ {
wallSliding = false; wallSliding = false;
airSpeed = Mathf.Clamp(airSpeed + airAcceleration * (rightInt + leftInt), -speed, speed); airSpeed = Mathf.Clamp(airSpeed + airAcceleration * (rightInt + leftInt), -speed, speed);
} }
if (leftTime >= wallClingTime) if (leftTime >= wallClingTime)
{ {
wallSliding = false; wallSliding = false;
airSpeed = Mathf.Clamp(airSpeed + airAcceleration * (rightInt + leftInt), -speed, speed); airSpeed = Mathf.Clamp(airSpeed + airAcceleration * (rightInt + leftInt), -speed, speed);
} }
//same as if not on wall and not pressing the buttons //same as if not on wall and not pressing the buttons
if (!inputState.GetButtonValue(right) && !inputState.GetButtonValue(left)) if (!inputState.GetButtonValue(right) && !inputState.GetButtonValue(left))
{ {
if (airSpeed != 0) if (airSpeed != 0)
{ {
airSpeed += (-1) * Mathf.Sign(airSpeed) * airAcceleration; airSpeed += (-1) * Mathf.Sign(airSpeed) * airAcceleration;
if (Mathf.Abs(airSpeed) < airAcceleration) if (Mathf.Abs(airSpeed) < airAcceleration)
airSpeed = 0; airSpeed = 0;
} }
} }
body.velocity = new Vector2(airSpeed, body.velocity.y); body.velocity = new Vector2(airSpeed, body.velocity.y);
//slow down y velocity //slow down y velocity
if(body.velocity.y < clingSpeed) if(body.velocity.y < clingSpeed)
{ {
body.velocity = new Vector2(body.velocity.x, -clingSpeed); body.velocity = new Vector2(body.velocity.x, -clingSpeed);
} }
} }
//change localScale depending on direction //change localScale depending on direction
if (inputState.GetButtonValue (right) && inputState.GetButtonValue (left)) if (inputState.GetButtonValue (right) && inputState.GetButtonValue (left))
...@@ -144,5 +144,5 @@ public class Walk : AbstractBehavior { ...@@ -144,5 +144,5 @@ public class Walk : AbstractBehavior {
{ {
inputState.direction = Directions.Left; inputState.direction = Directions.Left;
} }
} }
} }
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