core: improved building system
This commit is contained in:
@@ -2,6 +2,7 @@ local PlacementManager = {}
|
||||
|
||||
local ChunkManager = require("./ChunkManager")
|
||||
local Util = require("./Util")
|
||||
local RunService = game:GetService("RunService")
|
||||
|
||||
PlacementManager.ChunkFolder = ChunkManager.ChunkFolder
|
||||
|
||||
@@ -29,6 +30,7 @@ end
|
||||
|
||||
local Mouse: Mouse = nil
|
||||
local lastNormalId: Enum.NormalId? = nil
|
||||
local pendingBreakResync = {}
|
||||
|
||||
local function normalIdToOffset(normal: Enum.NormalId): Vector3
|
||||
if normal == Enum.NormalId.Top then
|
||||
@@ -121,27 +123,58 @@ function PlacementManager:PlaceBlock(cx, cy, cz, x, y, z, blockId: string)
|
||||
--print("placeblock")
|
||||
--local chunk = ChunkManager:GetChunk(cx, cy, cz)
|
||||
--chunk:CreateBlock(x, y, z, blockData)
|
||||
task.synchronize()
|
||||
placeRemote:FireServer(cx, cy, cz, x, y, z, blockId)
|
||||
task.desynchronize()
|
||||
end
|
||||
|
||||
-- FIRES REMOTE
|
||||
function PlacementManager:BreakBlock(cx, cy, cz, x, y, z)
|
||||
--print("breakblock")
|
||||
--local chunk = ChunkManager:GetChunk(cx, cy, cz)
|
||||
--chunk:RemoveBlock(x, y, z)
|
||||
print("[DEBUG] PlacementManager:BreakBlock called - Chunk:", cx, cy, cz, "Block:", x, y, z)
|
||||
local chunk = ChunkManager:GetChunk(cx, cy, cz)
|
||||
if chunk and not chunk:GetBlockAt(x, y, z) then
|
||||
print("[DEBUG] Client missing block; resyncing nearby chunks")
|
||||
ChunkManager:ResyncAroundChunk(cx, cy, cz, 1)
|
||||
task.defer(function()
|
||||
task.synchronize()
|
||||
RunService.RenderStepped:Wait()
|
||||
task.desynchronize()
|
||||
local refreshed = ChunkManager:GetChunk(cx, cy, cz)
|
||||
if refreshed and refreshed:GetBlockAt(x, y, z) then
|
||||
task.synchronize()
|
||||
breakRemote:FireServer(cx, cy, cz, x, y, z)
|
||||
task.desynchronize()
|
||||
print("[DEBUG] BreakBlock remote fired to server after resync")
|
||||
end
|
||||
end)
|
||||
return
|
||||
end
|
||||
task.synchronize()
|
||||
breakRemote:FireServer(cx, cy, cz, x, y, z)
|
||||
task.desynchronize()
|
||||
print("[DEBUG] BreakBlock remote fired to server")
|
||||
end
|
||||
|
||||
-- CLIENTSIDED
|
||||
function PlacementManager:PlaceBlockLocal(cx, cy, cz, x, y, z, blockData)
|
||||
-- CLIENTSIDED: only apply server-validated changes
|
||||
local function applyPlaceBlockLocal(cx, cy, cz, x, y, z, blockData)
|
||||
local chunk = ChunkManager:GetChunk(cx, cy, cz)
|
||||
chunk:CreateBlock(x, y, z, blockData)
|
||||
end
|
||||
|
||||
-- CLIENTSIDED
|
||||
function PlacementManager:BreakBlockLocal(cx, cy, cz, x, y, z)
|
||||
-- CLIENTSIDED: only apply server-validated changes
|
||||
local function applyBreakBlockLocal(cx, cy, cz, x, y, z)
|
||||
print("[DEBUG] PlacementManager:BreakBlockLocal called - Chunk:", cx, cy, cz, "Block:", x, y, z)
|
||||
local chunk = ChunkManager:GetChunk(cx, cy, cz)
|
||||
chunk:RemoveBlock(x, y, z)
|
||||
if chunk then
|
||||
print("[DEBUG] Found chunk, calling RemoveBlock")
|
||||
if chunk.RemoveBlockSmooth then
|
||||
chunk:RemoveBlockSmooth(x, y, z)
|
||||
else
|
||||
chunk:RemoveBlock(x, y, z)
|
||||
end
|
||||
else
|
||||
print("[DEBUG] Chunk not found at coords:", cx, cy, cz)
|
||||
end
|
||||
end
|
||||
|
||||
function PlacementManager:GetBlockAtMouse(): nil | {chunk:Vector3, block: Vector3}
|
||||
@@ -196,7 +229,7 @@ function PlacementManager:GetPlacementAtMouse(): nil | {chunk:Vector3, block: Ve
|
||||
end
|
||||
|
||||
function PlacementManager:Init()
|
||||
game:GetService("RunService").Heartbeat:Connect(function()
|
||||
game:GetService("RunService").RenderStepped:Connect(function()
|
||||
local a,b = pcall(function()
|
||||
PlacementManager:Raycast()
|
||||
end)
|
||||
@@ -210,10 +243,24 @@ function PlacementManager:Init()
|
||||
tickRemote.OnClientEvent:Connect(function(m, cx, cy, cz, x, y, z, d)
|
||||
--warn("PROPOGATED TICK", m, cx, cy, cz, x, y, z, d)
|
||||
if m == "B_C" then
|
||||
PlacementManager:PlaceBlockLocal(cx, cy, cz, x, y ,z, d)
|
||||
applyPlaceBlockLocal(cx, cy, cz, x, y ,z, d)
|
||||
end
|
||||
if m == "B_D" then
|
||||
PlacementManager:BreakBlockLocal(cx, cy, cz, x, y ,z)
|
||||
applyBreakBlockLocal(cx, cy, cz, x, y ,z)
|
||||
local key = `{cx},{cy},{cz}`
|
||||
if not pendingBreakResync[key] then
|
||||
pendingBreakResync[key] = true
|
||||
task.defer(function()
|
||||
task.synchronize()
|
||||
RunService.RenderStepped:Wait()
|
||||
task.desynchronize()
|
||||
pendingBreakResync[key] = nil
|
||||
ChunkManager:ResyncAroundChunk(cx, cy, cz, 1)
|
||||
end)
|
||||
end
|
||||
end
|
||||
if m == "C_R" then
|
||||
ChunkManager:RefreshChunk(cx, cy, cz)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user