refactor World
This commit is contained in:
+3
-2
@@ -29,6 +29,7 @@ Level::Level(
|
||||
events(std::make_unique<LevelEvents>()),
|
||||
entities(std::make_unique<Entities>(this)),
|
||||
settings(settings) {
|
||||
auto& worldInfo = world->getInfo();
|
||||
auto& cameraIndices = content->getIndices(ResourceType::CAMERA);
|
||||
for (size_t i = 0; i < cameraIndices.size(); i++) {
|
||||
auto camera = std::make_shared<Camera>();
|
||||
@@ -46,8 +47,8 @@ Level::Level(
|
||||
cameras.push_back(std::move(camera));
|
||||
}
|
||||
|
||||
if (world->nextEntityId) {
|
||||
entities->setNextID(world->nextEntityId);
|
||||
if (worldInfo.nextEntityId) {
|
||||
entities->setNextID(worldInfo.nextEntityId);
|
||||
}
|
||||
auto inv = std::make_shared<Inventory>(
|
||||
world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE
|
||||
|
||||
+36
-37
@@ -26,29 +26,22 @@ world_load_error::world_load_error(const std::string& message)
|
||||
}
|
||||
|
||||
World::World(
|
||||
std::string name,
|
||||
std::string generator,
|
||||
const fs::path& directory,
|
||||
uint64_t seed,
|
||||
EngineSettings& settings,
|
||||
WorldInfo info,
|
||||
std::unique_ptr<WorldFiles> worldFiles,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack>& packs
|
||||
)
|
||||
: name(std::move(name)),
|
||||
generator(std::move(generator)),
|
||||
seed(seed),
|
||||
content(content),
|
||||
packs(packs),
|
||||
wfile(std::make_unique<WorldFiles>(directory, settings.debug)) {
|
||||
}
|
||||
) : info(std::move(info)),
|
||||
content(content),
|
||||
packs(packs),
|
||||
wfile(std::move(worldFiles)) {}
|
||||
|
||||
World::~World() {
|
||||
}
|
||||
|
||||
void World::updateTimers(float delta) {
|
||||
daytime += delta * daytimeSpeed * DAYIME_SPECIFIC_SPEED;
|
||||
daytime = fmod(daytime, 1.0f);
|
||||
totalTime += delta;
|
||||
info.daytime += delta * info.daytimeSpeed * DAYIME_SPECIFIC_SPEED;
|
||||
info.daytime = std::fmod(info.daytime, 1.0f);
|
||||
info.totalTime += delta;
|
||||
}
|
||||
|
||||
void World::writeResources(const Content* content) {
|
||||
@@ -71,7 +64,7 @@ void World::writeResources(const Content* content) {
|
||||
void World::write(Level* level) {
|
||||
const Content* content = level->content;
|
||||
level->chunks->saveAll();
|
||||
nextEntityId = level->entities->peekNextID();
|
||||
info.nextEntityId = level->entities->peekNextID();
|
||||
wfile->write(this, content);
|
||||
|
||||
auto playerFile = dynamic::Map();
|
||||
@@ -95,8 +88,15 @@ std::unique_ptr<Level> World::create(
|
||||
const Content* content,
|
||||
const std::vector<ContentPack>& packs
|
||||
) {
|
||||
WorldInfo info {};
|
||||
info.name = name;
|
||||
info.generator = generator;
|
||||
info.seed = seed;
|
||||
auto world = std::make_unique<World>(
|
||||
name, generator, directory, seed, settings, content, packs
|
||||
info,
|
||||
std::make_unique<WorldFiles>(directory, settings.debug),
|
||||
content,
|
||||
packs
|
||||
);
|
||||
return std::make_unique<Level>(std::move(world), content, settings);
|
||||
}
|
||||
@@ -107,20 +107,21 @@ std::unique_ptr<Level> World::load(
|
||||
const Content* content,
|
||||
const std::vector<ContentPack>& packs
|
||||
) {
|
||||
auto worldFilesPtr = std::make_unique<WorldFiles>(directory, settings.debug);
|
||||
auto worldFiles = worldFilesPtr.get();
|
||||
auto info = worldFiles->readWorldInfo();
|
||||
if (!info.has_value()) {
|
||||
throw world_load_error("could not to find world.json");
|
||||
}
|
||||
logger.info() << "world version: " << info->major << "." << info->minor;
|
||||
|
||||
auto world = std::make_unique<World>(
|
||||
".",
|
||||
WorldGenerators::getDefaultGeneratorID(),
|
||||
directory,
|
||||
0,
|
||||
settings,
|
||||
info.value(),
|
||||
std::move(worldFilesPtr),
|
||||
content,
|
||||
packs
|
||||
);
|
||||
auto& wfile = world->wfile;
|
||||
|
||||
if (!wfile->readWorldInfo(world.get())) {
|
||||
throw world_load_error("could not to find world.json");
|
||||
}
|
||||
wfile->readResourcesData(content);
|
||||
|
||||
auto level = std::make_unique<Level>(std::move(world), content, settings);
|
||||
@@ -165,11 +166,11 @@ std::shared_ptr<ContentLUT> World::checkIndices(
|
||||
}
|
||||
|
||||
void World::setName(const std::string& name) {
|
||||
this->name = name;
|
||||
this->info.name = name;
|
||||
}
|
||||
|
||||
void World::setGenerator(const std::string& generator) {
|
||||
this->generator = generator;
|
||||
this->info.generator = generator;
|
||||
}
|
||||
|
||||
bool World::hasPack(const std::string& id) const {
|
||||
@@ -180,26 +181,26 @@ bool World::hasPack(const std::string& id) const {
|
||||
}
|
||||
|
||||
void World::setSeed(uint64_t seed) {
|
||||
this->seed = seed;
|
||||
this->info.seed = seed;
|
||||
}
|
||||
|
||||
std::string World::getName() const {
|
||||
return name;
|
||||
return info.name;
|
||||
}
|
||||
|
||||
uint64_t World::getSeed() const {
|
||||
return seed;
|
||||
return info.seed;
|
||||
}
|
||||
|
||||
std::string World::getGenerator() const {
|
||||
return generator;
|
||||
return info.generator;
|
||||
}
|
||||
|
||||
const std::vector<ContentPack>& World::getPacks() const {
|
||||
return packs;
|
||||
}
|
||||
|
||||
void World::deserialize(dynamic::Map* root) {
|
||||
void WorldInfo::deserialize(dynamic::Map* root) {
|
||||
name = root->get("name", name);
|
||||
generator = root->get("generator", generator);
|
||||
seed = root->get("seed", seed);
|
||||
@@ -208,10 +209,8 @@ void World::deserialize(dynamic::Map* root) {
|
||||
generator = WorldGenerators::getDefaultGeneratorID();
|
||||
}
|
||||
if (auto verobj = root->map("version")) {
|
||||
int major = 0, minor = -1;
|
||||
verobj->num("major", major);
|
||||
verobj->num("minor", minor);
|
||||
logger.info() << "world version: " << major << "." << minor;
|
||||
}
|
||||
if (auto timeobj = root->map("time")) {
|
||||
timeobj->num("day-time", daytime);
|
||||
@@ -225,7 +224,7 @@ void World::deserialize(dynamic::Map* root) {
|
||||
nextEntityId = root->get("next-entity-id", 1);
|
||||
}
|
||||
|
||||
std::unique_ptr<dynamic::Map> World::serialize() const {
|
||||
std::unique_ptr<dynamic::Map> WorldInfo::serialize() const {
|
||||
auto root = std::make_unique<dynamic::Map>();
|
||||
|
||||
auto& versionobj = root->putMap("version");
|
||||
|
||||
+30
-17
@@ -24,20 +24,12 @@ public:
|
||||
world_load_error(const std::string& message);
|
||||
};
|
||||
|
||||
/// @brief holds all world data except the level (chunks and objects)
|
||||
class World : Serializable {
|
||||
struct WorldInfo : public Serializable {
|
||||
std::string name;
|
||||
std::string generator;
|
||||
uint64_t seed;
|
||||
const Content* const content;
|
||||
std::vector<ContentPack> packs;
|
||||
|
||||
int64_t nextInventoryId = 0;
|
||||
|
||||
void writeResources(const Content* content);
|
||||
public:
|
||||
std::unique_ptr<WorldFiles> wfile;
|
||||
|
||||
/// @brief Day/night loop timer in range 0..1 where
|
||||
/// 0.0 - is midnight and
|
||||
/// 0.5 - is noon
|
||||
@@ -54,12 +46,28 @@ public:
|
||||
|
||||
entityid_t nextEntityId = 0;
|
||||
|
||||
int major = 0, minor = -1;
|
||||
|
||||
std::unique_ptr<dynamic::Map> serialize() const override;
|
||||
void deserialize(dynamic::Map* src) override;
|
||||
};
|
||||
|
||||
/// @brief holds all world data except the level (chunks and objects)
|
||||
class World {
|
||||
WorldInfo info {};
|
||||
|
||||
const Content* const content;
|
||||
std::vector<ContentPack> packs;
|
||||
|
||||
int64_t nextInventoryId = 0;
|
||||
|
||||
void writeResources(const Content* content);
|
||||
public:
|
||||
std::unique_ptr<WorldFiles> wfile;
|
||||
|
||||
World(
|
||||
std::string name,
|
||||
std::string generator,
|
||||
const fs::path& directory,
|
||||
uint64_t seed,
|
||||
EngineSettings& settings,
|
||||
WorldInfo info,
|
||||
std::unique_ptr<WorldFiles> wfile,
|
||||
const Content* content,
|
||||
const std::vector<ContentPack>& packs
|
||||
);
|
||||
@@ -134,6 +142,14 @@ public:
|
||||
/// @brief Get world generator id
|
||||
std::string getGenerator() const;
|
||||
|
||||
WorldInfo& getInfo() {
|
||||
return info;
|
||||
}
|
||||
|
||||
const WorldInfo& getInfo() const {
|
||||
return info;
|
||||
}
|
||||
|
||||
/// @brief Get vector of all content-packs installed in world
|
||||
const std::vector<ContentPack>& getPacks() const;
|
||||
|
||||
@@ -147,7 +163,4 @@ public:
|
||||
const Content* getContent() const {
|
||||
return content;
|
||||
}
|
||||
|
||||
std::unique_ptr<dynamic::Map> serialize() const override;
|
||||
void deserialize(dynamic::Map* src) override;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user