-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeys.c
98 lines (92 loc) · 3.22 KB
/
keys.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* keys.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ybouddou <ybouddou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/19 20:11:23 by ybouddou #+# #+# */
/* Updated: 2020/11/30 18:36:53 by ybouddou ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
int key_pressed(int key, t_cub3d *cub)
{
cub->keyboard[key] = 1;
return (0);
}
int key_released(int key, t_cub3d *cub)
{
cub->keyboard[key] = 0;
return (0);
}
void moves_up_down(t_cub3d *cub)
{
if (cub->keyboard[13])
{
if (cub->map[(int)(cub->posx + cub->dirx * cub->movespeed)]
[(int)(cub->posy)] == '0')
cub->posx += cub->dirx * cub->movespeed;
if (cub->map[(int)(cub->posx)][(int)(cub->posy + cub->diry *
cub->movespeed)] == '0')
cub->posy += cub->diry * cub->movespeed;
}
if (cub->keyboard[1])
{
if (cub->map[(int)(cub->posx - cub->dirx * cub->movespeed)]
[(int)(cub->posy)] == '0')
cub->posx -= cub->dirx * cub->movespeed;
if (cub->map[(int)(cub->posx)][(int)(cub->posy - cub->diry *
cub->movespeed)] == '0')
cub->posy -= cub->diry * cub->movespeed;
}
}
void movesides(t_cub3d *cub)
{
if (cub->keyboard[0])
{
if (cub->map[(int)(cub->posx - cub->planex * cub->movespeed)]
[(int)(cub->posy)] == '0')
cub->posx -= cub->planex * cub->movespeed;
if (cub->map[(int)(cub->posx)][(int)(cub->posy - cub->planey *
cub->movespeed)] == '0')
cub->posy -= cub->planey * cub->movespeed;
}
if (cub->keyboard[2])
{
if (cub->map[(int)(cub->posx + cub->planex * cub->movespeed)]
[(int)(cub->posy)] == '0')
cub->posx += cub->planex * cub->movespeed;
if (cub->map[(int)(cub->posx)][(int)(cub->posy + cub->planey *
cub->movespeed)] == '0')
cub->posy += cub->planey * cub->movespeed;
}
}
void look(t_cub3d *cub)
{
cub->olddirx = cub->dirx;
if (cub->keyboard[123])
{
cub->dirx = cub->dirx * cos(cub->rotatespeed) - cub->diry *
sin(cub->rotatespeed);
cub->diry = cub->olddirx * sin(cub->rotatespeed) + cub->diry *
cos(cub->rotatespeed);
cub->oldplanex = cub->planex;
cub->planex = cub->planex * cos(cub->rotatespeed) - cub->planey *
sin(cub->rotatespeed);
cub->planey = cub->oldplanex * sin(cub->rotatespeed) + cub->planey *
cos(cub->rotatespeed);
}
if (cub->keyboard[124])
{
cub->dirx = cub->dirx * cos(-cub->rotatespeed) - cub->diry *
sin(-cub->rotatespeed);
cub->diry = cub->olddirx * sin(-cub->rotatespeed) + cub->diry *
cos(-cub->rotatespeed);
cub->oldplanex = cub->planex;
cub->planex = cub->planex * cos(-cub->rotatespeed) - cub->planey *
sin(-cub->rotatespeed);
cub->planey = cub->oldplanex * sin(-cub->rotatespeed) + cub->planey *
cos(-cub->rotatespeed);
}
}