New world format, fixes

This commit is contained in:
MihailRis
2023-11-05 15:32:46 +03:00
committed by GitHub
parent 0c65b13f4e
commit 0cf8382e95
21 changed files with 350 additions and 173 deletions
+13 -6
View File
@@ -1,6 +1,8 @@
#include "Level.h"
#include "World.h"
#include "LevelEvents.h"
#include "../lighting/Lighting.h"
#include "../voxels/Chunk.h"
#include "../voxels/Chunks.h"
#include "../voxels/ChunksController.h"
#include "../voxels/ChunksStorage.h"
@@ -9,19 +11,24 @@
#include "../physics/PhysicsSolver.h"
#include "../objects/Player.h"
Level::Level(World* world, Player* player, Chunks* chunks, ChunksStorage* chunksStorage, PhysicsSolver* physics) :
world(world),
player(player),
chunks(chunks),
chunksStorage(chunksStorage),
physics(physics) {
Level::Level(World* world, Player* player, Chunks* chunks, ChunksStorage* chunksStorage, PhysicsSolver* physics, LevelEvents* events) :
world(world),
player(player),
chunks(chunks),
chunksStorage(chunksStorage),
physics(physics),
events(events) {
lighting = new Lighting(chunks);
chunksController = new ChunksController(this, chunks, lighting);
playerController = new PlayerController(this);
events->listen(EVT_CHUNK_HIDDEN, [this](lvl_event_type type, Chunk* chunk) {
this->chunksStorage->remove(chunk->x, chunk->z);
});
}
Level::~Level(){
delete chunks;
delete events;
delete physics;
delete player;
delete lighting;
+3 -1
View File
@@ -4,6 +4,7 @@
class World;
class Player;
class Chunks;
class LevelEvents;
class Lighting;
class PhysicsSolver;
class ChunksController;
@@ -20,7 +21,8 @@ public:
Lighting* lighting;
ChunksController* chunksController;
PlayerController* playerController;
Level(World* world, Player* player, Chunks* chunks, ChunksStorage* chunksStorage, PhysicsSolver* physics);
LevelEvents* events;
Level(World* world, Player* player, Chunks* chunks, ChunksStorage* chunksStorage, PhysicsSolver* physics, LevelEvents* events);
~Level();
void update(float delta, bool interactions);
+16
View File
@@ -0,0 +1,16 @@
#include "LevelEvents.h"
#include "../voxels/Chunk.h"
using std::vector;
void LevelEvents::listen(lvl_event_type type, chunk_event_func func) {
auto& callbacks = chunk_callbacks[type];
callbacks.push_back(func);
}
void LevelEvents::trigger(lvl_event_type type, Chunk* chunk) {
auto& callbacks = chunk_callbacks[type];
for (chunk_event_func func : callbacks) {
func(type, chunk);
}
}
+23
View File
@@ -0,0 +1,23 @@
#ifndef WORLD_LEVEL_EVENTS_H_
#define WORLD_LEVEL_EVENTS_H_
#include <functional>
#include <vector>
#include <unordered_map>
class Chunk;
enum lvl_event_type {
EVT_CHUNK_HIDDEN,
};
typedef std::function<void(lvl_event_type, Chunk*)> chunk_event_func;
class LevelEvents {
std::unordered_map<lvl_event_type, std::vector<chunk_event_func>> chunk_callbacks;
public:
void listen(lvl_event_type type, chunk_event_func func);
void trigger(lvl_event_type type, Chunk* chunk);
};
#endif // WORLD_LEVEL_EVENTS_H_
+6 -4
View File
@@ -10,11 +10,12 @@
#include "../objects/Player.h"
#include "../physics/PhysicsSolver.h"
#include "../window/Camera.h"
#include "../world/LevelEvents.h"
using std::shared_ptr;
World::World(std::string name, std::string directory, int seed) : name(name), seed(seed) {
wfile = new WorldFiles(directory, REGION_VOL * (CHUNK_VOL * 2 + 8));
wfile = new WorldFiles(directory, REGION_VOL * (CHUNK_DATA_LEN * 2 + 8));
}
World::~World(){
@@ -24,11 +25,11 @@ World::~World(){
void World::write(Level* level) {
Chunks* chunks = level->chunks;
for (unsigned int i = 0; i < chunks->volume; i++) {
for (size_t i = 0; i < chunks->volume; i++) {
shared_ptr<Chunk> chunk = chunks->chunks[i];
if (chunk == nullptr || !chunk->isUnsaved())
continue;
wfile->put((const ubyte*)chunk->voxels, chunk->x, chunk->z);
wfile->put(chunk.get());
}
wfile->write();
@@ -37,7 +38,8 @@ void World::write(Level* level) {
Level* World::loadLevel(Player* player) {
ChunksStorage* storage = new ChunksStorage();
Level* level = new Level(this, player, new Chunks(16, 16, 0, 0), storage, new PhysicsSolver(vec3(0, -19.6f, 0)));
LevelEvents* events = new LevelEvents();
Level* level = new Level(this, player, new Chunks(16, 16, 0, 0, events), storage, new PhysicsSolver(vec3(0, -19.6f, 0)), events);
wfile->readPlayer(player);
Camera* camera = player->camera;