Fix sorting + add sorting test.

This commit is contained in:
bakpakin
2015-06-18 16:02:16 -04:00
parent 1fd32dab9a
commit cb9ab332d2
2 changed files with 21 additions and 7 deletions
+13
View File
@@ -215,6 +215,19 @@ describe('tiny-ecs:', function()
assert.equals(1, world:getSystemIndex(oneTimeSystem)) assert.equals(1, world:getSystemIndex(oneTimeSystem))
end) end)
it("Sorts Entities in Sorting Systems", function()
local sortsys = tiny.system({}, {"sorted"})
sortsys.filter = tiny.requireAll("vel")
function sortsys:compare(e1, e2)
return e1.vel.x < e2.vel.x
end
world:add(sortsys)
world:update(0)
assert.equals(sortsys.entities[1], entity2)
assert.equals(sortsys.entities[2], entity3)
assert.equals(sortsys.entities[3], entity1)
end)
end) end)
end) end)
+8 -7
View File
@@ -50,8 +50,8 @@ local tiny_remove
-- value indicating if the Entity should be processed by the System. -- value indicating if the Entity should be processed by the System.
-- --
-- Filters must be added to Systems by setting the `filter` field of the System. -- Filters must be added to Systems by setting the `filter` field of the System.
-- Filter's returned by `tiny.requireAll` and `tiny.requireAny` are immutable -- Filter's returned by tiny-ecs's Filter functions are immutable and can be
-- and can be used by multiple Systems. -- used by multiple Systems.
-- --
-- local f1 = tiny.requireAll("position", "velocity", "size") -- local f1 = tiny.requireAll("position", "velocity", "size")
-- local f2 = tiny.requireAny("position", "velocity", "size") -- local f2 = tiny.requireAny("position", "velocity", "size")
@@ -254,7 +254,7 @@ end
-- Sorts Systems by a function system.sort(entity1, entity2) on modify. -- Sorts Systems by a function system.sort(entity1, entity2) on modify.
local function sortedSystemOnModify(system, dt) local function sortedSystemOnModify(system, dt)
local entities = system.entities local entities = system.entities
local entityIndices = system.entityIndices local indices = system.indices
local sortDelegate = system.sortDelegate local sortDelegate = system.sortDelegate
if not sortDelegate then if not sortDelegate then
local compare = system.compare local compare = system.compare
@@ -266,7 +266,7 @@ local function sortedSystemOnModify(system, dt)
tsort(entities, sortDelegate) tsort(entities, sortDelegate)
for i = 1, #entities do for i = 1, #entities do
local entity = entities[i] local entity = entities[i]
entityIndices[entity] = i indices[entity] = i
end end
end end
@@ -627,15 +627,16 @@ function tiny_manageEntities(world)
end end
--- Updates the World by dt (delta time). Takes an optional parameter, `filter`, --- Updates the World by dt (delta time). Takes an optional parameter, `filter`,
-- which is a Filter that selects Systems from the World. Only selected Systems -- which is a Filter that selects Systems from the World, and updates only those
-- are updated. Put this function in your main loop. -- Systems. If `filter` is not supplied, all Systems are updated. Put this
-- function in your main loop.
function tiny.update(world, dt, filter) function tiny.update(world, dt, filter)
tiny_manageSystems(world) tiny_manageSystems(world)
tiny_manageEntities(world) tiny_manageEntities(world)
local systems = world.systems local systems = world.systems
local system, update, onModify, entities local system, update, onModify
-- Iterate through Systems IN ORDER -- Iterate through Systems IN ORDER
for i = 1, #systems do for i = 1, #systems do