110 lines
3.8 KiB
C++
110 lines
3.8 KiB
C++
#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 "level_renderer.hpp"
|
|
#include "../../sprites/skymap/skymap.hpp"
|
|
#include "../camera/multiplayer_view.hpp"
|
|
#include "../physics/holes/holes_simulation.hpp"
|
|
#include "../physics/holes/layouts/hole_layout.hpp"
|
|
#include "../layer/global_layer.hpp"
|
|
|
|
void LevelLoader::loadLevel(const LevelConfig &levelConfig)
|
|
{
|
|
auto game = Game::getInstance();
|
|
game->clearGameObjects();
|
|
|
|
LOG(INFO) << "Loading level '" << levelConfig.name << "' ...";
|
|
|
|
game->setLevel(levelConfig);
|
|
InputMapper::getInstance()->allowNewInputIdentities = false;
|
|
|
|
MapSimulation::getInstance()->resetMap(levelConfig.worldMapSize);
|
|
HolesSimulation::getInstance()->clear();
|
|
GlobalLayer::getInstance()->clear();
|
|
HoleLayout::getInstance()->clear();
|
|
MultiplayerView::getInstance()->clear();
|
|
|
|
|
|
// Add views
|
|
game->addGameObject(MultiplayerView::getInstance());
|
|
|
|
// Add rendered level objects
|
|
std::shared_ptr<LevelRenderer> levelRenderer = std::make_shared<LevelRenderer>();
|
|
game->addGameObject(levelRenderer);
|
|
|
|
levelRenderer->addChild(std::make_shared<Skymap>(levelConfig.skyColors, levelConfig.worldMapSize));
|
|
levelRenderer->addChild(SpriteFactory::createTileMap(levelConfig.tileMapConfig));
|
|
|
|
if (DB_WORLD_GRID_RENDER)
|
|
{
|
|
levelRenderer->addChild(std::make_shared<GridDebugLayer>(0, 50, 0, 50));
|
|
}
|
|
|
|
levelRenderer->addChild(PlayerCollection::getInstance());
|
|
PlayerCollection::getInstance()->setSpawnPoints(levelConfig.playerSpawnPoints);
|
|
PlayerCollection::getInstance()->resetPlayers();
|
|
PlayerCollection::getInstance()->activatePlayers();
|
|
|
|
// Prepare collectables framework
|
|
auto maxDepth = (int) levelConfig.worldMapSize.x * 2;
|
|
auto collectablesCollection = CollectablesCollection::getInstance();
|
|
collectablesCollection->createEmpty(maxDepth);
|
|
levelRenderer->addChild(collectablesCollection);
|
|
|
|
// 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);
|
|
}
|
|
|
|
game->startCountdown(levelConfig.durationInSeconds);
|
|
|
|
// Must be last
|
|
game->addGameObject(GlobalLayer::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(INFO) << "Has depth " << collectable->getDepth() << ".";
|
|
CollectablesCollection::getInstance()->add(collectable);
|
|
HolesSimulation::getInstance()->addCollectable(collectable);
|
|
}
|
|
|
|
void LevelLoader::cleanUp()
|
|
{
|
|
auto game = Game::getInstance();
|
|
game->clearGameObjects();
|
|
game->setLevel(LevelConfig());
|
|
HolesSimulation::getInstance()->clear();
|
|
PlayerCollection::getInstance()->resetPlayers();
|
|
GlobalLayer::getInstance()->clear();
|
|
HoleLayout::getInstance()->clear();
|
|
|
|
LOG(INFO) << "Cleaned up level.";
|
|
}
|