From 8f1593e4e8bef498b7021e330e3558545c9543ee Mon Sep 17 00:00:00 2001 From: Jude Melton-Houghton Date: Sat, 22 Oct 2022 08:05:45 -0400 Subject: [PATCH] Add VoxelArea() constructor (#12886) --- builtin/game/voxelarea.lua | 11 ++++++++++- doc/lua_api.txt | 5 +++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/builtin/game/voxelarea.lua b/builtin/game/voxelarea.lua index 62f07d928..a9195213b 100644 --- a/builtin/game/voxelarea.lua +++ b/builtin/game/voxelarea.lua @@ -8,7 +8,10 @@ VoxelArea = { zstride = 0, } -function VoxelArea:new(o) +local class_metatable = {} +setmetatable(VoxelArea, class_metatable) + +local function new(self, o) o = o or {} setmetatable(o, self) self.__index = self @@ -20,6 +23,12 @@ function VoxelArea:new(o) return o end +function class_metatable:__call(MinEdge, MaxEdge) + return new(self, {MinEdge = MinEdge, MaxEdge = MaxEdge}) +end + +VoxelArea.new = new + function VoxelArea:getExtent() local MaxEdge, MinEdge = self.MaxEdge, self.MinEdge return vector_new( diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 4e3d1230c..fd4e2d3e5 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -4485,7 +4485,8 @@ Methods ----------- A helper class for voxel areas. -It can be created via `VoxelArea:new({MinEdge = pmin, MaxEdge = pmax})`. +It can be created via `VoxelArea(pmin, pmax)` or +`VoxelArea:new({MinEdge = pmin, MaxEdge = pmax})`. The coordinates are *inclusive*, like most other things in Minetest. ### Methods @@ -4533,7 +4534,7 @@ the axes in a voxel area: If, for example: - local area = VoxelArea:new({MinEdge = emin, MaxEdge = emax}) + local area = VoxelArea(emin, emax) The values of `ystride` and `zstride` can be obtained using `area.ystride` and `area.zstride`.