From a5c414dd4cdd8ac04e28fa40b8d4d092a2f805fe Mon Sep 17 00:00:00 2001 From: KikooDX Date: Wed, 23 Dec 2020 23:22:02 +0100 Subject: [PATCH] Jump (yay) and grace frames support. --- include/input.h | 3 ++- include/player.h | 3 +++ src/input.c | 1 + src/main.c | 3 ++- src/player.c | 25 ++++++++++++++++++++++++- 5 files changed, 32 insertions(+), 3 deletions(-) diff --git a/include/input.h b/include/input.h index e31087b..dc2c13d 100644 --- a/include/input.h +++ b/include/input.h @@ -4,13 +4,14 @@ #include #include -#define KEYS_COUNT 5 +#define KEYS_COUNT 6 enum { K_LEFT, K_RIGHT, K_UP, K_DOWN, + K_JUMP, K_EXIT }; diff --git a/include/player.h b/include/player.h index 31f1671..186a8ee 100644 --- a/include/player.h +++ b/include/player.h @@ -1,6 +1,8 @@ #ifndef _DEF_PLAYER #define _DEF_PLAYER +#include + #include "vec.h" typedef struct Player { @@ -9,6 +11,7 @@ typedef struct Player { Vec hbox; /* the bottom left corner of the player's hitbox */ Vec vbox; /* the bottom left corner of the player's visual box */ Vec origin; /* the origin of the player (offset) */ + uint grace; /* coyot jump */ } Player; #include "level.h" diff --git a/src/input.c b/src/input.c index 3912cb2..cf5581a 100644 --- a/src/input.c +++ b/src/input.c @@ -28,6 +28,7 @@ void input_init(Input *input) { input->keys[K_RIGHT] = KEY_RIGHT; input->keys[K_UP] = KEY_UP; input->keys[K_DOWN] = KEY_DOWN; + input->keys[K_JUMP] = KEY_SHIFT; input->keys[K_EXIT] = KEY_EXIT; for (int i = 0; i < KEYS_COUNT; ++i) { input->states[i] = S_UP; diff --git a/src/main.c b/src/main.c index a25783b..7129ce4 100644 --- a/src/main.c +++ b/src/main.c @@ -33,7 +33,8 @@ int play_level(uint level_id) { .spd = {0, 0}, .hbox = {TILE_SIZE - 1, TILE_SIZE - 1}, .vbox = {7, 7}, - .origin = {0 * VEC_PRECISION, 0 * VEC_PRECISION} + .origin = {0 * VEC_PRECISION, 0 * VEC_PRECISION}, + .grace = 0 }; vec_cpy(&player.pos, player.origin); /* place the player at "0/0"*/ diff --git a/src/player.c b/src/player.c index a876f83..ec5dd09 100644 --- a/src/player.c +++ b/src/player.c @@ -1,4 +1,5 @@ #include +#include #include "player.h" #include "vec.h" @@ -12,6 +13,8 @@ #define FRICTION 0.9 #define ACCELERATION (MAX_SPD * (1 - FRICTION)) #define GRAVITY PXS +#define JUMP_SPD (-128 * PXS) +#define GRACE_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) @@ -42,6 +45,9 @@ void player_move(Player *player, const Level *level) { destination.y += TILE_SIZE * sgn_spd_y; } destination.y -= TILE_SIZE * sgn_spd_y; + if (sgn_spd_y > 0) { /* the player was falling */ + player->grace = GRACE_UNITS; + } player->spd.y = 0; } /* move the player to their new position */ @@ -49,15 +55,32 @@ void player_move(Player *player, const Level *level) { } void player_step(Player *player, Input *input, const Level *level) { + /* 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. */ Vec move = { (input_is_down(input, K_RIGHT) - input_is_down(input, K_LEFT)), (input_is_down(input, K_DOWN) - input_is_down(input, K_UP)) }; + /* other keys */ + bool kp_jump = input_is_pressed(input, K_JUMP); int xacc = move.x * ACCELERATION; /* calculate horizontal acceleration */ player->spd.x *= FRICTION; /* apply horizontal friction */ player->spd.x += xacc; /* apply horizontal acceleration */ player->spd.y += GRAVITY; /* apply gravity */ - player_move(player, level); + /* Grace frames allow the player to jump a short + * time after leaving a platform. */ + if (player->grace) { + player->grace -= 1; + if (kp_jump) { + /* If the player try to jump and can, prevent + * them to do it again and assign them the + * corresponding y speed. */ + player->grace = 0; + player->spd.y = JUMP_SPD; + } + } + player_move(player, level); /* move the player according to their speed */ } void player_draw(Player *player, Camera *camera) {