]> git.dkaiser.de - 42/so_long.git/commitdiff
Add collectibles
authorDominik Kaiser <dkaiser@1-C-7.42heilbronn.de>
Mon, 20 May 2024 18:30:41 +0000 (20:30 +0200)
committerDominik Kaiser <dkaiser@1-C-7.42heilbronn.de>
Mon, 20 May 2024 18:30:41 +0000 (20:30 +0200)
Exit now only works after collecting everything.
I don't really like my current solution, but it works for now.

include/so_long.h
src/draw.c
src/init.c
src/map_utils.c
src/player_process.c
textures/collectible.png [new file with mode: 0644]
textures/exit.png [new file with mode: 0644]

index b0332d01bab654d473def98af1c6ed4ec4f8f587..6f3800dbcc37fb6559823175aa0eb42995684785 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/08 14:14:02 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/05/15 17:31:34 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/05/20 20:07:16 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -72,6 +72,7 @@ typedef struct s_tilemap
        char            *tiles;
        t_ivector       player_start_tile;
        t_ivector       exit_tile;
+       mlx_image_t *collectible_img;
 }                              t_tilemap;
 
 typedef struct s_game
@@ -90,13 +91,15 @@ void                        loop(void *params);
 void                   player_process(t_game *game);
 int                            draw(t_game *game);
 int                            draw_walls(t_game *game);
-void draw_exit(t_game *game);
+void                   draw_exit(t_game *game);
 void                   on_key_input(mlx_key_data_t event, void *params);
 t_vector               grid_to_screen_pos(t_ivector grid_pos, t_ivector tile_size);
 t_ivector              screen_to_grid_pos(t_vector screen_pos, t_ivector tile_size);
 enum e_tile            get_tile(t_tilemap *map, int x, int y);
+void                   set_tile(t_tilemap *map, int x, int y, enum e_tile type);
 int                            check_collision(t_collider a, t_collider b);
-int                            check_map_collision(t_collider collider, t_tilemap *map, enum e_tile type);
+int                            check_map_collision(t_collider collider, t_tilemap *map,
+                                       enum e_tile type);
 void                   move_and_slide(t_actor *actor, t_tilemap *map,
                                        double delta_time);
 int                            is_on_floor(t_collider collider, t_tilemap *map);
index 4e7e4e8a80cba824353b0ef678452167d3e7230a..5284d7422d4be79dfd93fd898336a1eddc437876 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/09 17:58:23 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/05/15 17:31:11 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/05/20 20:10:15 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -40,10 +40,14 @@ int draw_walls(t_game *game)
        int                             y;
        mlx_texture_t   *wall_texture;
        mlx_image_t             *wall_image;
+       mlx_texture_t *collectible_texture;
 
        wall_texture = mlx_load_png("textures/wall.png");
        wall_image = mlx_texture_to_image(game->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++;
index 7431c26d3b5ed5b492811fa240c7a325e47034f8..40f15f1dd904d66bc89793ce22f69b04ea18e447 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/09 14:50:09 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/05/15 17:19:06 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/05/20 18:31:33 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -21,6 +21,7 @@ int   init(t_game *game)
        game->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,
index 6d8fbd901b7b10a4963ba997882af3d0d22a5dea..a03a1e0b8a2d590d1cd022cee87deae7c68ab40a 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/14 13:19:34 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/05/15 15:04:31 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/05/20 17:52:49 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -34,3 +34,8 @@ enum e_tile   get_tile(t_tilemap *map, int x, int y)
 {
        return (map->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;
+}
index 0d4f0c345741293105e1d9bc9790c497478de491..7df0d53a32c9ad0a06a0cbf24222ca075ddf972d 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/14 12:40:05 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/05/15 17:53:54 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/05/20 20:29:07 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -14,7 +14,9 @@
 #include "so_long.h"
 
 static t_vector        get_direction_from_input(t_game *game);
-static int check_exit(t_actor *player, t_tilemap *map);
+static void            handle_collectible_collision(t_actor *player, t_tilemap *map);
+static void            collect_collectible(t_ivector pos, t_tilemap *map);
+static void            handle_exit_collision(t_actor *player, t_tilemap *map);
 
 void   player_process(t_game *game)
 {
@@ -24,23 +26,83 @@ void        player_process(t_game *game)
        player->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 (file)
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 (file)
index 0000000..52e45cc
Binary files /dev/null and b/textures/exit.png differ