-
Notifications
You must be signed in to change notification settings - Fork 0
/
collectible_utils.c
93 lines (84 loc) · 2.34 KB
/
collectible_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* collectible_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jbadaire <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/03 03:24:55 by jbadaire #+# #+# */
/* Updated: 2023/10/12 08:57:08 by jbadaire ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
t_collectible *load_collectibles(t_world *world)
{
int pos_x;
int pos_y;
t_collectible *collectible;
t_location location;
pos_x = 0;
pos_y = 0;
collectible = NULL;
while (world->map[pos_y])
{
while (world->map[pos_y] && pos_x < (int) ft_strlen(world->map[0]))
{
if (world->map[pos_y][pos_x] == 'C')
{
location.x = pos_x;
location.y = pos_y;
last(&collectible, create_collectible(location));
}
pos_x++;
}
pos_x = 0;
pos_y++;
}
return (collectible);
}
t_collectible *create_collectible(t_location location)
{
t_collectible *collectible;
collectible = malloc(sizeof (t_collectible));
if (!collectible)
return (NULL);
collectible->location = location;
collectible->collected = 0;
collectible->next = NULL;
return (collectible);
}
t_collectible *get_collectible_at(t_world world, t_location location)
{
while (world.player.collectibles)
{
if (loc_equals(location, world.player.collectibles->location))
return (world.player.collectibles);
world.player.collectibles = world.player.collectibles->next;
}
return (NULL);
}
void update_col(t_collectible *col, t_location loc, t_boolean bool)
{
while (col)
{
if (loc_equals(col->location, loc))
{
col->collected = bool;
break ;
}
col = col->next;
}
}
void last(t_collectible **lst, t_collectible *new)
{
t_collectible *tmp;
tmp = *lst;
if (tmp == NULL)
{
*lst = new;
return ;
}
while (tmp->next)
tmp = tmp->next;
tmp->next = new;
}