One step closer to multiplayer
This commit is contained in:
+14
-41
@@ -8,7 +8,6 @@
|
||||
#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"
|
||||
@@ -21,20 +20,18 @@ 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()) ,
|
||||
chunksStorage(std::make_unique<ChunksStorage>(this)),
|
||||
physics(std::make_unique<PhysicsSolver>(glm::vec3(0, -22.6f, 0))),
|
||||
events(std::make_unique<LevelEvents>()),
|
||||
settings(settings)
|
||||
{
|
||||
objCounter = 0;
|
||||
physics = new PhysicsSolver(glm::vec3(0, -22.6f, 0));
|
||||
auto inv = std::make_shared<Inventory>(0, DEF_PLAYER_INVENTORY_SIZE);
|
||||
player = spawnObject<Player>(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv);
|
||||
auto player = spawnObject<Player>(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.get(), events, content);
|
||||
lighting = new Lighting(content, chunks);
|
||||
uint matrixSize = (settings.chunks.loadDistance + settings.chunks.padding) * 2;
|
||||
chunks = std::make_unique<Chunks>(matrixSize, matrixSize, 0, 0,
|
||||
world->wfile.get(), events.get(), content);
|
||||
lighting = std::make_unique<Lighting>(content, chunks.get());
|
||||
|
||||
events->listen(EVT_CHUNK_HIDDEN, [this](lvl_event_type type, Chunk* chunk) {
|
||||
this->chunksStorage->remove(chunk->x, chunk->z);
|
||||
@@ -45,26 +42,16 @@ Level::Level(World* world, const Content* content, EngineSettings& settings)
|
||||
}
|
||||
|
||||
Level::~Level(){
|
||||
delete chunks;
|
||||
delete events;
|
||||
delete physics;
|
||||
delete lighting;
|
||||
delete chunksStorage;
|
||||
|
||||
for(auto obj : objects)
|
||||
{
|
||||
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);
|
||||
void Level::loadMatrix(int32_t x, int32_t z, uint32_t radius) {
|
||||
chunks->setCenter(x, z);
|
||||
radius = std::min(radius, settings.chunks.loadDistance + settings.chunks.padding * 2);
|
||||
if (chunks->w != radius) {
|
||||
chunks->resize(radius, radius);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,17 +59,3 @@ World* Level::getWorld() {
|
||||
return world.get();
|
||||
}
|
||||
|
||||
|
||||
template<class T, typename... Args>
|
||||
std::shared_ptr<T> Level::spawnObject(Args&&... args)
|
||||
{
|
||||
static_assert(std::is_base_of<Object, T>::value, "T must be a derived of Object class");
|
||||
std::shared_ptr<T> tObj = std::make_shared<T>(args...);
|
||||
|
||||
std::shared_ptr<Object> obj = std::dynamic_pointer_cast<Object, T>(tObj);
|
||||
objects.push_back(obj);
|
||||
obj->objectUID = objCounter;
|
||||
obj->spawned();
|
||||
objCounter += 1;
|
||||
return tObj;
|
||||
}
|
||||
|
||||
+27
-15
@@ -3,16 +3,13 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "../typedefs.h"
|
||||
#include "../settings.h"
|
||||
#include <list>
|
||||
#include "../interfaces/Object.h"
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
|
||||
class Content;
|
||||
class World;
|
||||
class Player;
|
||||
class Object;
|
||||
class Chunks;
|
||||
class Inventory;
|
||||
class Inventories;
|
||||
@@ -21,21 +18,19 @@ class Lighting;
|
||||
class PhysicsSolver;
|
||||
class ChunksStorage;
|
||||
|
||||
/* A level, contains chunks and objects */
|
||||
class Level {
|
||||
private:
|
||||
int objCounter;
|
||||
public:
|
||||
std::unique_ptr<World> world;
|
||||
const Content* const content;
|
||||
std::list<std::shared_ptr<Object>> objects;
|
||||
std::shared_ptr<Player> player;
|
||||
Chunks* chunks;
|
||||
ChunksStorage* chunksStorage;
|
||||
std::vector<std::shared_ptr<Object>> objects;
|
||||
std::unique_ptr<Chunks> chunks;
|
||||
std::unique_ptr<ChunksStorage> chunksStorage;
|
||||
std::unique_ptr<Inventories> inventories;
|
||||
|
||||
PhysicsSolver* physics;
|
||||
Lighting* lighting;
|
||||
LevelEvents* events;
|
||||
std::unique_ptr<PhysicsSolver> physics;
|
||||
std::unique_ptr<Lighting> lighting;
|
||||
std::unique_ptr<LevelEvents> events;
|
||||
|
||||
const EngineSettings& settings;
|
||||
|
||||
@@ -44,7 +39,7 @@ public:
|
||||
EngineSettings& settings);
|
||||
~Level();
|
||||
|
||||
void update();
|
||||
void loadMatrix(int32_t x, int32_t z, uint32_t radius);
|
||||
|
||||
World* getWorld();
|
||||
|
||||
@@ -52,7 +47,24 @@ public:
|
||||
// @param T class that derives the Object class
|
||||
// @param args pass arguments needed for T class constructor
|
||||
template<class T, typename... Args>
|
||||
std::shared_ptr<T> spawnObject(Args&&... args);
|
||||
std::shared_ptr<T> spawnObject(Args&&... args) {
|
||||
static_assert(std::is_base_of<Object, T>::value, "T must be a derived of Object class");
|
||||
std::shared_ptr<T> tObj = std::make_shared<T>(args...);
|
||||
|
||||
std::shared_ptr<Object> obj = std::dynamic_pointer_cast<Object, T>(tObj);
|
||||
obj->objectUID = objects.size();
|
||||
objects.push_back(obj);
|
||||
obj->spawned();
|
||||
return tObj;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
std::shared_ptr<T> getObject(uint64_t id) {
|
||||
static_assert(std::is_base_of<Object, T>::value, "T must be a derived of Object class");
|
||||
if (id >= objects.size()) return nullptr;
|
||||
std::shared_ptr<T> object = std::dynamic_pointer_cast<T>(objects[id]);
|
||||
return object;
|
||||
}
|
||||
};
|
||||
|
||||
#endif /* WORLD_LEVEL_H_ */
|
||||
|
||||
+14
-9
@@ -50,7 +50,7 @@ void World::updateTimers(float delta) {
|
||||
void World::write(Level* level) {
|
||||
const Content* content = level->content;
|
||||
|
||||
Chunks* chunks = level->chunks;
|
||||
Chunks* chunks = level->chunks.get();
|
||||
|
||||
for (size_t i = 0; i < chunks->volume; i++) {
|
||||
auto chunk = chunks->chunks[i];
|
||||
@@ -64,7 +64,11 @@ void World::write(Level* level) {
|
||||
}
|
||||
|
||||
wfile->write(this, content);
|
||||
wfile->writePlayer(level->player);
|
||||
for (auto object : level->objects) {
|
||||
if (std::shared_ptr<Player> player = std::dynamic_pointer_cast<Player>(object)) {
|
||||
wfile->writePlayer(player);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Level* World::create(std::string name,
|
||||
@@ -77,9 +81,6 @@ Level* World::create(std::string name,
|
||||
) {
|
||||
auto world = new World(name, generator, directory, seed, settings, content, packs);
|
||||
auto level = new Level(world, content, settings);
|
||||
auto inventory = level->player->getInventory();
|
||||
inventory->setId(world->getNextInventoryId());
|
||||
level->inventories->store(inventory);
|
||||
return level;
|
||||
}
|
||||
|
||||
@@ -97,9 +98,13 @@ Level* World::load(fs::path directory,
|
||||
}
|
||||
|
||||
auto level = new Level(world.get(), content, settings);
|
||||
wfile->readPlayer(level->player);
|
||||
level->inventories->store(level->player->getInventory());
|
||||
world.release();
|
||||
for (auto object : level->objects) {
|
||||
if (std::shared_ptr<Player> player = std::dynamic_pointer_cast<Player>(object)) {
|
||||
wfile->readPlayer(player);
|
||||
level->inventories->store(player->getInventory());
|
||||
}
|
||||
}
|
||||
(void)world.release();
|
||||
return level;
|
||||
}
|
||||
|
||||
@@ -193,4 +198,4 @@ std::unique_ptr<dynamic::Map> World::serialize() const {
|
||||
|
||||
root->put("next-inventory-id", nextInventoryId);
|
||||
return root;
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -27,6 +27,7 @@ public:
|
||||
world_load_error(std::string message);
|
||||
};
|
||||
|
||||
/* World - holds all world data except the level (chunks and objects) */
|
||||
class World : Serializable {
|
||||
std::string name;
|
||||
std::string generator;
|
||||
@@ -157,4 +158,4 @@ public:
|
||||
void deserialize(dynamic::Map *src) override;
|
||||
};
|
||||
|
||||
#endif /* WORLD_WORLD_H_ */
|
||||
#endif /* WORLD_WORLD_H_ */
|
||||
|
||||
Reference in New Issue
Block a user