Add processing system back to new API.

This commit is contained in:
bakpakin
2015-04-22 17:22:14 +08:00
parent b85fa59c74
commit 049d0eba76
2 changed files with 58 additions and 24 deletions
+8 -10
View File
@@ -85,21 +85,19 @@ describe('tiny-ecs:', function()
local world, entity1, entity2, entity3 local world, entity1, entity2, entity3
local moveSystem = tiny.system() local moveSystem = tiny.processingSystem()
moveSystem.filter = tiny.requireAll("xform", "vel") moveSystem.filter = tiny.requireAll("xform", "vel")
function moveSystem:update(world, entities, dt) function moveSystem:process(e, dt)
for e in pairs(entities) do local xform = e.xform
local xform = e.xform local vel = e.vel
local vel = e.vel local x, y = xform.x, xform.y
local x, y = xform.x, xform.y local xvel, yvel = vel.x, vel.y
local xvel, yvel = vel.x, vel.y xform.x, xform.y = x + xvel * dt, y + yvel * dt
xform.x, xform.y = x + xvel * dt, y + yvel * dt
end
end end
local timePassed = 0 local timePassed = 0
local oneTimeSystem = tiny.system() local oneTimeSystem = tiny.system()
function oneTimeSystem:update(world, entities, dt) function oneTimeSystem:update(entities, dt)
timePassed = timePassed + dt timePassed = timePassed + dt
end end
+50 -14
View File
@@ -8,15 +8,12 @@ tiny._VERSION = "1.0.0"
-- Local versions of standard lua functions -- Local versions of standard lua functions
local tinsert = table.insert local tinsert = table.insert
local tremove = table.remove local tremove = table.remove
local tconcat = table.concat
local pairs = pairs local pairs = pairs
local ipairs = ipairs local ipairs = ipairs
local setmetatable = setmetatable local setmetatable = setmetatable
local getmetatable = getmetatable
local type = type local type = type
-- Local versions of the library functions -- Local versions of the library functions
local tiny_system
local tiny_manageEntities local tiny_manageEntities
local tiny_manageSystems local tiny_manageSystems
local tiny_updateSystem local tiny_updateSystem
@@ -88,18 +85,17 @@ local function isSystem(table)
return table[systemTableKey] return table[systemTableKey]
end end
--- Marks a table conforming to the System interface as a System recognized by --- Creates a System. Systems are tables that contain at least one field;
-- tiny-ecs. Systems are tables that contain at least one field, an update -- an update function that takes parameters like so:
-- function that takes parameters like so: -- `function system:update(entities, dt)`. `entities` is an unordered table of
-- `function system:update(world, entities, dt)`. `world` is the World the System -- Entities with Entities as KEYS, and `dt` is the delta time. There are also a
-- belongs to, `entities` is an unordered table of Entities with Entities as KEYS, -- few other optional callbacks:
-- and `dt` is the delta time. There are also a few other optional callbacks:
-- `function system:filter(entity)` - returns a boolean, -- `function system:filter(entity)` - returns a boolean,
-- `function system:onAdd(entity)` - returns nil, -- `function system:onAdd(entity)` - returns nil,
-- `function system:onRemove(entity)` - returns nil. -- `function system:onRemove(entity)` - returns nil.
-- For Filters, it is conveient to use `tiny.requireAll` or `tiny.requireOne`, -- For Filters, it is conveient to use `tiny.requireAll` or `tiny.requireOne`,
-- but one can write their own filters as well. -- but one can write their own filters as well.
-- @param table A table to be used as System, or `nil` to create a new System. -- @param table A table to be used as a System, or `nil` to create a new System.
function tiny.system(table) function tiny.system(table)
if table == nil then if table == nil then
table = {} table = {}
@@ -107,9 +103,47 @@ function tiny.system(table)
table[systemTableKey] = true table[systemTableKey] = true
return table return table
end end
tiny_system = tiny.system
local worldMetaTable = { __index = tiny } -- Update function for all Processing Systems.
local function processingSystemUpdate(system, entities, dt)
local preProcess = system.preProcess
local process = system.process
local postProcess = system.postProcess
if preProcess then
preProcess(system, entities, dt)
end
if process then
for entity in pairs(entities) do
process(system, entity, dt)
end
end
if postProcess then
postProcess(system, entities, dt)
end
end
--- Creates a Processing System. A Processing System iterates through its
-- Entities in no particluar order, and updates them individually. It has two
-- important fields, `function system:process(entity, dt)`, and `function
-- system:filter(entity)`. `entities` is an unordered table of Entities with
-- Entities as KEYS, and `dt` is the delta time. There are also a few other
-- optional callbacks:
-- `function system:preProcess(entities, dt)` - returns nil,
-- `function system:postProcess(entities, dt)` - returns nil,
-- `function system:onAdd(entity)` - returns nil,
-- `function system:onRemove(entity)` - returns nil.
-- For Filters, it is conveient to use `tiny.requireAll` or `tiny.requireOne`,
-- but one can write their own filters as well.
-- @param table A table to be used as a System, or `nil` to create a new
-- Processing System.
function tiny.processingSystem(table)
if table == nil then
table = {}
end
table[systemTableKey] = true
table.update = processingSystemUpdate
return table
end
--- World functions. --- World functions.
-- A World is a container that manages Entities and Systems. The tiny-ecs module -- A World is a container that manages Entities and Systems. The tiny-ecs module
@@ -118,6 +152,8 @@ local worldMetaTable = { __index = tiny }
-- `tiny.add(world, e1, e2, e3)` is the same as `world:add(e1, e2, e3).` -- `tiny.add(world, e1, e2, e3)` is the same as `world:add(e1, e2, e3).`
-- @section World -- @section World
local worldMetaTable = { __index = tiny }
--- Creates a new World. --- Creates a new World.
-- Can optionally add default Systems and Entities. -- Can optionally add default Systems and Entities.
-- @param ... Systems and Entities to add to the World -- @param ... Systems and Entities to add to the World
@@ -134,7 +170,7 @@ function tiny.world(...)
-- Statuses: remove, add -- Statuses: remove, add
systemStatus = {}, systemStatus = {},
-- Set of Entities -- active are true, inactive are false -- Set of Entities
entities = {}, entities = {},
-- Number of Entities in World. -- Number of Entities in World.
@@ -244,7 +280,7 @@ tiny_remove = tiny.remove
-- @param dt Delta time -- @param dt Delta time
function tiny.updateSystem(world, system, dt) function tiny.updateSystem(world, system, dt)
local es = world.systemEntities[system] local es = world.systemEntities[system]
system:update(world, es, dt) system:update(es, dt)
end end
tiny_updateSystem = tiny.updateSystem tiny_updateSystem = tiny.updateSystem