-
Notifications
You must be signed in to change notification settings - Fork 0
/
so_long_move.c
109 lines (98 loc) · 3.21 KB
/
so_long_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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* so_long_move.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mjong <mjong@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/13 16:04:21 by mjong #+# #+# */
/* Updated: 2024/05/21 14:35:50 by mjong ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
void move_player3(t_game *game, uint32_t xdir, uint32_t ydir)
{
uint32_t prevxdir;
uint32_t prevydir;
prevxdir = game->xpos;
prevydir = game->ypos;
game->xpos += xdir;
game->ypos += ydir;
mlx_image_to_window(game->mlx, game->eexit, prevxdir, prevydir);
mlx_image_to_window(game->mlx, game->player, game->xpos, game->ypos);
game->movecount += 1;
ft_printf("Number of movements: %d\n", game->movecount);
}
void move_player2(t_game *game, uint32_t xdir, uint32_t ydir)
{
uint32_t prevxdir;
uint32_t prevydir;
prevxdir = game->xpos;
prevydir = game->ypos;
game->xpos += xdir;
game->ypos += ydir;
mlx_image_to_window(game->mlx, game->ffloor, prevxdir, prevydir);
mlx_image_to_window(game->mlx, game->player, game->xpos, game->ypos);
game->movecount += 1;
ft_printf("Number of movements: %d\n", game->movecount);
}
void move_player_e(t_game *game, uint32_t xdir, uint32_t ydir)
{
int x;
int y;
x = (game->xpos + xdir) / 100;
y = (game->ypos + ydir) / 100;
if (game->two_d_map[y][x] == 'E' && game->colnum > 0)
move_player2(game, xdir, ydir);
else if (game->two_d_map[y][x] == 'E' && game->colnum <= 0)
{
move_player2(game, xdir, ydir);
ft_printf("YOU WIN!\n");
ft_exitgame(game, "success");
}
}
void move_player(t_game *game, uint32_t xdir, uint32_t ydir)
{
int x;
int y;
uint32_t prevxdir;
uint32_t prevydir;
x = (game->xpos + xdir) / 100;
y = (game->ypos + ydir) / 100;
prevxdir = game->xpos;
prevydir = game->ypos;
if (game->two_d_map[prevydir / 100][prevxdir / 100] == 'E'
&& game->two_d_map[y][x] != '1')
move_player3(game, xdir, ydir);
else if (game->two_d_map[y][x] == '0' || game->two_d_map[y][x] == 'P')
move_player2(game, xdir, ydir);
else if (game->two_d_map[y][x] == 'C')
{
game->colnum--;
move_player2(game, xdir, ydir);
}
else if (game->two_d_map[y][x] == 'E')
move_player_e(game, xdir, ydir);
}
void ft_hooks(mlx_key_data_t keydata, t_game *game)
{
uint32_t xdir;
uint32_t ydir;
xdir = 0;
ydir = 0;
if (keydata.action == MLX_PRESS || keydata.action == MLX_REPEAT)
{
if (keydata.key == MLX_KEY_ESCAPE)
ft_exitgame(game, "exit");
if (keydata.key == MLX_KEY_W)
ydir = -100;
if (keydata.key == MLX_KEY_A)
xdir = -100;
if (keydata.key == MLX_KEY_S)
ydir = 100;
if (keydata.key == MLX_KEY_D)
xdir = 100;
if (xdir || ydir)
move_player(game, xdir, ydir);
}
}