-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey_events_2.c
110 lines (102 loc) · 3.46 KB
/
key_events_2.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
100
101
102
103
104
105
106
107
108
109
110
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* key_events_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sanan <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/11 14:00:22 by sanan #+# #+# */
/* Updated: 2023/10/11 14:57:26 by sanan ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
void move_back_and_forth(t_game *game)
{
if (game->keys[W])
{
if (game->map[(int)(game->pos.x + game->dir.x * MOVE_SPEED)] \
[(int)game->pos.y] == 0)
game->pos.x += game->dir.x * MOVE_SPEED;
if (game->map[(int)game->pos.x] \
[(int)(game->pos.y + game->dir.y * MOVE_SPEED)] == 0)
game->pos.y += game->dir.y * MOVE_SPEED;
}
if (game->keys[S])
{
if (game->map[(int)(game->pos.x - game->dir.x * MOVE_SPEED)] \
[(int)game->pos.y] == 0)
game->pos.x -= game->dir.x * MOVE_SPEED;
if (game->map[(int)game->pos.x] \
[(int)(game->pos.y - game->dir.y * MOVE_SPEED)] == 0)
game->pos.y -= game->dir.y * MOVE_SPEED;
}
}
void move_side_to_side(t_game *game)
{
if (game->keys[D])
{
if (game->map[(int)(game->pos.x + game->dir.y * MOVE_SPEED)] \
[(int)game->pos.y] == 0)
game->pos.x += game->dir.y * MOVE_SPEED;
if (game->map[(int)game->pos.x] \
[(int)(game->pos.y - game->dir.x * MOVE_SPEED)] == 0)
game->pos.y -= game->dir.x * MOVE_SPEED;
}
if (game->keys[A])
{
if (game->map[(int)(game->pos.x - game->dir.y * MOVE_SPEED)] \
[(int)game->pos.y] == 0)
game->pos.x -= game->dir.y * MOVE_SPEED;
if (game->map[(int)game->pos.x] \
[(int)(game->pos.y + game->dir.x * MOVE_SPEED)] == 0)
game->pos.y += game->dir.x * MOVE_SPEED;
}
}
void move_head_to_right(t_game *game)
{
double old_dir_x;
double old_plane_x;
if (game->keys[RIGHT])
{
old_dir_x = game->dir.x;
game->dir.x = game->dir.x * cos(-ROTATE_SPEED) - \
game->dir.y * sin(-ROTATE_SPEED);
game->dir.y = old_dir_x * sin(-ROTATE_SPEED) + \
game->dir.y * cos(-ROTATE_SPEED);
old_plane_x = game->plane.x;
game->plane.x = game->plane.x * cos(-ROTATE_SPEED) - \
game->plane.y * sin(-ROTATE_SPEED);
game->plane.y = old_plane_x * sin(-ROTATE_SPEED) + \
game->plane.y * cos(-ROTATE_SPEED);
}
}
void move_head_to_left(t_game *game)
{
double old_dir_x;
double old_plane_x;
if (game->keys[LEFT])
{
old_dir_x = game->dir.x;
game->dir.x = game->dir.x * cos(ROTATE_SPEED) - \
game->dir.y * sin(ROTATE_SPEED);
game->dir.y = old_dir_x * sin(ROTATE_SPEED) + \
game->dir.y * cos(ROTATE_SPEED);
old_plane_x = game->plane.x;
game->plane.x = game->plane.x * cos(ROTATE_SPEED) - \
game->plane.y * sin(ROTATE_SPEED);
game->plane.y = old_plane_x * sin(ROTATE_SPEED) + \
game->plane.y * cos(ROTATE_SPEED);
}
}
int read_keys_and_move(t_game *game)
{
if (game->keys[W] || game->keys[S])
move_back_and_forth(game);
if (game->keys[D] || game->keys[A])
move_side_to_side(game);
if (game->keys[RIGHT])
move_head_to_right(game);
if (game->keys[LEFT])
move_head_to_left(game);
return (1);
}