/* ************************************************************************** */
/*                                                                            */
/*                                                        ::::::::            */
/*   collision.c                                        :+:    :+:            */
/*                                                     +:+                    */
/*   By: qmennen <qmennen@student.codam.nl>           +#+                     */
/*                                                   +#+                      */
/*   Created: 2025/04/22 14:40:59 by qmennen       #+#    #+#                 */
/*   Updated: 2025/06/10 19:34:32 by whaffman      ########   odam.nl         */
/*                                                                            */
/* ************************************************************************** */

#include "collision.h"

static void	collect(t_game *game, t_sprite *sprite)
{
	t_player	*player;

	player = game->player;
	player->battery += 0.5f;
	if (player->battery > 1.f)
		player->battery = 1.f;
	sprite->type = SPRITE_TYPE_COLLECTED;
	game->scoreboard->collectibles++;
}

static void	damage_player(t_player *player, float amount)
{
	player->battery -= amount;
	if (player->battery < 0.f)
		player->battery = 0.f;
	player->hit_timer = 0.65f;
}

int	collision_sprite(t_game *game, double xa, double ya)
{
	t_player		*player;
	t_map			*map;
	t_sprite		*sprites;
	unsigned 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)
				damage_player(player, ATTACK_DAMAGE);
			else
				collect(game, &sprites[i]);
			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);
}
