Exit now only works after collecting everything.
I don't really like my current solution, but it works for now.
/* 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 */
/* */
/* ************************************************************************** */
char *tiles;
t_ivector player_start_tile;
t_ivector exit_tile;
+ mlx_image_t *collectible_img;
} t_tilemap;
typedef struct s_game
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);
/* 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 */
/* */
/* ************************************************************************** */
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;
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++;
/* 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 */
/* */
/* ************************************************************************** */
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);
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,
/* 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 */
/* */
/* ************************************************************************** */
{
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;
+}
/* 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 */
/* */
/* ************************************************************************** */
#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)
{
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)
{