From baa1a72703bff638a723db7f5ddd8f236b2ccae3 Mon Sep 17 00:00:00 2001 From: KikooDX Date: Mon, 21 Dec 2020 17:50:00 +0100 Subject: [PATCH] Added speed value to the player, separated collision/movement from it's main step event. TODO in player.c. --- include/player.h | 2 ++ src/main.c | 1 + src/player.c | 42 +++++++++++++++++++++++++++--------------- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/include/player.h b/include/player.h index 4fb7545..31f1671 100644 --- a/include/player.h +++ b/include/player.h @@ -5,6 +5,7 @@ typedef struct Player { Vec pos; + Vec spd; /* velocity */ 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) */ @@ -14,6 +15,7 @@ typedef struct Player { #include "camera.h" #include "input.h" +void player_move(Player *player, const Level *level); void player_step(Player *player, Input *input, const Level *level); 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/main.c b/src/main.c index 57d7045..c936b35 100644 --- a/src/main.c +++ b/src/main.c @@ -30,6 +30,7 @@ int play_level(uint level_id) { /* create player */ Player player = { //.pos = {TILE_SIZE, TILE_SIZE}, + .spd = {0, 0}, .hbox = {7 * VEC_PRECISION - 1, 7 * VEC_PRECISION - 1}, .vbox = {6, 6}, .origin = {4 * VEC_PRECISION, 4 * VEC_PRECISION} diff --git a/src/player.c b/src/player.c index 9e542a0..c75d88f 100644 --- a/src/player.c +++ b/src/player.c @@ -1,12 +1,36 @@ #include #include "player.h" +#include "vec.h" #include "conf.h" #include "camera.h" #include "input.h" #include "collide.h" +#define SGN(x) ((x > 0) ? (1) : ((x < 0) ? (-1) : (0))) + void player_move(Player *player, const Level *level) { + /* Try to move the player to a destination, and stop their + * momentum if they hit a wall. The player can move if they're + * stuck in a block, to avoid softlock in some situations. If + * the destination is solid and they aren't in a block, then we + * use the costy player_move function to snap them and reset + * their speed accordingly. + */ + Vec destination; + vec_cpy(&destination, player->pos); + vec_add(&destination, player->spd); + if (collide_full(player, player->pos, level, level->solid_layer) || + !collide_full(player, destination, level, level->solid_layer)) { + vec_cpy(&player->pos, destination); + } + else { + /* TODO + * Player can't move to destination, snap them. First + * move on x axis, then y axis. + */ + uint i = 0; + } } void player_step(Player *player, Input *input, const Level *level) { @@ -14,21 +38,9 @@ void player_step(Player *player, Input *input, const Level *level) { (input_is_down(input, K_RIGHT) - input_is_down(input, K_LEFT)), (input_is_down(input, K_DOWN) - input_is_down(input, K_UP)) }; - vec_mul(&move, 32 * PXS); /* set speed */ - Vec destination; - vec_cpy(&destination, player->pos); - vec_add(&destination, move); - /* The player can move if they're stuck in a block, to avoid - * softlock in some situations. If the destination is solid and - * they aren't in a block, then we use the costy player_move - * function to snap them and reset their speed accordingly. - */ - if (collide_full(player, player->pos, level, level->solid_layer) || - !collide_full(player, destination, level, level->solid_layer)) { - vec_cpy(&player->pos, destination); - } - else { - } + vec_mul(&move, 32 * PXS); /* calculate speed */ + vec_cpy(&player->spd, move); + player_move(player, level); } void player_draw(Player *player, Camera *camera) {