/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   collision.c                                        :+:    :+:            */
/*                                                     +:+                    */
/*   By: qmennen <qmennen@student.codam.nl>           +#+                     */
/*                                                   +#+                      */
/*   Created: 2025/04/22 14:40:59 by qmennen       #+#    #+#                 */
/*   Updated: 2025/05/23 15:23:14 by whaffman      ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include "collision.h"

void	collect(t_player *player)
{
	player->battery += 0.5f;
	if (player->battery > 1.f)
		player->battery = 1.f;
}

int		collision_sprite(t_map *map, t_player *player, double xa, double ya)
{
	t_sprite	*sprites;
	int			i;

	sprites = map->sprites;
	i = 0;
	while (i < map->n_sprites)
	{
		if (sprites[i].visible && sprites[i].collectible)
		{
			if (fabs(player->pos.x + xa - sprites[i].pos.x) < 0.5
				&& fabs(player->pos.y + ya - sprites[i].pos.y) < 0.5)
			{
				sprites[i].visible = 0;
				collect(player);
				return (1);
			}
		}
		i++;
	}
	return (0);
}

int	collision_horizontal(t_map *map, t_player *player, double xa)
{
	t_tile	tile;

	tile = get_tile(map, (int)((player->pos.x + xa)), (int)((player->pos.y)));
	return (tile != TILE_WALL && tile != TILE_VOID);
}

int	collision_vertical(t_map *map, t_player *player, double ya)
{
	t_tile	tile;

	tile = get_tile(map, (int)((player->pos.x)), (int)((player->pos.y + ya)));
	return (tile != TILE_WALL && tile != TILE_VOID);
}
