holesome/src/game/level/level_loader.cpp

75 lines
2.6 KiB
C++
Raw Normal View History

#include "level_loader.hpp"
#include "../game.h"
2023-06-11 15:54:05 +02:00
#include "../physics/map/map_simulation.hpp"
#include "../../debug/grid_debug_layer.h"
#include "../../levels.hpp"
#include "../collectables/collection/collectables_collection.hpp"
#include "../collectables/collectable_factory.hpp"
#include "../player/player_collection.hpp"
#include "../physics/hole/hole_depth_simulation.hpp"
void LevelLoader::loadLevel(const LevelConfig &levelConfig)
{
auto game = Game::getInstance();
game->clearGameObjects();
LOG(INFO) << "Loading level '" << levelConfig.name << "' ...";
game->setLevel(levelConfig);
MapSimulation::getInstance()->resetMap(levelConfig.worldMapSize);
PlayerCollection::getInstance()->clear();
// Add basic game objects
if (DEVELOPER_MODE)
{
game->addGameObject(std::make_shared<GridDebugLayer>(0, 50, 0, 50));
}
game->addGameObject(std::make_shared<TrackingView>());
game->addGameObject(PlayerCollection::getInstance());
PlayerCollection::getInstance()->setSpawnPoints(levelConfig.playerSpawnPoints);
// Prepare collectables framework
auto maxDepth = (int) levelConfig.worldMapSize.x * 2;
auto collectablesCollection = CollectablesCollection::getInstance();
collectablesCollection->createEmpty(maxDepth);
game->addGameObject(collectablesCollection);
// Spawn collectibles
for (auto const &collectableInfo: levelConfig.collectables)
{
spawnCollectable(collectableInfo);
}
// Add physics simulations
game->addGameObject(MapSimulation::getInstance());
// For every depth, add a hole depth simulation
for (int depth = 0; depth < maxDepth; depth++)
{
auto depthCollection = collectablesCollection->getDepthCollection(depth);
game->addGameObject(std::make_shared<HoleDepthSimulation>(depthCollection));
}
LOG(INFO) << "Finished loading level '" << levelConfig.name << "'.";
}
void LevelLoader::loadLevel(const std::string &levelName)
{
// Does level exist?
if (!all_levels.contains(levelName))
{
LOG(ERROR) << "Level '" << levelName << "' not found. Could not load it.";
throw std::invalid_argument("Could not load level.");
}
LevelLoader::loadLevel(all_levels.at(levelName));
}
void LevelLoader::spawnCollectable(const CollectableInLevel &collectableInfo)
{
LOG(INFO) << "Spawning collectable '" << collectableInfo.name << "' ...";
auto collectable = CollectableFactory::createFromInLevelConfig(collectableInfo);
LOG(DEBUG) << "Has depth " << collectable->getDepth() << ".";
CollectablesCollection::getInstance()->add(collectable);
}