/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   moves.c                                            :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: qmennen <qmennen@student.codam.nl>         +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2025/05/14 20:08:27 by whaffman          #+#    #+#             */
/*   Updated: 2025/06/04 18:42:35 by qmennen          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "cub3d.h"

static void	move(t_map *map, t_player *player, int dir, double delta)
{
	double		xa;
	double		ya;

	xa = dir * player->dir.x * player->speed * delta;
	ya = dir * player->dir.y * player->speed * delta;
	if (xa != 0 && collision_horizontal(map, player, xa))
	{
		player->pos.x += xa;
		player->is_moving = 1;
	}
	if (ya != 0 && collision_vertical(map, player, ya))
	{
		player->pos.y += ya;
		player->is_moving = 1;
	}
}

static void	strave(t_map *map, t_player *player, int dir, double delta)
{
	double		xa;
	double		ya;

	xa = dir * perp(player->dir).x * player->speed * delta;
	ya = dir * perp(player->dir).y * player->speed * delta;
	if (xa != 0 && collision_horizontal(map, player, xa))
	{
		player->pos.x += xa;
		player->is_moving = 1;
	}
	if (ya != 0 && collision_vertical(map, player, ya))
	{
		player->is_moving = 1;
		player->pos.y += ya;
	}
}

void	rotate(t_player *player, double rot_speed, double delta)
{
	player->dir = rot(player->dir, rot_speed * delta);
	player->camera = rot(player->camera, rot_speed * delta);
}

void	visit_area(t_game *game)
{
	int	i;
	int	x;
	int	y;

	i = 0;
	while (i < 9)
	{
		x = game->player->pos.x + (i % 3) - 1;
		y = game->player->pos.y + (i / 3) - 1;
		if (get_tile(game->map, x, y) == TILE_EMPTY)
		{
			game->map->grid[y][x] = TILE_VISITED;
		}
		i++;
	}
}

void	player_update(t_game *game, double delta_time)
{
	game->player->is_moving = 0;
	visit_area(game);
	if (get_key(game, MLX_KEY_W))
		move(game->map, game->player, 1, delta_time);
	else if (get_key(game, MLX_KEY_S))
		move(game->map, game->player, -1, delta_time);
	if (get_key(game, MLX_KEY_A))
		strave(game->map, game->player, -1, delta_time);
	else if (get_key(game, MLX_KEY_D))
		strave(game->map, game->player, 1, delta_time);
	if (get_key(game, MLX_KEY_LEFT))
		rotate(game->player, -2.5, delta_time);
	else if (get_key(game, MLX_KEY_RIGHT))
		rotate(game->player, 2.5, delta_time);
	if (get_key(game, MLX_KEY_SPACE))
	{
		game->screen->flash = 3;
		game->player->battery -= 0.1f;
		interact_door(game);
	}
	if (game->player->hit_timer > 0)
		game->player->hit_timer -= .1f;
}
