Allowed for entities to be in multiple systems. Fixed the removal issue.

This commit is contained in:
Justin van der Leij 2018-02-20 23:38:04 +01:00
parent c750ea119f
commit 100d6320c7
5 changed files with 28 additions and 10 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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