-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.c
80 lines (73 loc) · 2.77 KB
/
hooks.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hooks.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: antandre <antandre@student.42barcel> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/11 11:20:42 by antandre #+# #+# */
/* Updated: 2024/10/22 14:49:12 by antandre ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
static void move_up(t_game *game, int y, int x)
{
if (game->map.array[(y - IMG_W) / IMG_W][x / IMG_H] != '1')
{
game->img->pnj->instances[0].y -= IMG_W;
game->position.move++;
ft_printf("MOVES: %i\n", game->position.move);
}
}
static void move_down(t_game *game, int y, int x)
{
if (game->map.array[(y + IMG_W) / IMG_W][x / IMG_H] != '1')
{
game->img->pnj->instances[0].y += IMG_W;
game->position.move++;
ft_printf("MOVES: %i\n", game->position.move);
}
}
static void move_right(t_game *game, int y, int x)
{
if (game->map.array[y / IMG_W][(x + IMG_H) / IMG_H] != '1')
{
game->img->pnj->instances[0].x += IMG_H;
game->position.move++;
ft_printf("MOVES: %i\n", game->position.move);
}
}
static void move_left(t_game *game, int y, int x)
{
if (game->map.array[y / IMG_W][(x - IMG_H) / IMG_H] != '1')
{
game->img->pnj->instances[0].x -= IMG_H;
game->position.move++;
ft_printf("MOVES: %i\n", game->position.move);
}
}
void my_key_hook(mlx_key_data_t keydata, void *param)
{
t_game *game;
int *y;
int *x;
game = param;
y = &game->img->pnj->instances[0].y;
x = &game->img->pnj->instances[0].x;
if ((keydata.key == MLX_KEY_W && keydata.action == MLX_PRESS)
|| (keydata.key == MLX_KEY_UP && keydata.action == MLX_PRESS))
move_up(game, *y, *x);
if ((keydata.key == MLX_KEY_D && keydata.action == MLX_PRESS)
|| (keydata.key == MLX_KEY_RIGHT && keydata.action == MLX_PRESS))
move_right(game, *y, *x);
if ((keydata.key == MLX_KEY_S && keydata.action == MLX_PRESS)
|| (keydata.key == MLX_KEY_DOWN && keydata.action == MLX_PRESS))
move_down(game, *y, *x);
if ((keydata.key == MLX_KEY_A && keydata.action == MLX_PRESS)
|| (keydata.key == MLX_KEY_LEFT && keydata.action == MLX_PRESS))
move_left(game, *y, *x);
if (keydata.key == MLX_KEY_ESCAPE && keydata.action == MLX_PRESS)
mlx_close_window(game->mlx);
pick_collect(game, *y, *x);
finish_game(game, *y, *x);
}