-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameplay_checks.c
104 lines (95 loc) · 2.9 KB
/
gameplay_checks.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* gameplay_checks.c :+: :+: */
/* +:+ */
/* By: cschuijt <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/12/04 20:13:01 by cschuijt #+# #+# */
/* Updated: 2022/12/04 20:13:01 by cschuijt ######## odam.nl */
/* */
/* ************************************************************************** */
#include "so_long.h"
int finish_moving_and_run_checks(t_map *map)
{
map->lock_input = 0;
map->player->moves_taken++;
if (player_next_to_patrol(map))
return (game_over(map, 0));
if (map->content[map->player->pos] == 'C')
try_pick_up_collectible(map, map->player->pos);
if (map->content[map->player->pos] == 'E')
if (try_exit_map(map))
return (game_over(map, 1));
ft_printf("Moves: %d\n", map->player->moves_taken);
update_gui(map);
return (0);
}
void try_pick_up_collectible(t_map *map, size_t pos)
{
t_collectible *collectible;
t_sprite *sprite;
collectible = map->collectibles;
while (collectible)
{
if (collectible->pos == pos && !(collectible->picked_up))
{
collectible->picked_up = true;
sprite = find_or_create_sprite(map, map->bg_sprites, 178);
sprite->image->instances[collectible->instance].enabled = false;
map->col_grabbed++;
return ;
}
collectible = collectible->next;
}
}
int try_exit_map(t_map *map)
{
t_collectible *collectible;
collectible = map->collectibles;
while (collectible)
{
if (!collectible->picked_up)
return (0);
collectible = collectible->next;
}
return (1);
}
int player_next_to_patrol(t_map *map)
{
size_t personal_space[5];
t_patrol *patrol;
personal_space[0] = map->player->pos;
personal_space[1] = map->player->pos + 1;
personal_space[2] = map->player->pos - 1;
personal_space[3] = map->player->pos + map->width;
personal_space[4] = map->player->pos - map->width;
patrol = map->patrols;
while (patrol)
{
if (patrol->pos == personal_space[0] || \
patrol->pos == personal_space[1] || \
patrol->pos == personal_space[2] || \
patrol->pos == personal_space[3] || \
patrol->pos == personal_space[4])
{
return (1);
}
patrol = patrol->next;
}
return (0);
}
int game_over(t_map *map, int victory)
{
if (victory)
{
ft_printf("Victory at %d moves\n", map->player->moves_taken);
map->game_state = state_victory;
}
else
{
ft_printf("Game over at %d moves\n", map->player->moves_taken);
map->game_state = state_loss_animation;
}
return (1);
}