2023-01-09 14:41:06 -05:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* key.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2023/01/09 18:35:27 by cchauvet #+# #+# */
|
2023-01-16 06:53:52 -05:00
|
|
|
/* Updated: 2023/01/16 12:52:02 by cchauvet ### ########.fr */
|
2023-01-09 14:41:06 -05:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
#include "solong.h"
|
|
|
|
|
2023-01-13 10:10:11 -05:00
|
|
|
static void ft_case_update(char *new_pos, char *player_pos)
|
2023-01-09 14:41:06 -05:00
|
|
|
{
|
|
|
|
*new_pos = 'P';
|
|
|
|
*player_pos = '0';
|
2023-01-11 13:10:28 -05:00
|
|
|
}
|
|
|
|
|
2023-01-13 09:10:27 -05:00
|
|
|
static void ft_epileptic_mod(t_data *data)
|
2023-01-11 13:10:28 -05:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
i = 0;
|
2023-01-13 09:10:27 -05:00
|
|
|
while (i < NB_COLORS * 100)
|
2023-01-11 13:10:28 -05:00
|
|
|
{
|
2023-01-13 09:10:27 -05:00
|
|
|
ft_draw_img(data, data->assets[(i % NB_COLORS) + NB_COLORS], 0, 0);
|
|
|
|
write(1, "\a\a\a\a\a", 5);
|
2023-01-11 13:10:28 -05:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
ft_exit(data);
|
2023-01-09 14:41:06 -05:00
|
|
|
}
|
|
|
|
|
2023-01-13 09:10:27 -05:00
|
|
|
static void ft_move(t_data *data, char direction)
|
2023-01-09 14:41:06 -05:00
|
|
|
{
|
2023-01-10 13:04:46 -05:00
|
|
|
size_t tab[2];
|
2023-01-09 14:41:06 -05:00
|
|
|
char *new_pos;
|
|
|
|
|
2023-01-10 13:04:46 -05:00
|
|
|
tab[0] = data->map->player_pos[0];
|
|
|
|
tab[1] = data->map->player_pos[1];
|
2023-01-09 14:41:06 -05:00
|
|
|
if (direction == 'L')
|
2023-01-10 13:04:46 -05:00
|
|
|
new_pos = &data->map->patern[tab[1]][tab[0] - 1];
|
2023-01-09 14:41:06 -05:00
|
|
|
if (direction == 'U')
|
2023-01-10 13:04:46 -05:00
|
|
|
new_pos = &data->map->patern[tab[1] - 1][tab[0]];
|
2023-01-09 14:41:06 -05:00
|
|
|
if (direction == 'D')
|
2023-01-10 13:04:46 -05:00
|
|
|
new_pos = &data->map->patern[tab[1] + 1][tab[0]];
|
2023-01-09 14:41:06 -05:00
|
|
|
if (direction == 'R')
|
2023-01-10 13:04:46 -05:00
|
|
|
new_pos = &data->map->patern[tab[1]][tab[0] + 1];
|
2023-01-09 14:41:06 -05:00
|
|
|
if (*new_pos == 'C')
|
2023-01-10 13:04:46 -05:00
|
|
|
data->map->nb_collectable--;
|
2023-01-09 14:41:06 -05:00
|
|
|
if (*new_pos == '0' || *new_pos == 'C')
|
2023-01-13 10:10:11 -05:00
|
|
|
ft_case_update(new_pos, &data->map->patern[tab[1]][tab[0]]);
|
2023-01-10 13:04:46 -05:00
|
|
|
if (*new_pos == 'E')
|
|
|
|
{
|
|
|
|
if (data->map->nb_collectable == 0)
|
|
|
|
ft_exit(data);
|
|
|
|
}
|
2023-01-11 13:10:28 -05:00
|
|
|
if (*new_pos == 'B')
|
2023-01-13 09:10:27 -05:00
|
|
|
ft_epileptic_mod(data);
|
2023-01-09 14:41:06 -05:00
|
|
|
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');
|
2023-01-10 13:04:46 -05:00
|
|
|
else if (keycode == 65307)
|
|
|
|
ft_exit(data);
|
2023-01-09 14:41:06 -05:00
|
|
|
return (0);
|
|
|
|
}
|