Streamline entity lifetime

This commit is contained in:
Tjakka5 2019-12-19 08:47:38 +01:00
parent bc47eaa651
commit 038111d558
7 changed files with 117 additions and 163 deletions

View file

@ -14,9 +14,6 @@ Pool.__index = Pool
function Pool.new(name, filter)
local pool = setmetatable(List(), Pool)
pool.added = {}
pool.removed = {}
pool.name = name
pool.filter = filter
@ -25,18 +22,12 @@ function Pool.new(name, filter)
return pool
end
function Pool:flush()
for i = 1, math.max(#self.added, #self.removed) do
self.added[i], self.removed[i] = nil, nil
end
end
--- Checks if an Entity is eligible for the Pool.
-- @param e The Entity to check
-- @return True if the entity is eligible, false otherwise
function Pool:eligible(e)
for _, component in ipairs(self.filter) do
if not e[component] or e.removed[component] then
if not e[component] then
return false
end
end
@ -44,6 +35,22 @@ function Pool:eligible(e)
return true
end
function Pool:add(e)
List.add(self, e)
self:onEntityAdded(e)
end
function Pool:remove(e)
List.remove(self, e)
self:onEntityRemoved(e)
end
function Pool:onEntityAdded(e)
end
function Pool:onEntityRemoved(e)
end
return setmetatable(Pool, {
__index = List,
__call = function(_, ...)