#include "level_loader.hpp" #include "../game.h" #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" #include "level_renderer.hpp" #include "../../sprites/skymap/skymap.hpp" #include "../camera/multiplayer_view.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 views game->addGameObject(MultiplayerView::getInstance()); // Add rendered level objects std::shared_ptr levelRenderer = std::make_shared(); game->addGameObject(levelRenderer); levelRenderer->addChild(std::make_shared(levelConfig.skyColors, levelConfig.worldMapSize)); levelRenderer->addChild(SpriteFactory::createTileMap(levelConfig.tileMapConfig)); if (DB_WORLD_GRID_RENDER) { levelRenderer->addChild(std::make_shared(0, 50, 0, 50)); } levelRenderer->addChild(PlayerCollection::getInstance()); PlayerCollection::getInstance()->setSpawnPoints(levelConfig.playerSpawnPoints); // Prepare collectables framework auto maxDepth = (int) levelConfig.worldMapSize.x * 2; auto collectablesCollection = CollectablesCollection::getInstance(); collectablesCollection->createEmpty(maxDepth); levelRenderer->addChild(collectablesCollection); // Spawn collectibles for (auto const &collectableInfo: levelConfig.collectables) { spawnCollectable(collectableInfo); } // Add physics simulations game->addGameObject(MapSimulation::getInstance()); 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); }