core: improved building system

This commit is contained in:
2026-01-06 23:44:07 +02:00
parent 0ac6d6e214
commit 97e142d3b2
6 changed files with 286 additions and 18 deletions

View File

@@ -7,6 +7,23 @@ Chunk.AllChunks = {} :: {[typeof("")]: typeof(Chunk.new(0,0,0))}
local RunService = game:GetService("RunService")
local function normalizeCoord(n)
if typeof(n) ~= "number" then
return n
end
if n >= 0 then
return math.floor(n + 0.5)
end
return math.ceil(n - 0.5)
end
local function keyFromCoords(x, y, z)
x = normalizeCoord(x)
y = normalizeCoord(y)
z = normalizeCoord(z)
return `{tostring(x)},{tostring(y)},{tostring(z)}`
end
local function Swait(l)
for i = 1,l do
RunService.Stepped:Wait()
@@ -33,6 +50,7 @@ function Chunk.new(x,y,z)
self.loaded = false
self.loading = false
self.delayedRemoval = {}
self.data = {} :: {[typeof("")]: BlockData} -- "X,Y,Z": BlockData ("-1,-1,1": BlockData)
return self
@@ -49,6 +67,7 @@ function Chunk.from(x,y,z,data)
self.UpdateBlockBindableL = Instance.new("BindableEvent") :: BindableEvent
self.data = data :: {[typeof("")]: BlockData} -- "X,Y,Z": BlockData ("-1,-1,1": BlockData)
self.delayedRemoval = {}
return self
end
@@ -123,20 +142,42 @@ end
function Chunk:GetBlockAt(x,y,z)
task.desynchronize()
if not self.data[`{tostring(x)},{tostring(y)},{tostring(z)}`] then
if not self.data[keyFromCoords(x, y, z)] then
return nil
end
return self.data[`{tostring(x)},{tostring(y)},{tostring(z)}`]
return self.data[keyFromCoords(x, y, z)]
end
function Chunk:CreateBlock(x: number,y: number,z: number,d:BlockData)
self.data[`{tostring(x)},{tostring(y)},{tostring(z)}`] = d
self.data[keyFromCoords(x, y, z)] = d
self:PropogateChanges(x,y,z,d)
return self:GetBlockAt(x,y,z)
end
function Chunk:RemoveBlock(x, y, z)
self.data[x .. "," .. y .. "," .. z] = nil
print("[DEBUG] Chunk:RemoveBlock called - Chunk:", self.pos, "Block coords:", x, y, z)
local blockKey = keyFromCoords(x, y, z)
local existingBlock = self.data[blockKey]
if existingBlock then
print("[DEBUG] Removing existing block with ID:", existingBlock.id)
else
print("[DEBUG] No block found at coords", x, y, z)
end
self.data[blockKey] = nil
self:PropogateChanges(x,y,z,0)
end
function Chunk:RemoveBlockSmooth(x, y, z)
print("[DEBUG] Chunk:RemoveBlockSmooth called - Chunk:", self.pos, "Block coords:", x, y, z)
local blockKey = keyFromCoords(x, y, z)
local existingBlock = self.data[blockKey]
if existingBlock then
print("[DEBUG] Smooth removing existing block with ID:", existingBlock.id)
else
print("[DEBUG] Smooth remove: no block found at coords", x, y, z)
end
self.data[blockKey] = nil
self.delayedRemoval[blockKey] = true
self:PropogateChanges(x,y,z,0)
end