100 lines
2.8 KiB
C++
100 lines
2.8 KiB
C++
#include "collectables_collection.hpp"
|
|
#include "collectables_depth_collection.hpp"
|
|
#include "../../../logging/easylogging++.h"
|
|
|
|
std::shared_ptr<CollectablesCollection> CollectablesCollection::getInstance()
|
|
{
|
|
if (singletonInstance == nullptr)
|
|
{
|
|
singletonInstance = std::make_shared<CollectablesCollection>();
|
|
}
|
|
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<CollectablesDepthCollection>(d);
|
|
depthCollections[d] = depthCollection;
|
|
}
|
|
}
|
|
|
|
void CollectablesCollection::remove(const std::shared_ptr<Collectable> &collectable)
|
|
{
|
|
depthCollections[collectable->getDepth()]->remove(collectable);
|
|
|
|
}
|
|
|
|
void CollectablesCollection::add(const std::shared_ptr<Collectable> &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 = (int) 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();
|
|
}
|
|
}
|
|
}
|
|
|
|
std::shared_ptr<CollectablesDepthCollection> CollectablesCollection::getDepthCollection(int depth)
|
|
{
|
|
if (depthCollections.find(depth) == depthCollections.end())
|
|
{
|
|
LOG(ERROR) << "Depth collection for depth " << depth << " does not exist! Returning empty collection ...";
|
|
return std::make_shared<CollectablesDepthCollection>(depth);
|
|
}
|
|
|
|
return depthCollections[depth];
|
|
}
|