-
Notifications
You must be signed in to change notification settings - Fork 1
/
move.c
75 lines (71 loc) · 3.23 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* move.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: szerisen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/21 12:39:11 by szerisen #+# #+# */
/* Updated: 2023/04/20 20:46:23 by szerisen ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
static void ft_player_move(t_data *data, char position, int direction)
/* will put the correct orientation of the spaceship on the screen */
{
if (position == 'y' && direction == UP)
{
mlx_put_image_to_window(data->mlx, data->win, data->img->player_up,
(data->p_x * IMG_W), (data->p_y * IMG_H));
}
if (position == 'x' && direction == LEFT)
{
mlx_put_image_to_window(data->mlx, data->win, data->img->player_left,
(data->p_x * IMG_W), (data->p_y * IMG_H));
}
if (position == 'y' && direction == DOWN)
{
mlx_put_image_to_window(data->mlx, data->win, data->img->player_down,
(data->p_x * IMG_W), (data->p_y * IMG_H));
}
if (position == 'x' && direction == RIGHT)
{
mlx_put_image_to_window(data->mlx, data->win, data->img->player_right,
(data->p_x * IMG_W), (data->p_y * IMG_H));
}
}
static void ft_collect(t_data *data, char pos, int dir)
/* will collect the collectables
** will remove them from the map-struct as well as the screen */
{
data->collected++;
data->map->map[data->p_y][data->p_x] = '0';
mlx_put_image_to_window(data->mlx, data->win, data->img->background,
(data->p_x * IMG_W), (data->p_y * IMG_H));
ft_player_move(data, pos, dir);
}
void ft_move(t_data *data, char pos, int dir)
/* will check if a move is valid and move the player if valid */
{
mlx_put_image_to_window(data->mlx, data->win, data->img->background,
(data->p_x * IMG_W), (data->p_y * IMG_H));
if (pos == 'y' && data->map->map[data->p_y + 1 * dir][data->p_x] != '1'
&& (data->map->map[data->p_y + 1 * dir][data->p_x] != 'E'
|| data->collected == data->map->diamonds))
data->p_y = data->p_y + 1 * dir;
else if (pos == 'x' && data->map->map[data->p_y][data->p_x + 1 * dir] != '1'
&& (data->map->map[data->p_y][data->p_x + 1 * dir] != 'E'
|| data->collected == data->map->diamonds))
data->p_x = data->p_x + 1 * dir;
else if (pos == 'y' && data->map->map[data->p_y + 1 * dir][data->p_x] == 'E'
&& data->collected != data->map->diamonds)
printf("Collect all collectibles before leaving\n");
else if (pos == 'x' && data->map->map[data->p_y][data->p_x + 1 * dir] == 'E'
&& data->collected != data->map->diamonds)
printf("Collect all collectibles before leaving\n");
ft_player_move(data, pos, dir);
if (data->map->map[data->p_y][data->p_x] == 'C')
ft_collect(data, pos, dir);
mlx_do_sync(data->mlx);
printf("You moved %d times.\n", ++data->counter);
}