Fix bug with entity Count and adding already existing entities.

This commit is contained in:
bakpakin
2015-04-19 13:40:42 +08:00
parent f23941a903
commit d1e5ad731a
+32 -8
View File
@@ -12,6 +12,7 @@ local pairs = pairs
local ipairs = ipairs local ipairs = ipairs
local setmetatable = setmetatable local setmetatable = setmetatable
local getmetatable = getmetatable local getmetatable = getmetatable
local type = type
-- Local versions of the library functions -- Local versions of the library functions
local tiny_system local tiny_system
@@ -35,7 +36,11 @@ function tiny.requireAll(...)
local c local c
for i = 1, len do for i = 1, len do
c = components[i] c = components[i]
if e[c] == nil then if type(c) == 'function' then
if not c(e) then
return false
end
elseif e[c] == nil then
return false return false
end end
end end
@@ -53,7 +58,11 @@ function tiny.requireOne(...)
local c local c
for i = 1, len do for i = 1, len do
c = components[i] c = components[i]
if e[c] ~= nil then if type(c) == 'function' then
if c(e) then
return true
end
elseif e[c] ~= nil then
return true return true
end end
end end
@@ -76,13 +85,16 @@ local systemMetaTable = {}
-- takes one argument, an Entity -- takes one argument, an Entity
-- @param onRemove Similar to onAdd, but is instead called when an Entity is -- @param onRemove Similar to onAdd, but is instead called when an Entity is
-- removed from the System -- removed from the System
function tiny.system(callback, filter, entityCallback, onAdd, onRemove) -- @param postCallback similar to callback, but is called after Entites are
-- processed
function tiny.system(callback, filter, entityCallback, onAdd, onRemove, postCallback)
local ret = { local ret = {
callback = callback, callback = callback,
filter = filter, filter = filter,
entityCallback = entityCallback, entityCallback = entityCallback,
onAdd = onAdd, onAdd = onAdd,
onRemove = onRemove onRemove = onRemove,
postCallback = postCallback
} }
setmetatable(ret, systemMetaTable) setmetatable(ret, systemMetaTable)
return ret return ret
@@ -97,8 +109,10 @@ tiny_system = tiny.system
-- takes one argument, an Entity -- takes one argument, an Entity
-- @param onRemove Similar to onAdd, but is instead called when an Entity is -- @param onRemove Similar to onAdd, but is instead called when an Entity is
-- removed from the System -- removed from the System
function tiny.processingSystem(filter, entityCallback, onAdd, onRemove) -- @param postCallback similar to callback, but is called after Entites are
return tiny_system(nil, filter, entityCallback, onAdd, onRemove) -- processed
function tiny.processingSystem(filter, entityCallback, onAdd, onRemove, postCallback)
return tiny_system(nil, filter, entityCallback, onAdd, onRemove, postCallback)
end end
local worldMetaTable = { __index = tiny } local worldMetaTable = { __index = tiny }
@@ -206,6 +220,7 @@ tiny_remove = tiny.remove
function tiny.updateSystem(world, system, dt) function tiny.updateSystem(world, system, dt)
local callback = system.callback local callback = system.callback
local entityCallback = system.entityCallback local entityCallback = system.entityCallback
local postCallback = system.postCallback
if callback then if callback then
callback(dt) callback(dt)
@@ -220,6 +235,11 @@ function tiny.updateSystem(world, system, dt)
end end
end end
end end
if postCallback then
postCallback(dt)
end
end end
tiny_updateSystem = tiny.updateSystem tiny_updateSystem = tiny.updateSystem
@@ -290,8 +310,10 @@ function tiny.manageEntities(world)
-- Add, remove, or change Entities -- Add, remove, or change Entities
for e, s in pairs(entityStatus) do for e, s in pairs(entityStatus) do
if s == "add" then if s == "add" then
if not entities[e] then
deltaEntityCount = deltaEntityCount + 1
end
entities[e] = true entities[e] = true
deltaEntityCount = deltaEntityCount + 1
for sys, es in pairs(systemEntities) do for sys, es in pairs(systemEntities) do
local filter = sys.filter local filter = sys.filter
if filter then if filter then
@@ -304,8 +326,10 @@ function tiny.manageEntities(world)
end end
end end
elseif s == "remove" then elseif s == "remove" then
if entities[e] then
deltaEntityCount = deltaEntityCount - 1
end
entities[e] = nil entities[e] = nil
deltaEntityCount = deltaEntityCount - 1
for sys, es in pairs(systemEntities) do for sys, es in pairs(systemEntities) do
local onRemove = sys.onRemove local onRemove = sys.onRemove
if es[e] and onRemove then if es[e] and onRemove then