-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_position.c
101 lines (96 loc) · 2.72 KB
/
extract_position.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* extract_position.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vkatason <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/16 15:41:47 by vkatason #+# #+# */
/* Updated: 2024/06/12 17:40:24 by vkatason ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3D.h"
/**
* @brief Function to get the position of the player
* from the map and save it to the data struct.
*
* @param data Pointer to the main data struct
* @var i Counter for the data->map array
* @var j Counter for the data->map[i] array
* @var tmp Pointer to the current line
*
* @note In this function we store to parameters
* in the data struct:
* - mpx, mpy - the position of the player
* on the map
* - px, py - the position of the player
* in the center of the cell of the map.
* The difference between mpx, mpy and px,
* py is that the first one is integer
* and the second one is double.
* The first one is
* used to have reference while
* checking the map and the second
* one is used for painting purposes.
*/
void ft_get_player_position(t_data *data)
{
int i;
int j;
char *tmp;
i = 0;
while (data->map[i])
{
tmp = data->map[i];
j = 0;
while (tmp[j])
{
if (ft_strchr("NSWE", tmp[j]))
{
data->mpx = j;
data->mpy = i;
data->px = j + 0.5;
data->py = i + 0.5;
ft_check_player_direction(data, tmp[j]);
return ;
}
j++;
}
i++;
}
}
/**
* @brief Function to check the player's direction.
* Depending on the character it sets the direction
* to the data->dir array.
* N - North (0, -1)
* S - South (0, 1)
* W - West (-1, 0)
* E - East (1, 0)
*
* @param data Pointer to the main data struct
* @param c Character to check the direction
*/
void ft_check_player_direction(t_data *data, char c)
{
if (c == 'N')
{
data->dir[0] = -1;
data->dir[1] = 0;
}
else if (c == 'S')
{
data->dir[0] = 1;
data->dir[1] = 0;
}
else if (c == 'W')
{
data->dir[0] = 0;
data->dir[1] = -1;
}
else if (c == 'E')
{
data->dir[0] = 0;
data->dir[1] = 1;
}
}