diff options
| -rw-r--r-- | Makefile | 3 | ||||
| -rw-r--r-- | include/so_long.h | 6 | ||||
| -rw-r--r-- | src/collision.c | 35 | ||||
| -rw-r--r-- | src/loop.c | 12 | ||||
| -rw-r--r-- | src/player_process.c | 40 |
5 files changed, 84 insertions, 12 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 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 <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/14 12:41:08 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -80,8 +80,12 @@ 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); +int check_collision(t_vector a_pos, t_vector a_size, t_vector b_pos, + t_vector b_size); + #endif // SO_LONG_H 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); +} |
