diff --git a/fluid/entity.lua b/fluid/entity.lua index e29259e..f706433 100644 --- a/fluid/entity.lua +++ b/fluid/entity.lua @@ -1,3 +1,7 @@ +local PATH = (...):gsub('%.[^%.]+$', '') + +local List = require(PATH..".list") + local Entity = {} Entity.__index = Entity @@ -6,7 +10,8 @@ Entity.__index = Entity function Entity.new() local e = setmetatable({ components = {}, - instance = nil, + removed = {}, + instances = List(), }, Entity) return e @@ -26,15 +31,27 @@ end -- @param component The Component to remove -- @return self function Entity:remove(component) - self.components[component] = nil + self.removed[component] = true return self end +function Entity:destroy() + for i = 1, self.instances.size do + self.instances:get(i):removeEntity(self) + end +end + --- Checks the Entity against the pools again. -- @return self -function Entity:check() - self.instance:checkEntity(self) +function Entity:apply() + for i = 1, self.instances.size do + self.instances:get(i):checkEntity(self) + end + + for _, component in pairs(self.removed) do + self.components[component] = nil + end return self end diff --git a/fluid/instance.lua b/fluid/instance.lua index dd6f42e..d185846 100644 --- a/fluid/instance.lua +++ b/fluid/instance.lua @@ -19,7 +19,7 @@ function Instance.new() end function Instance:addEntity(e) - e.instance = self + e.instances:add(self) self.entities:add(e) self:checkEntity(e) end @@ -31,6 +31,7 @@ function Instance:checkEntity(e) end function Instance:removeEntity(e) + e.instances:remove(self) self.entities:remove(e) for _, system in ipairs(self.systems) do diff --git a/fluid/pool.lua b/fluid/pool.lua index 2f3eaf8..ed60ca4 100644 --- a/fluid/pool.lua +++ b/fluid/pool.lua @@ -10,13 +10,13 @@ function Pool.new(name, filter) pool.name = name pool.filter = filter - + return pool end function Pool:eligible(e) for _, component in ipairs(self.filter) do - if not e.components[component] then + if not e.components[component] or e.removed[component] then return false end end diff --git a/fluid/system.lua b/fluid/system.lua index fee95ea..0775f88 100644 --- a/fluid/system.lua +++ b/fluid/system.lua @@ -86,7 +86,7 @@ function System:__tryAdd(e) self.__all[e] = self.__all[e] + 1 end -function System:__tryRemove() +function System:__tryRemove(e) if self:__has(e) then self.__all[e] = self.__all[e] - 1 diff --git a/main.lua b/main.lua index 23151aa..1150623 100644 --- a/main.lua +++ b/main.lua @@ -76,13 +76,13 @@ end function RandomRemover:update(dt) self.time = self.time + dt - if self.time >= 0.5 then + if self.time >= 0.25 then self.time = 0 if self.pool.size > 0 then local i = love.math.random(1, self.pool.size) - Game:removeEntity(self.pool.objects[i]) + self.pool:get(i):destroy() end end