I've got these two template functions responsible of adding/removing actors in-game in GameEngine.h
:
template<typename T>
void add(T actor);
template<typename T>
void remove(T actor);
And these arrays:
std::set<std::shared_ptr<Tickable>> tickables;
std::set<std::shared_ptr<Hitbox>> hitboxes;
std::set<std::shared_ptr<sf::Drawable>> drawables;
std::set<std::shared_ptr<EventCatch>> eventCatchers;
Currently, the way i fill these is really... horrible, here's GameEngine.cpp
:
template<>
void GameEngine::add<std::shared_ptr<sf::Drawable>>(std::shared_ptr<sf::Drawable> actor)
{
drawables.insert(actor);
}
template<>
void GameEngine::add<std::shared_ptr<Hitbox>>(std::shared_ptr<Hitbox> actor)
{
hitboxes.insert(actor);
}
template<>
void GameEngine::add<std::shared_ptr<Tickable>>(std::shared_ptr<Tickable> actor)
{
tickables.insert(actor);
}
template<>
void GameEngine::add<std::shared_ptr<EventCatch>>(std::shared_ptr<EventCatch> actor)
{
eventCatchers.insert(actor);
}
template<>
void GameEngine::remove<std::shared_ptr<sf::Drawable>>(std::shared_ptr<sf::Drawable> actor)
{
drawables.erase(actor);
}
template<>
void GameEngine::remove<std::shared_ptr<Hitbox>>(std::shared_ptr<Hitbox> actor)
{
hitboxes.erase(actor);
}
template<>
void GameEngine::remove<std::shared_ptr<Tickable>>(std::shared_ptr<Tickable> actor)
{
tickables.erase(actor);
}
template<>
void GameEngine::remove<std::shared_ptr<EventCatch>>(std::shared_ptr<EventCatch> actor)
{
eventCatchers.erase(actor);
}
My knowledge about template / smart pointer is limited(yet), is there a better way to do this?
Even if that modifies the arrays, as long as i can call everything is the Loop:
The game loop looks like that in GameEngine.cpp
:
void GameEngine::GameLoop()
{
sf::Clock timer;
sf::Time tickRate;
bool run = true;
Level level(this);
while (run) {
clear();
for (std::shared_ptr<sf::Drawable> drawable : drawables)
draw(*drawable);
display();
sf::Event event;
while (pollEvent(event))
{
switch (event.type) {
case sf::Event::Closed:
close();
run = false;
break;
default:
for (std::shared_ptr<EventCatch> evt : eventCatchers)
evt->onEvent(event);
break;
}
}
for (std::shared_ptr<Tickable> tick : tickables)
tick->onTick(tickRate.asMicroseconds());
tickRate = timer.restart();
}
}