Skip to content

Commit

Permalink
WIP for find the position of the door in the matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean Teissier authored and Jean Teissier committed Sep 2, 2024
1 parent c40b6ef commit 937924d
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 13 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: tsuchen <tsuchen@student.42.fr> +#+ +:+ +#+ #
# By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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 #
# #
# **************************************************************************** #

Expand All @@ -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 \
Expand All @@ -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

Expand Down
3 changes: 2 additions & 1 deletion includes/cub3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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);
Expand Down
7 changes: 1 addition & 6 deletions srcs/events/key_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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)
{
Expand Down
72 changes: 72 additions & 0 deletions srcs/events/mouse_events.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mouse_events.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
5 changes: 3 additions & 2 deletions srcs/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tsuchen <tsuchen@student.42.fr> +#+ +:+ +#+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 937924d

Please sign in to comment.