diff options
| author | Dominik Kaiser | 2024-05-14 12:45:49 +0200 |
|---|---|---|
| committer | Dominik Kaiser | 2024-05-14 12:45:49 +0200 |
| commit | 300128b8b0eaf4e6dd04c076887721dc645c939e (patch) | |
| tree | 42ade0794bb118ded99c0528dde17d59c78eb047 /src | |
| parent | 7b021c4426f21bf20fed61459ddedb5224e25bc4 (diff) | |
| download | so_long-300128b8b0eaf4e6dd04c076887721dc645c939e.tar.gz so_long-300128b8b0eaf4e6dd04c076887721dc645c939e.zip | |
Outsource player processing and add collision func
Diffstat (limited to 'src')
| -rw-r--r-- | src/collision.c | 35 | ||||
| -rw-r--r-- | src/loop.c | 12 | ||||
| -rw-r--r-- | src/player_process.c | 40 |
3 files changed, 77 insertions, 10 deletions
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 <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/14 11:48:59 by dkaiser #+# #+# */ +/* Updated: 2024/05/14 12:26:02 by dkaiser ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "so_long.h" + +int check_collision(t_vector a_pos, t_vector a_size, t_vector b_pos, + t_vector 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); +} + @@ -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/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 <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/14 12:40:05 by dkaiser #+# #+# */ +/* Updated: 2024/05/14 12:44:17 by dkaiser ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#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; + 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); +} |
