#include "Level.h" #include "World.h" #include "LevelEvents.h" #include "../content/Content.h" #include "../lighting/Lighting.h" #include "../voxels/Chunk.h" #include "../voxels/Chunks.h" #include "../voxels/ChunksStorage.h" #include "../physics/Hitbox.h" #include "../physics/PhysicsSolver.h" #include "../interfaces/Object.h" #include "../objects/Player.h" #include "../items/Inventory.h" #include "../items/Inventories.h" const float DEF_PLAYER_Y = 100.0f; const float DEF_PLAYER_SPEED = 4.0f; const int DEF_PLAYER_INVENTORY_SIZE = 40; Level::Level(World* world, const Content* content, EngineSettings& settings) : world(world), content(content), chunksStorage(new ChunksStorage(this)), events(new LevelEvents()) , settings(settings) { objCounter = 0; physics = new PhysicsSolver(glm::vec3(0, -22.6f, 0)); auto inv = std::make_shared(world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE); player = spawnObject(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv); uint matrixSize = (settings.chunks.loadDistance+ settings.chunks.padding) * 2; chunks = new Chunks(matrixSize, matrixSize, 0, 0, world->wfile, events, content); lighting = new Lighting(content, chunks); events->listen(EVT_CHUNK_HIDDEN, [this](lvl_event_type type, Chunk* chunk) { this->chunksStorage->remove(chunk->x, chunk->z); }); inventories = std::make_unique(*this); inventories->store(player->getInventory()); } Level::~Level(){ delete chunks; delete events; delete physics; delete lighting; delete chunksStorage; for(auto obj : objects) { obj.reset(); } } void Level::update() { glm::vec3 position = player->hitbox->position; chunks->setCenter(position.x, position.z); int matrixSize = (settings.chunks.loadDistance+ settings.chunks.padding) * 2; if (chunks->w != matrixSize) { chunks->resize(matrixSize, matrixSize); } } World* Level::getWorld() { return world.get(); } template std::shared_ptr Level::spawnObject(Args&&... args) { static_assert(std::is_base_of::value, "T must be a derived of Object class"); std::shared_ptr tObj = std::make_shared(args...); std::shared_ptr obj = std::dynamic_pointer_cast(tObj); objects.push_back(obj); obj->objectUID = objCounter; obj->spawned(); objCounter += 1; return tObj; }