diff --git a/src/chunk.lua b/src/chunk.lua index 3839d9d..294d57b 100644 --- a/src/chunk.lua +++ b/src/chunk.lua @@ -58,6 +58,7 @@ function Chunk:load(thread) -- each vertex is currently 36 bytes self.verticesData = love.data.newByteData(maxVertices * 36) + -- add 1 to the size to include the border local blocks = {} for i = 0, CHUNK_SIZE + 1 do blocks[i] = {} @@ -77,11 +78,9 @@ function Chunk:getBlock(x, y, z) if y < 1 or y > CHUNK_HEIGHT then return 0 end -- translate to local coordinates - x = x - self.position.x - y = y - self.position.y - z = z - self.position.z + local i, j, k = x - self.position.x, y - self.position.y, z - self.position.z - return self.blocks[x][y][z] + return self.blocks[i][j][k] end function Chunk:setBlock(x, y, z, block) @@ -95,6 +94,23 @@ function Chunk:setBlock(x, y, z, block) self.blocks[i][j][k] = block self.dirty = true + + -- if border block, mark adjacent chunks as dirty + local adjacentX, adjacentZ + if i == 1 then + adjacentX = self.world:getChunk(x - 1, z) + elseif i == CHUNK_SIZE then + adjacentX = self.world:getChunk(x + 1, z) + end + + if k == 1 then + adjacentZ = self.world:getChunk(x, z - 1) + elseif k == CHUNK_SIZE then + adjacentZ = self.world:getChunk(x, z + 1) + end + + if adjacentX then adjacentX.dirty = true end + if adjacentZ then adjacentZ.dirty = true end end function Chunk:update() diff --git a/src/player.lua b/src/player.lua index 9fc9dad..f8acd3d 100644 --- a/src/player.lua +++ b/src/player.lua @@ -69,12 +69,6 @@ end function Player:draw() self.camera:draw() - if self.nextBlock then - local x, y, z = self.nextBlock:unpack() - local block = self.world:getBlock(x, y, z) - debug("light", block) - end - if self.currentBlock then local x, y, z = self.currentBlock:unpack() local chunk = self.world:getChunk(x, z) diff --git a/src/world.lua b/src/world.lua index 75428cc..cde27b9 100644 --- a/src/world.lua +++ b/src/world.lua @@ -11,9 +11,11 @@ function World:new() -- self:load() - self._generated = false + self.threads = {} - self.thread = love.thread.newThread("src/load_mesh.lua") + for i = 1, 4 do + self.threads[i] = love.thread.newThread("src/load_mesh.lua") + end self:loadChunk(0, 0) @@ -31,6 +33,16 @@ function World:save() bitser.dumpLoveFile("world.dat", chunks) end +function World:getThread() + for _, thread in pairs(self.threads) do + if thread:isRunning() == false then + return thread + end + end + + return nil +end + function World:load() local chunks = bitser.loadLoveFile("world.dat") @@ -46,11 +58,6 @@ function World:load() end function World:generateChunk(wx, wz) - if self._generated then return end - - -- only allow one chunk to be generated at a time - self._generated = true - local x = math.floor((wx-1) / CHUNK_SIZE) local z = math.floor((wz-1) / CHUNK_SIZE) @@ -108,21 +115,22 @@ function World:loadChunk(x, z) if chunk then chunk.loaded = true - - if chunk.dirty and not self.thread:isRunning() then - chunk:load(self.thread) - end else self:generateChunk(x, z) end end function World:update(dt) - self._generated = false - for i, v in pairs(self.chunks) do for j, chunk in pairs(v) do if chunk.loaded then + + if chunk.dirty then + local thread = self:getThread() + + if thread then chunk:load(thread) end + end + chunk:update(dt) end