Exemple #1
0
// evades predator thats is left,roght,up or down from this
bool Rabbit::Evade()
{
    if (metabolism.GetFoodLevel() <= 30)
        return false;

    World* world = World::GetWorld();

	BuildAdjCross();

	auto predicate = [&](sf::Vector2i& e) // look for predators
	{
		try
		{
			return !world->IsEmpty(e) && world->GetCreature(e)->FearMe(this);
		}
		catch (ArgumentOutOfBounds)
		{
			return false;
		}
	};

	auto it = std::find_if(adjFields.begin(), adjFields.end(), predicate);

	if (it != adjFields.end()) // if predator found
	{
		// dst is 2 fields in the opposite direction
        try
        {
            sf::Vector2i dst = GetLocation() + (GetLocation() - *it) * 2;
            MoveTo(dst);
        }
        catch (ArgumentOutOfBounds) // prevent rabbit running of the screen
        {
        }
		return true;
	}
	else
		return false;
}