-
Notifications
You must be signed in to change notification settings - Fork 0
/
move_player.c
99 lines (93 loc) · 3.49 KB
/
move_player.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* move_player.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ahajji <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/03 15:17:09 by ahajji #+# #+# */
/* Updated: 2023/12/10 17:40:57 by ahajji ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
void controle_angle(t_cub3d *data)
{
mlx_cursor_hook(data->mlx, move_mouse, (void *)data);
if (mlx_is_key_down(data->mlx, MLX_KEY_RIGHT))
{
data->angle += SPEED_ROTATE;
if (data->angle >= 360)
data->angle -= 360;
}
else if (mlx_is_key_down(data->mlx, MLX_KEY_LEFT))
{
data->angle -= SPEED_ROTATE;
if (data->angle <= 0)
data->angle += 360;
}
if (mlx_is_key_down(data->mlx, MLX_KEY_ESCAPE))
exit(0);
}
void controle_player(t_cub3d *data)
{
if (mlx_is_key_down(data->mlx, MLX_KEY_W) && check_wall(data))
{
data->px = data->px + (cos(to_rad(data->angle)) * MOVE_STEP);
data->py = data->py + (sin(to_rad(data->angle)) * MOVE_STEP);
}
else if (mlx_is_key_down(data->mlx, MLX_KEY_S) && check_wall(data))
{
data->px = data->px - (cos(to_rad(data->angle)) * MOVE_STEP);
data->py = data->py - (sin(to_rad(data->angle)) * MOVE_STEP);
}
else if (mlx_is_key_down(data->mlx, MLX_KEY_D) && check_wall(data))
{
data->px = data->px - cos(to_rad(90) - to_rad(data->angle)) * MOVE_STEP;
data->py = data->py + sin(to_rad(90) - to_rad(data->angle)) * MOVE_STEP;
}
else if (mlx_is_key_down(data->mlx, MLX_KEY_A) && check_wall(data))
{
data->px = data->px + cos(to_rad(90) - to_rad(data->angle)) * MOVE_STEP;
data->py = data->py - sin(to_rad(90) - to_rad(data->angle)) * MOVE_STEP;
}
}
void check_wall_part_tow(t_cub3d *data, int *x, int *y)
{
if (mlx_is_key_down(data->mlx, MLX_KEY_W))
{
*x = data->px + (cos(to_rad(data->angle)) * MOVE_STEP_V);
*y = data->py + (sin(to_rad(data->angle)) * MOVE_STEP_V);
}
else if (mlx_is_key_down(data->mlx, MLX_KEY_S))
{
*x = data->px - (cos(to_rad(data->angle)) * MOVE_STEP_V);
*y = data->py - (sin(to_rad(data->angle)) * MOVE_STEP_V);
}
}
int check_wall(t_cub3d *data)
{
int x;
int y;
check_wall_part_tow(data, &x, &y);
if (mlx_is_key_down(data->mlx, MLX_KEY_D)
&& !mlx_is_key_down(data->mlx, MLX_KEY_W)
&& !mlx_is_key_down(data->mlx, MLX_KEY_S))
{
x = data->px - cos(to_rad(90) - to_rad(data->angle)) * MOVE_STEP_V;
y = data->py + sin(to_rad(90) - to_rad(data->angle)) * MOVE_STEP_V;
}
else if (mlx_is_key_down(data->mlx, MLX_KEY_A)
&& !mlx_is_key_down(data->mlx, MLX_KEY_W)
&& !mlx_is_key_down(data->mlx, MLX_KEY_S))
{
x = data->px + cos(to_rad(90) - to_rad(data->angle)) * MOVE_STEP_V;
y = data->py - sin(to_rad(90) - to_rad(data->angle)) * MOVE_STEP_V;
}
if (data->map[(int)(y / data->size_shape)]
[(int)(x / data->size_shape)] == '1'
|| (data->map[(int)(y / data->size_shape)][(int)(data->px
/ data->size_shape)] == '1' && data->map[(int)(data->py
/ data->size_shape)][(int)(x / data->size_shape)] == '1'))
return (0);
return (1);
}