From: Dominik Kaiser Date: Wed, 15 May 2024 10:16:05 +0000 (+0200) Subject: Change collision checking and add player.size X-Git-Url: https://git.dkaiser.de/?a=commitdiff_plain;h=ed5342399d02ba5d1032a2ef99afdc1c875ed4d8;p=42%2Fso_long.git Change collision checking and add player.size Collision checking will now be done using the move_and_slide function. Instead of tile_size, now player.size will be used for checking collisions. --- diff --git a/include/so_long.h b/include/so_long.h index cddfa9e..97ef5c7 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,7 +6,7 @@ /* By: dkaiser position.x + player->velocity.x; + move_pos.y = player->position.y; + + if ((check_wall_collision(move_pos, player->size, map) & (RIGHT | LEFT)) == 0) + player->position.x = move_pos.x; + else + player->velocity.x = 0; + + move_pos.x = player->position.x; + move_pos.y = player->position.y + player->velocity.y; + if ((check_wall_collision(move_pos, player->size, map) & (UP | DOWN)) == 0) + player->position.y = move_pos.y; + else + player->velocity.y = 0; +} diff --git a/src/init.c b/src/init.c index 5277b5c..9959da6 100644 --- a/src/init.c +++ b/src/init.c @@ -6,7 +6,7 @@ /* By: dkaiser player; texture = mlx_load_png("textures/player.png"); - player->position.x = game->map.player_start_tile.x * 48; - player->position.y = game->map.player_start_tile.y * 48; + player->position.x = game->map.player_start_tile.x * game->map.grid_size.x; + player->position.y = game->map.player_start_tile.y * game->map.grid_size.y; player->velocity.x = 0; player->velocity.y = 0; + player->size.x = 44; + player->size.y = 44; player->img = mlx_texture_to_image(game->mlx, texture); - mlx_resize_image(player->img, 48, 48); + mlx_resize_image(player->img, player->size.x, player->size.y); mlx_image_to_window(game->mlx, player->img, player->position.x, player->position.y); } diff --git a/src/player_process.c b/src/player_process.c index 5fcf994..52277f8 100644 --- a/src/player_process.c +++ b/src/player_process.c @@ -6,7 +6,7 @@ /* By: dkaiser player; player->direction = get_direction_from_input(game); @@ -28,20 +26,7 @@ void player_process(t_game *game) player->velocity.y = player->direction.y * PLAYER_MOVE_SPEED * game->mlx->delta_time; - 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; + move_and_slide(player, &game->map); } static t_vector get_direction_from_input(t_game *game)