Minor refactor

This commit is contained in:
MihailRis 2023-12-07 13:45:34 +03:00
parent 2055cff0be
commit 551b230b94
10 changed files with 30 additions and 22 deletions

View File

@ -147,7 +147,7 @@ Panel* create_new_world_panel(Engine* engine, PagesControl* menu) {
auto folder = paths->getWorldsFolder()/u8path(nameutf8);
std::filesystem::create_directories(folder);
World* world = new World(nameutf8, folder, seed, settings);
auto screen = new LevelScreen(engine, world->load(settings, engine->getContent()));
auto screen = new LevelScreen(engine, world->create(settings, engine->getContent()));
engine->setScreen(shared_ptr<Screen>(screen));
});
panel->add(button);
@ -338,4 +338,4 @@ Panel* create_pause_panel(Engine* engine, PagesControl* menu) {
panel->add(shared_ptr<UINode>(button));
}
return panel;
}
}

View File

@ -235,7 +235,7 @@ void BlocksRenderer::blockCubeShaded(const ivec3& icoord,
const vec3& offset,
const vec3& size,
const UVRegion(&texfaces)[6],
const Block* block, ubyte states) {
const Block* block, ubyte rotation) {
ivec3 X(1, 0, 0);
ivec3 Y(0, 1, 0);
@ -244,7 +244,7 @@ void BlocksRenderer::blockCubeShaded(const ivec3& icoord,
ivec3 coord = icoord;
if (block->rotatable) {
auto& rotations = block->rotations;
auto& orient = rotations.variants[states & BLOCK_ROT_MASK];
auto& orient = rotations.variants[rotation];
X = orient.axisX;
Y = orient.axisY;
Z = orient.axisZ;
@ -411,7 +411,7 @@ void BlocksRenderer::render(const voxel* voxels) {
vec3 size = hitbox.size();
vec3 off = hitbox.min();
blockCubeShaded(ivec3(x,y,z), off, size, texfaces, &def, vox.states);
blockCubeShaded(ivec3(x,y,z), off, size, texfaces, &def, vox.rotation());
break;
}
default:

View File

@ -91,7 +91,7 @@ class BlocksRenderer {
const glm::vec3& size,
const UVRegion(&faces)[6],
const Block* block,
ubyte states);
ubyte rotation);
void blockXSprite(int x, int y, int z, const glm::vec3& size, const UVRegion& face1, const UVRegion& face2, float spread);
bool isOpenForLight(int x, int y, int z) const;

View File

@ -16,10 +16,10 @@ const float FLIGHT_SPEED_MUL = 4.0f;
const float CHEAT_SPEED_MUL = 5.0f;
const float JUMP_FORCE = 8.0f;
Player::Player(glm::vec3 position, float speed, Camera* camera) :
Player::Player(glm::vec3 position, float speed) :
speed(speed),
camera(camera),
choosenBlock(1) {
camera = new Camera(position, glm::radians(90.0f));
hitbox = new Hitbox(position, vec3(0.3f,0.9f,0.3f));
}

View File

@ -40,7 +40,7 @@ public:
float camX = 0.0f;
float camY = 0.0f;
Player(glm::vec3 position, float speed, Camera* camera);
Player(glm::vec3 position, float speed);
~Player();
void teleport(glm::vec3 position);

View File

@ -74,7 +74,7 @@ const AABB* Chunks::isObstacle(float x, float y, float z){
const Block* def = contentIds->getBlockDef(v->id);
if (def->obstacle) {
const AABB& hitbox = def->rotatable
? def->rt.hitboxes[v->states & BLOCK_ROT_MASK]
? def->rt.hitboxes[v->rotation()]
: def->hitbox;
if (def->rt.solid) {
return &hitbox;
@ -225,7 +225,9 @@ voxel* Chunks::rayCast(vec3 start,
// TODO: replace this dumb solution with something better
if (def && !def->rt.solid) {
const int gridSize = BLOCK_AABB_GRID * 2;
const AABB& box = def->rotatable ? def->rt.hitboxes[voxel->states & BLOCK_ROT_MASK] : def->hitbox;
const AABB& box = def->rotatable
? def->rt.hitboxes[voxel->rotation()]
: def->hitbox;
const int subs = gridSize;
iend = vec3(ix, iy, iz);
end -= iend;

View File

@ -43,15 +43,17 @@ void ChunksStorage::remove(int x, int z) {
}
std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
World* world = level->world;
auto chunk = shared_ptr<Chunk>(new Chunk(x, z));
store(chunk);
unique_ptr<ubyte> data(level->world->wfile->getChunk(chunk->x, chunk->z));
unique_ptr<ubyte> data(world->wfile->getChunk(chunk->x, chunk->z));
if (data) {
chunk->decode(data.get());
chunk->setLoaded(true);
}
light_t* lights = level->world->wfile->getLights(chunk->x, chunk->z);
light_t* lights = world->wfile->getLights(chunk->x, chunk->z);
if (lights) {
chunk->lightmap->set(lights);
chunk->setLoadedLights(true);

View File

@ -19,11 +19,11 @@ struct voxel {
blockid_t id;
uint8_t states;
inline uint8_t rotation() {
inline uint8_t rotation() const {
return states & BLOCK_ROT_MASK;
}
inline int8_t variant() {
inline int8_t variant() const {
return (states & BLOCK_VARIANT_MASK) >> 4;
}
};

View File

@ -56,17 +56,20 @@ void World::write(Level* level) {
wfile->writePlayer(level->player);
}
const float DEF_PLAYER_Y = 100.0f;
const float DEF_PLAYER_SPEED = 4.0f;
Level* World::create(EngineSettings& settings, const Content* content) {
Player* player = new Player(vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED);
return new Level(this, content, player, settings);
}
Level* World::load(EngineSettings& settings, const Content* content) {
wfile->readWorldInfo(this);
vec3 playerPosition = vec3(0, 100, 0);
Camera* camera = new Camera(playerPosition, glm::radians(90.0f));
Player* player = new Player(playerPosition, 4.0f, camera);
Player* player = new Player(vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED);
Level* level = new Level(this, content, player, settings);
wfile->readPlayer(player);
camera->rotation = glm::mat4(1.0f);
camera->rotate(player->camY, player->camX, 0);
return level;
}
}

View File

@ -35,6 +35,7 @@ public:
void updateTimers(float delta);
void write(Level* level);
Level* create(EngineSettings& settings, const Content* content);
Level* load(EngineSettings& settings, const Content* content);
};