64 lines
2.0 KiB
C
64 lines
2.0 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* key.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/01/09 18:35:27 by cchauvet #+# #+# */
|
|
/* Updated: 2023/01/16 15:39:43 by cchauvet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "solong.h"
|
|
|
|
static void ft_case_update(t_data *data, char *new_pos, char *player_pos)
|
|
{
|
|
*new_pos = 'P';
|
|
*player_pos = '0';
|
|
ft_printf("\r%d", ++data->nb_foots);
|
|
}
|
|
|
|
static void ft_move(t_data *data, char direction)
|
|
{
|
|
size_t tab[2];
|
|
char *new_pos;
|
|
|
|
tab[0] = data->map->player_pos[0];
|
|
tab[1] = data->map->player_pos[1];
|
|
if (direction == 'L')
|
|
new_pos = &data->map->patern[tab[1]][tab[0] - 1];
|
|
if (direction == 'U')
|
|
new_pos = &data->map->patern[tab[1] - 1][tab[0]];
|
|
if (direction == 'D')
|
|
new_pos = &data->map->patern[tab[1] + 1][tab[0]];
|
|
if (direction == 'R')
|
|
new_pos = &data->map->patern[tab[1]][tab[0] + 1];
|
|
if (*new_pos == 'C')
|
|
data->map->nb_collectable--;
|
|
if (*new_pos == '0' || *new_pos == 'C')
|
|
ft_case_update(data, new_pos,
|
|
&data->map->patern[tab[1]][tab[0]]);
|
|
if (*new_pos == 'E')
|
|
{
|
|
if (data->map->nb_collectable == 0)
|
|
ft_exit(data);
|
|
}
|
|
ft_draw_map(data);
|
|
}
|
|
|
|
int ft_key(int keycode, t_data *data)
|
|
{
|
|
if (keycode == 65361)
|
|
ft_move(data, 'L');
|
|
else if (keycode == 65363)
|
|
ft_move(data, 'R');
|
|
else if (keycode == 65362)
|
|
ft_move(data, 'U');
|
|
else if (keycode == 65364)
|
|
ft_move(data, 'D');
|
|
else if (keycode == 65307)
|
|
ft_exit(data);
|
|
return (0);
|
|
}
|