inventory regions, minor refactor

This commit is contained in:
MihailRis
2024-01-31 00:14:43 +03:00
parent a57fba4fa4
commit 49fdcdfb27
29 changed files with 259 additions and 160 deletions
+3 -1
View File
@@ -9,6 +9,7 @@
#include "../physics/Hitbox.h"
#include "../physics/PhysicsSolver.h"
#include "../objects/Player.h"
#include "../items/Inventory.h"
Level::Level(World* world, const Content* content, Player* player, EngineSettings& settings)
: world(world),
@@ -16,7 +17,8 @@ Level::Level(World* world, const Content* content, Player* player, EngineSetting
player(player),
chunksStorage(new ChunksStorage(this)),
events(new LevelEvents()) ,
settings(settings) {
settings(settings)
{
physics = new PhysicsSolver(glm::vec3(0, -22.6f, 0));
uint matrixSize = (settings.chunks.loadDistance+
+1
View File
@@ -10,6 +10,7 @@ class Content;
class World;
class Player;
class Chunks;
class Inventory;
class LevelEvents;
class Lighting;
class PhysicsSolver;
+55 -13
View File
@@ -1,6 +1,7 @@
#include "World.h"
#include <memory>
#include <iostream>
#include <glm/glm.hpp>
#include "Level.h"
@@ -71,20 +72,11 @@ Level* World::create(std::string name,
EngineSettings& settings,
const Content* content,
const std::vector<ContentPack>& packs) {
World* world = new World(name, directory, seed, settings, content, packs);
Player* player = new Player(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED);
auto world = new World(name, directory, seed, settings, content, packs);
auto player = new Player(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED);
return new Level(world, content, player, settings);
}
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);
}
return nullptr;
}
Level* World::load(fs::path directory,
EngineSettings& settings,
const Content* content,
@@ -98,14 +90,23 @@ Level* World::load(fs::path directory,
throw world_load_error("could not to find world.json");
}
Player* player = new Player(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED);
Level* level = new Level(world.get(), content, player, settings);
auto player = new Player(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED);
auto level = new Level(world.get(), content, player, settings);
wfile->readPlayer(player);
world.release();
return level;
}
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);
}
return nullptr;
}
void World::setName(const std::string& name) {
this->name = name;
}
@@ -133,3 +134,44 @@ uint64_t World::getSeed() const {
const std::vector<ContentPack>& World::getPacks() const {
return packs;
}
void World::deserialize(dynamic::Map* root) {
name = root->getStr("name", name);
seed = root->getInt("seed", seed);
auto verobj = root->map("version");
if (verobj) {
int major=0, minor=-1;
verobj->num("major", major);
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->getNum("next-inventory-id", 2);
}
std::unique_ptr<dynamic::Map> World::serialize() const {
auto root = std::make_unique<dynamic::Map>();
auto& versionobj = root->putMap("version");
versionobj.put("major", ENGINE_VERSION_MAJOR);
versionobj.put("minor", ENGINE_VERSION_MINOR);
root->put("name", name);
root->put("seed", seed);
auto& timeobj = root->putMap("time");
timeobj.put("day-time", daytime);
timeobj.put("day-time-speed", daytimeSpeed);
timeobj.put("total-time", totalTime);
root->put("next-inventory-id", nextInventoryId);
return root;
}
+12 -2
View File
@@ -8,7 +8,8 @@
#include "../typedefs.h"
#include "../settings.h"
#include "../util/timeutil.h"
#include "../data/dynamic.h"
#include "../interfaces/Serializable.h"
#include "../content/ContentPack.h"
class Content;
@@ -25,12 +26,14 @@ public:
world_load_error(std::string message);
};
class World {
class World : Serializable {
std::string name;
uint64_t seed;
EngineSettings& settings;
const Content* const content;
std::vector<ContentPack> packs;
uint nextInventoryId = 1;
public:
WorldFiles* wfile;
@@ -74,6 +77,13 @@ public:
std::string getName() const;
uint64_t getSeed() const;
const std::vector<ContentPack>& getPacks() const;
std::unique_ptr<dynamic::Map> serialize() const override;
void deserialize(dynamic::Map *src) override;
uint getNextInventoryId() {
return nextInventoryId++;
}
};
#endif /* WORLD_WORLD_H_ */