Added class Level for runtime world stuff

This commit is contained in:
MihailRis 2022-06-29 17:12:58 +03:00 committed by GitHub
parent 4ceba017c7
commit e8ca11894d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 81 additions and 35 deletions

View File

@ -4,12 +4,15 @@
# Add inputs and outputs from these tool invocations to the build variables # Add inputs and outputs from these tool invocations to the build variables
CPP_SRCS += \ CPP_SRCS += \
../src/world/Level.cpp \
../src/world/World.cpp ../src/world/World.cpp
OBJS += \ OBJS += \
./src/world/Level.o \
./src/world/World.o ./src/world/World.o
CPP_DEPS += \ CPP_DEPS += \
./src/world/Level.d \
./src/world/World.d ./src/world/World.d

View File

@ -5,6 +5,7 @@
#include "physics/PhysicsSolver.h" #include "physics/PhysicsSolver.h"
#include "physics/Hitbox.h" #include "physics/Hitbox.h"
#include "lighting/Lighting.h" #include "lighting/Lighting.h"
#include "world/Level.h"
#include "voxels/voxel.h" #include "voxels/voxel.h"
#include "voxels/Chunks.h" #include "voxels/Chunks.h"
#include "window/Camera.h" #include "window/Camera.h"
@ -162,8 +163,11 @@ void update_controls(PhysicsSolver* physics,
} }
} }
void update_interaction(Chunks* chunks, PhysicsSolver* physics, Player* player, Lighting* lighting, LineBatch* lineBatch){ void update_interaction(Level* level, LineBatch* lineBatch){
Chunks* chunks = level->chunks;
Player* player = level->player;
Camera* camera = player->camera; Camera* camera = player->camera;
Lighting* lighting = level->lighting;
vec3 end; vec3 end;
vec3 norm; vec3 norm;
vec3 iend; vec3 iend;
@ -182,7 +186,7 @@ void update_interaction(Chunks* chunks, PhysicsSolver* physics, Player* player,
int x = (int)(iend.x)+(int)(norm.x); int x = (int)(iend.x)+(int)(norm.x);
int y = (int)(iend.y)+(int)(norm.y); int y = (int)(iend.y)+(int)(norm.y);
int z = (int)(iend.z)+(int)(norm.z); int z = (int)(iend.z)+(int)(norm.z);
if (!physics->isBlockInside(x,y,z, player->hitbox)){ if (!level->physics->isBlockInside(x,y,z, player->hitbox)){
chunks->set(x, y, z, player->choosenBlock); chunks->set(x, y, z, player->choosenBlock);
lighting->onBlockSet(x,y,z, player->choosenBlock); lighting->onBlockSet(x,y,z, player->choosenBlock);
} }

View File

@ -4,10 +4,10 @@
class PhysicsSolver; class PhysicsSolver;
class Chunks; class Chunks;
class Player; class Player;
class Lighting;
class LineBatch; class LineBatch;
class Level;
void update_controls(PhysicsSolver* physics, Chunks* chunks, Player* player, float delta); void update_controls(PhysicsSolver* physics, Chunks* chunks, Player* player, float delta);
void update_interaction(Chunks* chunks, PhysicsSolver* physics, Player* player, Lighting* lighting, LineBatch* lineBatch); void update_interaction(Level* level, LineBatch* lineBatch);
#endif /* PLAYER_CONTROL_H_ */ #endif /* PLAYER_CONTROL_H_ */

View File

@ -42,6 +42,7 @@ using namespace glm;
#include "physics/Hitbox.h" #include "physics/Hitbox.h"
#include "physics/PhysicsSolver.h" #include "physics/PhysicsSolver.h"
#include "world/World.h" #include "world/World.h"
#include "world/Level.h"
#include "audio/Audio.h" #include "audio/Audio.h"
#include "audio/audioutil.h" #include "audio/audioutil.h"
@ -58,9 +59,9 @@ int HEIGHT = 720;
// Save all world data to files // Save all world data to files
void write_world(World* world){ void write_world(World* world, Level* level){
WorldFiles* wfile = world->wfile; WorldFiles* wfile = world->wfile;
Chunks* chunks = world->chunks; Chunks* chunks = level->chunks;
for (unsigned int i = 0; i < chunks->volume; i++){ for (unsigned int i = 0; i < chunks->volume; i++){
Chunk* chunk = chunks->chunks[i]; Chunk* chunk = chunks->chunks[i];
@ -72,6 +73,18 @@ void write_world(World* world){
wfile->write(); wfile->write();
} }
void update_level(World* world, Level* level, VoxelRenderer* renderer, vec3 position, float delta, long frame){
update_controls(level->physics, level->chunks, level->player, delta);
update_interaction(level, lineBatch);
level->chunks->setCenter(world->wfile, position.x,0,position.z);
level->chunksController->_buildMeshes(renderer, frame);
int freeLoaders = level->chunksController->countFreeLoaders();
for (int i = 0; i < freeLoaders; i++)
level->chunksController->loadVisible(world->wfile);
}
int main() { int main() {
setup_definitions(); setup_definitions();
@ -91,12 +104,11 @@ int main() {
} }
std::cout << "-- loading world" << std::endl; std::cout << "-- loading world" << std::endl;
Camera* camera = new Camera(vec3(-320,255,32), radians(90.0f)); Camera* camera = new Camera(vec3(-320,200,32), radians(90.0f));
World* world = new World("world-1", "world/", new Chunks(34,1,34, 0,0,0)); World* world = new World("world-1", "world/");
Chunks* chunks = world->chunks;
Player* player = new Player(vec3(camera->position), 4.0f, camera); Player* player = new Player(vec3(camera->position), 4.0f, camera);
Level* level = new Level(player, new Chunks(34,1,34, 0,0,0), new PhysicsSolver(vec3(0, -9.8f*2.0f, 0)));
world->wfile->readPlayer(player); world->wfile->readPlayer(player);
camera->rotation = mat4(1.0f); camera->rotation = mat4(1.0f);
camera->rotate(player->camY, player->camX, 0); camera->rotate(player->camY, player->camX, 0);
@ -104,13 +116,9 @@ int main() {
std::cout << "-- preparing systems" << std::endl; std::cout << "-- preparing systems" << std::endl;
VoxelRenderer renderer(1024*1024); VoxelRenderer renderer(1024*1024);
PhysicsSolver physics(vec3(0,-9.8f*2.0f,0));
Lighting lighting(chunks);
init_renderer(); init_renderer();
ChunksController chunksController(chunks, &lighting);
float lastTime = glfwGetTime(); float lastTime = glfwGetTime();
float delta = 0.0f; float delta = 0.0f;
@ -137,18 +145,9 @@ int main() {
devdata = !devdata; devdata = !devdata;
} }
update_controls(&physics, chunks, player, delta); update_level(world, level, &renderer, camera->position, delta, frame);
update_interaction(chunks, &physics, player, &lighting, lineBatch); draw_world(world, level, camera, assets, occlusion);
draw_hud(world, level, assets, devdata, fps);
chunks->setCenter(world->wfile, camera->position.x,0,camera->position.z);
chunksController._buildMeshes(&renderer, frame);
int freeLoaders = chunksController.countFreeLoaders();
for (int i = 0; i < freeLoaders; i++)
chunksController.loadVisible(world->wfile);
draw_world(player, camera, assets, world, occlusion);
draw_hud(player, assets, world, devdata, fps);
Window::swapBuffers(); Window::swapBuffers();
Events::pullEvents(); Events::pullEvents();
@ -156,7 +155,7 @@ int main() {
std::cout << "-- saving world" << std::endl; std::cout << "-- saving world" << std::endl;
world->wfile->writePlayer(player); world->wfile->writePlayer(player);
write_world(world); write_world(world, level);
delete world; delete world;
std::cout << "-- shutting down" << std::endl; std::cout << "-- shutting down" << std::endl;

19
src/world/Level.cpp Normal file
View File

@ -0,0 +1,19 @@
#include "Level.h"
#include "../lighting/Lighting.h"
#include "../voxels/ChunksController.h"
Level::Level(Player* player, Chunks* chunks, PhysicsSolver* physics) :
player(player),
chunks(chunks),
physics(physics) {
lighting = new Lighting(chunks);
chunksController = new ChunksController(chunks, lighting);
}
Level::~Level(){
delete chunks;
delete physics;
delete player;
delete lighting;
delete chunksController;
}

21
src/world/Level.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef WORLD_LEVEL_H_
#define WORLD_LEVEL_H_
class Player;
class Chunks;
class Lighting;
class PhysicsSolver;
class ChunksController;
class Level {
public:
Player* player;
Chunks* chunks;
PhysicsSolver* physics;
Lighting* lighting;
ChunksController* chunksController;
Level(Player* player, Chunks* chunks, PhysicsSolver* physics);
~Level();
};
#endif /* WORLD_LEVEL_H_ */

View File

@ -4,7 +4,7 @@
#include "../voxels/Chunk.h" #include "../voxels/Chunk.h"
#include "../voxels/Chunks.h" #include "../voxels/Chunks.h"
World::World(std::string name, std::string directory, Chunks* chunks) : name(name), chunks(chunks) { World::World(std::string name, std::string directory) : name(name) {
wfile = new WorldFiles(directory, REGION_VOL * (CHUNK_VOL * 2 + 8)); wfile = new WorldFiles(directory, REGION_VOL * (CHUNK_VOL * 2 + 8));
} }

View File

@ -10,9 +10,8 @@ class World {
public: public:
std::string name; std::string name;
WorldFiles* wfile; WorldFiles* wfile;
Chunks* chunks;
World(std::string name, std::string directory, Chunks* chunks); World(std::string name, std::string directory);
~World(); ~World();
}; };

View File

@ -110,8 +110,9 @@ bool chunks_comparator(size_t i, size_t j) {
} }
void draw_hud(Player* player, Assets* assets, World* world, bool devdata, int fps){ void draw_hud(World* world, Level* level, Assets* assets, bool devdata, int fps){
Chunks* chunks = world->chunks; Chunks* chunks = level->chunks;
Player* player = level->player;
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE); glDisable(GL_CULL_FACE);
@ -143,8 +144,8 @@ void draw_hud(Player* player, Assets* assets, World* world, bool devdata, int fp
batch->render(); batch->render();
} }
void draw_world(Player* player, Camera* camera, Assets* assets, World* world, bool occlusion){ void draw_world(World* world, Level* level, Camera* camera, Assets* assets, bool occlusion){
Chunks* chunks = world->chunks; Chunks* chunks = level->chunks;
glClearColor(0.7f,0.81f,1.0f,1); glClearColor(0.7f,0.81f,1.0f,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);