diff --git a/include/conf.h b/include/conf.h index 648ad38..1275fe9 100644 --- a/include/conf.h +++ b/include/conf.h @@ -1,7 +1,7 @@ #define VEC_PRECISION 1024 -#define UPS 128 +#define UPS 256 #define PXS (VEC_PRECISION / UPS) -#define TILE_SIZE 8 * VEC_PRECISION +#define TILE_SIZE (8 * VEC_PRECISION) #define VEC_DCENTER (Vec){DWIDTH / 2, DHEIGHT / 2} #ifdef FX9860G diff --git a/src/main.c b/src/main.c index 3233200..a25783b 100644 --- a/src/main.c +++ b/src/main.c @@ -33,7 +33,7 @@ int play_level(uint level_id) { .spd = {0, 0}, .hbox = {TILE_SIZE - 1, TILE_SIZE - 1}, .vbox = {7, 7}, - .origin = {4 * VEC_PRECISION, 4 * VEC_PRECISION} + .origin = {0 * VEC_PRECISION, 0 * VEC_PRECISION} }; vec_cpy(&player.pos, player.origin); /* place the player at "0/0"*/ diff --git a/src/player.c b/src/player.c index 5dfdd3e..e7bdd5a 100644 --- a/src/player.c +++ b/src/player.c @@ -8,27 +8,26 @@ #include "collide.h" #define SGN(x) ((x > 0) ? (1) : ((x < 0) ? (-1) : (0))) +#define PLAYER_COLLIDE(pos) player_collide(player, pos, level, level->solid_layer) void player_move(Player *player, const Level *level) { - /* TODO: Replace with tile snapping and increase UPS back to 256 - * if possible. */ - const int sgn_spd_x = SGN(player->spd.x); - const int sgn_spd_y = SGN(player->spd.y); - /* Try to move the player according to their speed, and move - * them backwards while they are in a wall to snap them in a - * correct position. */ + /* TODO: Take into account player.origin and allow to snap for + * more than one tile. THIS CODE WILL BREAK IF THE PLAYER SPEED + * IS SUPERIOR THAN TILE_SIZE! */ + //const int sgn_spd_x = SGN(player->spd.x); + //const int sgn_spd_y = SGN(player->spd.y); Vec destination; vec_cpy(&destination, player->pos); + /* snap the player to the grid if they hit a wall */ destination.x += player->spd.x; - /* move player backwards until wall is no longer hit */ - while (player_collide(player, destination, level, level->solid_layer)) { - destination.x -= sgn_spd_x; + if (PLAYER_COLLIDE(destination)) { + destination.x = player->pos.x - player->pos.x % TILE_SIZE; player->spd.x = 0; } - /* do the same thing for y */ + /* do the same for y */ destination.y += player->spd.y; - while (player_collide(player, destination, level, level->solid_layer)) { - destination.y -= sgn_spd_y; + if (PLAYER_COLLIDE(destination)) { + destination.y = player->pos.y - player->pos.y % TILE_SIZE; player->spd.y = 0; } /* move the player to their new position */