Combine the 'add entities' procedure in manage_entities with 'change

entities'
This commit is contained in:
bakpakin 2016-08-10 07:22:25 -04:00
parent d720d361e6
commit 83d806011b

View File

@ -419,9 +419,6 @@ local worldMetaTable
function tiny.world(...) function tiny.world(...)
local ret = setmetatable({ local ret = setmetatable({
-- List of Entities to add
entitiesToAdd = {},
-- List of Entities to remove -- List of Entities to remove
entitiesToRemove = {}, entitiesToRemove = {},
@ -458,13 +455,8 @@ end
-- Also call this on Entities that have changed Components such that they -- Also call this on Entities that have changed Components such that they
-- match different Filters. Returns the Entity. -- match different Filters. Returns the Entity.
function tiny.addEntity(world, entity) function tiny.addEntity(world, entity)
if world.entities[entity] then
local e2c = world.entitiesToChange local e2c = world.entitiesToChange
e2c[#e2c + 1] = entity e2c[#e2c + 1] = entity
else
local e2a = world.entitiesToAdd
e2a[#e2a + 1] = entity
end
return entity return entity
end end
tiny_addEntity = tiny.addEntity tiny_addEntity = tiny.addEntity
@ -496,7 +488,7 @@ function tiny.add(world, ...)
end end
tiny_add = tiny.add tiny_add = tiny.add
--- Removes an Entity to the World. Returns the Entity. --- Removes an Entity from the World. Returns the Entity.
function tiny.removeEntity(world, entity) function tiny.removeEntity(world, entity)
local e2r = world.entitiesToRemove local e2r = world.entitiesToRemove
e2r[#e2r + 1] = entity e2r[#e2r + 1] = entity
@ -621,17 +613,15 @@ end
-- Adds, removes, and changes Entities that have been marked. -- Adds, removes, and changes Entities that have been marked.
function tiny_manageEntities(world) function tiny_manageEntities(world)
local e2a = world.entitiesToAdd
local e2r = world.entitiesToRemove local e2r = world.entitiesToRemove
local e2c = world.entitiesToChange local e2c = world.entitiesToChange
-- Early exit -- Early exit
if #e2a == 0 and #e2r == 0 and #e2c == 0 then if #e2r == 0 and #e2c == 0 then
return return
end end
world.entitiesToChange = {} world.entitiesToChange = {}
world.entitiesToAdd = {}
world.entitiesToRemove = {} world.entitiesToRemove = {}
local entities = world.entities local entities = world.entities
@ -642,7 +632,13 @@ function tiny_manageEntities(world)
-- Change Entities -- Change Entities
for i = 1, #e2c do for i = 1, #e2c do
local entity = e2c[i] local entity = e2c[i]
if entities[entity] then -- Add if needed
if not entities[entity] then
local index = #entityList + 1
entities[entity] = index
entityList[index] = entity
entityCount = entityCount + 1
end
for j = 1, #systems do for j = 1, #systems do
local system = systems[j] local system = systems[j]
if not system.nocache then if not system.nocache then
@ -675,7 +671,6 @@ function tiny_manageEntities(world)
end end
end end
end end
end
e2c[i] = nil e2c[i] = nil
end end
@ -716,36 +711,6 @@ function tiny_manageEntities(world)
end end
end end
-- Add Entities
for i = 1, #e2a do
local entity = e2a[i]
if not entities[entity] then
local listIndex = #entityList + 1
entities[entity] = listIndex
entityList[listIndex] = entity
entityCount = entityCount + 1
for j = 1, #systems do
local system = systems[j]
if not system.nocache then
local ses = system.entities
local seis = system.indices
local filter = system.filter
if filter and filter(system, entity) then
system.modified = true
local index = #ses + 1
ses[index] = entity
seis[entity] = index
local onAdd = system.onAdd
if onAdd then
onAdd(system, entity)
end
end
end
end
end
e2a[i] = nil
end
-- Update Entity count -- Update Entity count
world.entityCount = entityCount world.entityCount = entityCount
end end
@ -757,7 +722,7 @@ function tiny.refresh(world)
tiny_manageSystems(world) tiny_manageSystems(world)
tiny_manageEntities(world) tiny_manageEntities(world)
local systems = world.systems local systems = world.systems
for i = 1, #systems do for i = #systems, 1, -1 do
local system = systems[i] local system = systems[i]
if system.active then if system.active then
local onModify = system.onModify local onModify = system.onModify
@ -783,24 +748,25 @@ function tiny.update(world, dt, filter)
-- Iterate through Systems IN REVERSE ORDER -- Iterate through Systems IN REVERSE ORDER
for i = #systems, 1, -1 do for i = #systems, 1, -1 do
local system = systems[i] local system = systems[i]
if system.active then
-- Call the modify callback on Systems that have been modified.
local onModify = system.onModify
if onModify and system.modified then
onModify(system, dt)
end
local preWrap = system.preWrap local preWrap = system.preWrap
if preWrap and system.active and if preWrap and
((not filter) or filter(world, system)) then ((not filter) or filter(world, system)) then
preWrap(system, dt) preWrap(system, dt)
end end
end end
end
-- Iterate through Systems IN ORDER -- Iterate through Systems IN ORDER
for i = 1, #systems do for i = 1, #systems do
local system = systems[i] local system = systems[i]
if system.active and ((not filter) or filter(world, system)) then if system.active and ((not filter) or filter(world, system)) then
-- Call the modify callback on Systems that have been modified.
local onModify = system.onModify
if onModify and system.modified then
onModify(system, dt)
end
-- Update Systems that have an update method (most Systems) -- Update Systems that have an update method (most Systems)
local update = system.update local update = system.update
if update then if update then