Example #1
0
void Wheat::Spawn()
{
	World* world = World::GetWorld();

	// roll die
	if (GetFoodLevel() > 50 && rand() % 100 < 5)
	{
		BuildAdjCross();

		auto it = std::find_if(adjFields.begin(), adjFields.end(), [&](auto& e) {
			try
			{
				return !world->GetGrass(e)->HasWheat();
			}
			catch (ArgumentOutOfBounds)
			{
				return false;
			}
		});

		if (it != adjFields.end())
			world->GetGrass(*it)->SpawnWheat();
	}
}
Example #2
0
// rabbit looks for highest grass and moves there
void Rabbit::Move()
{
    World* world = World::GetWorld();
    BuildAdjCross();
	ShuffleAdj();

    // find heighest grass
    int max = 0;
    sf::Vector2i* dst = nullptr;
    for (auto& pos : adjFields)
    {
		try
		{
            // break on wheat because wheat is the best
            if (world->IsEmpty(pos) && world->GetGrass(pos)->HasWheat())
            {
                dst = &pos;
                break;
            }
			if (world->IsEmpty(pos) && world->GetGrass(pos)->GetFoodLevel() > max)
			{
				max = world->GetGrass(pos)->GetFoodLevel();
				dst = &pos;
			}
		}
		catch (ArgumentOutOfBounds)
		{
		}
    }

    // not found
    if (dst == nullptr)
        return;

    world->MoveCreature(this, *dst);
}