Files
block-game/ReplicatedStorage/Shared/ChunkManager/BlockManager.lua
2026-01-08 22:58:58 +02:00

85 lines
2.3 KiB
Lua

--!native
--!optimize 2
local BlockManager = {}
BlockManager.BlockIdMappings = {} :: {BasePart}
for _,v in pairs(game:GetService("ReplicatedStorage"):WaitForChild("Blocks"):GetChildren()) do
BlockManager.BlockIdMappings[v:GetAttribute("n")] = v
end
BlockManager.UpdateIdMappings = {} :: {BasePart}
for _,v in pairs(game:GetService("ReplicatedStorage"):WaitForChild("BlockUpdateOperations"):GetChildren()) do
local success, reason = pcall(function()
BlockManager.UpdateIdMappings[v:GetAttribute("n")] = require(v)
end)
if not success then
warn("[BLOCKMANAGER] Invalid update operation",v:GetAttribute("n"),":",reason)
end
end
local warnedBlockIds = {}
function BlockManager:GetBlock(blockId: number, attr: {[typeof("")]: any}?)
if not BlockManager.BlockIdMappings[blockId] then
if not warnedBlockIds[blockId] then
warnedBlockIds[blockId] = true
warn("[BLOCKMANAGER] Invalid block id",blockId)
end
return script.invalid:Clone()
end
local b = BlockManager.BlockIdMappings[blockId]:Clone()
b.Size = Vector3.new(3.95,3.95,3.95)
for _,v in pairs(attr or {}) do
b:SetAttribute(_,v)
end
if BlockManager.UpdateIdMappings[blockId] then
local success, reason = pcall(function()
BlockManager.UpdateIdMappings[blockId](b)
end)
if not success then
warn("[BLOCKMANAGER] Failed update operation",blockId,":",reason)
end
end
return b
end
-- ChatGPT Generated Func!!!!
function BlockManager:GetBlockRotated(blockId: number, face: Enum.NormalId, attr: {[typeof("")]: any}?)
-- Returns block with id blockId, rotated so the given face (NormalId) points north (+X).
local block = BlockManager:GetBlock(blockId, attr)
local rot = CFrame.new()
if face == Enum.NormalId.Front then
rot = CFrame.Angles(0, 0, 0) -- no rot
elseif face == Enum.NormalId.Back then
rot = CFrame.Angles(0, math.rad(180), 0)
elseif face == Enum.NormalId.Left then
rot = CFrame.Angles(0, math.rad(90), 0)
elseif face == Enum.NormalId.Right then
rot = CFrame.Angles(0, math.rad(-90), 0)
elseif face == Enum.NormalId.Top then
rot = CFrame.Angles(math.rad(-90), 0, 0) -- top +x
elseif face == Enum.NormalId.Bottom then
rot = CFrame.Angles(math.rad(90), 0, 0) -- bottom +x
end
-- ocbwoy3's fix
block:PivotTo(rot)
block.CFrame = CFrame.new(rot.Position)
return block
end
return BlockManager