-
Notifications
You must be signed in to change notification settings - Fork 0
/
moveplayer.c
108 lines (98 loc) · 3.09 KB
/
moveplayer.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* moveplayer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abadidi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/24 05:55:45 by abadidi #+# #+# */
/* Updated: 2021/11/25 01:54:13 by abadidi ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
int iswall(t_data *data, int x, int y)
{
int new_x;
int new_y;
new_x = x / TS;
new_y = y / TS;
if (new_x < 0 || new_y < 0)
return (1);
if (new_x > TS * data->w || new_y > TS * data->h)
return (1);
if (data->map[new_y][new_x] == '1'
|| (data->map[new_y][new_x] == 'E' && !ft_isfsh(data)))
return (1);
return (0);
}
int iscollect(t_data *data, int x, int y)
{
int new_x;
int new_y;
int new_xt;
int new_yt;
new_x = x / TS;
new_y = y / TS;
new_xt = (x + TS / 2) / TS;
new_yt = (y + TS / 2) / TS;
if ((new_x >= 0 && new_x <= data->w && new_y >= 0
&& new_y <= data->h) && (data->map[new_y][new_x] == 'C'
|| data->map[new_y][new_xt] == 'C' ||
data->map[new_yt][new_x] == 'C' ||
data->map[new_yt][new_xt] == 'C'))
return (1);
return (0);
}
void collect(t_data *data, int x, int y)
{
int new_x;
int new_y;
new_x = x / TS;
new_y = y / TS;
if (new_x > 0 && new_x < data->w && new_y > 0
&& new_y < data->h && data->map[new_y][new_x] == 'E' && ft_isfsh(data))
{
write(1, "Exit\n", 5);
exit(0);
}
if (iscollect(data, x, y) == 1)
{
write(1, "Collect\n", 8);
data->map[new_y][new_x] = '0';
}
}
static void move_help(int key, t_data *data)
{
if (key == ESC)
ft_destroywindown(data);
if (key == W_KEY && iswall(data, data->p_x,
data->p_y - TS) == 0 && ft_printmv(data))
data->p_y -= TS;
if (key == S_KEY && iswall(data, data->p_x,
data->p_y + TS) == 0 && ft_printmv(data))
data->p_y += TS;
if (key == A_KEY && iswall(data, data->p_x - TS,
data->p_y) == 0 && ft_printmv(data))
data->p_x -= TS;
if (key == D_KEY && iswall(data, data->p_x + TS, data->p_y) == 0
&& ft_printmv(data))
data->p_x += TS;
}
int ft_movplayer(int key, void *d)
{
t_data *data;
data = (t_data *)d;
move_help(key, data);
collect(data, data->p_x, data->p_y);
mlx_clear_window(data->mlx, data->mlx_win);
if (data->img.img)
mlx_destroy_image(data->mlx, data->img.img);
data->img.img = mlx_new_image(data->mlx, data->w * TS, data->h * TS);
draw(data);
data->img.addr = (unsigned int *)mlx_get_data_addr(&data->img.img,
&(data->img.bits_per_pixel), &(data->img.line_length),
&(data->img.endian));
mlx_put_image_to_window(data->mlx, data->mlx_win,
data->img.img, 0, 0);
return (1);
}