/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_get_player_spawn.c                              :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:28:47 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/14 17:28:47 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <cub3d.h>
#include <math.h>
#include <stdint.h>

static void
	ft_get_e_dir(t_player *pl)
{
	float	sav_dir_x;
	float	sav_plane_x;

	sav_dir_x = pl->dir_x;
	pl->dir_x = -pl->dir_y;
	pl->dir_y = sav_dir_x;
	sav_plane_x = pl->plane_x;
	pl->plane_x = -pl->plane_y;
	pl->plane_y = sav_plane_x;
}

static void
	ft_get_s_dir(t_player *pl)
{
	pl->dir_x = -pl->dir_x;
	pl->dir_y = -pl->dir_y;
	pl->plane_x = -pl->plane_x;
	pl->plane_y = -pl->plane_y;
}

static void
	ft_get_w_dir(t_player *pl)
{
	float	sav_dir_x;
	float	sav_plane_x;

	sav_dir_x = pl->dir_x;
	pl->dir_x = pl->dir_y;
	pl->dir_y = -sav_dir_x;
	sav_plane_x = pl->plane_x;
	pl->plane_x = pl->plane_y;
	pl->plane_y = -sav_plane_x;
}

static void
	ft_get_start_side(char c, t_player *pl)
{
	if (c == 'E')
		ft_get_w_dir(pl);
	else if (c == 'S')
		ft_get_s_dir(pl);
	else if (c == 'W')
		ft_get_e_dir(pl);
}

void
	ft_get_player_spawn(t_player *plist, t_cub *clist)
{
	size_t	x;
	size_t	y;

	x = 1;
	y = 1;
	while (clist->mlist.map[y])
	{
		while (clist->mlist.map[y][x])
		{
			if (ft_ischarset(FT_CHRST_SPAWN, clist->mlist.map[y][x]))
			{
				plist->pos_x = x + 0.5;
				plist->pos_y = y + 0.5;
				plist->start_x = plist->pos_x;
				plist->start_y = plist->pos_y;
				ft_get_start_side(clist->mlist.map[y][x], plist);
				ft_get_sprite_spawn(clist);
				ft_get_trap_spawn(clist);
				ft_get_heal_spawn(clist);
				if (clist->isdead == 0)
					ft_get_weapon_spawn(clist);
				return ;
			}
			x++;
		}
		x = 1;
		y++;
	}
}