Remove parallel tables that hold System data in favor of one table.

This commit is contained in:
bakpakin 2015-04-26 09:55:55 +08:00
parent dc69636f72
commit 6531332ea8

View File

@ -182,20 +182,12 @@ function tiny.world(...)
-- Number of Entities in World. -- Number of Entities in World.
entityCount = 0, entityCount = 0,
-- List of Systems -- List of System Data. A data element is a table with 4
-- keys: system, indices, entities, and active.
systems = {}, systems = {},
-- Table of Systems to whether or not they are active.
activeSystems = {},
-- Table of Systems to System Indices -- Table of Systems to System Indices
systemIndices = {}, systemIndices = {}
-- Table of Systems to Lists of matching Entities
systemEntities = {},
-- Table of Systems to Tables of Entities to their Indices
systemEntityIndices = {}
} }
tiny_add(ret, ...) tiny_add(ret, ...)
@ -311,37 +303,27 @@ function tiny.manageSystems(world)
local systemIndices = world.systemIndices local systemIndices = world.systemIndices
local entities = world.entities local entities = world.entities
local systems = world.systems local systems = world.systems
local activeSystems = world.activeSystems
local systemEntities = world.systemEntities
local systemEntityIndices = world.systemEntityIndices
local system, index, filter, entityList, entityIndices, entityIndex, onRemove, onAdd local system, systemData, index, filter, entityList, entityIndices, entityIndex, onRemove, onAdd
-- Remove Systems -- Remove Systems
for i = 1, #s2r do for i = 1, #s2r do
system = s2r[i] system = s2r[i]
index = systemIndices[system] index = systemIndices[system]
if index then if index then
systemData = systems[index]
onRemove = system.onRemove onRemove = system.onRemove
if onRemove then if onRemove then
entityList = systemEntities[system] entityList = systemData.entities
for j = 1, #entityList do for j = 1, #entityList do
onRemove(system, entityList[j]) onRemove(system, entityList[j])
end end
end end
for j = index + 1, #systems do
systemIndices[systems[j]] = j - 1
end
systemIndices[system] = nil systemIndices[system] = nil
activeSystems[system] = nil
systemEntities[system] = nil
systemEntityIndices[system] = nil
tremove(systems, index) tremove(systems, index)
for j = index, #systems do
systemIndices[systems[j].system] = j
end
end end
s2r[i] = nil s2r[i] = nil
end end
@ -350,15 +332,12 @@ function tiny.manageSystems(world)
for i = 1, #s2a do for i = 1, #s2a do
system = s2a[i] system = s2a[i]
if not systemIndices[system] then if not systemIndices[system] then
entityList = {}
entityIndices = {}
systemData = { system = system, entities = entityList, indices = entityIndices, active = true }
index = #systems + 1 index = #systems + 1
systemIndices[system] = index systemIndices[system] = index
systems[index] = system systems[index] = systemData
activeSystems[system] = true
entityList = {}
systemEntities[system] = entityList
entityIndices = {}
systemEntityIndices[system] = entityIndices
-- Try to add Entities -- Try to add Entities
onAdd = system.onAdd onAdd = system.onAdd
@ -394,13 +373,10 @@ function tiny.manageEntities(world)
return return
end end
local entityStatus = world.entityStatus
local systemEntities = world.systemEntities
local entities = world.entities local entities = world.entities
local systems = world.systems local systems = world.systems
local entityCount = world.entityCount local entityCount = world.entityCount
local systemEntityIndices = world.systemEntityIndices local entity, system, systemData, index, onRemove, onAdd, ses, seis, filter, tmpEntity
local entity, system, index, onRemove, onAdd, ses, seis, filter, tmpEntity
-- Remove Entities -- Remove Entities
for i = 1, #e2r do for i = 1, #e2r do
@ -409,9 +385,10 @@ function tiny.manageEntities(world)
entities[entity] = nil entities[entity] = nil
for j = 1, #systems do for j = 1, #systems do
system = systems[j] systemData = systems[j]
ses = systemEntities[system] system = systemData.system
seis = systemEntityIndices[system] ses = systemData.entities
seis = systemData.indices
index = seis[entity] index = seis[entity]
if index then if index then
@ -441,9 +418,10 @@ function tiny.manageEntities(world)
entities[entity] = true entities[entity] = true
for j = 1, #systems do for j = 1, #systems do
system = systems[j] systemData = systems[j]
ses = systemEntities[system] system = systemData.system
seis = systemEntityIndices[system] ses = systemData.entities
seis = systemData.indices
filter = system.filter filter = system.filter
if filter and filter(system, entity) then if filter and filter(system, entity) then
index = #ses + 1 index = #ses + 1
@ -478,16 +456,14 @@ function tiny.update(world, dt)
tiny_manageSystems(world) tiny_manageSystems(world)
tiny_manageEntities(world) tiny_manageEntities(world)
local activeSystems = world.activeSystems
local systems = world.systems local systems = world.systems
local systemEntities = world.systemEntities local systemData
local system
-- Iterate through Systems IN ORDER -- Iterate through Systems IN ORDER
for i = 1, #systems do for i = 1, #systems do
system = systems[i] systemData = systems[i]
if activeSystems[system] then if systemData.active then
system:update(systemEntities[system], dt) systemData.system:update(systemData.entities, dt)
end end
end end
end end
@ -509,7 +485,7 @@ end
function tiny.clearSystems(world) function tiny.clearSystems(world)
local systems = world.systems local systems = world.systems
for i = #systems, 1, -1 do for i = #systems, 1, -1 do
tiny_removeSystem(world, systems[i]) tiny_removeSystem(world, systems[i].system)
end end
end end
@ -543,12 +519,13 @@ function tiny.setSystemIndex(world, system, index)
local systemIndices = world.systemIndices local systemIndices = world.systemIndices
local oldIndex = systemIndices[system] local oldIndex = systemIndices[system]
local systems = world.systems local systems = world.systems
local systemData = systems[oldIndex]
tremove(systems, oldIndex) tremove(systems, oldIndex)
tinsert(systems, index, system) tinsert(systems, index, systemData)
for i = oldIndex, index, index >= oldIndex and 1 or -1 do for i = oldIndex, index, index >= oldIndex and 1 or -1 do
systemIndices[systems[i]] = i systemIndices[systems[i].system] = i
end end
end end
@ -559,8 +536,8 @@ end
-- World. -- World.
function tiny.activate(world, ...) function tiny.activate(world, ...)
local args = {...} local args = {...}
for _, obj in ipairs(args) do for _, system in ipairs(args) do
world.activeSystems[obj] = true world.systems[world.systemIndices[system]].active = true
end end
end end
@ -573,8 +550,8 @@ end
-- World. -- World.
function tiny.deactivate(world, ...) function tiny.deactivate(world, ...)
local args = {...} local args = {...}
for _, obj in ipairs(args) do for _, system in ipairs(args) do
world.activeSystems[obj] = nil world.systems[world.systemIndices[system]].active = false
end end
end end