minetest-mods/xdecor/handlers/nodeboxes.lua

68 lines
1.5 KiB
Lua
Raw Normal View History

2019-12-14 17:47:31 +01:00
xdecor.box = {
slab_y = function(height, shift)
2021-05-15 22:34:12 +02:00
return {
-0.5,
-0.5 + (shift or 0),
-0.5,
0.5,
-0.5 + height + (shift or 0),
0.5
}
2019-12-14 17:47:31 +01:00
end,
slab_z = function(depth)
return {-0.5, -0.5, -0.5 + depth, 0.5, 0.5, 0.5}
end,
bar_y = function(radius)
return {-radius, -0.5, -radius, radius, 0.5, radius}
end,
cuboid = function(radius_x, radius_y, radius_z)
return {-radius_x, -radius_y, -radius_z, radius_x, radius_y, radius_z}
end
}
xdecor.nodebox = {
2021-05-15 22:34:12 +02:00
regular = {type = "regular"},
null = {
type = "fixed", fixed = {0,0,0,0,0,0}
}
2019-12-14 17:47:31 +01:00
}
xdecor.pixelbox = function(size, boxes)
local fixed = {}
2021-05-15 22:34:12 +02:00
for _, box in ipairs(boxes) do
2019-12-14 17:47:31 +01:00
-- `unpack` has been changed to `table.unpack` in newest Lua versions.
local x, y, z, w, h, l = unpack(box)
2021-05-15 22:34:12 +02:00
fixed[#fixed + 1] = {
2019-12-14 17:47:31 +01:00
(x / size) - 0.5,
(y / size) - 0.5,
(z / size) - 0.5,
((x + w) / size) - 0.5,
((y + h) / size) - 0.5,
((z + l) / size) - 0.5
}
end
2021-05-15 22:34:12 +02:00
return {type = "fixed", fixed = fixed}
2019-12-14 17:47:31 +01:00
end
local mt = {}
2021-05-15 22:34:12 +02:00
2019-12-14 17:47:31 +01:00
mt.__index = function(table, key)
local ref = xdecor.box[key]
local ref_type = type(ref)
if ref_type == "function" then
return function(...)
2021-05-15 22:34:12 +02:00
return {type = "fixed", fixed = ref(...)}
2019-12-14 17:47:31 +01:00
end
elseif ref_type == "table" then
2021-05-15 22:34:12 +02:00
return {type = "fixed", fixed = ref}
2019-12-14 17:47:31 +01:00
elseif ref_type == "nil" then
2021-05-15 22:34:12 +02:00
error(key .. "could not be found among nodebox presets and functions")
2019-12-14 17:47:31 +01:00
end
2021-05-15 22:34:12 +02:00
error("unexpected datatype " .. tostring(type(ref)) .. " while looking for " .. key)
2019-12-14 17:47:31 +01:00
end
setmetatable(xdecor.nodebox, mt)