-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoves.c
More file actions
123 lines (116 loc) · 3.98 KB
/
moves.c
File metadata and controls
123 lines (116 loc) · 3.98 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* moves.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kalshaer <kalshaer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/22 08:08:25 by kalshaer #+# #+# */
/* Updated: 2023/03/30 10:51:03 by kalshaer ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
static void moves_utils(t_mlx *mlx, char *s)
{
int x;
x = 0;
if (mlx->map[mlx->i][mlx->j] == 'K')
destory("You loss!\n", mlx, 1);
mlx_destroy_image(mlx->mlx, mlx->p.img);
mlx->p.img = mlx_xpm_file_to_image(mlx->mlx, s, &x, &x);
if (!(mlx->p.img))
errorh("Invalid image path or img\n", mlx->map, NULL);
if (mlx->map[mlx->i][mlx->j] == 'C' && mlx->count)
{
mlx->count--;
mlx->map[mlx->i][mlx->j] = 'S';
}
}
void move_up(t_mlx *mlx, int *moves)
{
if (mlx->map[mlx->i - 1][mlx->j] != '1')
{
*moves = *moves + 1;
mlx->i = mlx->i - 1;
moves_utils(mlx, D_UP);
mlx->p_y = mlx->p_y - PIXCEL;
if (mlx->map[mlx->i][mlx->j] == 'E' && mlx->count == 0)
destory("You win!\n", mlx, 1);
mlx_put_image_to_window (mlx->mlx, mlx->win, mlx->p.img, mlx->p_x,
mlx->p_y);
mlx_put_image_to_window (mlx->mlx, mlx->win, mlx->s.img, mlx->p_x,
mlx->p_y + PIXCEL);
if (mlx->map[mlx->i + 1][mlx->j] == 'S')
up_utils(mlx);
if (mlx->map[mlx->i + 1][mlx->j] == 'E')
mlx_put_image_to_window (mlx->mlx, mlx->win, mlx->e.img,
mlx->p_x, mlx->p_y + PIXCEL);
ft_printf("%d\n", *moves);
}
}
void move_right(t_mlx *mlx, int *moves)
{
if (mlx->map[mlx->i][mlx->j + 1] != '1')
{
*moves = *moves + 1;
mlx->j = mlx->j + 1;
moves_utils(mlx, D_RIGHT);
mlx->p_x += PIXCEL;
if (mlx->map[mlx->i][mlx->j] == 'E' && mlx->count == 0)
destory("You win!\n", mlx, 1);
mlx_put_image_to_window (mlx->mlx, mlx->win, mlx->p.img, mlx->p_x,
mlx->p_y);
mlx_put_image_to_window (mlx->mlx, mlx->win, mlx->s.img,
mlx->p_x - PIXCEL, mlx->p_y);
if (mlx->map[mlx->i][mlx->j - 1] == 'S')
right_utils(mlx);
if (mlx->map[mlx->i][mlx->j - 1] == 'E')
mlx_put_image_to_window (mlx->mlx, mlx->win, mlx->e.img,
mlx->p_x - PIXCEL, mlx->p_y);
ft_printf("%d\n", *moves);
}
}
void move_down(t_mlx *mlx, int *moves)
{
if (mlx->map[mlx->i + 1][mlx->j] != '1')
{
*moves = *moves + 1;
mlx->i = mlx->i + 1;
moves_utils(mlx, D_DOWN);
mlx->p_y += PIXCEL;
if (mlx->map[mlx->i][mlx->j] == 'E' && mlx->count == 0)
destory("You win!\n", mlx, 1);
mlx_put_image_to_window (mlx->mlx, mlx->win, mlx->p.img, mlx->p_x,
mlx->p_y);
mlx_put_image_to_window (mlx->mlx, mlx->win, mlx->s.img, mlx->p_x,
mlx->p_y - PIXCEL);
if (mlx->map[mlx->i - 1][mlx->j] == 'S')
down_utils(mlx);
if (mlx->map[mlx->i - 1][mlx->j] == 'E')
mlx_put_image_to_window (mlx->mlx, mlx->win, mlx->e.img, mlx->p_x,
mlx->p_y - PIXCEL);
ft_printf("%d\n", *moves);
}
}
void move_left(t_mlx *mlx, int *moves)
{
if (mlx->map[mlx->i][mlx->j - 1] != '1')
{
*moves = *moves + 1;
mlx->j = mlx->j - 1;
moves_utils(mlx, D_LEFT);
mlx->p_x -= PIXCEL;
if (mlx->map[mlx->i][mlx->j] == 'E' && mlx->count == 0)
destory("You win!\n", mlx, 1);
mlx_put_image_to_window (mlx->mlx, mlx->win, mlx->p.img, mlx->p_x,
mlx->p_y);
mlx_put_image_to_window (mlx->mlx, mlx->win, mlx->s.img,
mlx->p_x + PIXCEL, mlx->p_y);
if (mlx->map[mlx->i][mlx->j + 1] == 'S')
left_utils(mlx);
if (mlx->map[mlx->i][mlx->j + 1] == 'E')
mlx_put_image_to_window (mlx->mlx, mlx->win, mlx->e.img,
mlx->p_x + PIXCEL, mlx->p_y);
ft_printf("%d\n", *moves);
}
}