holesome/src/game/level/level_loader.cpp

87 lines
3.1 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"
2023-06-18 17:20:35 +02:00
#include "level_renderer.hpp"
#include "../../sprites/skymap/skymap.hpp"
2023-06-21 16:05:11 +02:00
#include "../camera/multiplayer_view.hpp"
2023-06-28 16:57:13 +02:00
#include "../physics/holes/holes_simulation.hpp"
#include "../physics/holes/layouts/hole_layout.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);
2023-06-28 16:57:13 +02:00
HolesSimulation::getInstance()->clear();
PlayerCollection::getInstance()->clear();
2023-06-28 16:57:13 +02:00
HoleLayout::getInstance()->clear();
2023-06-21 20:42:42 +02:00
// Add views
game->addGameObject(MultiplayerView::getInstance());
2023-06-18 17:20:35 +02:00
// Add rendered level objects
std::shared_ptr<LevelRenderer> levelRenderer = std::make_shared<LevelRenderer>();
game->addGameObject(levelRenderer);
2023-06-20 21:54:59 +02:00
levelRenderer->addChild(std::make_shared<Skymap>(levelConfig.skyColors, levelConfig.worldMapSize));
2023-06-18 17:20:35 +02:00
levelRenderer->addChild(SpriteFactory::createTileMap(levelConfig.tileMapConfig));
2023-06-17 19:48:51 +02:00
2023-06-17 21:25:33 +02:00
if (DB_WORLD_GRID_RENDER)
{
2023-06-18 17:20:35 +02:00
levelRenderer->addChild(std::make_shared<GridDebugLayer>(0, 50, 0, 50));
}
2023-06-18 17:20:35 +02:00
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);
2023-06-18 17:20:35 +02:00
levelRenderer->addChild(collectablesCollection);
2023-06-28 16:57:13 +02:00
// Add physics simulations
game->addGameObject(MapSimulation::getInstance());
game->addGameObject(HolesSimulation::getInstance());
game->addGameObject(HoleLayout::getInstance());
// Spawn collectibles
for (auto const &collectableInfo: levelConfig.collectables)
{
spawnCollectable(collectableInfo);
}
2023-06-18 17:20:35 +02:00
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(INFO) << "Has depth " << collectable->getDepth() << ".";
CollectablesCollection::getInstance()->add(collectable);
2023-06-28 16:57:13 +02:00
HolesSimulation::getInstance()->addCollectable(collectable);
}