-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_player_moves.c
143 lines (136 loc) · 4.39 KB
/
ft_player_moves.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_player_moves.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rabustam <rabustam@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/15 23:19:10 by rabustam #+# #+# */
/* Updated: 2022/08/15 23:55:20 by rabustam ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
void ft_move_up(t_data *game);
void ft_move_down(t_data *game);
void ft_move_right(t_data *game);
void ft_move_left(t_data *game);
void ft_move(int key, t_data *game);
void ft_move_up(t_data *game)
{
if (game->map.map[game->p_pos.y - 1][game->p_pos.x] != '1')
{
game->moves++;
if (game->map.map[game->p_pos.y - 1][game->p_pos.x] == 'C'\
|| game->map.map[game->p_pos.y - 1][game->p_pos.x] == '0')
{
if (game->map.map[game->p_pos.y - 1][game->p_pos.x] == 'C')
{
game->map.collectible--;
game->sock = 1;
ft_change_player(game, PIXEL, game->dir);
}
game->map.map[game->p_pos.y - 1][game->p_pos.x] = 'P';
game->map.map[game->p_pos.y][game->p_pos.x] = '0';
game->p_pos.y--;
ft_render(game);
}
else if (game->map.map[game->p_pos.y - 1][game->p_pos.x] == 'T')
ft_game_over(game);
else if (game->map.map[game->p_pos.y - 1][game->p_pos.x] == 'E')
ft_check_winner(game);
}
}
void ft_move_down(t_data *game)
{
if (game->map.map[game->p_pos.y + 1][game->p_pos.x] != '1')
{
game->moves++;
if (game->map.map[game->p_pos.y + 1][game->p_pos.x] == 'C'\
|| game->map.map[game->p_pos.y + 1][game->p_pos.x] == '0')
{
if (game->map.map[game->p_pos.y + 1][game->p_pos.x] == 'C')
{
game->map.collectible--;
game->sock = 1;
ft_change_player(game, PIXEL, game->dir);
}
game->map.map[game->p_pos.y + 1][game->p_pos.x] = 'P';
game->map.map[game->p_pos.y][game->p_pos.x] = '0';
game->p_pos.y++;
ft_render(game);
}
else if (game->map.map[game->p_pos.y + 1][game->p_pos.x] == 'T')
ft_game_over(game);
else if (game->map.map[game->p_pos.y + 1][game->p_pos.x] == 'E')
ft_check_winner(game);
}
}
void ft_move_right(t_data *game)
{
ft_change_player(game, PIXEL, 'r');
if (game->map.map[game->p_pos.y][game->p_pos.x + 1] != '1')
{
game->moves++;
if (game->map.map[game->p_pos.y][game->p_pos.x + 1] == 'C'\
|| game->map.map[game->p_pos.y][game->p_pos.x + 1] == '0')
{
if (game->map.map[game->p_pos.y][game->p_pos.x + 1] == 'C')
{
game->map.collectible--;
game->sock = 1;
ft_change_player(game, PIXEL, game->dir);
}
game->map.map[game->p_pos.y][game->p_pos.x + 1] = 'P';
game->map.map[game->p_pos.y][game->p_pos.x] = '0';
game->p_pos.x++;
ft_render(game);
}
else if (game->map.map[game->p_pos.y][game->p_pos.x + 1] == 'T')
ft_game_over(game);
else if (game->map.map[game->p_pos.y][game->p_pos.x + 1] == 'E')
ft_check_winner(game);
}
}
void ft_move_left(t_data *game)
{
ft_change_player(game, PIXEL, 'l');
if (game->map.map[game->p_pos.y][game->p_pos.x - 1] != '1')
{
game->moves++;
if (game->map.map[game->p_pos.y][game->p_pos.x - 1] == 'C'\
|| game->map.map[game->p_pos.y][game->p_pos.x - 1] == '0')
{
if (game->map.map[game->p_pos.y][game->p_pos.x - 1] == 'C')
{
game->map.collectible--;
game->sock = 1;
ft_change_player(game, PIXEL, game->dir);
}
game->map.map[game->p_pos.y][game->p_pos.x - 1] = 'P';
game->map.map[game->p_pos.y][game->p_pos.x] = '0';
game->p_pos.x--;
ft_render(game);
}
else if (game->map.map[game->p_pos.y][game->p_pos.x - 1] == 'T')
ft_game_over(game);
else if (game->map.map[game->p_pos.y][game->p_pos.x - 1] == 'E')
ft_check_winner(game);
}
}
void ft_move(int key, t_data *game)
{
if (key == 'w' || key == UP)
ft_move_up(game);
else if (key == 's' || key == DOWN)
ft_move_down(game);
else if (key == 'd' || key == RIGHT)
{
game->dir = 'r';
ft_move_right(game);
}
else if (key == 'a' || key == LEFT)
{
game->dir = 'l';
ft_move_left(game);
}
}