/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   player.c                                           :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: erey-bet <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2022/12/29 18:06:32 by erey-bet          #+#    #+#             */
/*   Updated: 2023/01/02 16:30:35 by erey-bet         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "../so_long.h"

static int	get_new_element(t_data *data, int x, int y)
{
	return(data->map[data->y_player + y][data->x_player + x]);
}

static void	set_new_element(t_data *data, int x, int y)
{
	data->map[data->y_player + y][data->x_player + x] = '0';
	if (!has_element(data->map, 'C'))
		data->map[data->y_player + y][data->x_player + x] = '3';

}

void	*set_position_player(t_data *data, t_xy *xy)
{
	data->x_player = xy->x;
	data->y_player = xy->y;
	free(xy);
	return (data);
}

void	move(t_data *data, int x, int y)
{
	if (get_new_element(data, x, y) == '1')
		return ;
	if (get_new_element(data, x, y) == 'C')
		set_new_element(data, x, y);
	if (get_new_element(data, x, y) == 'E' || get_new_element(data, x, y) == 'e')
		if (end(data))
			return ;
	data->x_player += x;
	data->y_player += y;
	ft_putnbr_fd(data->move++, 1);
	ft_putchar_fd('\n', 1);
}
