Class: add getInstance(). MapGen: fixes :new() methods. #18

This commit is contained in:
Van
2026-01-20 19:06:53 +03:00
parent c32d324bfb
commit ec614e6080
11 changed files with 72 additions and 33 deletions

View File

@@ -1,3 +1,5 @@
local setMetaTable, TableCopy = setmetatable, table.copy_with_metatables
---@class Class
Class = {}
@@ -5,5 +7,25 @@ Class = {}
---@param parent table
---@param child table
function Class.extend(parent ,child)
return setmetatable(child or {}, { __index = parent })
return setmetatable(child or {}, { __index = TableCopy(parent) })
end
---Intended for class constructors.
---
---Correctly creates an object of the class using `setmetatable()`.
---@generic T:table
---@param self T Parent class.
---@param params table Child object parameters.
---@param metatable table? The table that will become the metatable for the object. Note: The `__index` field will be overwritten by a copy of `self`.
---@return T
function Class.getInstance(self, params, metatable)
if metatable == nil then
metatable = {}
end
metatable.__index = TableCopy(self)
local instance = setMetaTable(params, metatable)
return instance
end