From 86430b853a3fce52b56fa105b0c075078a4f2143 Mon Sep 17 00:00:00 2001 From: mibi88 <76903855+mibi88@users.noreply.github.com> Date: Sun, 9 Jul 2023 15:45:46 +0200 Subject: [PATCH] Collisions are now completely working, just need to fill the array of all tiles where the player can't go through. --- src/map.c | 4 ++-- src/player.c | 16 +++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/map.c b/src/map.c index 0e86663..bae29d9 100644 --- a/src/map.c +++ b/src/map.c @@ -78,8 +78,8 @@ void render_map(Player *player, Map *map_level) { ytile = (tile / map_level->tileset_size) * T_HEIGHT; /* render */ dsubimage(x*T_WIDTH-mx, y*T_HEIGHT-my, - map_level->tileset, xtile, ytile, T_WIDTH, T_HEIGHT, - DIMAGE_NONE); + map_level->tileset, xtile, ytile, T_WIDTH, + T_HEIGHT, DIMAGE_NONE); } } } diff --git a/src/player.c b/src/player.c index bc2efcc..5a9a2e9 100644 --- a/src/player.c +++ b/src/player.c @@ -43,7 +43,8 @@ void player_move(Map *map_level, Player *player, Direction direction) { /* How this player movement will modify the player x and y. */ const char dx = one_px_mov[direction*2]*SPEED; const char dy = one_px_mov[direction*2+1]*SPEED; - /* If the player will collide with a hard tile. */ + /* If the player will collide with a hard tile or if the will go outside of + * the map. */ if(player_collision(map_level, player, direction, P_CENTER)){ /* If the will collide with the center of the player. */ player_fix_position(player, dx, dy); @@ -79,11 +80,16 @@ bool player_collision(Map *map_level, Player *player, Direction direction, }else if(!dy){ dy += nomov_axis_check; } - dx = dx*(P_WIDTH/2+(nomov_axis_check == P_CENTER)); - dy = dy*(P_HEIGHT/2+(nomov_axis_check == P_CENTER)); + dx = dx*(P_WIDTH/2+1); + dy = dy*(P_HEIGHT/2+1); /* The tile he will go to. */ - int player_tile_x = (player->x+dx)/T_WIDTH; - int player_tile_y = (player->y+dy)/T_HEIGHT; + int player_tile_x = player->x+dx; + int player_tile_y = player->y+dy; + /* Handle a negative position diffrently than a positive one. */ + if(player_tile_x < 0) player_tile_x = player_tile_x/T_WIDTH-1; + else player_tile_x = player_tile_x/T_WIDTH; + if(player_tile_y < 0) player_tile_y = player_tile_y/T_HEIGHT-1; + else player_tile_y = player_tile_y/T_HEIGHT; for(i=0;inblayers;i++){ /* if he's on a hard tile */ if(is_in((short int*)hard_tiles, HARD_TILES_AMOUNT,