Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display number of Soldier Ants #38

Closed
StevenJAckerman opened this issue Aug 10, 2021 · 1 comment
Closed

Display number of Soldier Ants #38

StevenJAckerman opened this issue Aug 10, 2021 · 1 comment

Comments

@StevenJAckerman
Copy link
Contributor

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.

@johnBuffer
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants