Clean up component vs baseComponent

This commit is contained in:
Tjakka5 2019-12-22 21:08:30 +01:00
parent f6536d5a0e
commit e0f88025ba
5 changed files with 52 additions and 50 deletions

View file

@ -36,8 +36,7 @@ end)
local test_system = System("test_system", {Components.test_comp_1}) local test_system = System("test_system", {Components.test_comp_1})
local function onEntityAdded(pool, e) -- luacheck: ignore local function onEntityAdded(pool, e) -- luacheck: ignore
local test_comp = e:get(Components.test_comp_1) print("Added")
print(test_comp.x)
end end
local function onEntityRemoved(pool, e) -- luacheck: ignore local function onEntityRemoved(pool, e) -- luacheck: ignore

View file

@ -15,39 +15,38 @@ function Component.new(name, populate)
error("bad argument #1 to 'Component.new' (string expected, got "..type(name)..")", 2) error("bad argument #1 to 'Component.new' (string expected, got "..type(name)..")", 2)
end end
if not (populate == nil or type(populate) == "function") then if not (type(populate) == "function") then
error("bad argument #2 to 'Component.new' (function/nil expected, got "..type(populate)..")", 2) error("bad argument #2 to 'Component.new' (function expected, got "..type(populate)..")", 2)
end end
local component = setmetatable({ local baseComponent = setmetatable({
__name = name, __name = name,
__populate = populate, __populate = populate,
__isComponent = true, __isBaseComponent = true,
}, Component) }, Component)
component.__mt = {__index = component} baseComponent.__mt = {__index = baseComponent}
Components.register(name, component) Components.register(name, baseComponent)
return component return baseComponent
end end
--- Creates and initializes a new Bag. --- Creates and initializes a new Component.
-- @param ... The values passed to the populate function -- @param ... The values passed to the populate function
-- @return A new initialized Bag -- @return A new initialized Component
function Component:__initialize(...) function Component:__initialize(...)
if self.__populate then local component = setmetatable({
local bag = setmetatable({
__baseComponent = self, __baseComponent = self,
__isComponent = true,
__isBaseComponent = false,
}, self) }, self)
self.__populate(bag, ...) self.__populate(component, ...)
return bag return component
end
return true
end end
return setmetatable(Component, { return setmetatable(Component, {

View file

@ -6,24 +6,24 @@ local Type = require(PATH..".type")
local Components = {} local Components = {}
function Components.register(name, component) function Components.register(name, baseComponent)
if (type(name) ~= "string") then if (type(name) ~= "string") then
error("bad argument #1 to 'Components.register' (string expected, got "..type(name)..")", 3) error("bad argument #1 to 'Components.register' (string expected, got "..type(name)..")", 3)
end end
if (not Type.isComponent(component)) then if (not Type.isBaseComponent(baseComponent)) then
error("bad argument #2 to 'Components.register' (component expected, got "..type(component)..")", 3) error("bad argument #2 to 'Components.register' (BaseComponent expected, got "..type(baseComponent)..")", 3)
end end
if (rawget(Components, name)) then if (rawget(Components, name)) then
error("bad argument #2 to 'Components.register' (Component with name '"..name.."' is already registerd)", 3) error("bad argument #2 to 'Components.register' (BaseComponent with name '"..name.."' is already registerd)", 3)
end end
Components[name] = component Components[name] = baseComponent
end end
return setmetatable(Components, { return setmetatable(Components, {
__index = function(_, name) __index = function(_, name)
error("Attempt to index component '"..tostring(name).."' that does not exist / was not registered", 2) error("Attempt to index BaseComponent '"..tostring(name).."' that does not exist / was not registered", 2)
end end
}) })

View file

@ -61,26 +61,26 @@ end
-- @param component The Component to add -- @param component The Component to add
-- @param ... The values passed to the Component -- @param ... The values passed to the Component
-- @return self -- @return self
function Entity:give(component, ...) function Entity:give(baseComponent, ...)
if not Type.isComponent(component) then if not Type.isBaseComponent(baseComponent) then
error("bad argument #1 to 'Entity:give' (Component expected, got "..type(component)..")", 2) error("bad argument #1 to 'Entity:give' (BaseComponent expected, got "..type(baseComponent)..")", 2)
end end
give(self, component, ...) give(self, baseComponent, ...)
return self return self
end end
function Entity:ensure(component, ...) function Entity:ensure(baseComponent, ...)
if not Type.isComponent(component) then if not Type.isBaseComponent(baseComponent) then
error("bad argument #1 to 'Entity:ensure' (Component expected, got "..type(component)..")", 2) error("bad argument #1 to 'Entity:ensure' (BaseComponent expected, got "..type(baseComponent)..")", 2)
end end
if self[component] then if self[baseComponent] then
return self return self
end end
give(self, component, ...) give(self, baseComponent, ...)
return self return self
end end
@ -88,12 +88,12 @@ end
--- Removes a component from an Entity. --- Removes a component from an Entity.
-- @param component The Component to remove -- @param component The Component to remove
-- @return self -- @return self
function Entity:remove(component) function Entity:remove(baseComponent)
if not Type.isComponent(component) then if not Type.isBaseComponent(baseComponent) then
error("bad argument #1 to 'Entity:remove' (Component expected, got "..type(component)..")") error("bad argument #1 to 'Entity:remove' (BaseComponent expected, got "..type(baseComponent)..")")
end end
remove(self, component) remove(self, baseComponent)
return self return self
end end
@ -141,23 +141,23 @@ end
--- Gets a Component from the Entity. --- Gets a Component from the Entity.
-- @param component The Component to get -- @param component The Component to get
-- @return The Bag from the Component -- @return The Bag from the Component
function Entity:get(component) function Entity:get(baseComponent)
if not Type.isComponent(component) then if not Type.isBaseComponent(baseComponent) then
error("bad argument #1 to 'Entity:get' (Component expected, got "..type(component)..")") error("bad argument #1 to 'Entity:get' (BaseComponent expected, got "..type(baseComponent)..")")
end end
return self[component] return self[baseComponent]
end end
--- Returns true if the Entity has the Component. --- Returns true if the Entity has the Component.
-- @param component The Component to check against -- @param component The Component to check against
-- @return True if the entity has the Bag. False otherwise -- @return True if the entity has the Bag. False otherwise
function Entity:has(component) function Entity:has(baseComponent)
if not Type.isComponent(component) then if not Type.isBaseComponent(baseComponent) then
error("bad argument #1 to 'Entity:has' (Component expected, got "..type(component)..")") error("bad argument #1 to 'Entity:has' (BaseComponent expected, got "..type(baseComponent)..")")
end end
return self[component] ~= nil return self[baseComponent] ~= nil
end end
function Entity:getComponents() function Entity:getComponents()

View file

@ -2,14 +2,18 @@
local Type = {} local Type = {}
function Type.isComponent(t)
return type(t) == "table" and t.__isComponent or false
end
function Type.isEntity(t) function Type.isEntity(t)
return type(t) == "table" and t.__isEntity or false return type(t) == "table" and t.__isEntity or false
end end
function Type.isBaseComponent(t)
return type(t) == "table" and t.__isBaseComponent or false
end
function Type.isComponent(t)
return type(t) == "table" and t.__isComponent or false
end
function Type.isBaseSystem(t) function Type.isBaseSystem(t)
return type(t) == "table" and t.__isBaseSystem or false return type(t) == "table" and t.__isBaseSystem or false
end end