#include "collectables_collection.hpp" #include "collectables_depth_collection.hpp" #include "../../../logging/easylogging++.h" std::shared_ptr CollectablesCollection::getInstance() { if (singletonInstance == nullptr) { singletonInstance = std::make_shared(); } return singletonInstance; } void CollectablesCollection::createEmpty(int maxDepth) { // Remove previous collections depthCollections.clear(); LOG(INFO) << "Creating empty collectables collection with a max depth of " << maxDepth << " ..."; // Create new collections for (int d = 0; d < maxDepth; d++) { auto depthCollection = std::make_shared(d); depthCollections[d] = depthCollection; } } void CollectablesCollection::remove(const std::shared_ptr &collectable) { depthCollections[collectable->getDepth()]->remove(collectable); } void CollectablesCollection::add(const std::shared_ptr &collectable) { depthCollections[collectable->getDepth()]->add(collectable); } void CollectablesCollection::update() { updateCollectables(); // Move collectables to new depth collections if necessary // First, clear history of all depth collections for (auto &[depth, depthCollection]: depthCollections) { depthCollection->clearHistory(); } // Then, move collectables to new depth collections for (auto &[depth, depthCollection]: depthCollections) { for (auto &collectable: depthCollection->collectables) { auto newDepth = collectable->getDepth(); if (newDepth != depth) { depthCollection->remove(collectable); depthCollections[newDepth]->add(collectable); } } } } void CollectablesCollection::draw(sf::RenderWindow *window) { // Render collectables in reverse order of depth int maxDepth = depthCollections.size(); for (int depth = maxDepth - 1; depth >= 0; depth--) { auto depthCollection = depthCollections.at(depth); for (auto &collectable: depthCollection->collectables) { collectable->draw(window); } } } void CollectablesCollection::updateCollectables() { for (auto &[depth, depthCollection]: depthCollections) { for (auto &collectable: depthCollection->collectables) { collectable->update(); } } }