From acd3fc114f4da9d7736de5f162109bc6cac6f900 Mon Sep 17 00:00:00 2001 From: En Yi Date: Sat, 1 Feb 2020 17:30:16 +0800 Subject: [PATCH] Add simple dash --- include/header.h | 4 +++- obj/player.c | 28 +++++++++++++++++++++++++++- utilities/math.c | 2 +- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/include/header.h b/include/header.h index 51b7d94..e5b0c3a 100644 --- a/include/header.h +++ b/include/header.h @@ -5,6 +5,8 @@ #define BEZIER_POINTS 5 #define LEFT KEY_LEFT #define RIGHT KEY_RIGHT +#define DOWN KEY_DOWN +#define UP KEY_UP #define JUMP KEY_SPACE #define DASH KEY_Z @@ -78,7 +80,7 @@ void scale_rect(struct kinematic_obj *obj); void adjust_hitbox(struct kinematic_obj *obj); // Math functions, math.c -long mag(Vector2 vec); +double mag(Vector2 vec); int sign(float val); Vector2 dir(Vector2 vec); void approach(double *val, double target, float f); diff --git a/obj/player.c b/obj/player.c index cde1412..3ed464f 100644 --- a/obj/player.c +++ b/obj/player.c @@ -19,6 +19,7 @@ static enum PLAYER_STATE state_buffer = IDLE; const unsigned int run_start_frames = 10; const unsigned int jump_squat_frames = 4; const unsigned int land_lag_frames = 6; +const unsigned int dash_time_frames = 5; unsigned int PLAYER_SIZE = 30; @@ -153,6 +154,7 @@ void player_input_check(struct player_obj *player){ player->kinematic.dim_reduction[3] = 0; player->kinematic.set_dim_reduction[3] = 0; player->kinematic.dim_reduction[1] = PLAYER_SIZE; + player->kinematic.set_dim_reduction[1] = 0; on_ground = true; state_buffer = IDLE; } @@ -181,7 +183,10 @@ void player_input_check(struct player_obj *player){ break; case DASHING: - + ++frame_counter; + if (frame_counter > dash_time_frames){ + player->state = JUMPING; + } break; case DASH_END: @@ -197,6 +202,27 @@ void player_input_check(struct player_obj *player){ short_hop = false; --jumps; } + if (IsKeyPressed(DASH)){ + player->kinematic.velocity.x = 0; + if (IsKeyDown(RIGHT)) + ++player->kinematic.velocity.x; + if (IsKeyDown(LEFT)) + --player->kinematic.velocity.x; + + player->kinematic.velocity.y = 0; + if (IsKeyDown(DOWN)) + ++player->kinematic.velocity.y; + if (IsKeyDown(UP)) + --player->kinematic.velocity.y; + + if (player->kinematic.velocity.x == 0 && player->kinematic.velocity.y == 0) + player->kinematic.velocity.x = sign(player->kinematic.velocity.x); + + double m = mag(player->kinematic.velocity); + player->kinematic.velocity.x *= 500/m; + player->kinematic.velocity.y *= 500/m; + player->state = DASHING; + } // Add mercy jump here if (on_ground == true && !place_meeting(&player->kinematic, (Vector2){0,1}) && player->state != JUMP_SQUAT){ diff --git a/utilities/math.c b/utilities/math.c index 1361e58..c545e15 100644 --- a/utilities/math.c +++ b/utilities/math.c @@ -1,7 +1,7 @@ #include "header.h" #include -long mag(Vector2 vec){ +double mag(Vector2 vec){ return sqrt(vec.x*vec.x + vec.y*vec.y); }