/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   collision.c                                        :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: qmennen <qmennen@student.codam.nl>         +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2025/04/22 14:40:59 by qmennen           #+#    #+#             */
/*   Updated: 2025/06/04 18:35:34 by qmennen          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#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_game *game, double xa, double ya)
{
	t_player	*player;
	t_map		*map;
	t_sprite	*sprites;
	int			i;

	player = game->player;
	map = game->map;
	sprites = map->sprites;
	i = 0;
	while (i < map->n_sprites)
	{
		if ((sprites[i].type == SPRITE_TYPE_COLLECTIBLE
				|| sprites[i].type == SPRITE_TYPE_ENEMY)
			&& (fabs(player->pos.x + xa - sprites[i].pos.x) < 0.5
				&& fabs(player->pos.y + ya - sprites[i].pos.y) < 0.5))
		{
			if (sprites[i].type == SPRITE_TYPE_ENEMY)
			{
				player->battery -= 0.001;
				player->hit_timer = .65f;				
			}
			else
			{
				sprites[i].type = SPRITE_TYPE_COLLECTED;
				collect(player);
				game->scoreboard->collectibles++;
			}
			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 && tile != TILE_DOOR);
}

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 && tile != TILE_DOOR);
}
