Add Entity:getComponents

This commit is contained in:
Tjakka5 2019-12-21 12:00:48 +01:00
parent 073aadf74b
commit bb508ee947

View file

@ -13,6 +13,8 @@ function Entity.new()
local e = setmetatable({ local e = setmetatable({
world = nil, world = nil,
__components = {},
__isDirty = true, __isDirty = true,
__wasAdded = false, __wasAdded = false,
__wasRemoved = false, __wasRemoved = false,
@ -23,15 +25,18 @@ function Entity.new()
return e return e
end end
local function give(e, component, ...) local function give(e, baseComponent, ...)
local comp = component:__initialize(...) local component = baseComponent:__initialize(...)
e[component] = comp
e[baseComponent] = component
e.__components[baseComponent] = component
e.__isDirty = true e.__isDirty = true
end end
local function remove(e, component) local function remove(e, baseComponent)
e[component] = nil e[baseComponent] = nil
e.__components[baseComponent] = nil
e.__isDirty = true e.__isDirty = true
end end
@ -119,6 +124,10 @@ function Entity:has(component)
return self[component] ~= nil return self[component] ~= nil
end end
function Entity:getComponents()
return self.__components
end
return setmetatable(Entity, { return setmetatable(Entity, {
__call = function(_, ...) __call = function(_, ...)
return Entity.new(...) return Entity.new(...)