/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_move_img.c                                      :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: yantoine <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/02/01 13:51:53 by yantoine          #+#    #+#             */
/*   Updated: 2024/02/22 02:28:34 by yantoine         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */
#include "../so_long.h"

static int	can_go(char emplacement, t_data *data)
{
	if (emplacement == '2')
	{
		data->coin += 1;
		ft_printf("coin: %d\n", data->coin);
	}
	else if (emplacement == '1')
		return (0);
	return (1);
}

static void	check_position(t_position position, char **map, t_data *data)
{
	if (map[position.y / 64][position.x / 64] == '2')
	{
		map[position.y / 64][position.x / 64] = '0';
	}
	else if (map[position.y / 64][position.x / 64] == '3')
	{
		if (data->coin == data->max_coin)
		{
			data->move += 1;
			ft_printf("nombre de deplacement: %d\n",
				data->move);
			ft_printf("GAGNEE\n");
			ft_exit(data);
		}
	}
	data->move += 1;
	return ;
}

void	ft_move_img(t_data *data, int keycode)
{
	if (keycode == 'w' && can_go(data->map[((data->player.position.y / 64)
					- 1)][((data->player.position.x) / 64)], data) == 1)
	{
		data->player.position.y -= 64;
		check_position(data->player.position, data->map, data);
	}
	if (keycode == 's' && can_go(data->map[((data->player.position.y) / 64) + 1]
			[(data->player.position.x) / 64], data) == 1)
	{
		data->player.position.y += 64;
		check_position(data->player.position, data->map, data);
	}
	if (keycode == 'a' && can_go(data->map[((data->player.position.y) / 64)]
			[((data->player.position.x) / 64) - 1], data) == 1)
	{
		data->player.position.x -= 64;
		check_position(data->player.position, data->map, data);
	}
	if (keycode == 'd' && can_go(data->map[((data->player.position.y) / 64)]
			[((data->player.position.x) / 64) + 1], data) == 1)
	{
		data->player.position.x += 64;
		check_position(data->player.position, data->map, data);
	}
	return ;
}
