-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.c
68 lines (63 loc) · 2.3 KB
/
game.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* game.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fatkeski <fatkeski@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/03 21:10:37 by fatkeski #+# #+# */
/* Updated: 2024/05/04 19:37:19 by fatkeski ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/so_long.h"
static int move(t_game *game, int step_x, int step_y)
{
char next;
char current;
current = game->map->vector[(game->player).y][(game->player).x];
next = game->map->vector[(game->player).y + step_y][(game->player).x
+ step_x];
if (next != WALL)
{
(game->move_count)++;
ft_printf("movement: %d\n", game->move_count);
if (next == COLLECTIBLE)
game->collectible_count--;
if (current != EXIT)
{
game->map->vector[(game->player).y][(game->player).x] = FLOOR;
game->map->vector[(game->exit).y][(game->exit).x] = EXIT;
}
game->map->vector[(game->player).y + step_y][(game->player).x
+ step_x] = PLAYER;
game->player.x = game->player.x + step_x;
game->player.y = game->player.y + step_y;
if ((next == EXIT) && (game->collectible_count == 0))
return (1);
}
return (0);
}
int key_handler(int key_code, t_game *game)
{
int move_response;
if (key_code == ESC)
exit_game(game);
if (key_code == W || key_code == UP)
move_response = move(game, 0, -1);
else if (key_code == S || key_code == DOWN)
move_response = move(game, 0, 1);
else if (key_code == D || key_code == RIGHT)
move_response = move(game, 1, 0);
else if (key_code == A || key_code == LEFT)
move_response = move(game, -1, 0);
else
return (0);
if (move_response == 1)
{
ft_printf("### YOU WON THE GAME TOTAL %d STEPS! ###\n",
game->move_count);
exit_game(game);
}
put_image_to_window(game);
return (0);
}