forked from yoxeric/SilverMoss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.c
101 lines (92 loc) · 3.26 KB
/
control.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* control.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: azaghlou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/23 15:52:00 by azaghlou #+# #+# */
/* Updated: 2023/09/04 23:22:44 by azaghlou ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
void move_up(t_game *game, t_vector2f pd)
{
t_vector2i ptile;
t_vector2f p;
p = game->player.pos;
if (mlx_is_key_down(game->mlx, MLX_KEY_W))
{
ptile.x = (int)(p.x + pd.x * 2) / game->tile_size;
ptile.y = (int)(p.y + pd.y * 2) / game->tile_size;
if (ptile.x >= 0 && ptile.y >= 0
&& ptile.x < game->map.size.x && ptile.y < game->map.size.y
&& game->map.map[ptile.y][ptile.x] != '1'
&& game->map.map[(int) p.y / game->tile_size][ptile.x] != '1'
&& game->map.map[ptile.y][(int) p.x / game->tile_size] != '1')
{
game->player.pos.x += pd.x;
game->player.pos.y += pd.y;
}
}
}
void move_down(t_game *game, t_vector2f pd)
{
t_vector2i ptile;
t_vector2f p;
p = game->player.pos;
if (mlx_is_key_down(game->mlx, MLX_KEY_S))
{
ptile.x = (int)(p.x - pd.x * 2) / game->tile_size;
ptile.y = (int)(p.y - pd.y * 2) / game->tile_size;
if (ptile.x >= 0 && ptile.y >= 0
&& ptile.x < game->map.size.x && ptile.y < game->map.size.y
&& game->map.map[ptile.y][ptile.x] != '1'
&& game->map.map[(int) p.y / game->tile_size][ptile.x] != '1'
&& game->map.map[ptile.y][(int) p.x / game->tile_size] != '1')
{
game->player.pos.x -= pd.x;
game->player.pos.y -= pd.y;
}
}
}
void move_right(t_game *game, t_vector2f pd)
{
t_vector2i ptile;
t_vector2f p;
p = game->player.pos;
if (mlx_is_key_down(game->mlx, MLX_KEY_D))
{
ptile.x = (int)(p.x - pd.y * 2) / game->tile_size;
ptile.y = (int)(p.y + pd.x * 2) / game->tile_size;
if (ptile.x >= 0 && ptile.y >= 0
&& ptile.x < game->map.size.x && ptile.y < game->map.size.y
&& game->map.map[ptile.y][ptile.x] != '1'
&& game->map.map[(int) p.y / game->tile_size][ptile.x] != '1'
&& game->map.map[ptile.y][(int) p.x / game->tile_size] != '1')
{
game->player.pos.x -= pd.y;
game->player.pos.y += pd.x;
}
}
}
void move_left(t_game *game, t_vector2f pd)
{
t_vector2i ptile;
t_vector2f p;
p = game->player.pos;
if (mlx_is_key_down(game->mlx, MLX_KEY_A))
{
ptile.x = (int)(p.x + pd.y * 2) / game->tile_size;
ptile.y = (int)(p.y - pd.x * 2) / game->tile_size;
if (ptile.x >= 0 && ptile.y >= 0
&& ptile.x < game->map.size.x && ptile.y < game->map.size.y
&& game->map.map[ptile.y][ptile.x] != '1'
&& game->map.map[(int) p.y / game->tile_size][ptile.x] != '1'
&& game->map.map[ptile.y][(int) p.x / game->tile_size] != '1')
{
game->player.pos.x += pd.y;
game->player.pos.y -= pd.x;
}
}
}