mirror of
https://github.com/Keyslam-Group/Concord.git
synced 2025-08-20 21:38:29 -04:00
main changes: - add parameter and return types where applicable - use @module and @classmod tags at the top of files - remove some redundant descriptions of return values, especially for functions that return a boolean recommended next steps: - more consistent grammar - add links to classes and functions in descriptions where appropriate - be consistent about naming Systems vs. SystemClasses and Components vs. ComponentClasses
48 lines
1.5 KiB
Lua
48 lines
1.5 KiB
Lua
--- Container for registered ComponentClasses
|
|
-- @module Components
|
|
|
|
local PATH = (...):gsub('%.[^%.]+$', '')
|
|
|
|
local Type = require(PATH..".type")
|
|
|
|
local Components = {}
|
|
|
|
--- Registers a ComponentClass.
|
|
-- @string name Name to register under
|
|
-- @tparam Component componentClass ComponentClass to register
|
|
function Components.register(name, componentClass)
|
|
if (type(name) ~= "string") then
|
|
error("bad argument #1 to 'Components.register' (string expected, got "..type(name)..")", 3)
|
|
end
|
|
|
|
if (not Type.isComponentClass(componentClass)) then
|
|
error("bad argument #2 to 'Components.register' (ComponentClass expected, got "..type(componentClass)..")", 3)
|
|
end
|
|
|
|
if (rawget(Components, name)) then
|
|
error("bad argument #2 to 'Components.register' (ComponentClass with name '"..name.."' was already registerd)", 3) -- luacheck: ignore
|
|
end
|
|
|
|
Components[name] = componentClass
|
|
componentClass.__name = name
|
|
end
|
|
|
|
--- Returns true if the containter has the ComponentClass with the specified name
|
|
-- @string name Name of the ComponentClass to check
|
|
-- @treturn boolean
|
|
function Components.has(name)
|
|
return Components[name] and true or false
|
|
end
|
|
|
|
--- Returns the ComponentClass with the specified name
|
|
-- @string name Name of the ComponentClass to get
|
|
-- @treturn Component
|
|
function Components.get(name)
|
|
return Components[name]
|
|
end
|
|
|
|
return setmetatable(Components, {
|
|
__index = function(_, name)
|
|
error("Attempt to index ComponentClass '"..tostring(name).."' that does not exist / was not registered", 2)
|
|
end
|
|
})
|