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

132
tiny.lua
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,37 +632,42 @@ 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
for j = 1, #systems do if not entities[entity] then
local system = systems[j] local index = #entityList + 1
if not system.nocache then entities[entity] = index
local ses = system.entities entityList[index] = entity
local seis = system.indices entityCount = entityCount + 1
local index = seis[entity] end
local filter = system.filter for j = 1, #systems do
if filter and filter(system, entity) then local system = systems[j]
if not index then if not system.nocache then
system.modified = true local ses = system.entities
index = #ses + 1 local seis = system.indices
ses[index] = entity local index = seis[entity]
seis[entity] = index local filter = system.filter
local onAdd = system.onAdd if filter and filter(system, entity) then
if onAdd then if not index then
onAdd(system, entity)
end
end
elseif index then
system.modified = true system.modified = true
local tmpEntity = ses[#ses] index = #ses + 1
ses[index] = tmpEntity ses[index] = entity
seis[tmpEntity] = index seis[entity] = index
seis[entity] = nil local onAdd = system.onAdd
ses[#ses] = nil if onAdd then
local onRemove = system.onRemove onAdd(system, entity)
if onRemove then
onRemove(system, entity)
end end
end end
elseif index then
system.modified = true
local tmpEntity = ses[#ses]
ses[index] = tmpEntity
seis[tmpEntity] = index
seis[entity] = nil
ses[#ses] = nil
local onRemove = system.onRemove
if onRemove then
onRemove(system, entity)
end
end end
end end
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,10 +748,17 @@ 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]
local preWrap = system.preWrap if system.active then
if preWrap and system.active and -- Call the modify callback on Systems that have been modified.
((not filter) or filter(world, system)) then local onModify = system.onModify
preWrap(system, dt) if onModify and system.modified then
onModify(system, dt)
end
local preWrap = system.preWrap
if preWrap and
((not filter) or filter(world, system)) then
preWrap(system, dt)
end
end end
end end
@ -795,12 +767,6 @@ function tiny.update(world, dt, filter)
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