2023-06-10 14:48:16 +02:00
|
|
|
#include "level_loader.hpp"
|
|
|
|
#include "../game.h"
|
2023-06-11 15:54:05 +02:00
|
|
|
#include "../physics/map/map_simulation.hpp"
|
2023-06-10 14:48:16 +02:00
|
|
|
#include "../../debug/grid_debug_layer.h"
|
|
|
|
#include "../../levels.hpp"
|
2023-06-12 21:02:04 +02:00
|
|
|
#include "../collectables/collection/collectables_collection.hpp"
|
|
|
|
#include "../collectables/collectable_factory.hpp"
|
2023-06-13 21:59:50 +02:00
|
|
|
#include "../player/player_collection.hpp"
|
|
|
|
#include "../physics/hole/hole_depth_simulation.hpp"
|
2023-06-10 14:48:16 +02:00
|
|
|
|
2023-06-13 21:59:50 +02:00
|
|
|
void LevelLoader::loadLevel(const LevelConfig &levelConfig)
|
2023-06-10 14:48:16 +02:00
|
|
|
{
|
2023-06-12 21:02:04 +02:00
|
|
|
auto game = Game::getInstance();
|
|
|
|
game->clearGameObjects();
|
2023-06-10 14:48:16 +02:00
|
|
|
|
|
|
|
LOG(INFO) << "Loading level '" << levelConfig.name << "' ...";
|
|
|
|
|
2023-06-12 21:02:04 +02:00
|
|
|
game->setLevel(levelConfig);
|
2023-06-10 14:48:16 +02:00
|
|
|
MapSimulation::getInstance()->resetMap(levelConfig.worldMapSize);
|
2023-06-13 21:59:50 +02:00
|
|
|
PlayerCollection::getInstance()->clear();
|
2023-06-10 14:48:16 +02:00
|
|
|
|
|
|
|
// Add basic game objects
|
2023-06-17 19:48:51 +02:00
|
|
|
game->addGameObject(SpriteFactory::createTileMap(levelConfig.tileMapConfig));
|
|
|
|
|
2023-06-10 14:48:16 +02:00
|
|
|
if (DEVELOPER_MODE)
|
|
|
|
{
|
2023-06-13 19:04:38 +02:00
|
|
|
game->addGameObject(std::make_shared<GridDebugLayer>(0, 50, 0, 50));
|
2023-06-10 14:48:16 +02:00
|
|
|
}
|
|
|
|
|
2023-06-13 19:04:38 +02:00
|
|
|
game->addGameObject(std::make_shared<TrackingView>());
|
2023-06-13 21:59:50 +02:00
|
|
|
game->addGameObject(PlayerCollection::getInstance());
|
|
|
|
PlayerCollection::getInstance()->setSpawnPoints(levelConfig.playerSpawnPoints);
|
2023-06-12 21:02:04 +02:00
|
|
|
|
|
|
|
// Prepare collectables framework
|
2023-06-13 21:59:50 +02:00
|
|
|
auto maxDepth = (int) levelConfig.worldMapSize.x * 2;
|
|
|
|
auto collectablesCollection = CollectablesCollection::getInstance();
|
|
|
|
collectablesCollection->createEmpty(maxDepth);
|
|
|
|
game->addGameObject(collectablesCollection);
|
2023-06-12 21:02:04 +02:00
|
|
|
|
|
|
|
// Spawn collectibles
|
|
|
|
for (auto const &collectableInfo: levelConfig.collectables)
|
|
|
|
{
|
|
|
|
spawnCollectable(collectableInfo);
|
|
|
|
}
|
2023-06-10 14:48:16 +02:00
|
|
|
|
2023-06-13 21:59:50 +02:00
|
|
|
// Add physics simulations
|
|
|
|
game->addGameObject(MapSimulation::getInstance());
|
|
|
|
|
2023-06-10 14:48:16 +02:00
|
|
|
LOG(INFO) << "Finished loading level '" << levelConfig.name << "'.";
|
|
|
|
}
|
|
|
|
|
|
|
|
void LevelLoader::loadLevel(const std::string &levelName)
|
|
|
|
{
|
|
|
|
// Does level exist?
|
2023-06-12 21:02:04 +02:00
|
|
|
if (!all_levels.contains(levelName))
|
2023-06-10 14:48:16 +02:00
|
|
|
{
|
|
|
|
LOG(ERROR) << "Level '" << levelName << "' not found. Could not load it.";
|
|
|
|
throw std::invalid_argument("Could not load level.");
|
|
|
|
}
|
|
|
|
|
2023-06-12 21:02:04 +02:00
|
|
|
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);
|
2023-06-10 14:48:16 +02:00
|
|
|
}
|