Add helper functions to container. Allow name to be gotten

This commit is contained in:
Tjakka5 2020-01-04 13:40:18 +01:00
parent 6cd66e6737
commit 55ae5fd987
8 changed files with 97 additions and 2 deletions

View file

@ -14,6 +14,7 @@ function Assemblage.new(assemble)
local assemblage = setmetatable({
__assemble = assemble,
__name = nil,
__isAssemblage = true,
}, Assemblage.__mt)
@ -30,6 +31,18 @@ function Assemblage:assemble(e, ...)
return self
end
--- Returns true if the Assemblage has a name.
-- @return True if the Assemblage has a name, false otherwise
function Assemblage:hasName()
return self.__name and true or false
end
--- Returns the name of the Assemblage.
-- @return Name of the Assemblage
function Assemblage:getName()
return self.__name
end
return setmetatable(Assemblage, {
__call = function(_, ...)
return Assemblage.new(...)

View file

@ -24,8 +24,24 @@ function Assemblages.register(name, assemblage)
end
Assemblages[name] = assemblage
assemblage.__name = name
end
--- Returns true if the containter has the Assemblage with the name
-- @param name Name of the Assemblage to check
-- @return True if the containter has the Assemblage with the name, false otherwise
function Assemblages.has(name)
return Assemblages[name] and true or false
end
--- Returns the Assemblage with the name
-- @param name Name of the Assemblage to get
-- @return Assemblage with the name
function Assemblages.get(name)
return Assemblages[name]
end
return setmetatable(Assemblages, {
__index = function(_, name)
error("Attempt to index assemblage '"..tostring(name).."' that does not exist / was not registered", 2)

View file

@ -63,10 +63,14 @@ function Component:__initialize(...)
return component
end
--- Returns true if the Component has a name.
-- @return True if the Component has a name, false otherwise
function Component:hasName()
return self.__name and true or false
end
--- Returns the name of the Component.
-- @return Name of the Component
function Component:getName()
return self.__name
end

View file

@ -27,15 +27,20 @@ function Components.register(name, componentClass)
componentClass.__name = name
end
--- Returns true if the containter has the ComponentClass with the name
-- @param name Name of the ComponentClass to check
-- @return True if the containter has the ComponentClass with the name, false otherwise
function Components.has(name)
return Components[name] and true or false
end
--- Returns the ComponentClass with the name
-- @param name Name of the ComponentClass to get
-- @return ComponentClass with the name
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)

View file

@ -53,8 +53,10 @@ System.mt = {
-- @return A new SystemClass
function System.new(...)
local systemClass = setmetatable({
__isSystemClass = true,
__filter = {...},
__name = nil,
__isSystemClass = true,
}, System.mt)
systemClass.__index = systemClass
@ -178,6 +180,18 @@ function System:getWorld()
return self.__world
end
--- Returns true if the System has a name.
-- @return True if the System has a name, false otherwise
function System:hasName()
return self.__name and true or false
end
--- Returns the name of the System.
-- @return Name of the System
function System:getName()
return self.__name
end
--- Callback for system initialization.
-- @param world The World the System was added to
function System:init(world) -- luacheck: ignore

View file

@ -24,6 +24,21 @@ function Systems.register(name, systemClass)
end
Systems[name] = systemClass
systemClass.__name = name
end
--- Returns true if the containter has the SystemClass with the name
-- @param name Name of the SystemClass to check
-- @return True if the containter has the SystemClass with the name, false otherwise
function Systems.has(name)
return Systems[name] and true or false
end
--- Returns the SystemClass with the name
-- @param name Name of the SystemClass to get
-- @return SystemClass with the name
function Systems.get(name)
return Systems[name]
end
return setmetatable(Systems, {

View file

@ -33,6 +33,7 @@ function World.new()
__systemLookup = {},
__name = nil,
__isWorld = true,
}, World.__mt)
@ -316,6 +317,18 @@ function World:deserialize(data, append)
self:__flush()
end
--- Returns true if the World has a name.
-- @return True if the World has a name, false otherwise
function World:hasName()
return self.__name and true or false
end
--- Returns the name of the World.
-- @return Name of the World
function World:getName()
return self.__name
end
--- Callback for when an Entity is added to the World.
-- @param e The Entity that was added
function World:onEntityAdded(e) -- luacheck: ignore

View file

@ -24,6 +24,21 @@ function Worlds.register(name, world)
end
Worlds[name] = world
world.__name = name
end
--- Returns true if the containter has the World with the name
-- @param name Name of the World to check
-- @return True if the containter has the World with the name, false otherwise
function Worlds.has(name)
return Worlds[name] and true or false
end
--- Returns the World with the name
-- @param name Name of the World to get
-- @return World with the name
function Worlds.get(name)
return Worlds[name]
end
return setmetatable(Worlds, {