From 1a9052c0a299b34f4148a8f8d6b35d629a1e12e0 Mon Sep 17 00:00:00 2001 From: KikooDX Date: Sun, 27 Dec 2020 01:45:58 +0100 Subject: [PATCH] Input buffering system, implemented for jumping. --- include/player.h | 2 +- src/input.c | 2 +- src/main.c | 2 +- src/player.c | 7 ++++--- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/include/player.h b/include/player.h index c517ec7..23f4eaa 100644 --- a/include/player.h +++ b/include/player.h @@ -17,7 +17,7 @@ typedef struct Player { #include "input.h" void player_move(Player *player, const Level *level); -void player_step(Player *player, Input *input, const Level *level); +void player_step(Player *player, Input *input, const Level *level, uint step); void player_draw(Player *player, Camera *camera); void player_draw_debug(Player *player, uint step, const Level *level, uint layer_id); diff --git a/src/input.c b/src/input.c index 1f7111a..1a3221d 100644 --- a/src/input.c +++ b/src/input.c @@ -60,7 +60,7 @@ bool input_is_pressed(Input *input, uint8_t key) { } uint input_last_press(Input *input, uint8_t key, uint step) { - return input->last_press[key] - step; + return step - input->last_press[key]; } bool input_is_down(Input *input, uint8_t key) { diff --git a/src/main.c b/src/main.c index 7cdc741..8e73870 100644 --- a/src/main.c +++ b/src/main.c @@ -85,7 +85,7 @@ int callback(volatile void *arg) { void step_event(Player *player, const Level *level, Camera *camera, Input *input, uint step) { //getkey(); input_step(input, step); - player_step(player, input, level); + player_step(player, input, level, step); level_step(level); camera_step(camera); } diff --git a/src/player.c b/src/player.c index 6f43b6b..81e5db2 100644 --- a/src/player.c +++ b/src/player.c @@ -15,6 +15,7 @@ #define GRAVITY PXS #define JUMP_SPD (-128 * PXS) #define GRACE_UNITS (int)(UPS / 5) +#define EARLY_UNITS (int)(UPS / 5) #define SGN(x) ((x > 0) ? (1) : ((x < 0) ? (-1) : (0))) #define PLAYER_COLLIDE(pos) player_collide(player, pos, level, level->solid_layer) @@ -54,7 +55,7 @@ void player_move(Player *player, const Level *level) { vec_cpy(&player->pos, destination); } -void player_step(Player *player, Input *input, const Level *level) { +void player_step(Player *player, Input *input, const Level *level, uint step) { /* Get directionnal input and assign it to move.x/move.y: * i.e., if the player hold left and down move will have * move.x = -1 and move.y = 1. */ @@ -63,7 +64,7 @@ void player_step(Player *player, Input *input, const Level *level) { (INPUT_DOWN(K_DOWN) - INPUT_DOWN(K_UP)) }; /* other keys */ - bool kp_jump = INPUT_PRESSED(K_JUMP); + bool k_jump = INPUT_DOWN(K_JUMP); int xacc = move.x * ACCELERATION; /* calculate horizontal acceleration */ player->spd.x *= FRICTION; /* apply horizontal friction */ player->spd.x += xacc; /* apply horizontal acceleration */ @@ -72,7 +73,7 @@ void player_step(Player *player, Input *input, const Level *level) { * time after leaving a platform. */ if (player->grace) { player->grace -= 1; - if (kp_jump) { + if (k_jump && (INPUT_LAST_PRESS(K_JUMP) <= EARLY_UNITS)) { /* If the player try to jump and can, prevent * them to do it again and assign them the * corresponding y speed. */