Commit 61a84506 authored by Conner Stern's avatar Conner Stern

made dash use animationcurve

parent 24be4a20
...@@ -7,10 +7,21 @@ using UnityEngine; ...@@ -7,10 +7,21 @@ using UnityEngine;
public class Dash : AbstractBehavior { public class Dash : AbstractBehavior {
//The velocity curve over time
[SerializeField] private AnimationCurve dashVelocityCurve;
private float distRatio {
get{ return dashVelocityCurve.keys[dashVelocityCurve.length - 1].time / dashDistance;}
}
private float distMultiplier = 1;
//The time of the dash
//[SerializeField] private float dashTime;
//Distance to dash
[SerializeField] private float dashDistance;
Jump jumpScript;
public float coolDown = 1f; public float coolDown = 1f;
public float dashforce = 5;
public float holdTime = 0.4f; public float holdTime = 0.4f;
public float minVelocity = 0.5f;
private bool _isDashing = false; private bool _isDashing = false;
private Transform dashMeter; private Transform dashMeter;
...@@ -19,6 +30,8 @@ public class Dash : AbstractBehavior { ...@@ -19,6 +30,8 @@ public class Dash : AbstractBehavior {
private Walk walk; private Walk walk;
private bool jumpDuringDash = false;
// Call to check if currently dashing // Call to check if currently dashing
public bool isDashing public bool isDashing
{ {
...@@ -30,9 +43,30 @@ public class Dash : AbstractBehavior { ...@@ -30,9 +43,30 @@ public class Dash : AbstractBehavior {
} }
void Start() { void Start() {
jumpScript = GetComponent<Jump>();
dashMeter = GameObject.FindGameObjectWithTag("DashMeter").transform; dashMeter = GameObject.FindGameObjectWithTag("DashMeter").transform;
coll = GetComponent<CircleCollider2D>(); coll = GetComponent<CircleCollider2D>();
walk = GetComponent<Walk>(); walk = GetComponent<Walk>();
distMultiplier = ApproximateSpeedMultiplier(dashVelocityCurve, 100);
//Setting up the v0 and a with Kenimatic Equations (Deprecated)
//dashVelocity = CalculateDashVelocity(dashDistance, dashTime, finalVelocity);
//dashDeceleration = CalculateDashDeceleration(finalVelocity, dashTime, dashVelocity);
}
float ApproximateSpeedMultiplier(AnimationCurve velocityOverTime, int samples){
float time = velocityOverTime.Evaluate(velocityOverTime.keys[dashVelocityCurve.length - 1].time);
float distTraveled = 0;
for(float i = 0; i < time; i += time / samples){
distTraveled += velocityOverTime.Evaluate(i);
}
distTraveled /= samples;
return (dashDistance / distTraveled);
} }
void Update() { void Update() {
...@@ -41,6 +75,10 @@ public class Dash : AbstractBehavior { ...@@ -41,6 +75,10 @@ public class Dash : AbstractBehavior {
} }
UpdateUI(); UpdateUI();
animator.SetBool("is dashing", isDashing); animator.SetBool("is dashing", isDashing);
if (jumpScript._jumpRequested && isDashing){
print("jumped");
jumpDuringDash = true;
}
} }
// Should be called every frame // Should be called every frame
...@@ -63,35 +101,37 @@ public class Dash : AbstractBehavior { ...@@ -63,35 +101,37 @@ public class Dash : AbstractBehavior {
if (walk != null) { if (walk != null) {
walk.enabled = false; walk.enabled = false;
} }
//stores distance remaining in dash
float distRemaining = dashDistance;
// dash direction vector // dash direction vector
Vector2 dir = ((int) direction) * Vector2.right; Vector2 dir = ((int) direction) * Vector2.right;
// Cooldown // Cooldown
lastDash = Time.time + coolDown; lastDash = Time.time + coolDown;
// Is dashing // Is dashing
isDashing = true; isDashing = true;
// Set initial velocity
body.velocity = dir * dashforce;
// Disable gravity // Disable gravity
float initialGrav = body.gravityScale; float initialGrav = body.gravityScale;
body.gravityScale = 0; body.gravityScale = 0;
// results for circle cast // results for circle cast
RaycastHit2D[] results = new RaycastHit2D[8]; RaycastHit2D[] results = new RaycastHit2D[8];
// Decelerate // Decelerate
for (float i = 0; i < holdTime; i += Time.deltaTime) { while(distRemaining >= 0) {
// Calculate velocity //Set initial velocity
Vector2 targetVel = (dir * dashforce) * (1 - (i / holdTime)); float currentVel = dashVelocityCurve.Evaluate(distRatio * (dashDistance - distRemaining));
//check if we need to stop dashing
if(body.velocity.magnitude < minVelocity || collisionState.onWall) { if(distRemaining <= 0 || collisionState.onWall || jumpDuringDash) {
// Velocity is low enough or collision with wall // Velocity is low enough or collision with wall
jumpDuringDash = false;
print("ended dash");
break; break;
} }
// Apply velocity // Apply velocity
body.velocity = targetVel; body.velocity = currentVel * dir;
print(body.velocity);
// Apply dist remaining reduciton
distRemaining -= currentVel * Time.deltaTime;
float checkDistance = body.velocity.magnitude * Time.deltaTime; float checkDistance = body.velocity.magnitude * Time.deltaTime;
...@@ -104,7 +144,7 @@ public class Dash : AbstractBehavior { ...@@ -104,7 +144,7 @@ public class Dash : AbstractBehavior {
breakable.Break(); breakable.Break();
} }
} }
// Wait // Wait
yield return new WaitForEndOfFrame(); yield return new WaitForEndOfFrame();
} }
...@@ -118,7 +158,18 @@ public class Dash : AbstractBehavior { ...@@ -118,7 +158,18 @@ public class Dash : AbstractBehavior {
// Is not dashing // Is not dashing
isDashing = false; isDashing = false;
} }
/*
private float CalculateDashVelocity(float dashDist, float dashtTime, float finalVel){
//manipulated equation "d = .5 * (v + v0) * t" to get v by itself
float velocity = 2 * (dashDist / dashTime) - finalVel;
return velocity;
}
*/
private float CalculateDashDeceleration(float finalVel, float dashTime, float startVel){
//manipulated equation v = v0 + at to get a by itself
float acceleration = (finalVel - startVel) / dashTime;
return acceleration;
}
public void ActivateIgnoringCooldown(Directions direction) { public void ActivateIgnoringCooldown(Directions direction) {
StartCoroutine(DashRoutine(direction)); StartCoroutine(DashRoutine(direction));
} }
......
...@@ -10,6 +10,9 @@ public class Jump : AbstractBehavior { ...@@ -10,6 +10,9 @@ public class Jump : AbstractBehavior {
public float wallJumpHeight = 3; public float wallJumpHeight = 3;
public float wallJumpHorizontalSpeed = 5; public float wallJumpHorizontalSpeed = 5;
public bool _jumpRequested{
get{return jumpRequested;}
}
bool jumpRequested; bool jumpRequested;
int airJumpsMade = 0; int airJumpsMade = 0;
......
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