-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_movement.c
104 lines (98 loc) · 3.43 KB
/
ft_movement.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_movement.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asfaihi <asfaihi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/16 12:25:37 by asfaihi #+# #+# */
/* Updated: 2021/02/22 15:14:03 by asfaihi ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
int check_movement(int x, int y, t_mlx *mlx)
{
if (mlx->world_map[x][y] != '1' && mlx->world_map[x][y] != '2')
return (1);
return (0);
}
void ft_move_left_right(t_mlx *mlx)
{
if (mlx->keys.d == 1)
{
if (check_movement((int)mlx->player.posx, (int)(mlx->player.posy -
mlx->player.dirx * mlx->player.movespeed), mlx))
mlx->player.posy -= mlx->player.dirx * mlx->player.movespeed * 0.5;
if (check_movement((int)(mlx->player.posx + mlx->player.diry *
mlx->player.movespeed), (int)mlx->player.posy, mlx))
mlx->player.posx += mlx->player.diry * mlx->player.movespeed * 0.5;
}
if (mlx->keys.a == 1)
{
if (check_movement((int)mlx->player.posx, (int)(mlx->player.posy +
mlx->player.dirx * mlx->player.movespeed), mlx))
mlx->player.posy += mlx->player.dirx * mlx->player.movespeed * 0.5;
if (check_movement((int)(mlx->player.posx - mlx->player.diry *
mlx->player.movespeed), (int)mlx->player.posy, mlx))
mlx->player.posx -= mlx->player.diry * mlx->player.movespeed * 0.5;
}
}
void ft_movement(t_mlx *mlx)
{
if (mlx->keys.w == 1)
{
if (check_movement((int)(mlx->player.posx + mlx->player.dirx *
mlx->player.movespeed), (int)mlx->player.posy, mlx))
mlx->player.posx += mlx->player.dirx * mlx->player.movespeed * 0.5;
if (check_movement((int)mlx->player.posx, (int)(mlx->player.posy +
mlx->player.diry * mlx->player.movespeed), mlx))
mlx->player.posy += mlx->player.diry * mlx->player.movespeed * 0.5;
}
if (mlx->keys.s == 1)
{
if (check_movement((int)(mlx->player.posx - mlx->player.dirx *
mlx->player.movespeed), (int)mlx->player.posy, mlx))
mlx->player.posx -= mlx->player.dirx * mlx->player.movespeed * 0.5;
if (check_movement((int)mlx->player.posx, (int)(mlx->player.posy -
mlx->player.diry * mlx->player.movespeed), mlx))
mlx->player.posy -= mlx->player.diry * mlx->player.movespeed * 0.5;
}
if (mlx->keys.d == 1 || mlx->keys.a == 1)
ft_move_left_right(mlx);
if (mlx->keys.right == 1 || mlx->keys.left == 1)
ft_rotation(mlx);
}
int ft_key_pressed(int key, t_mlx *mlx)
{
if (key == 13)
mlx->keys.w = 1;
if (key == 1)
mlx->keys.s = 1;
if (key == 0)
mlx->keys.a = 1;
if (key == 2)
mlx->keys.d = 1;
if (key == 123)
mlx->keys.left = 1;
if (key == 124)
mlx->keys.right = 1;
return (0);
}
int ft_key_released(int key, t_mlx *mlx)
{
if (key == 13)
mlx->keys.w = 0;
if (key == 1)
mlx->keys.s = 0;
if (key == 0)
mlx->keys.a = 0;
if (key == 2)
mlx->keys.d = 0;
if (key == 123)
mlx->keys.left = 0;
if (key == 124)
mlx->keys.right = 0;
if (key == 53)
ft_close_game(mlx, 0);
return (0);
}