Add onAddToWorld and onRemoveFromWorld callbacks to systems. Also, adding or removing systems to a world twice raises an error.

This commit is contained in:
bakpakin
2015-09-08 13:36:27 -04:00
parent f3b16a40f6
commit b870b5a34e
2 changed files with 25 additions and 14 deletions
+7 -2
View File
@@ -120,6 +120,11 @@ describe('tiny-ecs:', function()
timePassed = 0 timePassed = 0
end) end)
after_each(function()
world:clearSystems()
world:refresh()
end)
it("Create World", function() it("Create World", function()
assert.equals(world:getEntityCount(), 3) assert.equals(world:getEntityCount(), 3)
assert.equals(world:getSystemCount(), 2) assert.equals(world:getSystemCount(), 2)
@@ -196,7 +201,7 @@ describe('tiny-ecs:', function()
it("Add Systems Multiple Times", function() it("Add Systems Multiple Times", function()
world:update(1) world:update(1)
world:add(moveSystem, oneTimeSystem) assert.has_error(function() world:add(moveSystem, oneTimeSystem) end, "System already belongs to a World.")
world:update(2) world:update(2)
assert.equals(2, world:getSystemCount()) assert.equals(2, world:getSystemCount())
assert.equals(3, world:getEntityCount()) assert.equals(3, world:getEntityCount())
@@ -206,7 +211,7 @@ describe('tiny-ecs:', function()
world:update(1) world:update(1)
world:remove(moveSystem) world:remove(moveSystem)
world:update(2) world:update(2)
world:remove(moveSystem) assert.has_error(function() world:remove(moveSystem) end, "System does not belong to this World.")
world:update(2) world:update(2)
assert.equals(1, world:getSystemCount()) assert.equals(1, world:getSystemCount())
assert.equals(3, world:getEntityCount()) assert.equals(3, world:getEntityCount())
+7 -1
View File
@@ -476,7 +476,6 @@ function tiny_manageSystems(world)
for i = 1, #s2r do for i = 1, #s2r do
local system = s2r[i] local system = s2r[i]
local index = system.index local index = system.index
if system.world == world then
local onRemove = system.onRemove local onRemove = system.onRemove
if onRemove then if onRemove then
local entityList = system.entities local entityList = system.entities
@@ -488,6 +487,9 @@ function tiny_manageSystems(world)
for j = index, #systems do for j = index, #systems do
systems[j].index = j systems[j].index = j
end end
local onRemoveFromWorld = system.onRemoveFromWorld
if onRemoveFromWorld then
onRemoveFromWorld(system, world)
end end
s2r[i] = nil s2r[i] = nil
@@ -514,6 +516,10 @@ function tiny_manageSystems(world)
local index = #systems + 1 local index = #systems + 1
system.index = index system.index = index
systems[index] = system systems[index] = system
local onAddToWorld = system.onAddToWorld
if onAddToWorld then
onAddToWorld(system, world)
end
-- Try to add Entities -- Try to add Entities
local onAdd = system.onAdd local onAdd = system.onAdd