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
+18 -18
View File
@@ -22,15 +22,15 @@
const uint MAX_WORK_PER_FRAME = 128;
const uint MIN_SURROUNDING = 9;
ChunksController::ChunksController(Level* level, uint padding)
ChunksController::ChunksController(Level& level, uint padding)
: level(level),
chunks(level->chunks.get()),
lighting(level->lighting.get()),
chunks(*level.chunks),
lighting(*level.lighting),
padding(padding),
generator(std::make_unique<WorldGenerator>(
level->content->generators.require(level->getWorld()->getGenerator()),
level->content,
level->getWorld()->getSeed()
level.content->generators.require(level.getWorld()->getGenerator()),
level.content,
level.getWorld()->getSeed()
)) {}
ChunksController::~ChunksController() = default;
@@ -56,8 +56,8 @@ void ChunksController::update(
}
bool ChunksController::loadVisible() {
int sizeX = chunks->getWidth();
int sizeY = chunks->getHeight();
int sizeX = chunks.getWidth();
int sizeY = chunks.getHeight();
int nearX = 0;
int nearZ = 0;
@@ -66,7 +66,7 @@ bool ChunksController::loadVisible() {
for (uint z = padding; z < sizeY - padding; z++) {
for (uint x = padding; x < sizeX - padding; x++) {
int index = z * sizeX + x;
auto& chunk = chunks->getChunks()[index];
auto& chunk = chunks.getChunks()[index];
if (chunk != nullptr) {
if (chunk->flags.loaded && !chunk->flags.lighted) {
if (buildLights(chunk)) {
@@ -87,12 +87,12 @@ bool ChunksController::loadVisible() {
}
}
const auto& chunk = chunks->getChunks()[nearZ * sizeX + nearX];
const auto& chunk = chunks.getChunks()[nearZ * sizeX + nearX];
if (chunk != nullptr || !assigned) {
return false;
}
int offsetX = chunks->getOffsetX();
int offsetY = chunks->getOffsetY();
int offsetX = chunks.getOffsetX();
int offsetY = chunks.getOffsetY();
createChunk(nearX + offsetX, nearZ + offsetY);
return true;
}
@@ -101,15 +101,15 @@ bool ChunksController::buildLights(const std::shared_ptr<Chunk>& chunk) {
int surrounding = 0;
for (int oz = -1; oz <= 1; oz++) {
for (int ox = -1; ox <= 1; ox++) {
if (chunks->getChunk(chunk->x + ox, chunk->z + oz)) surrounding++;
if (chunks.getChunk(chunk->x + ox, chunk->z + oz)) surrounding++;
}
}
if (surrounding == MIN_SURROUNDING) {
bool lightsCache = chunk->flags.loadedLights;
if (!lightsCache) {
lighting->buildSkyLight(chunk->x, chunk->z);
lighting.buildSkyLight(chunk->x, chunk->z);
}
lighting->onChunkLoaded(chunk->x, chunk->z, !lightsCache);
lighting.onChunkLoaded(chunk->x, chunk->z, !lightsCache);
chunk->flags.lighted = true;
return true;
}
@@ -117,8 +117,8 @@ bool ChunksController::buildLights(const std::shared_ptr<Chunk>& chunk) {
}
void ChunksController::createChunk(int x, int z) {
auto chunk = level->chunksStorage->create(x, z);
chunks->putChunk(chunk);
auto chunk = level.chunksStorage->create(x, z);
chunks.putChunk(chunk);
auto& chunkFlags = chunk->flags;
if (!chunkFlags.loaded) {
@@ -128,7 +128,7 @@ void ChunksController::createChunk(int x, int z) {
chunk->updateHeights();
if (!chunkFlags.loadedLights) {
Lighting::prebuildSkyLight(chunk.get(), level->content->getIndices());
Lighting::prebuildSkyLight(chunk.get(), level.content->getIndices());
}
chunkFlags.loaded = true;
chunkFlags.ready = true;