From: Dominik Kaiser Date: Tue, 14 May 2024 13:13:26 +0000 (+0200) Subject: Implement (somewhat) functional collision X-Git-Url: https://git.dkaiser.de/?a=commitdiff_plain;h=5a7e68ce0331077594dc2d85c81fc087d80734c7;p=42%2Fso_long.git Implement (somewhat) functional collision --- diff --git a/include/so_long.h b/include/so_long.h index 6834620..cddfa9e 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,7 +6,7 @@ /* By: dkaiser b_pos.x && a_pos.y < b_size.y + b_pos.y && a_pos.y + a_size.y > b_pos.y) - { - if (a_pos.x < b_pos.x) - result |= RIGHT; - if (a_pos.x > b_pos.x) - result |= LEFT; - if (a_pos.y < b_pos.y) - result |= DOWN; - if (a_pos.y > b_pos.y) - result |= UP; - } + { + if (a_pos.x < b_pos.x) + result |= RIGHT; + if (a_pos.x > b_pos.x) + result |= LEFT; + if (a_pos.y < b_pos.y) + result |= DOWN; + if (a_pos.y > b_pos.y) + result |= UP; + } return (result); } +int check_wall_collision(t_vector a_pos, t_ivector a_size, t_tilemap *map) +{ + int result; + t_ivector a_tile; + t_ivector check_tile; + int x; + int y; + + result = 0; + a_tile = screen_to_grid_pos(a_pos, map->tile_size); + x = -1; + while (x <= 1) + { + y = -1; + while (y <= 1) + { + check_tile.x = a_tile.x + x; + check_tile.y = a_tile.y + y; + result |= check_wall_collision_with_cell(a_pos, a_size, map, + check_tile); + y++; + } + x++; + } + return (result); +} + +static int check_wall_collision_with_cell(t_vector a_pos, t_ivector a_size, + t_tilemap *map, t_ivector tile) +{ + t_vector wall_pos; + t_ivector wall_size; + + if (get_tile(map, tile.x, tile.y) == WALL) + { + wall_pos = grid_to_screen_pos(tile, map->tile_size); + wall_size.x = map->tile_size.x; + wall_size.y = map->tile_size.y; + return (check_collision(a_pos, a_size, wall_pos, wall_size)); + } + return (0); +} diff --git a/src/map_utils.c b/src/map_utils.c index 54577a8..82acaa9 100644 --- a/src/map_utils.c +++ b/src/map_utils.c @@ -6,7 +6,7 @@ /* By: dkaiser tiles[y * map->grid_size.x + x]; +} diff --git a/src/player_process.c b/src/player_process.c index 8cabe9a..5fcf994 100644 --- a/src/player_process.c +++ b/src/player_process.c @@ -6,26 +6,42 @@ /* By: dkaiser player; - player.direction = get_direction_from_input(game); - player.velocity.x = player.direction.x * PLAYER_MOVE_SPEED + player = &game->player; + player->direction = get_direction_from_input(game); + player->velocity.x = player->direction.x * PLAYER_MOVE_SPEED * game->mlx->delta_time; - player.velocity.y = player.direction.y * PLAYER_MOVE_SPEED + player->velocity.y = player->direction.y * PLAYER_MOVE_SPEED * game->mlx->delta_time; - player.position.x += player.velocity.x; - player.position.y += player.velocity.y; + + collision_pos.x = player->position.x + player->velocity.x; + collision_pos.y = player->position.y + player->velocity.y; + collision = check_wall_collision(collision_pos, game->map.tile_size, &game->map); + if (collision & LEFT && player->velocity.x < 0) + player->velocity.x = 0; + if (collision & RIGHT && player->velocity.x > 0) + player->velocity.x = 0; + if (collision & UP && player->velocity.y < 0) + player->velocity.y = 0; + if (collision & DOWN && player->velocity.y > 0) + player->velocity.y = 0; + + player->position.x += player->velocity.x; + player->position.y += player->velocity.y; } static t_vector get_direction_from_input(t_game *game)