holesome/src/game/level/level_loader.cpp

61 lines
2 KiB
C++

#include "level_loader.hpp"
#include "../game.h"
#include "../physics/map/map_simulation.hpp"
#include "../../debug/grid_debug_layer.h"
#include "../player/player_spawner.hpp"
#include "../../levels.hpp"
#include "../collectables/collection/collectables_collection.hpp"
#include "../collectables/collectable_factory.hpp"
void LevelLoader::loadLevel(LevelConfig levelConfig)
{
auto game = Game::getInstance();
game->clearGameObjects();
LOG(INFO) << "Loading level '" << levelConfig.name << "' ...";
game->setLevel(levelConfig);
MapSimulation::getInstance()->resetMap(levelConfig.worldMapSize);
// Add basic game objects
if (DEVELOPER_MODE)
{
game->addGameObject(new GridDebugLayer(0, 50, 0, 50));
}
game->addGameObject(new TrackingView());
game->addGameObject(new PlayerSpawner(levelConfig.playerSpawnPoints));
// Prepare collectables framework
auto maxDepth = levelConfig.worldMapSize.x * 2;
CollectablesCollection::getInstance()->createEmpty(maxDepth);
game->addGameObject(CollectablesCollection::getInstance().get());
// Spawn collectibles
for (auto const &collectableInfo: levelConfig.collectables)
{
spawnCollectable(collectableInfo);
}
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);
}