Merge remote-tracking branch 'upstream/main'

This commit is contained in:
DanielProl1xy
2024-02-19 10:23:29 +03:00
154 changed files with 6818 additions and 2760 deletions
+9 -2
View File
@@ -10,20 +10,25 @@
#include "../physics/PhysicsSolver.h"
#include "../objects/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) {
settings(settings)
{
physics = new PhysicsSolver(glm::vec3(0, -22.6f, 0));
player = spawnObjectOfClass<Player>(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED);
auto inv = std::make_shared<Inventory>(0, DEF_PLAYER_INVENTORY_SIZE);
player = spawnObjectOfClass<Player>(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv);
uint matrixSize = (settings.chunks.loadDistance+
@@ -35,6 +40,8 @@ Level::Level(World* world, const Content* content, EngineSettings& settings)
events->listen(EVT_CHUNK_HIDDEN, [this](lvl_event_type type, Chunk* chunk) {
this->chunksStorage->remove(chunk->x, chunk->z);
});
inventories = std::make_unique<Inventories>(*this);
}
Level::~Level(){
+3
View File
@@ -12,6 +12,8 @@ class World;
class Player;
class Object;
class Chunks;
class Inventory;
class Inventories;
class LevelEvents;
class Lighting;
class PhysicsSolver;
@@ -25,6 +27,7 @@ public:
Player* player;
Chunks* chunks;
ChunksStorage* chunksStorage;
std::unique_ptr<Inventories> inventories;
PhysicsSolver* physics;
Lighting* lighting;
+58 -11
View File
@@ -1,6 +1,7 @@
#include "World.h"
#include <memory>
#include <iostream>
#include <glm/glm.hpp>
#include "Level.h"
@@ -12,6 +13,7 @@
#include "../voxels/ChunksStorage.h"
#include "../objects/Player.h"
#include "../window/Camera.h"
#include "../items/Inventories.h"
world_load_error::world_load_error(std::string message)
: std::runtime_error(message) {
@@ -62,23 +64,18 @@ void World::write(Level* level) {
wfile->writePlayer(level->player);
}
Level* World::create(std::string name,
fs::path directory,
uint64_t seed,
EngineSettings& settings,
const Content* content,
const std::vector<ContentPack>& packs) {
World* world = new World(name, directory, seed, settings, content, packs);
return new Level(world, content, 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;
auto world = new World(name, directory, seed, settings, content, packs);
auto level = new Level(world, content, settings);
level->inventories->store(level->player->getInventory());
return level;
}
Level* World::load(fs::path directory,
@@ -96,11 +93,20 @@ Level* World::load(fs::path directory,
Level* level = new Level(world.get(), content, settings);
wfile->readPlayer(level->player);
level->inventories->store(level->player->getInventory());
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;
}
@@ -128,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;
}
+80 -8
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,19 +26,22 @@ 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;
int64_t nextInventoryId = 1;
public:
WorldFiles* wfile;
/* Day/night loop timer in range 0..1
0.0 - is midnight
0.5 - is noon
*/
/**
* Day/night loop timer in range 0..1
* 0.0 - is midnight
* 0.5 - is noon
*/
float daytime = timeutil::time_value(10, 00, 00);
float daytimeSpeed = 1.0f/60.0f/24.0f;
double totalTime = 0.0;
@@ -50,18 +54,53 @@ public:
std::vector<ContentPack> packs);
~World();
/**
* Update world day-time and total time
* @param delta delta-time
*/
void updateTimers(float delta);
/**
* Write all unsaved level data to the world directory
*/
void write(Level* level);
static ContentLUT* checkIndices(const fs::path& directory,
const Content* content);
/**
* Check world indices and generate ContentLUT if convert required
* @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);
/**
* Create new world
* @param name internal world name
* @param directory root world directory
* @param seed world generation seed
* @param settings current engine settings
* @param content current engine Content instance
* with all world content-packs applied
* @param packs vector of all world content-packs
* @return Level instance containing World instance
*/
static Level* create(std::string name,
fs::path directory,
uint64_t seed,
EngineSettings& settings,
const Content* content,
const std::vector<ContentPack>& packs);
/**
* Load an existing world
* @param directory root world directory
* @param settings current engine settings
* @param content current engine Content instance
* with all world content-packs applied
* @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(fs::path directory,
EngineSettings& settings,
const Content* content,
@@ -70,10 +109,43 @@ public:
void setName(const std::string& name);
void setSeed(uint64_t seed);
/**
* Check if world has content-pack installed
* @param id content-pack id
*/
bool hasPack(const std::string& id) const;
/**
* Get internal world name (not the folder name)
* @return name stored in world.json
*/
std::string getName() const;
/** Get world generation seed */
uint64_t getSeed() const;
/**
* Get vector of all content-packs installed in world
*/
const std::vector<ContentPack>& getPacks() const;
/**
* Get next inventory id and increment it's counter
* @return integer >= 1
*/
int64_t getNextInventoryId() {
return nextInventoryId++;
}
/**
* Get current world Content instance
*/
const Content* getContent() const {
return content;
}
std::unique_ptr<dynamic::Map> serialize() const override;
void deserialize(dynamic::Map *src) override;
};
#endif /* WORLD_WORLD_H_ */