I have this problem:
this is my loop for previously used child type
std::vector<Coin>::iterator coin;
for (coin = coinVec.begin(); coin != coinVec.end(); ++coin)
{
sf::FloatRect coinBounds = coin->getGlobalBounds();
if (viewBox.intersects(coinBounds))
{
if (playerBounds.intersects(coinBounds))
{
coin = coinVec.erase(coin);
break;
}
else coin->setTextureRect(currentFrame);
}
}
And the similar one for std::vector<Heal>
.
I rebuild my code structure to: Coin is now child of Collectables.
There is now only one vector: std::vector<Collectables>
which contains all collactable child classes objects like Coin, Heal etc.. Is there a way to make the code work with only one loop and iterators? If yes, how would you do that?
Thanks
I find out that my code is so universal, that I dont need to separate vector elements at all.
My solution here:
std::vector<Collectables>::iterator collect;
for (collect = collectVec.begin(); collect != collectVec.end(); ++collect)
{
sf::FloatRect collectBounds = collect->getGlobalBounds();
if (viewBox.intersects(collectBounds))
{
if (playerBounds.intersects(collectBounds))
{
collect = collectVec.erase(collect);
break;
}
else collect->setTextureRect(currentFrame);
}
}
And if you really need to separate elements, try this:
if (collect->getType() == ResourceManager::enums::textures::coin)
{
} else
if (collect->getType() == ResourceManager::enums::textures::health)
{
}
Where getType stores simply int id matching enum value.