diff options
| -rw-r--r-- | Makefile | 3 | ||||
| -rw-r--r-- | include/so_long.h | 12 | ||||
| -rw-r--r-- | src/collision.c | 105 | ||||
| -rw-r--r-- | src/init.c | 10 | ||||
| -rw-r--r-- | src/loop.c | 12 | ||||
| -rw-r--r-- | src/map_utils.c | 36 | ||||
| -rw-r--r-- | src/player_process.c | 41 |
7 files changed, 203 insertions, 16 deletions
@@ -6,7 +6,8 @@ HEADERS = -Iinclude -Ilibft -IMLX42/include LIBS = -Llibft -lft -lm -LMLX42/build -lmlx42 -ldl -lglfw -pthread VPATH := src -SRC = main.c init.c loop.c input.c draw.c tilemap.c +SRC = main.c init.c loop.c input.c draw.c tilemap.c player_process.c \ + collision.c map_utils.c OBJ_DIR := obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/include/so_long.h b/include/so_long.h index e9097d1..97ef5c7 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/08 14:14:02 by dkaiser #+# #+# */ -/* Updated: 2024/05/11 16:10:26 by dkaiser ### ########.fr */ +/* Updated: 2024/05/15 12:06:35 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,6 +55,7 @@ typedef struct s_player t_vector position; t_vector direction; t_vector velocity; + t_ivector size; mlx_image_t *img; } t_player; @@ -80,8 +81,17 @@ int load_map_from_file(t_tilemap *tilemap, char *filename); int init(t_game *game); void loop(void *params); +void player_process(t_game *game); int draw(t_game *game); int draw_map(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); +int check_collision(t_vector a_pos, t_ivector a_size, + t_vector b_pos, t_ivector b_size); +int check_wall_collision(t_vector a_pos, t_ivector a_size, + t_tilemap *map); +void move_and_slide(t_player *player, t_tilemap *map); #endif // SO_LONG_H diff --git a/src/collision.c b/src/collision.c new file mode 100644 index 0000000..d21e321 --- /dev/null +++ b/src/collision.c @@ -0,0 +1,105 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* collision.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/14 11:48:59 by dkaiser #+# #+# */ +/* Updated: 2024/05/15 12:06:55 by dkaiser ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "so_long.h" + +static int check_wall_collision_with_cell(t_vector a_pos, t_ivector a_size, + t_tilemap *map, t_ivector tile); + +/* + * Checks if a and b are colliding and returns the direction relative to a. + * Example: The top left edge of a and the bottom right of b overlap. + * In this case, check_collision() will return (UP | LEFT). + */ +int check_collision(t_vector a_pos, t_ivector a_size, t_vector b_pos, + t_ivector b_size) +{ + int result; + + result = 0; + if (a_pos.x < b_size.x + b_pos.x && a_pos.x + a_size.x > b_pos.x + && a_pos.y < b_size.y + b_pos.y && a_pos.y + a_size.y > b_pos.y) + { + if (a_pos.x < b_pos.x) + result |= RIGHT; + if (a_pos.x > b_pos.x) + result |= LEFT; + if (a_pos.y < b_pos.y) + result |= DOWN; + if (a_pos.y > b_pos.y) + result |= UP; + } + return (result); +} + +int check_wall_collision(t_vector a_pos, t_ivector a_size, t_tilemap *map) +{ + int result; + t_ivector a_tile; + t_ivector check_tile; + int x; + int y; + + result = 0; + a_tile = screen_to_grid_pos(a_pos, map->tile_size); + x = -1; + while (x <= 1) + { + y = -1; + while (y <= 1) + { + check_tile.x = a_tile.x + x; + check_tile.y = a_tile.y + y; + result |= check_wall_collision_with_cell(a_pos, a_size, map, + check_tile); + y++; + } + x++; + } + return (result); +} + +static int check_wall_collision_with_cell(t_vector a_pos, t_ivector a_size, + t_tilemap *map, t_ivector tile) +{ + t_vector wall_pos; + t_ivector wall_size; + + if (get_tile(map, tile.x, tile.y) == WALL) + { + wall_pos = grid_to_screen_pos(tile, map->tile_size); + wall_size.x = map->tile_size.x; + wall_size.y = map->tile_size.y; + return (check_collision(a_pos, a_size, wall_pos, wall_size)); + } + return (0); +} + +void move_and_slide(t_player *player, t_tilemap *map) +{ + t_vector move_pos; + + move_pos.x = player->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; +} @@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 14:50:09 by dkaiser #+# #+# */ -/* Updated: 2024/05/11 16:23:54 by dkaiser ### ########.fr */ +/* Updated: 2024/05/15 12:13:03 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,12 +40,14 @@ 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 * 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); } @@ -6,11 +6,10 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/09 15:09:24 by dkaiser #+# #+# */ -/* Updated: 2024/05/10 13:18:51 by dkaiser ### ########.fr */ +/* Updated: 2024/05/14 12:45:26 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "so_long.h" void loop(void *params) @@ -18,13 +17,6 @@ void loop(void *params) t_game *game; game = (t_game *)params; - game->player.direction.x = ((game->input_direction & RIGHT) != 0) - - ((game->input_direction & LEFT) != 0); - game->player.direction.y = ((game->input_direction & DOWN) != 0) - - ((game->input_direction & UP) != 0); - game->player.position.x += game->player.direction.x * PLAYER_MOVE_SPEED - * game->mlx->delta_time; - game->player.position.y += game->player.direction.y * PLAYER_MOVE_SPEED - * game->mlx->delta_time; + player_process(game); draw(game); } diff --git a/src/map_utils.c b/src/map_utils.c new file mode 100644 index 0000000..82acaa9 --- /dev/null +++ b/src/map_utils.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* map_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/14 13:19:34 by dkaiser #+# #+# */ +/* Updated: 2024/05/14 14:44:39 by dkaiser ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "so_long.h" + +t_vector grid_to_screen_pos(t_ivector grid_pos, t_ivector tile_size) +{ + t_vector screen_pos; + + screen_pos.x = grid_pos.x * tile_size.x; + screen_pos.y = grid_pos.y * tile_size.y; + return (screen_pos); +} + +t_ivector screen_to_grid_pos(t_vector screen_pos, t_ivector tile_size) +{ + t_ivector grid_pos; + + grid_pos.x = screen_pos.x / tile_size.x; + grid_pos.y = screen_pos.y / tile_size.y; + return (grid_pos); +} + +enum e_tile get_tile(t_tilemap *map, int x, int y) +{ + return map->tiles[y * map->grid_size.x + x]; +} diff --git a/src/player_process.c b/src/player_process.c new file mode 100644 index 0000000..52277f8 --- /dev/null +++ b/src/player_process.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* player_process.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/14 12:40:05 by dkaiser #+# #+# */ +/* Updated: 2024/05/15 12:03:52 by dkaiser ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_printf.h" +#include "so_long.h" + +static t_vector get_direction_from_input(t_game *game); + +void player_process(t_game *game) +{ + t_player *player; + + player = &game->player; + player->direction = get_direction_from_input(game); + player->velocity.x = player->direction.x * PLAYER_MOVE_SPEED + * game->mlx->delta_time; + player->velocity.y = player->direction.y * PLAYER_MOVE_SPEED + * game->mlx->delta_time; + + move_and_slide(player, &game->map); +} + +static t_vector get_direction_from_input(t_game *game) +{ + t_vector result; + + result.x = ((game->input_direction & RIGHT) != 0) + - ((game->input_direction & LEFT) != 0); + result.y = ((game->input_direction & DOWN) != 0) + - ((game->input_direction & UP) != 0); + return (result); +} |
