Input buffering system, implemented for jumping.

This commit is contained in:
KikooDX 2020-12-27 01:45:58 +01:00
parent b8f66a4f05
commit 1a9052c0a2
4 changed files with 7 additions and 6 deletions

View File

@ -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);

View File

@ -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) {

View File

@ -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);
}

View File

@ -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. */