Skip to content

Commit

Permalink
RenderDistance/Procedurallychunkgen PART 3 (bugged/unfinished)
Browse files Browse the repository at this point in the history
  • Loading branch information
quentin452 committed Feb 27, 2024
1 parent 0117043 commit f1bcea1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/client/drawhud.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function DrawCanevas()
end

function DrawF3()
love.graphics.setColor(0, 0, 0)
love.graphics.setColor(1, 1, 1)
love.graphics.print(
"x: "
.. math.floor(ThePlayer.x + 0.5)
Expand Down
36 changes: 27 additions & 9 deletions src/utils/mouselogic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,40 @@ function MouseLogicOnPlay(x, y, b)
value = PlayerInventory.items[PlayerInventory.hotbarSelect] or 0
end

local cx, cy, cz = pos and pos.x, pos and pos.y, pos and pos.z
local chunk = pos and pos.chunk

-- Check if the block is not placed at Y 128
if chunk and ThePlayer and ThePlayer.cursorpos and ThePlayer.cursorHit and cy and cy < 128 then
chunk:setVoxel(cx, cy, cz, value, true)
if chunk and ThePlayer and ThePlayer.cursorpos and ThePlayer.cursorHit and pos.y and pos.y < 128 then
chunk:setVoxel(pos.x, pos.y, pos.z, value, true)
LightingUpdate()
-- chunk:updateModel(cx, cy, cz)
-- LuaCraftPrintLoggingNormal("---")
-- LuaCraftPrintLoggingNormal(cx, cy, cz)
-- LuaCraftPrintLoggingNormal(cx % ChunkSize, cy % SliceHeight, cz % ChunkSize)
elseif pos.x and pos.z and pos.y >= WorldHeight and ThePlayer.cursorpos and ThePlayer.cursorHit == true then

for _, chunkSlice in ipairs(chunk.slices) do
renderChunkSlice(chunkSlice, ThePlayer.x, ThePlayer.y, ThePlayer.z)
UpdateNeighboringChunks(chunk, pos.y)
end
elseif pos and pos.x and pos.z and pos.y >= WorldHeight and ThePlayer.cursorpos and ThePlayer.cursorHit == true then
hudMessage = "you cannot place blocks at Y = " .. WorldHeight .. " or more"
hudTimeLeft = 3
end
end
function UpdateNeighboringChunks(chunk, y)
local neighborOffsets = {
{ -1, 0 },
{ 1, 0 },
{ 0, -1 },
{ 0, 1 },
}

for _, offset in ipairs(neighborOffsets) do
local neighborChunk = GetChunkRaw(chunk.x + offset[1], chunk.z + offset[2])
if neighborChunk then
local sliceIndex = math.floor(y / SliceHeight) + 1
local neighborSlice = neighborChunk.slices[sliceIndex]
if neighborSlice then
neighborSlice.alreadyrendered = false
end
end
end
end

function KeyPressed(k)
-- simplified hotbar number press code, thanks nico-abram!
Expand Down
17 changes: 8 additions & 9 deletions src/world/chunk.lua
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ function NewChunk(x, z)
for i = 1, WorldHeight / SliceHeight do
sliceUpdates[i] = { false, false, false, false, false }
end

-- find which slices need to be updated
for i = 1, #self.changes do
local index = math.floor((self.changes[i][2] - 1) / SliceHeight) + 1
Expand Down Expand Up @@ -349,39 +348,39 @@ function NewChunk(x, z)

if sliceUpdates[i][2] then
local chunk = GetChunkRaw(self.x - 1, self.z)
if chunk ~= nil then
if chunk then
local neighborSlice = chunk.slices[i]
if neighborSlice ~= nil then
if neighborSlice then
neighborSlice:updateModel()
end
end
end

if sliceUpdates[i][3] then
local chunk = GetChunkRaw(self.x + 1, self.z)
if chunk ~= nil then
if chunk then
local neighborSlice = chunk.slices[i]
if neighborSlice ~= nil then
if neighborSlice then
neighborSlice:updateModel()
end
end
end

if sliceUpdates[i][4] or sliceUpdates[i][5] then
local chunk = GetChunkRaw(self.x, self.z - 1)
if chunk ~= nil then
if chunk then
local neighborSlice = chunk.slices[i]
if neighborSlice ~= nil then
if neighborSlice then
neighborSlice:updateModel()
end
end
end

if sliceUpdates[i][4] or sliceUpdates[i][5] then
local chunk = GetChunkRaw(self.x, self.z + 1)
if chunk ~= nil then
if chunk then
local neighborSlice = chunk.slices[i]
if neighborSlice ~= nil then
if neighborSlice then
neighborSlice:updateModel()
end
end
Expand Down
19 changes: 13 additions & 6 deletions src/world/updatelogic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,19 @@ function UpdateGame(dt)

local renderChunks = getRenderChunks(playerX, playerY, playerZ)
for _, chunk in ipairs(renderChunks) do
if chunk.isInitialized and chunk.active and #chunk.changes > 0 then
chunk:updateModel()
if chunk.isInitialized and chunk.active then
-- Only update the model if there are changes
if #chunk.changes > 0 then
chunk:updateModel()
end

for _, chunkSlice in ipairs(chunk.slices) do
if chunkSlice.active then
renderChunkSlice(chunkSlice, playerX, playerY, playerZ)
if not chunkSlice.alreadyrendered then
renderChunkSlice(chunkSlice, playerX, playerY, playerZ)
end
else
chunkSlice.alreadyrendered = false
end
end
end
Expand All @@ -38,7 +46,6 @@ function UpdateGame(dt)

-- update all things in ThingList update queue
updateThingList(dt)

-- update 3D scene with dt only if PhysicsStep is true
if PhysicsStep then
Scene:update()
Expand Down Expand Up @@ -99,7 +106,6 @@ function getRenderChunks(playerX, playerY, playerZ)

return renderChunks
end

function renderChunkSlice(chunkSlice, playerX, playerY, playerZ)
local model = {}

Expand All @@ -114,7 +120,7 @@ function renderChunkSlice(chunkSlice, playerX, playerY, playerZ)
(chunkSlice.x - 1) * ChunkSize + i - 1, 1 * j * scale, (chunkSlice.z - 1) * ChunkSize + k - 1

if thisTransparency < 3 then
--LuaCraftPrintLoggingNormal("Rendering voxel:", i, j, k, "at position:", x, y, z)
-- LuaCraftPrintLoggingNormal("Rendering voxel:", i, j, k, "at position:", x, y, z)
TileRendering(chunkSlice, i, j, k, x, y, z, thisLight, model, scale)
BlockRendering(chunkSlice, i, j, k, x, y, z, thisTransparency, thisLight, model, scale)
end
Expand All @@ -123,6 +129,7 @@ function renderChunkSlice(chunkSlice, playerX, playerY, playerZ)
end

chunkSlice.model:setVerts(model)
chunkSlice.alreadyrendered = true
end

function updateThingList(dt)
Expand Down

0 comments on commit f1bcea1

Please sign in to comment.