diff --git a/Makefile b/Makefile index ae58812..b1c84f6 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,10 @@ # ::: :::::::: # # Makefile :+: :+: :+: # # +:+ +:+ +:+ # -# By: tsuchen +#+ +:+ +#+ # +# By: jteissie +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/08/15 12:02:08 by tsuchen #+# #+# # -# Updated: 2024/09/02 11:11:09 by tsuchen ### ########.fr # +# Updated: 2024/09/02 15:07:22 by jteissie ### ########.fr # # # # **************************************************************************** # @@ -15,7 +15,7 @@ NAME = cub3d SRCS_M = main.c \ game_init.c \ cleanup.c \ - color_utils.c + color_utils.c SRCS_PS = parser.c \ @@ -24,7 +24,7 @@ SRCS_PS = parser.c \ get_textures.c \ get_textures_utils.c -SRCS_EV = key_events.c wall_collision.c +SRCS_EV = key_events.c wall_collision.c mouse_events.c SRCS_MAP = map.c diff --git a/includes/cub3d.h b/includes/cub3d.h index fb61857..c3b31a6 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,7 +6,7 @@ /* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/15 00:19:44 by tsuchen #+# #+# */ -/* Updated: 2024/09/02 14:12:16 by jteissie ### ########.fr */ +/* Updated: 2024/09/02 15:07:28 by jteissie ### ########.fr */ /* */ /* ************************************************************************** */ @@ -127,6 +127,7 @@ int cleanup(t_data *data); int game_init(t_data *data); int key_events(int keycode, t_data *data); void move_check(t_vec *step, t_data *data, int add_or_sub); +int mouse_press(int button, int x, int y, t_data *data); /* color utils*/ int create_trgb(int t, int r, int g, int b); int get_color(int trgb, char index); diff --git a/srcs/events/key_events.c b/srcs/events/key_events.c index 367c5d0..446b58f 100644 --- a/srcs/events/key_events.c +++ b/srcs/events/key_events.c @@ -6,7 +6,7 @@ /* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/27 18:40:50 by jteissie #+# #+# */ -/* Updated: 2024/09/02 14:53:27 by jteissie ### ########.fr */ +/* Updated: 2024/09/02 15:07:28 by jteissie ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,11 +54,6 @@ static void move_camera(t_data *data, t_rot dir) return ; } -// static void interact_door(t_data *data) -// { -// (void)data; -// return ; -// } int key_events(int keycode, t_data *data) { diff --git a/srcs/events/mouse_events.c b/srcs/events/mouse_events.c new file mode 100644 index 0000000..4e96666 --- /dev/null +++ b/srcs/events/mouse_events.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mouse_events.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jteissie +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/02 15:02:38 by jteissie #+# #+# */ +/* Updated: 2024/09/02 15:50:21 by jteissie ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +static double get_door_dist(t_vec *ray, t_data *data) +{ + t_vec ray_pos; + t_vec unit_dist; + t_vec dist_ray; + + ray->door = FALSE; + vec_init(&ray_pos, data->p_pos.x, data->p_pos.y); + unit_dist.x = sqrt(1 + ((ray->y * ray->y) / (ray->x * ray->x))); + unit_dist.y = sqrt(1 + ((ray->x * ray->x) / (ray->y * ray->y))); + rc_ray_init(&dist_ray, &ray_pos, ray, &unit_dist); + while (1) + { + data->side = rc_dda(&dist_ray, &unit_dist, &ray_pos, ray); + if (data->map[(int)ray_pos.x][(int)ray_pos.y] == '1') + return (1); + else if (data->map[(int)ray_pos.x][(int)ray_pos.y] == 'D') + break ; + } + if (data->side == 1) + return ((dist_ray.y - unit_dist.y) * vec_cos(ray, &data->p_dir)); + else + return ((dist_ray.x - unit_dist.x) * vec_cos(ray, &data->p_dir)); +} + +static void change_door_state(t_data *data, double dist) +{ + +} + +static void interact_door(t_data *data) +{ + t_vec p_ray; + double dist; + + p_ray.x = data->p_dir.x; + p_ray.y = data->p_dir.y; + printf("p_ray.x: %f\n", p_ray.x); + printf("p_ray.y: %f\n", p_ray.y); + dist = get_door_dist(&p_ray, data); + if (dist > 0.5) + { + printf("Door too far!\n"); + return ; + } + printf("In door range!\n"); + change_door_state(data, dist); + return ; +} + +int mouse_press(int button, int x, int y, t_data *data) +{ + (void)x; + (void)y; + if (button == 1) + interact_door(data); + return (SUCCESS); +} diff --git a/srcs/main.c b/srcs/main.c index b43239c..683fba7 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* main.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: tsuchen +#+ +:+ +#+ */ +/* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/15 12:03:33 by tsuchen #+# #+# */ -/* Updated: 2024/08/30 14:09:06 by tsuchen ### ########.fr */ +/* Updated: 2024/09/02 15:08:00 by jteissie ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,6 +62,7 @@ void start_game(t_data *data) mlx_loop_hook(data->mlx, rc_rendering, data); mlx_hook(data->window, KeyPress, KeyPressMask, &key_events, data); mlx_hook(data->window, DestroyNotify, StructureNotifyMask, &cleanup, data); + mlx_hook(data->window, ButtonPress, ButtonPressMask, &mouse_press, data); mlx_loop(data->mlx); }