minor controllers refactor

This commit is contained in:
MihailRis
2024-11-20 18:03:39 +03:00
parent 9e02f1122f
commit 005bcfb436
5 changed files with 56 additions and 58 deletions
+26 -28
View File
@@ -14,10 +14,10 @@
#include "world/Level.hpp"
#include "world/World.hpp"
BlocksController::BlocksController(Level* level, uint padding)
BlocksController::BlocksController(const Level& level, uint padding)
: level(level),
chunks(level->chunks.get()),
lighting(level->lighting.get()),
chunks(*level.chunks),
lighting(*level.lighting),
randTickClock(20, 3),
blocksTickClock(20, 1),
worldTickClock(20, 1),
@@ -34,8 +34,8 @@ void BlocksController::updateSides(int x, int y, int z) {
}
void BlocksController::updateSides(int x, int y, int z, int w, int h, int d) {
voxel* vox = chunks->get(x, y, z);
const auto& def = level->content->getIndices()->blocks.require(vox->id);
voxel* vox = chunks.get(x, y, z);
const auto& def = level.content->getIndices()->blocks.require(vox->id);
const auto& rot = def.rotations.variants[vox->state.rotation];
const auto& xaxis = rot.axisX;
const auto& yaxis = rot.axisY;
@@ -62,8 +62,8 @@ void BlocksController::breakBlock(
onBlockInteraction(
player, glm::ivec3(x, y, z), def, BlockInteraction::destruction
);
chunks->set(x, y, z, 0, {});
lighting->onBlockSet(x, y, z, 0);
chunks.set(x, y, z, 0, {});
lighting.onBlockSet(x, y, z, 0);
scripting::on_block_broken(player, def, glm::ivec3(x, y, z));
if (def.rt.extended) {
updateSides(x, y, z , def.size.x, def.size.y, def.size.z);
@@ -78,8 +78,8 @@ void BlocksController::placeBlock(
onBlockInteraction(
player, glm::ivec3(x, y, z), def, BlockInteraction::placing
);
chunks->set(x, y, z, def.rt.id, state);
lighting->onBlockSet(x, y, z, def.rt.id);
chunks.set(x, y, z, def.rt.id, state);
lighting.onBlockSet(x, y, z, def.rt.id);
scripting::on_block_placed(player, def, glm::ivec3(x, y, z));
if (def.rt.extended) {
updateSides(x, y, z , def.size.x, def.size.y, def.size.z);
@@ -89,12 +89,12 @@ void BlocksController::placeBlock(
}
void BlocksController::updateBlock(int x, int y, int z) {
voxel* vox = chunks->get(x, y, z);
voxel* vox = chunks.get(x, y, z);
if (vox == nullptr) return;
const auto& def = level->content->getIndices()->blocks.require(vox->id);
const auto& def = level.content->getIndices()->blocks.require(vox->id);
if (def.grounded) {
const auto& vec = get_ground_direction(def, vox->state.rotation);
if (!chunks->isSolidBlock(x + vec.x, y + vec.y, z + vec.z)) {
if (!chunks.isSolidBlock(x + vec.x, y + vec.y, z + vec.z)) {
breakBlock(nullptr, def, x, y, z);
return;
}
@@ -117,12 +117,11 @@ void BlocksController::update(float delta) {
}
void BlocksController::onBlocksTick(int tickid, int parts) {
auto content = level->content;
auto indices = content->getIndices();
const auto& indices = level.content->getIndices()->blocks;
int tickRate = blocksTickClock.getTickRate();
for (size_t id = 0; id < indices->blocks.count(); id++) {
for (size_t id = 0; id < indices.count(); id++) {
if ((id + tickid) % parts != 0) continue;
auto& def = indices->blocks.require(id);
auto& def = indices.require(id);
auto interval = def.tickInterval;
if (def.rt.funcsset.onblockstick && tickid / parts % interval == 0) {
scripting::on_blocks_tick(def, tickRate / interval);
@@ -155,9 +154,9 @@ void BlocksController::randomTick(
}
void BlocksController::randomTick(int tickid, int parts) {
auto indices = level->content->getIndices();
int width = chunks->getWidth();
int height = chunks->getHeight();
auto indices = level.content->getIndices();
int width = chunks.getWidth();
int height = chunks.getHeight();
int segments = 4;
for (uint z = padding; z < height - padding; z++) {
@@ -166,7 +165,7 @@ void BlocksController::randomTick(int tickid, int parts) {
if ((index + tickid) % parts != 0) {
continue;
}
auto& chunk = chunks->getChunks()[index];
auto& chunk = chunks.getChunks()[index];
if (chunk == nullptr || !chunk->flags.lighted) {
continue;
}
@@ -176,7 +175,7 @@ void BlocksController::randomTick(int tickid, int parts) {
}
int64_t BlocksController::createBlockInventory(int x, int y, int z) {
auto chunk = chunks->getChunkByVoxel(x, y, z);
auto chunk = chunks.getChunkByVoxel(x, y, z);
if (chunk == nullptr) {
return 0;
}
@@ -184,21 +183,20 @@ int64_t BlocksController::createBlockInventory(int x, int y, int z) {
int lz = z - chunk->z * CHUNK_D;
auto inv = chunk->getBlockInventory(lx, y, lz);
if (inv == nullptr) {
auto indices = level->content->getIndices();
auto& def =
indices->blocks.require(chunk->voxels[vox_index(lx, y, lz)].id);
const auto& indices = level.content->getIndices()->blocks;
auto& def = indices.require(chunk->voxels[vox_index(lx, y, lz)].id);
int invsize = def.inventorySize;
if (invsize == 0) {
return 0;
}
inv = level->inventories->create(invsize);
inv = level.inventories->create(invsize);
chunk->addBlockInventory(inv, lx, y, lz);
}
return inv->getId();
}
void BlocksController::bindInventory(int64_t invid, int x, int y, int z) {
auto chunk = chunks->getChunkByVoxel(x, y, z);
auto chunk = chunks.getChunkByVoxel(x, y, z);
if (chunk == nullptr) {
throw std::runtime_error("block does not exists");
}
@@ -207,11 +205,11 @@ void BlocksController::bindInventory(int64_t invid, int x, int y, int z) {
}
int lx = x - chunk->x * CHUNK_W;
int lz = z - chunk->z * CHUNK_D;
chunk->addBlockInventory(level->inventories->get(invid), lx, y, lz);
chunk->addBlockInventory(level.inventories->get(invid), lx, y, lz);
}
void BlocksController::unbindInventory(int x, int y, int z) {
auto chunk = chunks->getChunkByVoxel(x, y, z);
auto chunk = chunks.getChunkByVoxel(x, y, z);
if (chunk == nullptr) {
throw std::runtime_error("block does not exists");
}