Remove use of systemData - instead, Systems now have mutable data.

This removes a level of indirection and prevents passing around of entities.
This commit is contained in:
bakpakin
2015-05-04 09:09:34 +08:00
parent 5e7d2ea7fc
commit 9991ff7474
2 changed files with 40 additions and 58 deletions
+1 -1
View File
@@ -97,7 +97,7 @@ describe('tiny-ecs:', function()
local timePassed = 0 local timePassed = 0
local oneTimeSystem = tiny.system() local oneTimeSystem = tiny.system()
function oneTimeSystem:update(entities, dt) function oneTimeSystem:update(dt)
timePassed = timePassed + dt timePassed = timePassed + dt
end end
+39 -57
View File
@@ -124,14 +124,15 @@ function tiny.system(table)
end end
-- Update function for all Processing Systems. -- Update function for all Processing Systems.
local function processingSystemUpdate(system, entities, dt) local function processingSystemUpdate(system, dt)
local entities = system.entities
local preProcess = system.preProcess local preProcess = system.preProcess
local process = system.process local process = system.process
local postProcess = system.postProcess local postProcess = system.postProcess
local entity local entity
if preProcess then if preProcess then
preProcess(system, entities, dt) preProcess(system, dt)
end end
if process then if process then
@@ -143,7 +144,7 @@ local function processingSystemUpdate(system, entities, dt)
end end
if postProcess then if postProcess then
postProcess(system, entities, dt) postProcess(system, dt)
end end
end end
@@ -172,17 +173,16 @@ function tiny.processingSystem(table)
end end
-- Sorts Systems by a function system.sort(entity1, entity2) on modify. -- Sorts Systems by a function system.sort(entity1, entity2) on modify.
local function sortedSystemOnModify(system, systemData, dt) local function sortedSystemOnModify(system, dt)
local system = systemData.system local entities = system.entities
local entities = systemData.entities local entityIndices = system.entityIndices
local entityIndices = systemData.entityIndices local sortDelegate = system.sortDelegate
local sortDelegate = systemData.sortDelegate
if not sortDelegate then if not sortDelegate then
local sort = system.sort local sort = system.sort
sortDelegate = function(e1, e2) sortDelegate = function(e1, e2)
sort(system, e1, e2) sort(system, e1, e2)
end end
systemData.sortDelegate = sortDelegate system.sortDelegate = sortDelegate
end end
tsort(entities, sortDelegate) tsort(entities, sortDelegate)
for i = 1, #entities do for i = 1, #entities do
@@ -348,15 +348,6 @@ function tiny.remove(world, ...)
end end
tiny_remove = tiny.remove tiny_remove = tiny.remove
--- Updates a System.
-- @param world
-- @param system A System in the World to update
-- @param dt Delta time
function tiny.updateSystem(world, system, dt)
local es = world.systemEntities[system]
system:update(es, dt)
end
-- Adds and removes Systems that have been marked from the World. -- Adds and removes Systems that have been marked from the World.
function tiny_manageSystems(world) function tiny_manageSystems(world)
@@ -370,7 +361,7 @@ 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 system, systemData, index, filter local system, index, filter
local entityList, entityIndices, entityIndex, onRemove, onAdd local entityList, entityIndices, entityIndex, onRemove, onAdd
-- Remove Systems -- Remove Systems
@@ -378,10 +369,9 @@ function tiny_manageSystems(world)
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 = systemData.entities entityList = system.entities
for j = 1, #entityList do for j = 1, #entityList do
onRemove(system, entityList[j]) onRemove(system, entityList[j])
end end
@@ -389,7 +379,7 @@ function tiny_manageSystems(world)
systemIndices[system] = nil systemIndices[system] = nil
tremove(systems, index) tremove(systems, index)
for j = index, #systems do for j = index, #systems do
systemIndices[systems[j].system] = j systemIndices[systems[j]] = j
end end
end end
s2r[i] = nil s2r[i] = nil
@@ -401,16 +391,13 @@ function tiny_manageSystems(world)
if not systemIndices[system] then if not systemIndices[system] then
entityList = {} entityList = {}
entityIndices = {} entityIndices = {}
systemData = { system.entities = entityList
system = system, system.indices = entityIndices
entities = entityList, system.active = true
indices = entityIndices, system.modified = true
active = true,
modified = true
}
index = #systems + 1 index = #systems + 1
systemIndices[system] = index systemIndices[system] = index
systems[index] = systemData systems[index] = system
-- Try to add Entities -- Try to add Entities
onAdd = system.onAdd onAdd = system.onAdd
@@ -446,7 +433,7 @@ function tiny_manageEntities(world)
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 entity, system, systemData, index local entity, system, index
local onRemove, onAdd, ses, seis, filter, tmpEntity local onRemove, onAdd, ses, seis, filter, tmpEntity
-- Remove Entities -- Remove Entities
@@ -456,14 +443,13 @@ function tiny_manageEntities(world)
entities[entity] = nil entities[entity] = nil
for j = 1, #systems do for j = 1, #systems do
systemData = systems[j] system = systems[j]
system = systemData.system ses = system.entities
ses = systemData.entities seis = system.indices
seis = systemData.indices
index = seis[entity] index = seis[entity]
if index then if index then
systemData.modified = true system.modified = true
tmpEntity = ses[#ses] tmpEntity = ses[#ses]
ses[index] = tmpEntity ses[index] = tmpEntity
seis[tmpEntity] = index seis[tmpEntity] = index
@@ -490,13 +476,12 @@ function tiny_manageEntities(world)
entities[entity] = true entities[entity] = true
for j = 1, #systems do for j = 1, #systems do
systemData = systems[j] system = systems[j]
system = systemData.system ses = system.entities
ses = systemData.entities seis = system.indices
seis = systemData.indices
filter = system.filter filter = system.filter
if filter and filter(system, entity) then if filter and filter(system, entity) then
systemData.modified = true system.modified = true
index = #ses + 1 index = #ses + 1
ses[index] = entity ses[index] = entity
seis[entity] = index seis[entity] = index
@@ -529,29 +514,26 @@ function tiny.update(world, dt)
tiny_manageEntities(world) tiny_manageEntities(world)
local systems = world.systems local systems = world.systems
local systemData, system, update, onModify, entities local system, update, onModify, entities
-- Iterate through Systems IN ORDER -- Iterate through Systems IN ORDER
for i = 1, #systems do for i = 1, #systems do
systemData = systems[i] system = systems[i]
if systemData.active then if system.active then
system = systemData.system
-- Call the modify callback on Systems that have been modified. -- Call the modify callback on Systems that have been modified.
if systemData.modified then onModify = system.onModify
onModify = system.onModify if onModify and system.modified then
if onModify then onModify(system, dt)
onModify(system, systemData, dt)
end
end end
--Update Systems that have an update method (most Systems) --Update Systems that have an update method (most Systems)
update = system.update update = system.update
if update then if update then
update(system, systemData.entities, dt) update(system, dt)
end end
systemData.modified = false system.modified = false
end end
end end
end end
@@ -607,13 +589,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] local system = systems[oldIndex]
tremove(systems, oldIndex) tremove(systems, oldIndex)
tinsert(systems, index, systemData) tinsert(systems, index, system)
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].system] = i systemIndices[systems[i]] = i
end end
end end
@@ -623,7 +605,7 @@ end
-- @param system System to activate. The System must already be added to the -- @param system System to activate. The System must already be added to the
-- World. -- World.
function tiny.activate(world, system) function tiny.activate(world, system)
world.systems[world.systemIndices[system]].active = true system.active = true
end end
--- Deactivates a System in the World. --- Deactivates a System in the World.
@@ -634,7 +616,7 @@ end
-- @param system System to deactivate. The System must already be added to the -- @param system System to deactivate. The System must already be added to the
-- World. -- World.
function tiny.deactivate(world, system) function tiny.deactivate(world, system)
world.systems[world.systemIndices[system]].active = false system.active = false
end end
return tiny return tiny