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 function onEntityAdded(pool, e) -- luacheck: ignore
local test_comp = e:get(Components.test_comp_1)
print(test_comp.x)
print("Added")
end
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)
end
if not (populate == nil or type(populate) == "function") then
error("bad argument #2 to 'Component.new' (function/nil expected, got "..type(populate)..")", 2)
if not (type(populate) == "function") then
error("bad argument #2 to 'Component.new' (function expected, got "..type(populate)..")", 2)
end
local component = setmetatable({
local baseComponent = setmetatable({
__name = name,
__populate = populate,
__isComponent = true,
__isBaseComponent = true,
}, Component)
component.__mt = {__index = component}
baseComponent.__mt = {__index = baseComponent}
Components.register(name, component)
Components.register(name, baseComponent)
return component
return baseComponent
end
--- Creates and initializes a new Bag.
--- Creates and initializes a new Component.
-- @param ... The values passed to the populate function
-- @return A new initialized Bag
-- @return A new initialized Component
function Component:__initialize(...)
if self.__populate then
local bag = setmetatable({
__baseComponent = self,
}, self)
local component = setmetatable({
__baseComponent = self,
self.__populate(bag, ...)
__isComponent = true,
__isBaseComponent = false,
}, self)
return bag
end
self.__populate(component, ...)
return true
return component
end
return setmetatable(Component, {

View file

@ -6,24 +6,24 @@ local Type = require(PATH..".type")
local Components = {}
function Components.register(name, component)
function Components.register(name, baseComponent)
if (type(name) ~= "string") then
error("bad argument #1 to 'Components.register' (string expected, got "..type(name)..")", 3)
end
if (not Type.isComponent(component)) then
error("bad argument #2 to 'Components.register' (component expected, got "..type(component)..")", 3)
if (not Type.isBaseComponent(baseComponent)) then
error("bad argument #2 to 'Components.register' (BaseComponent expected, got "..type(baseComponent)..")", 3)
end
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
Components[name] = component
Components[name] = baseComponent
end
return setmetatable(Components, {
__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
})

View file

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

View file

@ -2,14 +2,18 @@
local Type = {}
function Type.isComponent(t)
return type(t) == "table" and t.__isComponent or false
end
function Type.isEntity(t)
return type(t) == "table" and t.__isEntity or false
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)
return type(t) == "table" and t.__isBaseSystem or false
end