26 Commits

Author SHA1 Message Date
bakpakin
927652597c Update to version 1.1-4. 2015-06-19 19:08:06 -04:00
bakpakin
6b3af0f2da Remove 2.0-1 from existence. 2015-06-19 18:55:06 -04:00
bakpakin
31f3396db1 Change System API again.
Remove tiny.getSystemEntityCount because it's useless and deprecation is for chumps.
2015-06-19 18:48:18 -04:00
bakpakin
cb9ab332d2 Fix sorting + add sorting test. 2015-06-18 16:02:16 -04:00
bakpakin
1fd32dab9a Add "world" flag in Systems. Clean up Systems when removing them from Worlds. 2015-06-18 15:19:21 -04:00
bakpakin
0a12897695 Correct use of 'self' in intervalSystemUpdate 2015-06-17 10:35:47 -04:00
bakpakin
e44ac6cdb3 Update to version 2.0-1 2015-06-16 23:48:57 -04:00
bakpakin
6f299470d0 Add CHANGELOG.md 2015-06-16 23:44:19 -04:00
bakpakin
61ffd2551b Readd tiny.processingSystem(table). 2015-06-16 23:26:48 -04:00
bakpakin
c66ed98a5a Add License sticker to README.md. 2015-06-16 23:13:53 -04:00
bakpakin
2621bd16a3 Add tiny.getSystemEntityCount(system). 2015-06-16 22:51:23 -04:00
bakpakin
c408267932 Update documentation. 2015-06-16 22:39:10 -04:00
bakpakin
557927577d Fix sorting. Change API for creating Systems to be more flexible. 2015-06-16 21:15:30 -04:00
bakpakin
cbfc3d360f Add attributes to tiny.system. Implement Interval Systems. 2015-06-16 20:28:21 -04:00
bakpakin
d35d62ed3c Add optional filter to tiny.update(world, dt, [filter]) 2015-06-16 19:45:09 -04:00
bakpakin
74666ca0c3 Fix adding inactive Systems. 2015-06-16 19:14:56 -04:00
bakpakin
5978ace463 Update to version 1.1-3 2015-06-14 10:22:03 -04:00
bakpakin
a504bd6336 Add removeSystem to World metatable. 2015-06-14 10:17:18 -04:00
bakpakin
69ae37586e Fix scm rockspec. 2015-06-07 20:05:56 -04:00
bakpakin
44fb94c457 Add scm rockspec. Fix README. 2015-06-07 18:47:50 -04:00
bakpakin
9230dad1e1 Update README.md to direct users to demo. 2015-05-17 08:26:04 +08:00
bakpakin
e4bf3f3767 Update to version 1.1-2. 2015-05-07 19:53:51 +08:00
bakpakin
71e97c12f6 Improve System documentation. 2015-05-07 19:45:28 +08:00
bakpakin
7c4ff7747c Change names of tiny.requireOne and tiny.rejectOne to tiny.requireAny and tiny.rejectAny. 2015-05-07 17:32:32 +08:00
bakpakin
808407f7c4 Fix 'tiny.add' and 'tiny.remove' iterating nil values. 2015-05-05 22:21:39 +08:00
bakpakin
153f326612 Add two new filter functions - rejectAll and rejectOne. 2015-05-05 22:03:04 +08:00
8 changed files with 370 additions and 156 deletions
+12 -12
View File
@@ -1,13 +1,18 @@
# tiny-ecs #
[![License](http://img.shields.io/badge/Licence-MIT-brightgreen.svg)](LICENSE)
Tiny-ecs is an Entity Component System for lua that's simple, flexible, and useful.
Because of lua's tabular nature, Entity Component Systems are a natural choice
for simulating large and complex systems. For more explanation on Entity
Component Systems, here is some
[basic info](http://en.wikipedia.org/wiki/Entity_component_system "Wikipedia").
Tiny-ecs also works well with Objected Oriented programming in lua because
Tiny-ecs also works well with objected oriented programming in lua because
Systems and Entities do not use metatables. This means you can subclass your
Systems and Entities, and use existing lua class frameworks with tiny-ecs, no problem.
For an example on how to use tiny-ecs with object-oriented lua, take a look at the
demo branch, specifically the systems and entities sub-directories.
## Overview ##
Tiny-ecs has four important types: Worlds, Filters, Systems, and Entities.
@@ -29,7 +34,7 @@ Systems in tiny-ecs describe how to update Entities. Systems select certain Enti
using a Filter, and then only update those select Entities. Some Systems don't
update Entities, and instead just act as function callbacks every update. Tiny-ecs
provides functions for creating Systems easily, as well as creating Systems that
can be used in an Object Orientented fashion.
can be used in an object oriented fashion.
### Filters ###
Filters are used to select Entities. Filters can be any lua function, but
@@ -64,7 +69,7 @@ end
## Use It ##
Copy paste tiny.lua into your source folder. For stability and consistent API,
please use a tagged release or use luarocks. Tagged releases will have a version
number in `tiny._VERSION`, while other commits will just have the string 'dev'.
number in `tiny._VERSION`, while other commits will just have the string 'scm'.
## Luarocks ##
Tiny-ecs is also on [Luarocks](https://luarocks.org/) and can be installed with
@@ -73,7 +78,7 @@ Tiny-ecs is also on [Luarocks](https://luarocks.org/) and can be installed with
## Demo ##
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
[LOVE](https://love2d.org/), an amazing game framework for lua.
[LÖVE](https://love2d.org/), an amazing game framework for lua.
## Testing ##
Tiny-ecs uses [busted](http://olivinelabs.com/busted/) for testing. Install and run
@@ -81,11 +86,6 @@ Tiny-ecs uses [busted](http://olivinelabs.com/busted/) for testing. Install and
## Documentation ##
See API [here](http://bakpakin.github.io/tiny-ecs/doc/).
Documentation can be generated locally with [LDoc](http://stevedonovan.github.io/ldoc/).
## TODO ##
* More testing
* Performance testing / optimization
* Add more System types
* Improve Documentation
For the most up-to-date documentation, read the source code, or generate the HTML
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
View File
@@ -10,3 +10,4 @@ style = '!fixed'
package = 'tiny-ecs'
not_luadoc = true
boilerplate = true
no_return_or_parms = true
+24
View File
@@ -0,0 +1,24 @@
package = "tiny-ecs"
version = "1.1-2"
source = {
url = "git://github.com/bakpakin/tiny-ecs",
tag = "1.1-2"
}
description = {
summary = "Entity Component System for Lua.",
detailed = [[
Pure Lua implementation of an easy to use, compact, fast, and flexible
Entity Component System. Works well with Object Orientation.
]],
homepage = "https://github.com/bakpakin/tiny-ecs",
license = "MIT"
}
dependencies = {
"lua >= 5.1"
}
build = {
type = "builtin",
modules = {
tiny = "tiny.lua"
}
}
+24
View File
@@ -0,0 +1,24 @@
package = "tiny-ecs"
version = "1.1-3"
source = {
url = "git://github.com/bakpakin/tiny-ecs",
tag = "1.1-3"
}
description = {
summary = "Entity Component System for Lua.",
detailed = [[
Pure Lua implementation of an easy to use, compact, fast, and flexible
Entity Component System. Works well with Object Orientation.
]],
homepage = "https://github.com/bakpakin/tiny-ecs",
license = "MIT"
}
dependencies = {
"lua >= 5.1"
}
build = {
type = "builtin",
modules = {
tiny = "tiny.lua"
}
}
+24
View File
@@ -0,0 +1,24 @@
package = "tiny-ecs"
version = "1.1-4"
source = {
url = "git://github.com/bakpakin/tiny-ecs",
tag = "1.1-4"
}
description = {
summary = "Entity Component System for Lua.",
detailed = [[
Pure Lua implementation of an easy to use, compact, fast, and flexible
Entity Component System. Works well with Object Orientation.
]],
homepage = "https://github.com/bakpakin/tiny-ecs",
license = "MIT"
}
dependencies = {
"lua >= 5.1"
}
build = {
type = "builtin",
modules = {
tiny = "tiny.lua"
}
}
+29 -1
View File
@@ -56,7 +56,14 @@ describe('tiny-ecs:', function()
local ftap = tiny.requireAll("spinalTap")
local fvel = tiny.requireAll("vel")
local fxform = tiny.requireAll("xform")
local fall = tiny.requireOne("spinalTap", "onlyTen", "littleMan")
local fall = tiny.requireAny("spinalTap", "onlyTen", "littleMan")
-- Only select Entities without "spinalTap"
local frtap = tiny.rejectAny("spinalTap")
-- Select Entities without all three: "spinalTap", "onlyTen", and
-- "littleMan"
local frall = tiny.rejectAll("spinalTap", "onlyTen", "littleMan")
assert.truthy(fall(nil, entity1))
assert.truthy(ftap(nil, entity1))
@@ -68,6 +75,14 @@ describe('tiny-ecs:', function()
assert.truthy(fall(nil, entity2))
assert.truthy(fall(nil, entity3))
assert.falsy(frtap(nil, entity1))
assert.truthy(frtap(nil, entity2))
assert.truthy(frtap(nil, entity3))
assert.truthy(frall(nil, entity1))
assert.truthy(frall(nil, entity2))
assert.truthy(frall(nil, entity3))
end)
end)
@@ -200,6 +215,19 @@ describe('tiny-ecs:', function()
assert.equals(1, world:getSystemIndex(oneTimeSystem))
end)
it("Sorts Entities in Sorting Systems", function()
local sortsys = tiny.sortedProcessingSystem()
sortsys.filter = tiny.requireAll("vel")
function sortsys:compare(e1, e2)
return e1.vel.x < e2.vel.x
end
world:add(sortsys)
world:update(0)
assert.equals(sortsys.entities[1], entity2)
assert.equals(sortsys.entities[2], entity3)
assert.equals(sortsys.entities[3], entity1)
end)
end)
end)
+23
View File
@@ -0,0 +1,23 @@
package = "tiny-ecs"
version = "scm-0"
source = {
url = "git://github.com/bakpakin/tiny-ecs",
}
description = {
summary = "Entity Component System for Lua.",
detailed = [[
Pure Lua implementation of an easy to use, compact, fast, and flexible
Entity Component System. Works well with Object Orientation.
]],
homepage = "https://github.com/bakpakin/tiny-ecs",
license = "MIT"
}
dependencies = {
"lua >= 5.1"
}
build = {
type = "builtin",
modules = {
tiny = "tiny.lua"
}
}
+223 -133
View File
@@ -23,16 +23,16 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-- @author Calvin Rose
-- @license MIT
-- @copyright 2015
local tiny = { _VERSION = "1.1-1" }
local tiny = { _VERSION = "1.1-4" }
-- Local versions of standard lua functions
local tinsert = table.insert
local tremove = table.remove
local tsort = table.sort
local pairs = pairs
local ipairs = ipairs
local setmetatable = setmetatable
local type = type
local select = select
-- Local versions of the library functions
local tiny_manageEntities
@@ -50,11 +50,11 @@ local tiny_remove
-- value indicating if the Entity should be processed by the System.
--
-- Filters must be added to Systems by setting the `filter` field of the System.
-- Filter's returned by `tiny.requireAll` and `tiny.requireOne` are immutable
-- and can be used by multiple Systems.
-- Filter's returned by tiny-ecs's Filter functions are immutable and can be
-- used by multiple Systems.
--
-- local f1 = tiny.requireAll("position", "velocity", "size")
-- local f2 = tiny.requireOne("position", "velocity", "size")
-- local f2 = tiny.requireAny("position", "velocity", "size")
--
-- local e1 = {
-- position = {2, 3},
@@ -75,11 +75,18 @@ local tiny_remove
-- print(f1(nil, e1), f1(nil, e2), f1(nil, e3)) -- prints true, false, false
-- print(f2(nil, e1), f2(nil, e2), f2(nil, e3)) -- prints true, true, true
--
-- Filters can also be passed as arguments to other Filter constructors. This is
-- a powerful way to create complex, custom Filters that select a very specific
-- set of Entities.
--
-- -- Selects Entities with an "image" Component, but not Entities with a
-- -- "Player" or "Enemy" Component.
-- filter = tiny.requireAll("image", tiny.rejectAny("Player", "Enemy"))
--
-- @section Filter
--- Makes a Filter that selects Entities with all specified Components and
-- Filters.
-- @param ... List of required Components and other Filters.
function tiny.requireAll(...)
local components = {...}
local len = #components
@@ -101,8 +108,7 @@ end
--- Makes a Filter that selects Entities with at least one of the specified
-- Components and Filters.
-- @param ... List of required Components and other Filters.
function tiny.requireOne(...)
function tiny.requireAny(...)
local components = {...}
local len = #components
return function(system, e)
@@ -121,45 +127,107 @@ function tiny.requireOne(...)
end
end
--- System functions.
-- A System is a wrapper around function callbacks for manipulating Entities.
-- @section System
-- Use an empty table as a key for identifying Systems. Any table that contains
-- this key is considered a System rather than an Entity.
local systemTableKey = { "SYSTEM_TABLE_KEY" }
-- Check if tables are systems.
local function isSystem(table)
return table[systemTableKey]
--- Makes a Filter that rejects Entities with all specified Components and
-- Filters, and selects all other Entities.
function tiny.rejectAll(...)
local components = {...}
local len = #components
return function(system, e)
local c
for i = 1, len do
c = components[i]
if type(c) == 'function' then
if not c(system, e) then
return true
end
elseif e[c] == nil then
return true
end
end
return false
end
end
--- Creates a System. Systems are tables that contain at least one method;
--- Makes a Filter that rejects Entities with at least one of the specified
-- Components and Filters, and selects all other Entities.
function tiny.rejectAny(...)
local components = {...}
local len = #components
return function(system, e)
local c
for i = 1, len do
c = components[i]
if type(c) == 'function' then
if c(system, e) then
return false
end
elseif e[c] ~= nil then
return false
end
end
return true
end
end
--- System functions.
-- A System is a wrapper around function callbacks for manipulating Entities.
-- Systems are implemented as tables that contain at least one method;
-- an update function that takes parameters like so:
--
-- * `function system:update(dt)`.
--
-- There are also a few other optional callbacks:
--
-- * `function system:filter(entity)`
-- * `function system:onAdd(entity)`
-- * `function system:onRemove(entity)`
-- * `function system:onModify(dt)`
-- * `function system:filter(entity)` - Returns true if this System should
-- include this Entity, otherwise should return false. If this isn't specified,
-- no Entities are included in the System.
-- * `function system:onAdd(entity)` - Called when an Entity is added to the
-- System.
-- * `function system:onRemove(entity)` - Called when an Entity is removed
-- from the System.
-- * `function system:onModify(dt)` - Called when the System is modified by
-- adding or removing Entities from the System.
--
-- For Filters, it is conveient to use `tiny.requireAll` or `tiny.requireOne`,
-- but one can write their own filters as well. Set the Filter of your System
-- like so:
-- For Filters, it is convenient to use `tiny.requireAll` or `tiny.requireAny`,
-- but one can write their own filters as well. Set the Filter of a System like
-- so:
-- system.filter = tiny.requireAll("a", "b", "c")
-- or
-- function system:filter(entity)
-- return entity.myRequiredComponentName ~= nil
-- end
--
-- @param table A table to be used as a System, or `nil` to create a new System.
function tiny.system(table)
table = table or {}
table[systemTableKey] = true
return table
-- All Systems also have a few important fields that are initialized when the
-- system is added to the World. A few are important, and few should be less
-- commonly used.
--
-- * The `world` field points to the World that the System belongs to. Useful
-- for adding and removing Entities from the world dynamically via the System.
-- * The `active` flag is whether or not the System is updated automatically.
-- Inactive Systems should be updated manually or not at all via
-- `system:update(dt)`. Defaults to true.
-- * The 'entities' field is an ordered list of Entities in the System. This
-- list can be used to quickly iterate through all Entities in a System.
-- * The `interval` field is an optional field that makes Systems update at
-- certain intervals using buffered time, regardless of World update frequency.
-- For example, to make a System update once a second, set the System's interval
-- to 1.
-- * The `indices` field is a table of Entity keys to their indices in the
-- `entities` list. Most Systems can ignore this.
-- * The `modified` flag is an indicator if the System has been modified in
-- the last update. If so, the `onModify` callback will be called on the System
-- in the next update, if it has one. This is usually managed by tiny-ecs, so
-- users should mostly ignore this, too.
--
-- @section System
-- Use an empty table as a key for identifying Systems. Any table that contains
-- this key is considered a System rather than an Entity.
local systemTableKey = { "SYSTEM_TABLE_KEY" }
-- Checks if a table is a System.
local function isSystem(table)
return table[systemTableKey]
end
-- Update function for all Processing Systems.
@@ -187,22 +255,44 @@ local function processingSystemUpdate(system, 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)`
-- * `function system:filter(entity)`
--
-- There are also a few other optional callbacks, including the optional
-- callbacks in `tiny.system`:
--
-- * `function system:preProcess(entities, dt)`
-- * `function system:postProcess(entities, dt)`
--
-- @param table A table to be used as a System, or `nil` to create a new
-- Processing System.
-- Sorts Systems by a function system.sort(entity1, entity2) on modify.
local function sortedSystemOnModify(system, dt)
local entities = system.entities
local indices = system.indices
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]
indices[entity] = i
end
end
--- Creates a new System or System class from the supplied table. If `table` is
-- nil, creates a new table.
function tiny.system(table)
table = table or {}
table[systemTableKey] = true
return table
end
--- Creates a new Processing System or Processing System class. Processing
-- Systems process each entity individual, and are usually what is needed.
-- Processing Systems have three extra callbacks besides those inheritted from
-- vanilla Systems.
-- * `function system:preProcess(entities, dt)` - Called before iterating
-- over each Entity.
-- * `function system:process(entities, dt)` - Called for each Entity in
-- the System.
-- * `function system:postProcess(entity, dt)` - Called after iteration.
-- Processing Systems have their own `update` method, so don't implement a
-- a custom `update` callback for Processing Systems.
-- @see system
function tiny.processingSystem(table)
table = table or {}
@@ -211,44 +301,31 @@ function tiny.processingSystem(table)
return table
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
--- Creates a new Sorted System or Sorted System class. Sorted Systems sort
-- their Entities according to a user-defined method, `system:compare(e1, e2)`,
-- which should return true if `e1` should come before `e2` and false otherwise.
-- Sorted Systems also override the default System's `onModify` callback, so be
-- careful if defining a custom callback. However, for processing the sorted
-- entities, consider `tiny.sortedProcessingSystem(table)`.
-- @see system
function tiny.sortedSystem(table)
table = table or {}
table[systemTableKey] = true
table.onModify = sortedSystemOnModify
return table
end
--- Creates a Sorted Processing System. A Sorted System iterates through its
-- Entities in a specific order, and updates them individually. It has three
-- important methods:
--
-- * `function system:process(entity, dt)`
-- * `function system:compare(entity1, entity2)`
-- * `function system:filter(entity)`
--
-- Sorted Systems have the same optitonal callbacks as ProcessingSystems.
-- @param table A table to be used as a System, or `nil` to create a new
-- Sorted System.
--- Creates a new Sorted Processing System or Sorted Processing System class.
-- Sorted Processing Systems have both the aspects of Processing Systems and
-- Sorted Systems.
-- @see system
-- @see processingSystem
function tiny.sortedSystem(table)
-- @see sortedSystem
function tiny.sortedProcessingSystem(table)
table = table or {}
table[systemTableKey] = true
table.update = processingSystemUpdate
table.onModify = sortedSystemOnModify
table.sort = sortedSystemOnModify
return table
end
@@ -256,20 +333,17 @@ end
-- A World is a container that manages Entities and Systems. Typically, a
-- program uses one World at a time.
--
-- The tiny-ecs module is set to be the `__index` of all World tables, so the
-- often clearer syntax of `world:method()` can be used for any function in the
-- library. For example, `tiny.add(world, e1, e2, e3)` is the same as
-- `world:add(e1, e2, e3).`
-- For all World functions except `tiny.world(...)`, object-oriented syntax can
-- be used instead of the documented syntax. For example,
-- `tiny.add(world, e1, e2, e3)` is the same as `world:add(e1, e2, e3)`.
-- @section World
local worldMetaTable = { __index = tiny }
-- Forward declaration
local worldMetaTable
--- Creates a new World.
-- Can optionally add default Systems and Entities.
-- @param ... Systems and Entities to add to the World
-- @return A new World
function tiny.world(...)
local ret = {
-- List of Entities to add
@@ -303,14 +377,11 @@ function tiny.world(...)
tiny_manageEntities(ret)
return setmetatable(ret, worldMetaTable)
end
--- Adds an Entity to the world.
-- Also call this on Entities that have changed Components such that they
-- match different Filters.
-- @param world
-- @param entity
function tiny.addEntity(world, entity)
local e2a = world.entitiesToAdd
e2a[#e2a + 1] = entity
@@ -321,8 +392,6 @@ end
tiny_addEntity = tiny.addEntity
--- Adds a System to the world.
-- @param world
-- @param system
function tiny.addSystem(world, system)
local s2a = world.systemsToAdd
s2a[#s2a + 1] = system
@@ -330,25 +399,22 @@ end
tiny_addSystem = tiny.addSystem
--- Shortcut for adding multiple Entities and Systems to the World.
-- @param world
-- @param ... Systems and Entities
-- @see addEntity
-- @see addSystem
function tiny.add(world, ...)
local args = {...}
for _, obj in ipairs(args) do
local obj
for i = 1, select("#", ...) do
obj = select(i, ...)
if obj then
if isSystem(obj) then
tiny_addSystem(world, obj)
else -- Assume obj is an Entity
tiny_addEntity(world, obj)
end
end
end
end
tiny_add = tiny.add
--- Removes an Entity to the World.
-- @param world
-- @param entity
function tiny.removeEntity(world, entity)
local e2r = world.entitiesToRemove
e2r[#e2r + 1] = entity
@@ -356,8 +422,6 @@ end
tiny_removeEntity = tiny.removeEntity
--- Removes a System from the world.
-- @param world
-- @param system
function tiny.removeSystem(world, system)
local s2r = world.systemsToRemove
s2r[#s2r + 1] = system
@@ -365,25 +429,23 @@ end
tiny_removeSystem = tiny.removeSystem
--- Shortcut for removing multiple Entities and Systems from the World.
-- @param world
-- @param ... Systems and Entities
-- @see removeEntity
-- @see removeSystem
function tiny.remove(world, ...)
local args = {...}
for _, obj in ipairs(args) do
local obj
for i = 1, select("#", ...) do
obj = select(i, ...)
if obj then
if isSystem(obj) then
tiny_removeSystem(world, obj)
else -- Assume obj is an Entity
tiny_removeEntity(world, obj)
end
end
end
end
tiny_remove = tiny.remove
-- Adds and removes Systems that have been marked from the World.
function tiny_manageSystems(world)
local s2a, s2r = world.systemsToAdd, world.systemsToRemove
-- Early exit
@@ -416,6 +478,11 @@ function tiny_manageSystems(world)
end
end
s2r[i] = nil
-- Clean up System
system.world = nil
system.entities = nil
system.indices = nil
end
-- Add Systems
@@ -426,8 +493,11 @@ function tiny_manageSystems(world)
entityIndices = {}
system.entities = entityList
system.indices = entityIndices
if system.active == nil then
system.active = true
end
system.modified = true
system.world = world
index = #systems + 1
systemIndices[system] = index
systems[index] = system
@@ -450,12 +520,10 @@ function tiny_manageSystems(world)
end
s2a[i] = nil
end
end
-- Adds and removes Entities that have been marked.
function tiny_manageEntities(world)
local e2a, e2r = world.entitiesToAdd, world.entitiesToRemove
-- Early exit
@@ -501,7 +569,6 @@ function tiny_manageEntities(world)
e2r[i] = nil
end
-- Add Entities
for i = 1, #e2a do
entity = e2a[i]
@@ -533,25 +600,24 @@ function tiny_manageEntities(world)
-- Update Entity count
world.entityCount = entityCount
end
--- Updates the World.
-- Put this in your main loop.
-- @param world
-- @param dt Delta time
function tiny.update(world, dt)
--- Updates the World by dt (delta time). Takes an optional parameter, `filter`,
-- which is a Filter that selects Systems from the World, and updates only those
-- Systems. If `filter` is not supplied, all Systems are updated. Put this
-- function in your main loop.
function tiny.update(world, dt, filter)
tiny_manageSystems(world)
tiny_manageEntities(world)
local systems = world.systems
local system, update, onModify, entities
local system, update, interval, onModify
-- Iterate through Systems IN ORDER
for i = 1, #systems do
system = systems[i]
if system.active then
if system.active and ((not filter) or filter(world, system)) then
-- Call the modify callback on Systems that have been modified.
onModify = system.onModify
@@ -562,8 +628,20 @@ function tiny.update(world, dt)
--Update Systems that have an update method (most Systems)
update = system.update
if update then
interval = system.interval
if interval then
local bufferedTime = (system.bufferedTime or 0) + dt
while bufferedTime >= interval do
bufferedTime = bufferedTime - interval
if update then
update(system, interval)
end
end
system.bufferedTime = bufferedTime
else
update(system, dt)
end
end
system.modified = false
end
@@ -571,7 +649,6 @@ function tiny.update(world, dt)
end
--- Removes all Entities from the World.
-- @param world
function tiny.clearEntities(world)
for e in pairs(world.entities) do
tiny_removeEntity(world, e)
@@ -579,7 +656,6 @@ function tiny.clearEntities(world)
end
--- Removes all Systems from the World.
-- @param world
function tiny.clearSystems(world)
local systems = world.systems
for i = #systems, 1, -1 do
@@ -587,32 +663,25 @@ function tiny.clearSystems(world)
end
end
--- Gets count of Entities in World.
-- @param world
--- Gets number of Entities in the World.
function tiny.getEntityCount(world)
return world.entityCount
end
--- Gets count of Systems in World.
-- @param world
--- Gets number of Systems in World.
function tiny.getSystemCount(world)
return #(world.systems)
end
--- Gets the index of a System in the World. Lower indexed Systems are processed
-- before higher indexed systems.
-- @param world
-- @param system
function tiny.getSystemIndex(world, system)
return world.systemIndices[system]
end
--- Sets the index of a System in the World. Changes the order in
-- which they Systems processed, because lower indexed Systems are processed
-- first.
-- @param world
-- @param system
-- @param index
--- Sets the index of a System in the World, and returns the old index. Changes
-- the order in which they Systems processed, because lower indexed Systems are
-- processed first.
function tiny.setSystemIndex(world, system, index)
local systemIndices = world.systemIndices
local oldIndex = systemIndices[system]
@@ -625,6 +694,27 @@ function tiny.setSystemIndex(world, system, index)
for i = oldIndex, index, index >= oldIndex and 1 or -1 do
systemIndices[systems[i]] = i
end
return oldIndex
end
-- Construct world metatable.
worldMetaTable = {
__index = {
add = tiny.add,
addEntity = tiny.addEntity,
addSystem = tiny.addSystem,
remove = tiny.remove,
removeEntity = tiny.removeEntity,
removeSystem = tiny.removeSystem,
update = tiny.update,
clearEntities = tiny.clearEntities,
clearSystems = tiny.clearSystems,
getEntityCount = tiny.getEntityCount,
getSystemCount = tiny.getSystemCount,
getSystemIndex = tiny.getSystemIndex,
setSystemIndex = tiny.setSystemIndex
}
}
return tiny