Remove tiny.activate and tiny.deactivate.

This commit is contained in:
bakpakin
2015-05-04 11:46:18 +08:00
parent 264d835334
commit 994f3b6533
2 changed files with 21 additions and 56 deletions
+14 -23
View File
@@ -1,24 +1,15 @@
local tiny = require "tiny" local tiny = require "tiny"
print("tiny version: " .. tiny._VERSION)
-- Taken from answer at http://stackoverflow.com/questions/640642/how-do-you-copy-a-lua-table-by-value
local function deep_copy(o, seen)
seen = seen or {}
if o == nil then return nil end
if seen[o] then return seen[o] end
local no local function deep_copy(x)
if type(o) == 'table' then if type(x) == 'table' then
no = {} local nx = {}
seen[o] = no for k, v in next, x, nil do
nx[deep_copy(k)] = deep_copy(v)
for k, v in next, o, nil do end
no[deep_copy(k, seen)] = deep_copy(v, seen) return nx
else
return x
end end
setmetatable(no, deep_copy(getmetatable(o), seen))
else -- number, string, boolean, etc
no = o
end
return no
end end
local entityTemplate1 = { local entityTemplate1 = {
@@ -138,15 +129,15 @@ describe('tiny-ecs:', function()
assert.equals(0, world:getSystemCount()) assert.equals(0, world:getSystemCount())
end) end)
it("Disable and Enable Systems", function() it("Deactivate and Activate Systems", function()
world:deactivate(moveSystem) moveSystem.active = false
world:deactivate(oneTimeSystem) oneTimeSystem.active = false
world:update(1) world:update(1)
assert.equals(world:getSystemCount(), 2) assert.equals(world:getSystemCount(), 2)
assert.equals(timePassed, 0) assert.equals(timePassed, 0)
assert.equals(entity1.xform.x, entityTemplate1.xform.x) assert.equals(entity1.xform.x, entityTemplate1.xform.x)
world:activate(moveSystem) moveSystem.active = true
world:activate(oneTimeSystem) oneTimeSystem.active = true
world:update(1) world:update(1)
assert.equals(timePassed, 1) assert.equals(timePassed, 1)
assert.are_not.equal(entity1.xform.x, entityTemplate1.xform.x) assert.are_not.equal(entity1.xform.x, entityTemplate1.xform.x)
+7 -33
View File
@@ -52,12 +52,12 @@ local tiny_remove
function tiny.requireAll(...) function tiny.requireAll(...)
local components = {...} local components = {...}
local len = #components local len = #components
return function(_, e) return function(system, e)
local c local c
for i = 1, len do for i = 1, len do
c = components[i] c = components[i]
if type(c) == 'function' then if type(c) == 'function' then
if not c(_, e) then if not c(system, e) then
return false return false
end end
elseif e[c] == nil then elseif e[c] == nil then
@@ -74,12 +74,12 @@ end
function tiny.requireOne(...) function tiny.requireOne(...)
local components = {...} local components = {...}
local len = #components local len = #components
return function(_, e) return function(system, e)
local c local c
for i = 1, len do for i = 1, len do
c = components[i] c = components[i]
if type(c) == 'function' then if type(c) == 'function' then
if c(_, e) then if c(system, e) then
return true return true
end end
elseif e[c] ~= nil then elseif e[c] ~= nil then
@@ -116,9 +116,7 @@ end
-- but one can write their own filters as well. -- but one can write their own filters as well.
-- @param table A table to be used as a System, or `nil` to create a new System. -- @param table A table to be used as a System, or `nil` to create a new System.
function tiny.system(table) function tiny.system(table)
if table == nil then table = table or {}
table = {}
end
table[systemTableKey] = true table[systemTableKey] = true
return table return table
end end
@@ -164,9 +162,7 @@ end
-- @param table A table to be used as a System, or `nil` to create a new -- @param table A table to be used as a System, or `nil` to create a new
-- Processing System. -- Processing System.
function tiny.processingSystem(table) function tiny.processingSystem(table)
if table == nil then table = table or {}
table = {}
end
table[systemTableKey] = true table[systemTableKey] = true
table.update = processingSystemUpdate table.update = processingSystemUpdate
return table return table
@@ -207,9 +203,7 @@ end
-- @param table A table to be used as a System, or `nil` to create a new -- @param table A table to be used as a System, or `nil` to create a new
-- Processing System. -- Processing System.
function tiny.sortedSystem(table) function tiny.sortedSystem(table)
if table == nil then table = table or {}
table = {}
end
table[systemTableKey] = true table[systemTableKey] = true
table.update = processingSystemUpdate table.update = processingSystemUpdate
table.onModify = sortedSystemOnModify table.onModify = sortedSystemOnModify
@@ -599,24 +593,4 @@ function tiny.setSystemIndex(world, system, index)
end end
end end
--- Activate a System in the World.
-- Activated Systems will be update whenever tiny.update(world, dt) is called.
-- @param world
-- @param system System to activate. The System must already be added to the
-- World.
function tiny.activate(world, system)
system.active = true
end
--- Deactivates a System in the World.
-- Deactivated Systems must be update manually, and will not update when the
-- rest of World updates. They will, however, process new Entities added while
-- the System is deactivated.
-- @param world
-- @param system System to deactivate. The System must already be added to the
-- World.
function tiny.deactivate(world, system)
system.active = false
end
return tiny return tiny