From: Dominik Kaiser Date: Mon, 20 May 2024 18:30:41 +0000 (+0200) Subject: Add collectibles X-Git-Url: https://git.dkaiser.de/?a=commitdiff_plain;h=3ad8aa9e66f7e159b175a9a52e5ad00bbfd90734;p=42%2Fso_long.git Add collectibles Exit now only works after collecting everything. I don't really like my current solution, but it works for now. --- diff --git a/include/so_long.h b/include/so_long.h index b0332d0..6f3800d 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,7 +6,7 @@ /* By: dkaiser mlx, wall_texture); + collectible_texture = mlx_load_png("textures/collectible.png"); + game->map.collectible_img = mlx_texture_to_image(game->mlx, collectible_texture); mlx_resize_image(wall_image, 48, 48); + mlx_resize_image(game->map.collectible_img, 48, 48); game->map.tile_size.x = 48; game->map.tile_size.y = 48; x = 0; @@ -55,6 +59,9 @@ int draw_walls(t_game *game) if (game->map.tiles[y * game->map.grid_size.x + x] == WALL) mlx_image_to_window(game->mlx, wall_image, x * game->map.tile_size.x, y * game->map.tile_size.y); + if (game->map.tiles[y * game->map.grid_size.x + x] == COLLECTIBLE) + mlx_image_to_window(game->mlx, game->map.collectible_img, x + * game->map.tile_size.x, y * game->map.tile_size.y); y++; } x++; diff --git a/src/init.c b/src/init.c index 7431c26..40f15f1 100644 --- a/src/init.c +++ b/src/init.c @@ -6,7 +6,7 @@ /* By: dkaiser mlx = mlx_init(1920, 1080, "so_long", false); // TODO: make size and title dynamic game->input_direction = ZERO; + game->map.tile_size = (t_ivector){48, 48}; init_hooks(game); init_player(game); return (0); @@ -39,12 +40,10 @@ static void init_player(t_game *game) player = &game->player; texture = mlx_load_png("textures/player.png"); - 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->position = grid_to_screen_pos(game->map.player_start_tile, + game->map.tile_size); + player->velocity = (t_vector){0, 0}; + player->size = (t_ivector){44, 44}; player->img = mlx_texture_to_image(game->mlx, texture); mlx_resize_image(player->img, player->size.x, player->size.y); mlx_image_to_window(game->mlx, player->img, player->position.x, diff --git a/src/map_utils.c b/src/map_utils.c index 6d8fbd9..a03a1e0 100644 --- a/src/map_utils.c +++ b/src/map_utils.c @@ -6,7 +6,7 @@ /* By: dkaiser tiles[y * map->grid_size.x + x]); } + +void set_tile(t_tilemap *map, int x, int y, enum e_tile type) +{ + map->tiles[y * map->grid_size.x + x] = type; +} diff --git a/src/player_process.c b/src/player_process.c index 0d4f0c3..7df0d53 100644 --- a/src/player_process.c +++ b/src/player_process.c @@ -6,7 +6,7 @@ /* By: dkaiser direction = get_direction_from_input(game); player->velocity.x = player->direction.x * PLAYER_MOVE_SPEED; player->velocity.y += 50; - if (player->direction.y == -1 && is_on_floor( - (t_collider){player->position, + if (player->direction.y == -1 && is_on_floor((t_collider){player->position, player->size}, &game->map)) player->velocity.y = -1000; - if (check_exit(player, &game->map)) - exit(0); + handle_collectible_collision(player, &game->map); + handle_exit_collision(player, &game->map); move_and_slide(player, &game->map, game->mlx->delta_time); } -static int check_exit(t_actor *player, t_tilemap *map) +static void handle_collectible_collision(t_actor *player, t_tilemap *map) { - t_collider player_collider; + t_collider player_collider; + int collision; + t_ivector player_tile; player_collider = (t_collider){player->position, player->size}; - return (check_map_collision(player_collider, map, EXIT)); + player_tile = screen_to_grid_pos(player->position, map->tile_size); + collision = check_map_collision(player_collider, map, COLLECTIBLE); + if (collision & UP) + { + if (collision & LEFT) + collect_collectible(player_tile, map); + if (collision & RIGHT) + collect_collectible((t_ivector){player_tile.x + 1, player_tile.y}, + map); + } + if (collision & DOWN) + { + if (collision & LEFT) + collect_collectible((t_ivector){player_tile.x, player_tile.y + 1}, + map); + if (collision & RIGHT) + collect_collectible((t_ivector){player_tile.x + 1, player_tile.y + + 1}, map); + } +} + +static void collect_collectible(t_ivector pos, t_tilemap *map) +{ + size_t i; + t_vector collectible_pos; + t_ivector collectible_tile; + + ft_printf("Collected a collectile.\n"); + set_tile(map, pos.x, pos.y, EMPTY); + i = 0; + while (i < map->collectible_img->count) + { + collectible_pos.x = map->collectible_img->instances[i].x; + collectible_pos.y = map->collectible_img->instances[i].y; + collectible_tile = screen_to_grid_pos(collectible_pos, map->tile_size); + if (pos.x == collectible_tile.x && pos.y == collectible_tile.y) + { + map->collectible_img->instances[i].enabled = 0; + break ; + } + i++; + } } +static void handle_exit_collision(t_actor *player, t_tilemap *map) +{ + t_collider player_collider; + size_t i; + + player_collider = (t_collider){player->position, player->size}; + if (check_map_collision(player_collider, map, EXIT)) + { + i = 0; + while (i < map->collectible_img->count) + { + if (map->collectible_img->instances[i].enabled) + return ; + i++; + } + exit(0); + } +} static t_vector get_direction_from_input(t_game *game) { diff --git a/textures/collectible.png b/textures/collectible.png new file mode 100644 index 0000000..46f38dc Binary files /dev/null and b/textures/collectible.png differ diff --git a/textures/exit.png b/textures/exit.png new file mode 100644 index 0000000..52e45cc Binary files /dev/null and b/textures/exit.png differ