mirror of
https://github.com/Keyslam-Group/Concord.git
synced 2025-09-02 04:13:58 -04:00
Removed the need for manual flushing. Systems now know about their instance
This commit is contained in:
parent
358a455c8b
commit
acd77fafa5
3 changed files with 23 additions and 19 deletions
|
@ -52,18 +52,20 @@ end
|
|||
--- Completely removes all marked Entities in the Instance.
|
||||
-- @return self
|
||||
function Instance:flush()
|
||||
for i = 1, #self.removed do
|
||||
local e = self.removed[i]
|
||||
if #self.removed > 0 then
|
||||
for i = 1, #self.removed do
|
||||
local e = self.removed[i]
|
||||
|
||||
e.instances:remove(self)
|
||||
self.entities:remove(e)
|
||||
e.instances:remove(self)
|
||||
self.entities:remove(e)
|
||||
|
||||
for i = 1, self.systems.size do
|
||||
self.systems:get(i):__remove(e)
|
||||
for i = 1, self.systems.size do
|
||||
self.systems:get(i):__remove(e)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
self.removed = {}
|
||||
self.removed = {}
|
||||
end
|
||||
|
||||
return self
|
||||
end
|
||||
|
@ -75,8 +77,13 @@ end
|
|||
-- @param enabled If the system is enabled. Defaults to true
|
||||
-- @return self
|
||||
function Instance:addSystem(system, eventName, callback, enabled)
|
||||
if system.__instance then
|
||||
error("System already in instance '" ..system.__instance.."'")
|
||||
end
|
||||
|
||||
if not self.systems:has(system) then
|
||||
self.systems:add(system)
|
||||
system.__instance = self
|
||||
end
|
||||
|
||||
if eventName then
|
||||
|
@ -141,6 +148,8 @@ end
|
|||
-- @param ... Parameters passed to listeners
|
||||
-- @return self
|
||||
function Instance:emit(eventName, ...)
|
||||
self:flush()
|
||||
|
||||
local listeners = self.events[eventName]
|
||||
|
||||
if listeners then
|
||||
|
|
|
@ -129,6 +129,12 @@ function System:__tryRemove(e)
|
|||
end
|
||||
end
|
||||
|
||||
--- Returns the Instance the System is in.
|
||||
-- @return The Instance
|
||||
function System:getInstance()
|
||||
return self.__instance
|
||||
end
|
||||
|
||||
--- Returns if the System has the Entity.
|
||||
-- @param The Entity to check for
|
||||
-- @return True if the System has the Entity. False otherwise
|
||||
|
|
11
main.lua
11
main.lua
|
@ -89,21 +89,10 @@ function RandomRemover:update(dt)
|
|||
love.window.setTitle(love.timer.getFPS())
|
||||
end
|
||||
|
||||
local RemovalSystem = System({})
|
||||
function RemovalSystem:init(instance)
|
||||
self.instance = instance
|
||||
end
|
||||
|
||||
function RemovalSystem:update()
|
||||
self.instance:flush()
|
||||
end
|
||||
|
||||
Game:addSystem(RandomRemover(), "update")
|
||||
Game:addSystem(RectangleRenderer(), "draw")
|
||||
Game:addSystem(CircleRenderer(), "draw")
|
||||
|
||||
Game:addSystem(RemovalSystem(Game), "update", false)
|
||||
|
||||
for i = 1, 100 do
|
||||
local e = Entity()
|
||||
e:give(Position, love.math.random(0, 700), love.math.random(0, 700))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue