-
Notifications
You must be signed in to change notification settings - Fork 1
/
move.c
executable file
·127 lines (121 loc) · 3.9 KB
/
move.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* move.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abenheni <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 14:16:52 by abenheni #+# #+# */
/* Updated: 2023/02/15 14:16:55 by abenheni ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
static void move_handle1(t_long *vars, t_help *dt)
{
if (vars->coins > 0 && vars->map[vars->y / 30][(vars->x - 30) / 30] == 'E')
return ;
vars->moves++;
ft_putstr("moves : ");
ft_putnbr_fd(vars->moves, 1);
ft_putstr("\n");
if (vars->coins == 0 && vars->map[vars->y / 30][(vars->x - 30) / 30] == 'E')
exit(0);
vars->x -= 30;
if (vars->map[vars->y / 30][(vars->x) / 30] == 'C')
{
vars->coins--;
vars->map[vars->y / 30][(vars->x) / 30] = '0';
}
mlx_clear_window(vars->mlx, vars->wind);
render_map(vars, dt);
mlx_put_image_to_window(vars->mlx,
vars->wind, vars->img[2], vars->x, vars->y);
}
static void move_handle2(t_long *vars, t_help *dt)
{
if (vars->coins > 0 && vars->map[vars->y / 30][(vars->x + 30) / 30] == 'E')
return ;
vars->moves++;
ft_putstr("moves : ");
ft_putnbr_fd(vars->moves, 1);
ft_putstr("\n");
if (vars->coins == 0 && vars->map[vars->y / 30][(vars->x + 30) / 30] == 'E')
exit(0);
vars->x += 30;
if (vars->map[vars->y / 30][(vars->x) / 30] == 'C')
{
vars->map[vars->y / 30][(vars->x) / 30] = '0';
vars->coins--;
}
mlx_clear_window(vars->mlx, vars->wind);
render_map(vars, dt);
mlx_put_image_to_window(vars->mlx,
vars->wind, vars->img[2], vars->x, vars->y);
}
static void move_handle3(t_long *vars, t_help *dt)
{
if (vars->coins > 0
&& vars->map[(vars->y - 30) / 30][(vars->x) / 30] == 'E')
return ;
vars->moves++;
ft_putstr("moves : ");
ft_putnbr_fd(vars->moves, 1);
ft_putstr("\n");
if (vars->coins == 0
&& vars->map[(vars->y - 30) / 30][(vars->x) / 30] == 'E')
exit(0);
vars->y -= 30;
if (vars->map[vars->y / 30][(vars->x) / 30] == 'C')
{
vars->map[vars->y / 30][(vars->x) / 30] = '0';
vars->coins--;
}
mlx_clear_window(vars->mlx, vars->wind);
render_map(vars, dt);
mlx_put_image_to_window(vars->mlx,
vars->wind, vars->img[2], vars->x, vars->y);
}
static void move_handle4(t_long *vars, t_help *dt)
{
if (vars->coins > 0
&& vars->map[(vars->y + 30) / 30][(vars->x) / 30] == 'E')
return ;
vars->moves++;
ft_putstr("moves : ");
ft_putnbr_fd(vars->moves, 1);
ft_putstr("\n");
if (vars->coins == 0
&& vars->map[(vars->y + 30) / 30][(vars->x) / 30] == 'E')
exit(0);
vars->y += 30;
if (vars->map[vars->y / 30][(vars->x) / 30] == 'C')
{
vars->map[vars->y / 30][(vars->x) / 30] = '0';
vars->coins--;
}
mlx_clear_window(vars->mlx, vars->wind);
render_map(vars, dt);
mlx_put_image_to_window(vars->mlx,
vars->wind, vars->img[2], vars->x, vars->y);
}
int move(int keycode, t_long *vars, t_help *dt)
{
if (keycode == 53)
{
exit(0);
}
if (vars->map[vars->y / 30][(vars->x) / 30] == 'E' && vars->coins == 0)
{
ft_free(vars->map);
exit(0);
}
if (keycode == 0 && vars->map[vars->y / 30][(vars->x - 30) / 30] != '1')
move_handle1(vars, dt);
if (keycode == 2 && vars->map[vars->y / 30][(vars->x + 30) / 30] != '1')
move_handle2(vars, dt);
if (keycode == 13 && vars->map[(vars->y - 30) / 30][(vars->x) / 30] != '1')
move_handle3(vars, dt);
if (keycode == 1 && vars->map[(vars->y + 30) / 30][(vars->x) / 30] != '1')
move_handle4(vars, dt);
return (0);
}