Skip to content

Commit

Permalink
update adjacent chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
brennop committed Apr 6, 2023
1 parent 04d5571 commit 46c9084
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
24 changes: 20 additions & 4 deletions src/chunk.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {}
Expand All @@ -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)
Expand All @@ -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()
Expand Down
6 changes: 0 additions & 6 deletions src/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
34 changes: 21 additions & 13 deletions src/world.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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")

Expand All @@ -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)

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 46c9084

Please sign in to comment.