-
Notifications
You must be signed in to change notification settings - Fork 0
/
player_movement.c
147 lines (137 loc) · 3.66 KB
/
player_movement.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* player_movement.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rmitache <rmitache@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/07 16:09:18 by rmitache #+# #+# */
/* Updated: 2023/08/09 22:25:36 by rmitache ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
int legal_moves(t_game *game, int x, int y)
{
if (game->map[y][x] == '1')
return (0);
if (game->map[y][x] == 'C')
{
game->player.coins++;
game->map[y][x] = '0';
}
else if (game->map[y][x] == 'E')
{
if (game->player.coins != game->c_c)
{
ft_printf("Pick the remaining coins!\n");
return (0);
}
ft_victory(game);
return (1);
}
return (1);
}
void move_right(t_game *game)
{
int ypos;
int xpos;
int next_xpos;
t_image image;
image.path = "texture/player/player_right0.xpm";
image.wlx = *game;
ypos = game->player.y;
xpos = game->player.x + SIZE;
/* if (game->map[(ypos / SIZE)][(xpos / SIZE)] == 'E'
&& game->player.coins != game->c_c)
{
next_xpos = xpos + SIZE;
xpos = next_xpos;
} */
if (legal_moves(game, (xpos / SIZE), (ypos / SIZE)))
{
update(game, xpos, ypos);
game->player.x = xpos;
game->player.y = ypos;
game->player.moves++;
put_image(image, xpos, ypos);
ft_printf("Player Moves: %d\n", game->player.moves);
}
}
void move_left(t_game *game)
{
int ypos;
int xpos;
int prev_xpos;
t_image image;
image.path = "texture/player/player_right0.xpm";
image.wlx = *game;
ypos = game->player.y;
xpos = game->player.x - SIZE;
/* if (game->map[(ypos / SIZE)][(xpos / SIZE)] == 'E'
&& game->player.coins != game->c_c)
{
prev_xpos = xpos - SIZE;
xpos = prev_xpos;
} */
if (legal_moves(game, (xpos / SIZE), (ypos / SIZE)))
{
update(game, xpos, ypos);
game->player.x = xpos;
game->player.y = ypos;
put_image(image, xpos, ypos);
game->player.moves++;
ft_printf("Player Moves: %d\n", game->player.moves);
}
}
void move_up(t_game *game)
{
int ypos;
int xpos;
int prev_ypos;
t_image image;
image.path = "texture/player/player_right0.xpm";
image.wlx = *game;
ypos = game->player.y - SIZE;
xpos = game->player.x;
/* if (game->map[(ypos / SIZE)][(xpos / SIZE)] == 'E'
&& game->player.coins != game->c_c)
{
prev_ypos = ypos - SIZE;
ypos = prev_ypos;
} */
if (legal_moves(game, (xpos / SIZE), (ypos / SIZE)))
{
update(game, xpos, ypos);
game->player.x = xpos;
game->player.y = ypos;
put_image(image, xpos, ypos);
game->player.moves++;
ft_printf("Player Moves: %d\n", game->player.moves);
}
}
void move_down(t_game *game)
{
int ypos;
int xpos;
int next_ypos;
t_image image;
image.path = "texture/player/player_right0.xpm";
image.wlx = *game;
ypos = game->player.y + SIZE;
xpos = game->player.x;
/* if (game->map[(ypos / SIZE)][(xpos / SIZE)] == 'E'
&& game->player.coins != game->c_c)
{
next_ypos = ypos + SIZE;
ypos = next_ypos;
} */
if (legal_moves(game, (xpos / SIZE), (ypos / SIZE)))
{
update(game, xpos, ypos);
game->player.x = xpos;
game->player.y = ypos;
put_image(image, xpos, ypos);
game->player.moves++;
ft_printf("Player Moves: %d\n", game->player.moves);
}
}