-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.c
More file actions
143 lines (129 loc) · 3.23 KB
/
tools.c
File metadata and controls
143 lines (129 loc) · 3.23 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tools.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yetsabe <yetsabe@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/22 19:43:47 by yeparra- #+# #+# */
/* Updated: 2025/04/14 15:47:41 by yetsabe ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
// Locates the 'P' tile on the map and stores its coordinates in
//the pos_x and pos_y fields, which are later used by the update function
void find_player(t_game *game)
{
int i;
int j;
if (!game || !game->map)
return ;
i = 1;
while (game->map[i] != NULL)
{
j = 0;
while (game->map[i][j] != '\0')
{
if (game->map[i][j] == 'P')
{
game->pos_x = i;
game->pos_y = j;
return ;
}
j++;
}
i++;
}
}
int ft_len(char **array)
{
int i;
i = 0;
while (array[i])
i++;
return (i);
}
// Frees the memory of one or two matrices, if they exist
void free_arrays(char **map1, char **map2)
{
int i;
i = 0;
if (map1)
{
while (map1[i])
{
free(map1[i]);
i++;
}
free(map1);
}
i = 0;
if (map2)
{
while (map2[i])
{
free(map2[i]);
i++;
}
free(map2);
}
}
// Assigns images to the structure and links them
//to the corresponding character based on the map
void put_image(t_game *game, char tile, int x, int y)
{
void *img;
img = NULL;
if (tile == '1')
img = game->wall_img;
else if (tile == '0')
img = game->floor_img;
else if (tile == 'C')
img = game->collectible_img;
else if (tile == 'P')
img = game->player_img;
else if (tile == 'E')
img = game->exit_img;
if (img)
{
x = x * TILE_SIZE;
y = y * TILE_SIZE;
mlx_put_image_to_window(game->win.mlx, game->win.win, img, x, y);
}
else
{
write(1, "ERROR! La imagen no ha podido ser colocada!\n", 45);
close_game(game);
}
}
// Performs different actions depending on where the player moves:
// collects 'C'; if the player reaches 'E' and there are no 'C' left on the map, the game ends.
// Otherwise, the function returns. Moves the 'P' character to the new position if standing on '0',
// removes 'P' from the previous position, and updates it to the new location.
void handle_tile_interaction(t_game *game, int x, int y)
{
if (game->map[x][y] == 'C')
{
game->map[x][y] = 'P';
game->map[game->pos_x][game->pos_y] = '0';
}
else if (game->map[x][y] == 'E')
{
if (count_collectibles(game->map, game->map_height) == 0)
{
game->move_count++;
game->map[x][y] = 'P';
game->move_str = ft_itoa(game->move_count);
free(game->move_str);
close_game(game);
}
else
return ;
}
else if (game->map[x][y] == '0')
{
game->map[game->pos_x][game->pos_y] = '0';
update(game, x, y);
game->map[game->pos_x][game->pos_y] = 'P';
}
}