Fix Adding systems and items to the world inside a call to system:onAdd()

This commit is contained in:
bakpakin 2015-08-24 12:52:11 -04:00
parent a5af4b6ecd
commit 9834dfa9d6

View File

@ -348,7 +348,7 @@ local worldMetaTable
-- Can optionally add default Systems and Entities. Returns the new World along -- Can optionally add default Systems and Entities. Returns the new World along
-- with default Entities and Systems. -- with default Entities and Systems.
function tiny.world(...) function tiny.world(...)
local ret = { local ret = setmetatable({
-- List of Entities to add -- List of Entities to add
entitiesToAdd = {}, entitiesToAdd = {},
@ -370,13 +370,13 @@ function tiny.world(...)
-- List of Systems -- List of Systems
systems = {} systems = {}
} }, worldMetaTable)
tiny_add(ret, ...) tiny_add(ret, ...)
tiny_manageSystems(ret) tiny_manageSystems(ret)
tiny_manageEntities(ret) tiny_manageEntities(ret)
return setmetatable(ret, worldMetaTable), ... return ret, ...
end end
--- Adds an Entity to the world. --- Adds an Entity to the world.
@ -461,6 +461,9 @@ function tiny_manageSystems(world)
return return
end end
world.systemsToAdd = {}
world.systemsToRemove = {}
local entities = world.entities local entities = world.entities
local systems = world.systems local systems = world.systems
local system, index, filter local system, index, filter
@ -531,6 +534,7 @@ end
-- Adds and removes Entities that have been marked. -- Adds and removes Entities that have been marked.
function tiny_manageEntities(world) function tiny_manageEntities(world)
local e2a, e2r = world.entitiesToAdd, world.entitiesToRemove local e2a, e2r = world.entitiesToAdd, world.entitiesToRemove
-- Early exit -- Early exit
@ -538,6 +542,9 @@ function tiny_manageEntities(world)
return return
end end
world.entitiesToAdd = {}
world.entitiesToRemove = {}
local entities = world.entities local entities = world.entities
local systems = world.systems local systems = world.systems
local entityCount = world.entityCount local entityCount = world.entityCount
@ -696,9 +703,7 @@ end
--- Sets the index of a System in the World, and returns the old index. Changes --- Sets the index of a System in the World, and returns the old index. Changes
-- the order in which they Systems processed, because lower indexed Systems are -- the order in which they Systems processed, because lower indexed Systems are
-- processed first. If 'index' < 0, then sets 'index' relative to the last index -- processed first. Returns the old system.index.
-- in the World; -1 is the index of the last System, -2 is the second to last,
-- and so on. Returns the old system.index.
function tiny.setSystemIndex(world, system, index) function tiny.setSystemIndex(world, system, index)
local oldIndex = system.index local oldIndex = system.index
local systems = world.systems local systems = world.systems
@ -734,7 +739,10 @@ worldMetaTable = {
getSystemCount = tiny.getSystemCount, getSystemCount = tiny.getSystemCount,
getSystemIndex = tiny.getSystemIndex, getSystemIndex = tiny.getSystemIndex,
setSystemIndex = tiny.setSystemIndex setSystemIndex = tiny.setSystemIndex
} },
__tostring = function(self)
return "tiny-ecs_World"
end
} }
return tiny return tiny