#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(const 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(std::make_shared(0, 50, 0, 50)); } game->addGameObject(std::make_shared()); game->addGameObject(std::make_shared(levelConfig.playerSpawnPoints)); // Prepare collectables framework auto maxDepth = levelConfig.worldMapSize.x * 2; CollectablesCollection::getInstance()->createEmpty(maxDepth); game->addGameObject(CollectablesCollection::getInstance()); // 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); }