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 --- src/collision.c | 35 +++++++++++++++++++++++++++++++++++ src/loop.c | 12 ++---------- src/player_process.c | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 src/collision.c create mode 100644 src/player_process.c (limited to 'src') diff --git a/src/collision.c b/src/collision.c new file mode 100644 index 0000000..fee1452 --- /dev/null +++ b/src/collision.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* collision.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* 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