johnBuffer/AntSimulator

Display number of Soldier Ants

Closed this issue · 1 comments

I was interested in knowing the number of soldiers in the ant population. So I played with the following changes:

Added the following to the bottom of colony.hpp:

uint32_t soldiersCount()
{
	return std::count_if(ants.begin(), ants.end(), [](Ant& a) { return a.type == Ant::Type::Soldier; });
}

Changes to colony_renderer.hpp:

Added a soldiersCount variable:

uint32_t ants_count = 0;
uint32_t soldiers_count = 0;

Changed updateData( ) to retrieve the soldiersCount:

	ants_count = to<int32_t>(colony.ants.size());
	soldiers_count = to<int32_t>(colony.soldiersCount());

And then changed the render( ) to show the soldiersCount and right-justify the pop_diff:

	text.setCharacterSize(20);
	text.setFillColor(sf::Color::White);
	text.setPosition(position.x + padding, position.y);
	text.setString("Population " + toStr(soldiers_count) + "/" + toStr(ants_count));
	target.draw(text);

	text.setCharacterSize(14);
	text.setFillColor(pop_diff >= 0 ? sf::Color::Green : sf::Color::Red);
	const std::string sign = (pop_diff >= 0 ? "+" : "");
	text.setString("(" + sign + toStr(pop_diff) + " Ants over last 60s)");
	// right justify
	sf::FloatRect bounds = text.getLocalBounds();
	text.setPosition((position.x + population.width + padding) - bounds.width, position.y + 0.5f * padding);

	target.draw(text);

Kudos for writing this simulation - everybody who has seen it work has been fascinated.

Thank you very much for sharing this!
If you want you can create a pull request to add this into the project, I think it is a great idea.