diff --git a/main.lua b/main.lua index 6b9f293..f155a83 100644 --- a/main.lua +++ b/main.lua @@ -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 diff --git a/src/component.lua b/src/component.lua index e1ef5aa..fd532ea 100644 --- a/src/component.lua +++ b/src/component.lua @@ -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, { diff --git a/src/components.lua b/src/components.lua index 63d40e0..2249774 100644 --- a/src/components.lua +++ b/src/components.lua @@ -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 }) \ No newline at end of file diff --git a/src/entity.lua b/src/entity.lua index 25b063e..98d1c97 100644 --- a/src/entity.lua +++ b/src/entity.lua @@ -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() diff --git a/src/type.lua b/src/type.lua index 3da59d9..a7ba264 100644 --- a/src/type.lua +++ b/src/type.lua @@ -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