#include <Player.h>
#include <Fruit.h>
#include <Random.h>

Fruit::Fruit(const sf::Vector2i gridPos_in, const Color& color_in)
	: gridPos(gridPos_in)
	, color(color_in)
{ }

sf::Vector2f Fruit::windowPos(sf::Vector2f gameBoundsOrigin, const float size) const
{
	return {gameBoundsOrigin.x + (static_cast<float>(gridPos.x) * size), gameBoundsOrigin.y + (static_cast<float>(gridPos.y) * size)};
}

void Fruit::respawn(const std::vector<SnakeNode>& body_in, const unsigned int gridCount_in)
{
    unsigned int tempX;
    unsigned int tempY;
    bool valid;
    do
    {
        valid = true;
        tempX = Random::get(0u, gridCount_in - 1);
        tempY = Random::get(0u, gridCount_in - 1);
        for (const auto& node : body_in)
        {
            if (node.gridPos.x == tempX and node.gridPos.y == tempY)
            {
                valid = false;
            }
        }
    } while (!valid);
        
    this->gridPos = {tempX, tempY};

}