memory related refactor
This commit is contained in:
+7
-4
@@ -12,8 +12,11 @@
|
||||
#include "../items/Inventory.hpp"
|
||||
#include "../items/Inventories.hpp"
|
||||
|
||||
Level::Level(World* world, const Content* content, EngineSettings& settings)
|
||||
: world(world),
|
||||
Level::Level(
|
||||
std::unique_ptr<World> world,
|
||||
const Content* content,
|
||||
EngineSettings& settings
|
||||
) : world(std::move(world)),
|
||||
content(content),
|
||||
chunksStorage(std::make_unique<ChunksStorage>(this)),
|
||||
physics(std::make_unique<PhysicsSolver>(glm::vec3(0, -22.6f, 0))),
|
||||
@@ -21,7 +24,7 @@ Level::Level(World* world, const Content* content, EngineSettings& settings)
|
||||
settings(settings)
|
||||
{
|
||||
auto inv = std::make_shared<Inventory>(
|
||||
world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE
|
||||
this->world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE
|
||||
);
|
||||
auto player = spawnObject<Player>(
|
||||
glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv
|
||||
@@ -32,7 +35,7 @@ Level::Level(World* world, const Content* content, EngineSettings& settings)
|
||||
settings.chunks.padding.get()
|
||||
) * 2;
|
||||
chunks = std::make_unique<Chunks>(
|
||||
matrixSize, matrixSize, 0, 0, world->wfile.get(), events.get(), content
|
||||
matrixSize, matrixSize, 0, 0, this->world->wfile.get(), events.get(), content
|
||||
);
|
||||
lighting = std::make_unique<Lighting>(content, chunks.get());
|
||||
|
||||
|
||||
+5
-1
@@ -38,7 +38,11 @@ public:
|
||||
|
||||
const EngineSettings& settings;
|
||||
|
||||
Level(World* world, const Content* content, EngineSettings& settings);
|
||||
Level(
|
||||
std::unique_ptr<World> world,
|
||||
const Content* content,
|
||||
EngineSettings& settings
|
||||
);
|
||||
~Level();
|
||||
|
||||
void loadMatrix(int32_t x, int32_t z, uint32_t radius);
|
||||
|
||||
@@ -11,7 +11,7 @@ enum lvl_event_type {
|
||||
EVT_CHUNK_HIDDEN,
|
||||
};
|
||||
|
||||
typedef std::function<void(lvl_event_type, Chunk*)> chunk_event_func;
|
||||
using chunk_event_func = std::function<void(lvl_event_type, Chunk*)>;
|
||||
|
||||
class LevelEvents {
|
||||
std::unordered_map<lvl_event_type, std::vector<chunk_event_func>> chunk_callbacks;
|
||||
|
||||
+41
-32
@@ -1,8 +1,10 @@
|
||||
#include "World.hpp"
|
||||
|
||||
#include "Level.hpp"
|
||||
|
||||
#include "../content/Content.hpp"
|
||||
#include "../content/ContentLUT.hpp"
|
||||
#include "../debug/Logger.hpp"
|
||||
#include "../files/WorldFiles.hpp"
|
||||
#include "../items/Inventories.hpp"
|
||||
#include "../objects/Player.hpp"
|
||||
@@ -13,9 +15,10 @@
|
||||
#include "../world/WorldGenerators.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
static debug::Logger logger("world");
|
||||
|
||||
world_load_error::world_load_error(std::string message)
|
||||
: std::runtime_error(message) {
|
||||
}
|
||||
@@ -64,35 +67,38 @@ void World::write(Level* level) {
|
||||
}
|
||||
|
||||
wfile->write(this, content);
|
||||
auto playerFile = dynamic::Map();
|
||||
{
|
||||
auto& players = playerFile.putList("players");
|
||||
for (auto object : level->objects) {
|
||||
if (std::shared_ptr<Player> player = std::dynamic_pointer_cast<Player>(object)) {
|
||||
players.put(player->serialize());
|
||||
}
|
||||
auto playerFile = dynamic::Map();
|
||||
|
||||
auto& players = playerFile.putList("players");
|
||||
for (auto object : level->objects) {
|
||||
if (auto player = std::dynamic_pointer_cast<Player>(object)) {
|
||||
players.put(player->serialize());
|
||||
}
|
||||
}
|
||||
files::write_json(wfile->getPlayerFile(), &playerFile);
|
||||
}
|
||||
|
||||
Level* World::create(std::string name,
|
||||
std::string generator,
|
||||
fs::path directory,
|
||||
uint64_t seed,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack>& packs
|
||||
std::unique_ptr<Level> World::create(
|
||||
std::string name,
|
||||
std::string generator,
|
||||
fs::path directory,
|
||||
uint64_t seed,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack>& packs
|
||||
) {
|
||||
auto world = new World(name, generator, directory, seed, settings, content, packs);
|
||||
auto level = new Level(world, content, settings);
|
||||
return level;
|
||||
auto world = std::make_unique<World>(
|
||||
name, generator, directory, seed, settings, content, packs
|
||||
);
|
||||
return std::make_unique<Level>(std::move(world), content, settings);
|
||||
}
|
||||
|
||||
Level* World::load(fs::path directory,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack>& packs) {
|
||||
std::unique_ptr<Level> World::load(
|
||||
fs::path directory,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack>& packs
|
||||
) {
|
||||
auto world = std::make_unique<World>(
|
||||
".", WorldGenerators::getDefaultGeneratorID(), directory, 0, settings, content, packs
|
||||
);
|
||||
@@ -102,23 +108,27 @@ Level* World::load(fs::path directory,
|
||||
throw world_load_error("could not to find world.json");
|
||||
}
|
||||
|
||||
auto level = new Level(world.get(), content, settings);
|
||||
auto level = std::make_unique<Level>(std::move(world), content, settings);
|
||||
{
|
||||
fs::path file = wfile->getPlayerFile();
|
||||
if (!fs::is_regular_file(file)) {
|
||||
std::cerr << "warning: player.json does not exists" << std::endl;
|
||||
logger.warning() << "player.json does not exists";
|
||||
} else {
|
||||
auto playerFile = files::read_json(file);
|
||||
if (playerFile->has("players")) {
|
||||
level->objects.clear();
|
||||
auto players = playerFile->list("players");
|
||||
for (size_t i = 0; i < players->size(); i++) {
|
||||
auto player = level->spawnObject<Player>(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, level->inventories->create(DEF_PLAYER_INVENTORY_SIZE));
|
||||
auto player = level->spawnObject<Player>(
|
||||
glm::vec3(0, DEF_PLAYER_Y, 0),
|
||||
DEF_PLAYER_SPEED,
|
||||
level->inventories->create(DEF_PLAYER_INVENTORY_SIZE)
|
||||
);
|
||||
player->deserialize(players->map(i));
|
||||
level->inventories->store(player->getInventory());
|
||||
}
|
||||
} else {
|
||||
auto player = level->getObject<Player>(0);
|
||||
auto player = level->getObject<Player>(0);
|
||||
player->deserialize(playerFile.get());
|
||||
level->inventories->store(player->getInventory());
|
||||
}
|
||||
@@ -128,8 +138,10 @@ Level* World::load(fs::path directory,
|
||||
return level;
|
||||
}
|
||||
|
||||
ContentLUT* World::checkIndices(const fs::path& directory,
|
||||
const Content* content) {
|
||||
std::shared_ptr<ContentLUT> World::checkIndices(
|
||||
const fs::path& directory,
|
||||
const Content* content
|
||||
) {
|
||||
fs::path indicesFile = directory/fs::path("indices.json");
|
||||
if (fs::is_regular_file(indicesFile)) {
|
||||
return ContentLUT::create(indicesFile, content);
|
||||
@@ -178,10 +190,9 @@ void World::deserialize(dynamic::Map* root) {
|
||||
generator = root->get("generator", generator);
|
||||
seed = root->get("seed", seed);
|
||||
|
||||
if(generator == "") {
|
||||
if (generator == "") {
|
||||
generator = WorldGenerators::getDefaultGeneratorID();
|
||||
}
|
||||
|
||||
auto verobj = root->map("version");
|
||||
if (verobj) {
|
||||
int major=0, minor=-1;
|
||||
@@ -189,14 +200,12 @@ void World::deserialize(dynamic::Map* root) {
|
||||
verobj->num("minor", minor);
|
||||
std::cout << "world version: " << major << "." << minor << std::endl;
|
||||
}
|
||||
|
||||
auto timeobj = root->map("time");
|
||||
if (timeobj) {
|
||||
timeobj->num("day-time", daytime);
|
||||
timeobj->num("day-time-speed", daytimeSpeed);
|
||||
timeobj->num("total-time", totalTime);
|
||||
}
|
||||
|
||||
nextInventoryId = root->get("next-inventory-id", 2);
|
||||
}
|
||||
|
||||
|
||||
+6
-7
@@ -12,13 +12,10 @@
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <filesystem>
|
||||
#include <stdexcept>
|
||||
|
||||
class Content;
|
||||
class WorldFiles;
|
||||
class Chunks;
|
||||
class Level;
|
||||
class Player;
|
||||
class ContentLUT;
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
@@ -75,7 +72,9 @@ public:
|
||||
/// @param directory world directory
|
||||
/// @param content current Content instance
|
||||
/// @return ContentLUT if world convert required else nullptr
|
||||
static ContentLUT* checkIndices(const fs::path& directory, const Content* content);
|
||||
static std::shared_ptr<ContentLUT> checkIndices(
|
||||
const fs::path& directory, const Content* content
|
||||
);
|
||||
|
||||
/// @brief Create new world
|
||||
/// @param name internal world name
|
||||
@@ -87,7 +86,7 @@ public:
|
||||
/// with all world content-packs applied
|
||||
/// @param packs vector of all world content-packs
|
||||
/// @return Level instance containing World instance
|
||||
static Level* create(
|
||||
static std::unique_ptr<Level> create(
|
||||
std::string name,
|
||||
std::string generator,
|
||||
fs::path directory,
|
||||
@@ -105,7 +104,7 @@ public:
|
||||
/// @param packs vector of all world content-packs
|
||||
/// @return Level instance containing World instance
|
||||
/// @throws world_load_error on world.json load error
|
||||
static Level* load(
|
||||
static std::unique_ptr<Level> load(
|
||||
fs::path directory,
|
||||
EngineSettings& settings,
|
||||
const Content* content,
|
||||
@@ -148,4 +147,4 @@ public:
|
||||
void deserialize(dynamic::Map *src) override;
|
||||
};
|
||||
|
||||
#endif /* WORLD_WORLD_HPP_ */
|
||||
#endif // WORLD_WORLD_HPP_
|
||||
|
||||
Reference in New Issue
Block a user