nodeboxes.lua 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. xdecor.box = {
  2. slab_y = function(height, shift)
  3. return {
  4. -0.5,
  5. -0.5 + (shift or 0),
  6. -0.5,
  7. 0.5,
  8. -0.5 + height + (shift or 0),
  9. 0.5
  10. }
  11. end,
  12. slab_z = function(depth)
  13. return {-0.5, -0.5, -0.5 + depth, 0.5, 0.5, 0.5}
  14. end,
  15. bar_y = function(radius)
  16. return {-radius, -0.5, -radius, radius, 0.5, radius}
  17. end,
  18. cuboid = function(radius_x, radius_y, radius_z)
  19. return {-radius_x, -radius_y, -radius_z, radius_x, radius_y, radius_z}
  20. end
  21. }
  22. xdecor.nodebox = {
  23. regular = {type = "regular"},
  24. null = {
  25. type = "fixed", fixed = {0,0,0,0,0,0}
  26. }
  27. }
  28. xdecor.pixelbox = function(size, boxes)
  29. local fixed = {}
  30. for _, box in ipairs(boxes) do
  31. -- `unpack` has been changed to `table.unpack` in newest Lua versions.
  32. local x, y, z, w, h, l = unpack(box)
  33. fixed[#fixed + 1] = {
  34. (x / size) - 0.5,
  35. (y / size) - 0.5,
  36. (z / size) - 0.5,
  37. ((x + w) / size) - 0.5,
  38. ((y + h) / size) - 0.5,
  39. ((z + l) / size) - 0.5
  40. }
  41. end
  42. return {type = "fixed", fixed = fixed}
  43. end
  44. local mt = {}
  45. mt.__index = function(table, key)
  46. local ref = xdecor.box[key]
  47. local ref_type = type(ref)
  48. if ref_type == "function" then
  49. return function(...)
  50. return {type = "fixed", fixed = ref(...)}
  51. end
  52. elseif ref_type == "table" then
  53. return {type = "fixed", fixed = ref}
  54. elseif ref_type == "nil" then
  55. error(key .. "could not be found among nodebox presets and functions")
  56. end
  57. error("unexpected datatype " .. tostring(type(ref)) .. " while looking for " .. key)
  58. end
  59. setmetatable(xdecor.nodebox, mt)