Transform NodeFactory to Factory
This commit is contained in:
5
mods/CORE/Factory/init.lua
Normal file
5
mods/CORE/Factory/init.lua
Normal file
@@ -0,0 +1,5 @@
|
||||
local Factory = Mod:new()
|
||||
|
||||
Factory.Class = Factory.require('Factory')
|
||||
|
||||
Api.addModToGlobalSpace(Factory, 'Core.Factory')
|
||||
1
mods/CORE/Factory/mod.conf
Normal file
1
mods/CORE/Factory/mod.conf
Normal file
@@ -0,0 +1 @@
|
||||
name = smc__core__factory
|
||||
38
mods/CORE/Factory/src/Factory.lua
Normal file
38
mods/CORE/Factory/src/Factory.lua
Normal file
@@ -0,0 +1,38 @@
|
||||
---@class Factory
|
||||
---@field defaultItemDef Item.ItemDefinition | Node.NodeDefinition
|
||||
---@field itemClass Item | Node
|
||||
local Factory = {}
|
||||
|
||||
---@param itemClass Item | Node
|
||||
---@param defaultItemDef Item.ItemDefinition | Node.NodeDefinition Parameters that all nodes registered with this factory must have.
|
||||
---@return Factory
|
||||
function Factory:new(itemClass, defaultItemDef)
|
||||
---@type Factory
|
||||
local instance = setmetatable({
|
||||
defaultItemDef = defaultItemDef,
|
||||
itemClass = itemClass,
|
||||
}, {__index = self})
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
---The aleas `table.merge` for EmmyLua
|
||||
---@type fun(...):(Item.ItemDefinition)
|
||||
local defMerge = function(...)
|
||||
return table.merge(..., true)
|
||||
end
|
||||
|
||||
---@param itemDefs Item.ItemDefinition[] | Node.NodeDefinition[] List of nodes with parameters unique to them.
|
||||
function Factory:registerItems(itemDefs)
|
||||
---@param nodeDef Item.ItemDefinition | Node.NodeDefinition
|
||||
for _, nodeDef in ipairs(itemDefs) do
|
||||
|
||||
---@type Item.ItemDefinition
|
||||
local nodeDef = defMerge(nodeDef, self.defaultItemDef)
|
||||
|
||||
self.itemClass:new(nodeDef)
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
return Factory
|
||||
Reference in New Issue
Block a user