Add attributes to tiny.system. Implement Interval Systems.

This commit is contained in:
bakpakin
2015-06-16 20:28:21 -04:00
parent d35d62ed3c
commit cbfc3d360f
3 changed files with 69 additions and 32 deletions
+2 -1
View File
@@ -75,7 +75,7 @@ Tiny-ecs is also on [Luarocks](https://luarocks.org/) and can be installed with
## Demo ## ## Demo ##
Check out the [demo](https://github.com/bakpakin/tiny-ecs/tree/demo-commandokibbles), a game Check out the [demo](https://github.com/bakpakin/tiny-ecs/tree/demo-commandokibbles), a game
originally written for Ludum Dare 32 with the theme 'An Unconventional Weapon'. The demo uses originally written for Ludum Dare 32 with the theme 'An Unconventional Weapon'. The demo uses
[LOVE](https://love2d.org/), an amazing game framework for lua. [LÖVE](https://love2d.org/), an amazing game framework for lua.
## Testing ## ## Testing ##
Tiny-ecs uses [busted](http://olivinelabs.com/busted/) for testing. Install and run Tiny-ecs uses [busted](http://olivinelabs.com/busted/) for testing. Install and run
@@ -85,3 +85,4 @@ Tiny-ecs uses [busted](http://olivinelabs.com/busted/) for testing. Install and
See API [here](http://bakpakin.github.io/tiny-ecs/doc/). See API [here](http://bakpakin.github.io/tiny-ecs/doc/).
For the most up-to-date documentation, read the source code, or generate the HTML For the most up-to-date documentation, read the source code, or generate the HTML
locally with [LDoc](http://stevedonovan.github.io/ldoc/). locally with [LDoc](http://stevedonovan.github.io/ldoc/).
See the original forum thread [here](https://love2d.org/forums/viewtopic.php?f=5&t=79937&p=182589).
+1 -1
View File
@@ -91,7 +91,7 @@ describe('tiny-ecs:', function()
local world, entity1, entity2, entity3 local world, entity1, entity2, entity3
local moveSystem = tiny.processingSystem() local moveSystem = tiny.system({}, {"process"})
moveSystem.filter = tiny.requireAll("xform", "vel") moveSystem.filter = tiny.requireAll("xform", "vel")
function moveSystem:process(e, dt) function moveSystem:process(e, dt)
local xform = e.xform local xform = e.xform
+66 -30
View File
@@ -30,7 +30,6 @@ local tinsert = table.insert
local tremove = table.remove local tremove = table.remove
local tsort = table.sort local tsort = table.sort
local pairs = pairs local pairs = pairs
local ipairs = ipairs
local setmetatable = setmetatable local setmetatable = setmetatable
local type = type local type = type
local select = select local select = select
@@ -224,15 +223,6 @@ local function isSystem(table)
return table[systemTableKey] return table[systemTableKey]
end end
--- Creates a default System.
-- @param table A table to be used as a System, or `nil` to create a new System.
-- @return A new System or System class
function tiny.system(table)
table = table or {}
table[systemTableKey] = true
return table
end
-- Update function for all Processing Systems. -- Update function for all Processing Systems.
local function processingSystemUpdate(system, dt) local function processingSystemUpdate(system, dt)
local entities = system.entities local entities = system.entities
@@ -258,6 +248,72 @@ local function processingSystemUpdate(system, dt)
end end
end end
-- Sorts Systems by a function system.sort(entity1, entity2) on modify.
local function sortedSystemOnModify(system, dt)
local entities = system.entities
local entityIndices = system.entityIndices
local sortDelegate = system.sortDelegate
if not sortDelegate then
local compare = system.compare
sortDelegate = function(e1, e2)
return compare(system, e1, e2)
end
system.sortDelegate = sortDelegate
end
tsort(entities, sortDelegate)
for i = 1, #entities do
local entity = entities[i]
entityIndices[entity] = i
end
end
-- Update function for Interval Systems.
local function intervalSystemUpdate(system, dt)
local interval = self.interval or 1
local intervalUpdate = self.intervalUpdate
local bufferedTime = (self.bufferedTime or 0) + dt
while bufferedTime >= interval do
bufferedTime = bufferedTime - interval
intervalUpdate(system, interval)
end
self.bufferedTime = bufferedTime
end
--- Creates a new System.
-- @param table A table to be used as a System, or `nil` to create a new System.
-- @param attributes An optional list of System attributes.
-- @return A new System or System class
function tiny.system(table, attributes)
table = table or {}
table[systemTableKey] = true
-- Get attributes into a table as the keys.
attributes = attributes or {}
attributesMap = {}
for i = 1, #attributes do
attributesMap[attributes[i]] = true
end
-- Set up Interval Systems.
local updateMethodName = "update"
if attributesMap.interval then
table.update = intervalSystemUpdate
updateMethodName = "intervalSystemUpdate"
end
-- Set up Sorted Systems.
if attributesMap.sorted then
table.onModify = sortedSystemOnModify
end
-- Set up Processing Systems.
if attributesMap.process then
table[updateMethodName] = processingSystemUpdate
end
return table
end
--- Creates a Processing System. --- Creates a Processing System.
-- --
-- A Processing System iterates through its Entities in no particluar order, and -- A Processing System iterates through its Entities in no particluar order, and
@@ -283,25 +339,6 @@ function tiny.processingSystem(table)
return table return table
end end
-- Sorts Systems by a function system.sort(entity1, entity2) on modify.
local function sortedSystemOnModify(system, dt)
local entities = system.entities
local entityIndices = system.entityIndices
local sortDelegate = system.sortDelegate
if not sortDelegate then
local compare = system.compare
sortDelegate = function(e1, e2)
compare(system, e1, e2)
end
system.sortDelegate = sortDelegate
end
tsort(entities, sortDelegate)
for i = 1, #entities do
local entity = entities[i]
entityIndices[entity] = i
end
end
--- Creates a Sorted Processing System. A Sorted System iterates through its --- Creates a Sorted Processing System. A Sorted System iterates through its
-- Entities in a specific order, and updates them individually. It has three -- Entities in a specific order, and updates them individually. It has three
-- important methods: -- important methods:
@@ -321,7 +358,6 @@ function tiny.sortedSystem(table)
table[systemTableKey] = true table[systemTableKey] = true
table.update = processingSystemUpdate table.update = processingSystemUpdate
table.onModify = sortedSystemOnModify table.onModify = sortedSystemOnModify
table.sort = sortedSystemOnModify
return table return table
end end