Commit db9e221a authored by Tanner Grehawick's avatar Tanner Grehawick

directional dash

parent f39acef8
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
......@@ -7,101 +7,107 @@ using UnityEngine;
public class Dash : AbstractBehavior {
public float coolDown = 1f;
public float dashforce = 5;
public float holdTime = 0.4f;
public float minVelocity = 0.5f;
private bool _isDashing = false;
private Transform dashMeter;
private float lastDash;
private CircleCollider2D collider;
// Call to check if currently dashing
public bool isDashing
{
get { return _isDashing; }
private set
{
_isDashing = value;
}
}
void Start() {
dashMeter = GameObject.FindGameObjectWithTag("DashMeter").transform;
collider = GetComponent<CircleCollider2D>();
}
public float coolDown = 1f;
public float dashforce = 5;
public float holdTime = 0.4f;
public float minVelocity = 0.5f;
private bool _isDashing = false;
private Transform dashMeter;
private float lastDash;
private CircleCollider2D collider;
// Call to check if currently dashing
public bool isDashing
{
get { return _isDashing; }
private set
{
_isDashing = value;
}
}
void Update() {
DashCheck();
void Start() {
dashMeter = GameObject.FindGameObjectWithTag("DashMeter").transform;
collider = GetComponent<CircleCollider2D>();
}
// Should be called every frame
public void DashCheck() {
// Button pressed, not in cooldown, and not on wall
if (inputState.GetButtonValue(inputButtons[0]) && Time.time > lastDash && !collisionState.onWall) {
StartCoroutine(DoDash());
}
void Update() {
if (inputState.GetButtonValue(inputButtons[0])) {
Activate();
}
UpdateUI();
}
UpdateUI();
}
// Should be called every frame
public void Activate(Directions direction) {
// not in cooldown and not on wall
if (Time.time > lastDash && !collisionState.onWall) {
StartCoroutine(DashRoutine(direction));
}
}
public void Activate() { Activate(inputState.direction); }
void UpdateUI() {
void UpdateUI() {
float progress = 1 - ((lastDash - Time.time)/coolDown);
dashMeter.localScale = new Vector2(Mathf.Clamp01(progress), dashMeter.localScale.y);
}
// Dash
IEnumerator DoDash() {
// Cooldown
lastDash = Time.time + coolDown;
// Is dashing
isDashing = true;
// Set initial velocity
body.velocity = transform.right * dashforce;
// Disable gravity
float initialGrav = body.gravityScale;
body.gravityScale = 0;
// results for circle cast
RaycastHit2D[] results = new RaycastHit2D[8];
// Decelerate
for (float i = 0; i < holdTime; i += Time.deltaTime) {
// Calculate velocity
Vector2 targetVel = (transform.right * dashforce) * (1 - (i / holdTime));
if(body.velocity.magnitude < minVelocity || collisionState.onWall) {
// Velocity is low enough or collision with wall
break;
}
// Apply velocity
body.velocity = targetVel;
float checkDistance = body.velocity.magnitude * Time.deltaTime;
// check for breakable in front
int resultCount = Physics2D.CircleCastNonAlloc(body.position, collider.radius, body.velocity, results, checkDistance);
for (int r = 0; r < resultCount; r ++) {
RaycastHit2D result = results[r];
Breakable breakable = result.collider.GetComponent<Breakable>();
if (breakable != null && breakable.breakOnPlayerDash) {
breakable.Break();
}
dashMeter.localScale = new Vector2(Mathf.Clamp01(progress), dashMeter.localScale.y);
}
// Dash
IEnumerator DashRoutine(Directions direction) {
// dash direction vector
Vector2 dir = ((int) direction) * Vector2.right;
// Cooldown
lastDash = Time.time + coolDown;
// Is dashing
isDashing = true;
// Set initial velocity
body.velocity = dir * dashforce;
// Disable gravity
float initialGrav = body.gravityScale;
body.gravityScale = 0;
// results for circle cast
RaycastHit2D[] results = new RaycastHit2D[8];
// Decelerate
for (float i = 0; i < holdTime; i += Time.deltaTime) {
// Calculate velocity
Vector2 targetVel = (dir * dashforce) * (1 - (i / holdTime));
if(body.velocity.magnitude < minVelocity || collisionState.onWall) {
// Velocity is low enough or collision with wall
break;
}
// Apply velocity
body.velocity = targetVel;
float checkDistance = body.velocity.magnitude * Time.deltaTime;
// check for breakable in front
int resultCount = Physics2D.CircleCastNonAlloc(body.position, collider.radius, dir, results, checkDistance);
for (int r = 0; r < resultCount; r ++) {
RaycastHit2D result = results[r];
Breakable breakable = result.collider.GetComponent<Breakable>();
if (breakable != null && breakable.breakOnPlayerDash) {
breakable.Break();
}
}
// Wait
yield return new WaitForEndOfFrame();
}
// Wait
yield return new WaitForEndOfFrame();
}
// Restore gravity
body.gravityScale = initialGrav;
// Is not dashing
isDashing = false;
}
// Restore gravity
body.gravityScale = initialGrav;
// Is not dashing
isDashing = false;
}
public void DashIgnoringCooldown() {
StartCoroutine(DoDash());
public void ActivateIgnoringCooldown(Directions direction) {
StartCoroutine(DashRoutine(direction));
}
public void ActivateIgnoringCooldown() { ActivateIgnoringCooldown(inputState.direction); }
}
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