From 300128b8b0eaf4e6dd04c076887721dc645c939e Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 14 May 2024 12:45:49 +0200 Subject: [PATCH] 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); +} -- 2.47.2