-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
104 lines (95 loc) · 3.15 KB
/
utils.c
File metadata and controls
104 lines (95 loc) · 3.15 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sfelici <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/21 15:22:28 by sfelici #+# #+# */
/* Updated: 2025/02/21 15:22:42 by sfelici ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
void ft_cleanup(t_vars *vars)
{
if (vars->textures.player_img)
mlx_destroy_image(vars->mlx, vars->textures.player_img);
if (vars->textures.floor_img)
mlx_destroy_image(vars->mlx, vars->textures.floor_img);
if (vars->textures.wall_img)
mlx_destroy_image(vars->mlx, vars->textures.wall_img);
if (vars->textures.collectible_img)
mlx_destroy_image(vars->mlx, vars->textures.collectible_img);
if (vars->textures.exit_img_closed)
mlx_destroy_image(vars->mlx, vars->textures.exit_img_closed);
if (vars->textures.exit_img_open)
mlx_destroy_image(vars->mlx, vars->textures.exit_img_open);
if (vars->textures.enemy_img)
mlx_destroy_image(vars->mlx, vars->textures.enemy_img);
if (vars->textures.explosion_img)
mlx_destroy_image(vars->mlx, vars->textures.explosion_img);
if (vars->textures.flint_img)
mlx_destroy_image(vars->mlx, vars->textures.flint_img);
free_map(vars->map);
if (vars->win)
mlx_destroy_window(vars->mlx, vars->win);
if (vars->mlx)
{
mlx_destroy_display(vars->mlx);
free(vars->mlx);
}
}
void free_map(char **map)
{
int i;
if (!map)
return ;
i = 0;
while (map[i])
{
free(map[i]);
i++;
}
free(map);
}
int is_move_valid(t_vars *vars, int new_x, int new_y)
{
if (vars->map[new_y][new_x] == '1')
return (0);
if (vars->map[new_y][new_x] == 'E' && vars->map_info.collectible_count > 0)
return (0);
return (1);
}
void process_tile(t_vars *vars, int new_x, int new_y)
{
char *num;
char *msg;
if (vars->map[new_y][new_x] == 'V')
game_over(vars);
if (vars->map[new_y][new_x] == 'C')
vars->map_info.collectible_count--;
if (vars->map[new_y][new_x] == 'E' && vars->map_info.collectible_count == 0)
{
draw_map(vars);
num = ft_itoa(++vars->move_count);
msg = ft_strjoin("You won in moves: ", num);
free(num);
mlx_string_put(vars->mlx, vars->win, 10, 10, 0xFFFFFF, msg);
mlx_string_put(vars->mlx, vars->win, 10, 10 + 1, 0xFFFFFF, msg);
free(msg);
mlx_do_sync(vars->mlx);
sleep(1);
ft_cleanup(vars);
exit(0);
}
}
void game_over(t_vars *vars)
{
kill_enemy(vars, vars->map_info.player_x, vars->map_info.player_y);
mlx_string_put(vars->mlx, vars->win, 10, 10, 0xFF0000, "GAME OVER!");
mlx_string_put(vars->mlx, vars->win, 10, 10 + 1, 0xFF0000, "GAME OVER!");
mlx_do_sync(vars->mlx);
sleep(2);
ft_cleanup(vars);
exit(0);
}