From 300128b8b0eaf4e6dd04c076887721dc645c939e Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 14 May 2024 12:45:49 +0200 Subject: Outsource player processing and add collision func --- Makefile | 3 ++- include/so_long.h | 6 +++++- src/collision.c | 35 +++++++++++++++++++++++++++++++++++ src/loop.c | 12 ++---------- src/player_process.c | 40 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 12 deletions(-) create mode 100644 src/collision.c create mode 100644 src/player_process.c diff --git a/Makefile b/Makefile index 763c897..84d93f3 100644 --- a/Makefile +++ b/Makefile @@ -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 OBJ_DIR := obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/include/so_long.h b/include/so_long.h index e9097d1..bd40371 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,7 +6,7 @@ /* By: dkaiser 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); +} + diff --git a/src/loop.c b/src/loop.c index 62b5ed9..c4f95b3 100644 --- a/src/loop.c +++ b/src/loop.c @@ -6,11 +6,10 @@ /* By: dkaiser 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/player_process.c b/src/player_process.c new file mode 100644 index 0000000..8cabe9a --- /dev/null +++ b/src/player_process.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* player_process.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser 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; + player.position.x += player.velocity.x; + player.position.y += player.velocity.y; +} + +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); +} -- cgit v1.2.3 From 11c24e171b4dca0b751fc3f43b4d72121106599e Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 14 May 2024 14:12:42 +0200 Subject: Add map_utils.c --- Makefile | 2 +- include/so_long.h | 5 +++-- src/map_utils.c | 31 +++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 src/map_utils.c diff --git a/Makefile b/Makefile index 84d93f3..51de300 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ 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 player_process.c \ - collision.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 bd40371..6834620 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,7 +6,7 @@ /* By: dkaiser 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; - } + { + 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); +} diff --git a/src/map_utils.c b/src/map_utils.c index 54577a8..82acaa9 100644 --- a/src/map_utils.c +++ b/src/map_utils.c @@ -6,7 +6,7 @@ /* By: dkaiser tiles[y * map->grid_size.x + x]; +} diff --git a/src/player_process.c b/src/player_process.c index 8cabe9a..5fcf994 100644 --- a/src/player_process.c +++ b/src/player_process.c @@ -6,26 +6,42 @@ /* By: dkaiser player; - player.direction = get_direction_from_input(game); - player.velocity.x = player.direction.x * PLAYER_MOVE_SPEED + 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 + player->velocity.y = player->direction.y * PLAYER_MOVE_SPEED * game->mlx->delta_time; - player.position.x += player.velocity.x; - player.position.y += player.velocity.y; + + collision_pos.x = player->position.x + player->velocity.x; + collision_pos.y = player->position.y + player->velocity.y; + collision = check_wall_collision(collision_pos, game->map.tile_size, &game->map); + if (collision & LEFT && player->velocity.x < 0) + player->velocity.x = 0; + if (collision & RIGHT && player->velocity.x > 0) + player->velocity.x = 0; + if (collision & UP && player->velocity.y < 0) + player->velocity.y = 0; + if (collision & DOWN && player->velocity.y > 0) + player->velocity.y = 0; + + player->position.x += player->velocity.x; + player->position.y += player->velocity.y; } static t_vector get_direction_from_input(t_game *game) -- cgit v1.2.3 From ed5342399d02ba5d1032a2ef99afdc1c875ed4d8 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Wed, 15 May 2024 12:16:05 +0200 Subject: Change collision checking and add player.size Collision checking will now be done using the move_and_slide function. Instead of tile_size, now player.size will be used for checking collisions. --- include/so_long.h | 4 +++- src/collision.c | 24 ++++++++++++++++++++++-- src/init.c | 10 ++++++---- src/player_process.c | 19 ++----------------- 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/include/so_long.h b/include/so_long.h index cddfa9e..97ef5c7 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,7 +6,7 @@ /* By: dkaiser 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; +} diff --git a/src/init.c b/src/init.c index 5277b5c..9959da6 100644 --- a/src/init.c +++ b/src/init.c @@ -6,7 +6,7 @@ /* By: dkaiser 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); } diff --git a/src/player_process.c b/src/player_process.c index 5fcf994..52277f8 100644 --- a/src/player_process.c +++ b/src/player_process.c @@ -6,7 +6,7 @@ /* By: dkaiser player; player->direction = get_direction_from_input(game); @@ -28,20 +26,7 @@ void player_process(t_game *game) player->velocity.y = player->direction.y * PLAYER_MOVE_SPEED * game->mlx->delta_time; - collision_pos.x = player->position.x + player->velocity.x; - collision_pos.y = player->position.y + player->velocity.y; - collision = check_wall_collision(collision_pos, game->map.tile_size, &game->map); - if (collision & LEFT && player->velocity.x < 0) - player->velocity.x = 0; - if (collision & RIGHT && player->velocity.x > 0) - player->velocity.x = 0; - if (collision & UP && player->velocity.y < 0) - player->velocity.y = 0; - if (collision & DOWN && player->velocity.y > 0) - player->velocity.y = 0; - - player->position.x += player->velocity.x; - player->position.y += player->velocity.y; + move_and_slide(player, &game->map); } static t_vector get_direction_from_input(t_game *game) -- cgit v1.2.3