-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.c
More file actions
82 lines (76 loc) · 2.36 KB
/
Copy pathinput.c
File metadata and controls
82 lines (76 loc) · 2.36 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* input.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mpedraza <mpedraza@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/16 17:32:17 by mpedraza #+# #+# */
/* Updated: 2026/01/23 18:50:14 by mpedraza ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
static void update_game_state(t_game *g)
{
if (g->p.c == g->i.c && g->p.a == g->i.a)
g->e.status = OPEN;
else
g->e.status = CLOSED;
if (g->p.c == 0 && g->p.a == 0)
g->p.status = BASE;
if (g->p.c == g->i.c && g->p.a == g->i.a)
g->p.status = READY;
else if (g->p.c == g->i.c && g->p.a != g->i.a)
g->p.status = SAKE;
else if (g->p.c != g->i.c && g->p.a == g->i.a)
g->p.status = NORI;
}
static void execute_move(t_game *g, int d_x, int d_y)
{
g->map[g->p.y][g->p.x] = '0';
g->p.x = d_x;
g->p.y = d_y;
g->map[d_y][d_x] = 'P';
g->p.moves += 1;
print_status(g);
}
static void parse_move(t_game *g, int move_x, int move_y)
{
int d_x;
int d_y;
d_x = g->p.x + move_x;
d_y = g->p.y + move_y;
if (g->map[d_y][d_x] == '1' || g->p.status == BENTO)
return ;
if (g->map[d_y][d_x] == 'E')
{
if (g->e.status == CLOSED)
return ;
if (g->e.status == OPEN)
g->p.status = BENTO;
}
if (g->map[d_y][d_x] == 'C' || g->map[d_y][d_x] == 'A')
{
if (g->map[d_y][d_x] == 'C')
g->p.c++;
else
g->p.a++;
update_game_state(g);
}
execute_move(g, d_x, d_y);
render_map(g);
}
int handle_keypress(int keycode, t_game *g)
{
if (keycode == KEY_ESC)
exit_game(g, 0);
else if (keycode == KEY_UP || keycode == KEY_W)
parse_move(g, 0, -1);
else if (keycode == KEY_DOWN || keycode == KEY_S)
parse_move(g, 0, 1);
else if (keycode == KEY_LEFT || keycode == KEY_A)
parse_move(g, -1, 0);
else if (keycode == KEY_RIGHT || keycode == KEY_D)
parse_move(g, 1, 0);
return (0);
}