diff options
| author | Dominik Kaiser | 2024-05-29 13:11:19 +0200 |
|---|---|---|
| committer | GitHub | 2024-05-29 13:11:19 +0200 |
| commit | 7b230503231e154c5400413b4f9b87dd6fcd8304 (patch) | |
| tree | aaa06976a569ad3f3452121800e6bf33fc8d14ac /src | |
| parent | cb6f98a5fa7bb2ed361abe68b3000f2e3f578ea7 (diff) | |
| parent | 3ad8aa9e66f7e159b175a9a52e5ad00bbfd90734 (diff) | |
| download | so_long-7b230503231e154c5400413b4f9b87dd6fcd8304.tar.gz so_long-7b230503231e154c5400413b4f9b87dd6fcd8304.zip | |
Merge collectibles-and-exit into master
Collectibles and exit
Diffstat (limited to 'src')
| -rw-r--r-- | src/draw.c | 36 | ||||
| -rw-r--r-- | src/init.c | 19 | ||||
| -rw-r--r-- | src/main.c | 3 | ||||
| -rw-r--r-- | src/map_utils.c | 7 | ||||
| -rw-r--r-- | src/player_process.c | 80 |
5 files changed, 122 insertions, 23 deletions
@@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 17:58:23 by dkaiser #+# #+# */ -/* Updated: 2024/05/15 16:41:15 by dkaiser ### ########.fr */ +/* Updated: 2024/05/20 20:10:15 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,16 +14,40 @@ #include "libft.h" #include "so_long.h" +int draw(t_game *game) +{ + game->player.img->instances[0].x = game->player.position.x; + game->player.img->instances[0].y = game->player.position.y; + return (0); +} + +void draw_exit(t_game *game) +{ + mlx_texture_t *exit_texture; + mlx_image_t *exit_image; + t_vector pos; + + exit_texture = mlx_load_png("textures/exit.png"); + exit_image = mlx_texture_to_image(game->mlx, exit_texture); + mlx_resize_image(exit_image, game->map.tile_size.x, game->map.tile_size.y); + pos = grid_to_screen_pos(game->map.exit_tile, game->map.tile_size); + mlx_image_to_window(game->mlx, exit_image, pos.x, pos.y); +} + int draw_walls(t_game *game) { int x; 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; @@ -35,16 +59,12 @@ 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++; } return (0); } - -int draw(t_game *game) -{ - game->player.img->instances[0].x = game->player.position.x; - game->player.img->instances[0].y = game->player.position.y; - return (0); -} @@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 14:50:09 by dkaiser #+# #+# */ -/* Updated: 2024/05/15 15:09:44 by dkaiser ### ########.fr */ +/* Updated: 2024/05/20 18:31:33 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,15 +14,16 @@ #include "so_long.h" static void init_hooks(t_game *game); -static void init_actor(t_game *game); +static void init_player(t_game *game); 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_actor(game); + init_player(game); return (0); } @@ -32,19 +33,17 @@ static void init_hooks(t_game *game) mlx_key_hook(game->mlx, on_key_input, game); } -static void init_actor(t_game *game) +static void init_player(t_game *game) { mlx_texture_t *texture; t_actor *player; 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, @@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/08 14:14:13 by dkaiser #+# #+# */ -/* Updated: 2024/05/15 16:41:35 by dkaiser ### ########.fr */ +/* Updated: 2024/05/15 17:31:46 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,5 +22,6 @@ int main(int argc, char *argv[]) return (1); init(&game); draw_walls(&game); + draw_exit(&game); mlx_loop(game.mlx); } 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 <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; +} diff --git a/src/player_process.c b/src/player_process.c index c285c4d..7df0d53 100644 --- a/src/player_process.c +++ b/src/player_process.c @@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/14 12:40:05 by dkaiser #+# #+# */ -/* Updated: 2024/05/15 16:55:29 by dkaiser ### ########.fr */ +/* Updated: 2024/05/20 20:29:07 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,9 @@ #include "so_long.h" static t_vector get_direction_from_input(t_game *game); +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) { @@ -23,13 +26,84 @@ 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; + handle_collectible_collision(player, &game->map); + handle_exit_collision(player, &game->map); move_and_slide(player, &game->map, game->mlx->delta_time); } +static void handle_collectible_collision(t_actor *player, t_tilemap *map) +{ + t_collider player_collider; + int collision; + t_ivector player_tile; + + player_collider = (t_collider){player->position, player->size}; + 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) { t_vector result; |
