mirror of
https://github.com/bakpakin/tiny-ecs.git
synced 2026-07-22 07:56:52 -06:00
Compare commits
34 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b1c7e2ba83 | ||
|
|
6b549fcbc1 | ||
|
|
b9d994ad50 | ||
|
|
ce8ca35f7e | ||
|
|
19ae03707f | ||
|
|
c1f1225248 | ||
|
|
45e8552054 | ||
|
|
f187dce4fd | ||
|
|
927652597c | ||
|
|
6b3af0f2da | ||
|
|
31f3396db1 | ||
|
|
cb9ab332d2 | ||
|
|
1fd32dab9a | ||
|
|
0a12897695 | ||
|
|
e44ac6cdb3 | ||
|
|
6f299470d0 | ||
|
|
61ffd2551b | ||
|
|
c66ed98a5a | ||
|
|
2621bd16a3 | ||
|
|
c408267932 | ||
|
|
557927577d | ||
|
|
cbfc3d360f | ||
|
|
d35d62ed3c | ||
|
|
74666ca0c3 | ||
|
|
5978ace463 | ||
|
|
a504bd6336 | ||
|
|
69ae37586e | ||
|
|
44fb94c457 | ||
|
|
9230dad1e1 | ||
|
|
e4bf3f3767 | ||
|
|
71e97c12f6 | ||
|
|
7c4ff7747c | ||
|
|
808407f7c4 | ||
|
|
153f326612 |
@@ -1,13 +1,18 @@
|
|||||||
# tiny-ecs #
|
# tiny-ecs #
|
||||||
|
|
||||||
|
[](LICENSE)
|
||||||
|
|
||||||
Tiny-ecs is an Entity Component System for lua that's simple, flexible, and useful.
|
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
|
Because of lua's tabular nature, Entity Component Systems are a natural choice
|
||||||
for simulating large and complex systems. For more explanation on Entity
|
for simulating large and complex systems. For more explanation on Entity
|
||||||
Component Systems, here is some
|
Component Systems, here is some
|
||||||
[basic info](http://en.wikipedia.org/wiki/Entity_component_system "Wikipedia").
|
[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 do not use metatables. This means you can subclass your
|
||||||
Systems and Entities, and use existing lua class frameworks with tiny-ecs, no problem.
|
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 ##
|
## Overview ##
|
||||||
Tiny-ecs has four important types: Worlds, Filters, Systems, and Entities.
|
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
|
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
|
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
|
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 ###
|
||||||
Filters are used to select Entities. Filters can be any lua function, but
|
Filters are used to select Entities. Filters can be any lua function, but
|
||||||
@@ -64,7 +69,7 @@ end
|
|||||||
## Use It ##
|
## Use It ##
|
||||||
Copy paste tiny.lua into your source folder. For stability and consistent API,
|
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
|
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 ##
|
## Luarocks ##
|
||||||
Tiny-ecs is also on [Luarocks](https://luarocks.org/) and can be installed with
|
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 ##
|
## 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
|
||||||
@@ -81,11 +86,6 @@ Tiny-ecs uses [busted](http://olivinelabs.com/busted/) for testing. Install and
|
|||||||
|
|
||||||
## Documentation ##
|
## Documentation ##
|
||||||
See API [here](http://bakpakin.github.io/tiny-ecs/doc/).
|
See API [here](http://bakpakin.github.io/tiny-ecs/doc/).
|
||||||
Documentation can be generated locally with [LDoc](http://stevedonovan.github.io/ldoc/).
|
For the most up-to-date documentation, read the source code, or generate the HTML
|
||||||
|
locally with [LDoc](http://stevedonovan.github.io/ldoc/).
|
||||||
## TODO ##
|
See the original forum thread [here](https://love2d.org/forums/viewtopic.php?f=5&t=79937&p=182589).
|
||||||
|
|
||||||
* More testing
|
|
||||||
* Performance testing / optimization
|
|
||||||
* Add more System types
|
|
||||||
* Improve Documentation
|
|
||||||
|
|||||||
@@ -10,3 +10,4 @@ style = '!fixed'
|
|||||||
package = 'tiny-ecs'
|
package = 'tiny-ecs'
|
||||||
not_luadoc = true
|
not_luadoc = true
|
||||||
boilerplate = true
|
boilerplate = true
|
||||||
|
no_return_or_parms = true
|
||||||
|
|||||||
@@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package = "tiny-ecs"
|
||||||
|
version = "1.1-5"
|
||||||
|
source = {
|
||||||
|
url = "git://github.com/bakpakin/tiny-ecs",
|
||||||
|
tag = "1.1-5"
|
||||||
|
}
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package = "tiny-ecs"
|
||||||
|
version = "1.1-6"
|
||||||
|
source = {
|
||||||
|
url = "git://github.com/bakpakin/tiny-ecs",
|
||||||
|
tag = "1.1-6"
|
||||||
|
}
|
||||||
|
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"
|
||||||
|
}
|
||||||
|
}
|
||||||
+31
-3
@@ -56,7 +56,14 @@ describe('tiny-ecs:', function()
|
|||||||
local ftap = tiny.requireAll("spinalTap")
|
local ftap = tiny.requireAll("spinalTap")
|
||||||
local fvel = tiny.requireAll("vel")
|
local fvel = tiny.requireAll("vel")
|
||||||
local fxform = tiny.requireAll("xform")
|
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(fall(nil, entity1))
|
||||||
assert.truthy(ftap(nil, entity1))
|
assert.truthy(ftap(nil, entity1))
|
||||||
@@ -68,6 +75,14 @@ describe('tiny-ecs:', function()
|
|||||||
assert.truthy(fall(nil, entity2))
|
assert.truthy(fall(nil, entity2))
|
||||||
assert.truthy(fall(nil, entity3))
|
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)
|
||||||
|
|
||||||
end)
|
end)
|
||||||
@@ -196,8 +211,21 @@ describe('tiny-ecs:', function()
|
|||||||
world:update(1)
|
world:update(1)
|
||||||
world:setSystemIndex(moveSystem, 2)
|
world:setSystemIndex(moveSystem, 2)
|
||||||
world:update(1)
|
world:update(1)
|
||||||
assert.equals(2, world:getSystemIndex(moveSystem))
|
assert.equals(2, moveSystem.index)
|
||||||
assert.equals(1, world:getSystemIndex(oneTimeSystem))
|
assert.equals(1, oneTimeSystem.index)
|
||||||
|
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)
|
end)
|
||||||
|
|||||||
@@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,16 +23,16 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||||||
-- @author Calvin Rose
|
-- @author Calvin Rose
|
||||||
-- @license MIT
|
-- @license MIT
|
||||||
-- @copyright 2015
|
-- @copyright 2015
|
||||||
local tiny = { _VERSION = "1.1-1" }
|
local tiny = { _VERSION = "1.1-6" }
|
||||||
|
|
||||||
-- 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 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 versions of the library functions
|
-- Local versions of the library functions
|
||||||
local tiny_manageEntities
|
local tiny_manageEntities
|
||||||
@@ -50,11 +50,11 @@ local tiny_remove
|
|||||||
-- value indicating if the Entity should be processed by the System.
|
-- 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.
|
-- 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
|
-- Filter's returned by tiny-ecs's Filter functions are immutable and can be
|
||||||
-- and can be used by multiple Systems.
|
-- used by multiple Systems.
|
||||||
--
|
--
|
||||||
-- local f1 = tiny.requireAll("position", "velocity", "size")
|
-- local f1 = tiny.requireAll("position", "velocity", "size")
|
||||||
-- local f2 = tiny.requireOne("position", "velocity", "size")
|
-- local f2 = tiny.requireAny("position", "velocity", "size")
|
||||||
--
|
--
|
||||||
-- local e1 = {
|
-- local e1 = {
|
||||||
-- position = {2, 3},
|
-- 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(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
|
-- 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
|
-- @section Filter
|
||||||
|
|
||||||
--- Makes a Filter that selects Entities with all specified Components and
|
--- Makes a Filter that selects Entities with all specified Components and
|
||||||
-- Filters.
|
-- Filters.
|
||||||
-- @param ... List of required Components and other Filters.
|
|
||||||
function tiny.requireAll(...)
|
function tiny.requireAll(...)
|
||||||
local components = {...}
|
local components = {...}
|
||||||
local len = #components
|
local len = #components
|
||||||
@@ -101,8 +108,7 @@ end
|
|||||||
|
|
||||||
--- Makes a Filter that selects Entities with at least one of the specified
|
--- Makes a Filter that selects Entities with at least one of the specified
|
||||||
-- Components and Filters.
|
-- Components and Filters.
|
||||||
-- @param ... List of required Components and other Filters.
|
function tiny.requireAny(...)
|
||||||
function tiny.requireOne(...)
|
|
||||||
local components = {...}
|
local components = {...}
|
||||||
local len = #components
|
local len = #components
|
||||||
return function(system, e)
|
return function(system, e)
|
||||||
@@ -121,45 +127,110 @@ function tiny.requireOne(...)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- System functions.
|
--- Makes a Filter that rejects Entities with all specified Components and
|
||||||
-- A System is a wrapper around function callbacks for manipulating Entities.
|
-- Filters, and selects all other Entities.
|
||||||
-- @section System
|
function tiny.rejectAll(...)
|
||||||
|
local components = {...}
|
||||||
-- Use an empty table as a key for identifying Systems. Any table that contains
|
local len = #components
|
||||||
-- this key is considered a System rather than an Entity.
|
return function(system, e)
|
||||||
local systemTableKey = { "SYSTEM_TABLE_KEY" }
|
local c
|
||||||
|
for i = 1, len do
|
||||||
-- Check if tables are systems.
|
c = components[i]
|
||||||
local function isSystem(table)
|
if type(c) == 'function' then
|
||||||
return table[systemTableKey]
|
if not c(system, e) then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
elseif e[c] == nil then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
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:
|
-- an update function that takes parameters like so:
|
||||||
--
|
--
|
||||||
-- * `function system:update(dt)`.
|
-- * `function system:update(dt)`.
|
||||||
--
|
--
|
||||||
-- There are also a few other optional callbacks:
|
-- There are also a few other optional callbacks:
|
||||||
--
|
--
|
||||||
-- * `function system:filter(entity)`
|
-- * `function system:filter(entity)` - Returns true if this System should
|
||||||
-- * `function system:onAdd(entity)`
|
-- include this Entity, otherwise should return false. If this isn't specified,
|
||||||
-- * `function system:onRemove(entity)`
|
-- no Entities are included in the System.
|
||||||
-- * `function system:onModify(dt)`
|
-- * `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`,
|
-- 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 your System
|
-- but one can write their own filters as well. Set the Filter of a System like
|
||||||
-- like so:
|
-- so:
|
||||||
-- system.filter = tiny.requireAll("a", "b", "c")
|
-- system.filter = tiny.requireAll("a", "b", "c")
|
||||||
-- or
|
-- or
|
||||||
-- function system:filter(entity)
|
-- function system:filter(entity)
|
||||||
-- return entity.myRequiredComponentName ~= nil
|
-- return entity.myRequiredComponentName ~= nil
|
||||||
-- end
|
-- end
|
||||||
--
|
--
|
||||||
-- @param table A table to be used as a System, or `nil` to create a new System.
|
-- All Systems also have a few important fields that are initialized when the
|
||||||
function tiny.system(table)
|
-- system is added to the World. A few are important, and few should be less
|
||||||
table = table or {}
|
-- commonly used.
|
||||||
table[systemTableKey] = true
|
--
|
||||||
return table
|
-- * 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 `index` field is the System's index in the World. Lower indexed
|
||||||
|
-- Systems are processed before higher indices. The `index` is a read only
|
||||||
|
-- field; to set the `index`, use `tiny.setSystemIndex(world, system)`.
|
||||||
|
-- * 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
|
end
|
||||||
|
|
||||||
-- Update function for all Processing Systems.
|
-- Update function for all Processing Systems.
|
||||||
@@ -187,22 +258,44 @@ local function processingSystemUpdate(system, dt)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Creates a 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.
|
||||||
--
|
--
|
||||||
-- A Processing System iterates through its Entities in no particluar order, and
|
-- function system:preProcess(entities, dt) -- Called before iteration.
|
||||||
-- updates them individually. It has two important fields:
|
-- function system:process(entities, dt) -- Process each entity.
|
||||||
|
-- function system:postProcess(entity, dt) -- Called after iteration.
|
||||||
--
|
--
|
||||||
-- * `function system:process(entity, dt)`
|
-- Processing Systems have their own `update` method, so don't implement a
|
||||||
-- * `function system:filter(entity)`
|
-- a custom `update` callback for Processing Systems.
|
||||||
--
|
|
||||||
-- 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.
|
|
||||||
-- @see system
|
-- @see system
|
||||||
function tiny.processingSystem(table)
|
function tiny.processingSystem(table)
|
||||||
table = table or {}
|
table = table or {}
|
||||||
@@ -211,44 +304,31 @@ function tiny.processingSystem(table)
|
|||||||
return table
|
return table
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Sorts Systems by a function system.sort(entity1, entity2) on modify.
|
--- Creates a new Sorted System or Sorted System class. Sorted Systems sort
|
||||||
local function sortedSystemOnModify(system, dt)
|
-- their Entities according to a user-defined method, `system:compare(e1, e2)`,
|
||||||
local entities = system.entities
|
-- which should return true if `e1` should come before `e2` and false otherwise.
|
||||||
local entityIndices = system.entityIndices
|
-- Sorted Systems also override the default System's `onModify` callback, so be
|
||||||
local sortDelegate = system.sortDelegate
|
-- careful if defining a custom callback. However, for processing the sorted
|
||||||
if not sortDelegate then
|
-- entities, consider `tiny.sortedProcessingSystem(table)`.
|
||||||
local compare = system.compare
|
-- @see system
|
||||||
sortDelegate = function(e1, e2)
|
function tiny.sortedSystem(table)
|
||||||
compare(system, e1, e2)
|
table = table or {}
|
||||||
end
|
table[systemTableKey] = true
|
||||||
system.sortDelegate = sortDelegate
|
table.onModify = sortedSystemOnModify
|
||||||
end
|
return table
|
||||||
tsort(entities, sortDelegate)
|
|
||||||
for i = 1, #entities do
|
|
||||||
local entity = entities[i]
|
|
||||||
entityIndices[entity] = i
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Creates a Sorted Processing System. A Sorted System iterates through its
|
--- Creates a new Sorted Processing System or Sorted Processing System class.
|
||||||
-- Entities in a specific order, and updates them individually. It has three
|
-- Sorted Processing Systems have both the aspects of Processing Systems and
|
||||||
-- important methods:
|
-- Sorted Systems.
|
||||||
--
|
|
||||||
-- * `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.
|
|
||||||
-- @see system
|
-- @see system
|
||||||
-- @see processingSystem
|
-- @see processingSystem
|
||||||
function tiny.sortedSystem(table)
|
-- @see sortedSystem
|
||||||
|
function tiny.sortedProcessingSystem(table)
|
||||||
table = table or {}
|
table = table or {}
|
||||||
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
|
||||||
|
|
||||||
@@ -256,20 +336,18 @@ end
|
|||||||
-- A World is a container that manages Entities and Systems. Typically, a
|
-- A World is a container that manages Entities and Systems. Typically, a
|
||||||
-- program uses one World at a time.
|
-- program uses one World at a time.
|
||||||
--
|
--
|
||||||
-- The tiny-ecs module is set to be the `__index` of all World tables, so the
|
-- For all World functions except `tiny.world(...)`, object-oriented syntax can
|
||||||
-- often clearer syntax of `world:method()` can be used for any function in the
|
-- be used instead of the documented syntax. For example,
|
||||||
-- library. For example, `tiny.add(world, e1, e2, e3)` is the same as
|
-- `tiny.add(world, e1, e2, e3)` is the same as `world:add(e1, e2, e3)`.
|
||||||
-- `world:add(e1, e2, e3).`
|
|
||||||
-- @section World
|
-- @section World
|
||||||
|
|
||||||
local worldMetaTable = { __index = tiny }
|
-- Forward declaration
|
||||||
|
local worldMetaTable
|
||||||
|
|
||||||
--- Creates a new World.
|
--- Creates a new World.
|
||||||
-- Can optionally add default Systems and Entities.
|
-- Can optionally add default Systems and Entities. Returns the new World along
|
||||||
-- @param ... Systems and Entities to add to the World
|
-- with default Entities and Systems.
|
||||||
-- @return A new World
|
|
||||||
function tiny.world(...)
|
function tiny.world(...)
|
||||||
|
|
||||||
local ret = {
|
local ret = {
|
||||||
|
|
||||||
-- List of Entities to add
|
-- List of Entities to add
|
||||||
@@ -287,103 +365,95 @@ function tiny.world(...)
|
|||||||
-- Set of Entities
|
-- Set of Entities
|
||||||
entities = {},
|
entities = {},
|
||||||
|
|
||||||
-- Number of Entities in World.
|
-- Number of Entities in World
|
||||||
entityCount = 0,
|
entityCount = 0,
|
||||||
|
|
||||||
-- List of System Data. A data element is a table with 4
|
-- List of Systems
|
||||||
-- keys: system, indices, entities, and active.
|
systems = {}
|
||||||
systems = {},
|
|
||||||
|
|
||||||
-- Table of Systems to System Indices
|
|
||||||
systemIndices = {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tiny_add(ret, ...)
|
tiny_add(ret, ...)
|
||||||
tiny_manageSystems(ret)
|
tiny_manageSystems(ret)
|
||||||
tiny_manageEntities(ret)
|
tiny_manageEntities(ret)
|
||||||
|
|
||||||
return setmetatable(ret, worldMetaTable)
|
return setmetatable(ret, worldMetaTable), ...
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Adds an Entity to the world.
|
--- Adds an Entity to the world.
|
||||||
-- Also call this on Entities that have changed Components such that they
|
-- Also call this on Entities that have changed Components such that they
|
||||||
-- match different Filters.
|
-- match different Filters. Returns the Entity.
|
||||||
-- @param world
|
|
||||||
-- @param entity
|
|
||||||
function tiny.addEntity(world, entity)
|
function tiny.addEntity(world, entity)
|
||||||
local e2a = world.entitiesToAdd
|
local e2a = world.entitiesToAdd
|
||||||
e2a[#e2a + 1] = entity
|
e2a[#e2a + 1] = entity
|
||||||
if world.entities[entity] then
|
if world.entities[entity] then
|
||||||
tiny_removeEntity(world, entity)
|
tiny_removeEntity(world, entity)
|
||||||
end
|
end
|
||||||
|
return entity
|
||||||
end
|
end
|
||||||
tiny_addEntity = tiny.addEntity
|
tiny_addEntity = tiny.addEntity
|
||||||
|
|
||||||
--- Adds a System to the world.
|
--- Adds a System to the world. Returns the System.
|
||||||
-- @param world
|
|
||||||
-- @param system
|
|
||||||
function tiny.addSystem(world, system)
|
function tiny.addSystem(world, system)
|
||||||
local s2a = world.systemsToAdd
|
local s2a = world.systemsToAdd
|
||||||
s2a[#s2a + 1] = system
|
s2a[#s2a + 1] = system
|
||||||
|
return system
|
||||||
end
|
end
|
||||||
tiny_addSystem = tiny.addSystem
|
tiny_addSystem = tiny.addSystem
|
||||||
|
|
||||||
--- Shortcut for adding multiple Entities and Systems to the World.
|
--- Shortcut for adding multiple Entities and Systems to the World. Returns all
|
||||||
-- @param world
|
-- added Entities and Systems.
|
||||||
-- @param ... Systems and Entities
|
|
||||||
-- @see addEntity
|
|
||||||
-- @see addSystem
|
|
||||||
function tiny.add(world, ...)
|
function tiny.add(world, ...)
|
||||||
local args = {...}
|
local obj
|
||||||
for _, obj in ipairs(args) do
|
for i = 1, select("#", ...) do
|
||||||
if isSystem(obj) then
|
obj = select(i, ...)
|
||||||
tiny_addSystem(world, obj)
|
if obj then
|
||||||
else -- Assume obj is an Entity
|
if isSystem(obj) then
|
||||||
tiny_addEntity(world, obj)
|
tiny_addSystem(world, obj)
|
||||||
|
else -- Assume obj is an Entity
|
||||||
|
tiny_addEntity(world, obj)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
return ...
|
||||||
end
|
end
|
||||||
tiny_add = tiny.add
|
tiny_add = tiny.add
|
||||||
|
|
||||||
--- Removes an Entity to the World.
|
--- Removes an Entity to the World. Returns the Entity.
|
||||||
-- @param world
|
|
||||||
-- @param entity
|
|
||||||
function tiny.removeEntity(world, entity)
|
function tiny.removeEntity(world, entity)
|
||||||
local e2r = world.entitiesToRemove
|
local e2r = world.entitiesToRemove
|
||||||
e2r[#e2r + 1] = entity
|
e2r[#e2r + 1] = entity
|
||||||
|
return entity
|
||||||
end
|
end
|
||||||
tiny_removeEntity = tiny.removeEntity
|
tiny_removeEntity = tiny.removeEntity
|
||||||
|
|
||||||
--- Removes a System from the world.
|
--- Removes a System from the world. Returns the System.
|
||||||
-- @param world
|
|
||||||
-- @param system
|
|
||||||
function tiny.removeSystem(world, system)
|
function tiny.removeSystem(world, system)
|
||||||
local s2r = world.systemsToRemove
|
local s2r = world.systemsToRemove
|
||||||
s2r[#s2r + 1] = system
|
s2r[#s2r + 1] = system
|
||||||
|
return system
|
||||||
end
|
end
|
||||||
tiny_removeSystem = tiny.removeSystem
|
tiny_removeSystem = tiny.removeSystem
|
||||||
|
|
||||||
--- Shortcut for removing multiple Entities and Systems from the World.
|
--- Shortcut for removing multiple Entities and Systems from the World. Returns
|
||||||
-- @param world
|
-- all rmeove Systems and Entities
|
||||||
-- @param ... Systems and Entities
|
|
||||||
-- @see removeEntity
|
|
||||||
-- @see removeSystem
|
|
||||||
function tiny.remove(world, ...)
|
function tiny.remove(world, ...)
|
||||||
local args = {...}
|
local obj
|
||||||
for _, obj in ipairs(args) do
|
for i = 1, select("#", ...) do
|
||||||
if isSystem(obj) then
|
obj = select(i, ...)
|
||||||
tiny_removeSystem(world, obj)
|
if obj then
|
||||||
else -- Assume obj is an Entity
|
if isSystem(obj) then
|
||||||
tiny_removeEntity(world, obj)
|
tiny_removeSystem(world, obj)
|
||||||
|
else -- Assume obj is an Entity
|
||||||
|
tiny_removeEntity(world, obj)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
return ...
|
||||||
end
|
end
|
||||||
tiny_remove = tiny.remove
|
tiny_remove = tiny.remove
|
||||||
|
|
||||||
-- 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)
|
||||||
|
|
||||||
local s2a, s2r = world.systemsToAdd, world.systemsToRemove
|
local s2a, s2r = world.systemsToAdd, world.systemsToRemove
|
||||||
|
|
||||||
-- Early exit
|
-- Early exit
|
||||||
@@ -391,7 +461,6 @@ function tiny_manageSystems(world)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local systemIndices = world.systemIndices
|
|
||||||
local entities = world.entities
|
local entities = world.entities
|
||||||
local systems = world.systems
|
local systems = world.systems
|
||||||
local system, index, filter
|
local system, index, filter
|
||||||
@@ -400,8 +469,8 @@ function tiny_manageSystems(world)
|
|||||||
-- Remove Systems
|
-- Remove Systems
|
||||||
for i = 1, #s2r do
|
for i = 1, #s2r do
|
||||||
system = s2r[i]
|
system = s2r[i]
|
||||||
index = systemIndices[system]
|
index = system.index
|
||||||
if index then
|
if system.world == world then
|
||||||
onRemove = system.onRemove
|
onRemove = system.onRemove
|
||||||
if onRemove then
|
if onRemove then
|
||||||
entityList = system.entities
|
entityList = system.entities
|
||||||
@@ -409,27 +478,35 @@ function tiny_manageSystems(world)
|
|||||||
onRemove(system, entityList[j])
|
onRemove(system, entityList[j])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
systemIndices[system] = nil
|
|
||||||
tremove(systems, index)
|
tremove(systems, index)
|
||||||
for j = index, #systems do
|
for j = index, #systems do
|
||||||
systemIndices[systems[j]] = j
|
systems[j].index = j
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
s2r[i] = nil
|
s2r[i] = nil
|
||||||
|
|
||||||
|
-- Clean up System
|
||||||
|
system.world = nil
|
||||||
|
system.entities = nil
|
||||||
|
system.indices = nil
|
||||||
|
system.index = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Add Systems
|
-- Add Systems
|
||||||
for i = 1, #s2a do
|
for i = 1, #s2a do
|
||||||
system = s2a[i]
|
system = s2a[i]
|
||||||
if not systemIndices[system] then
|
if systems[system.index] ~= system then
|
||||||
entityList = {}
|
entityList = {}
|
||||||
entityIndices = {}
|
entityIndices = {}
|
||||||
system.entities = entityList
|
system.entities = entityList
|
||||||
system.indices = entityIndices
|
system.indices = entityIndices
|
||||||
system.active = true
|
if system.active == nil then
|
||||||
|
system.active = true
|
||||||
|
end
|
||||||
system.modified = true
|
system.modified = true
|
||||||
|
system.world = world
|
||||||
index = #systems + 1
|
index = #systems + 1
|
||||||
systemIndices[system] = index
|
system.index = index
|
||||||
systems[index] = system
|
systems[index] = system
|
||||||
|
|
||||||
-- Try to add Entities
|
-- Try to add Entities
|
||||||
@@ -450,12 +527,10 @@ function tiny_manageSystems(world)
|
|||||||
end
|
end
|
||||||
s2a[i] = nil
|
s2a[i] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Adds and removes Entities that have been marked.
|
-- Adds and removes Entities that have been marked.
|
||||||
function tiny_manageEntities(world)
|
function tiny_manageEntities(world)
|
||||||
|
|
||||||
local e2a, e2r = world.entitiesToAdd, world.entitiesToRemove
|
local e2a, e2r = world.entitiesToAdd, world.entitiesToRemove
|
||||||
|
|
||||||
-- Early exit
|
-- Early exit
|
||||||
@@ -474,6 +549,7 @@ function tiny_manageEntities(world)
|
|||||||
entity = e2r[i]
|
entity = e2r[i]
|
||||||
if entities[entity] then
|
if entities[entity] then
|
||||||
entities[entity] = nil
|
entities[entity] = nil
|
||||||
|
entityCount = entityCount - 1
|
||||||
|
|
||||||
for j = 1, #systems do
|
for j = 1, #systems do
|
||||||
system = systems[j]
|
system = systems[j]
|
||||||
@@ -488,7 +564,6 @@ function tiny_manageEntities(world)
|
|||||||
seis[tmpEntity] = index
|
seis[tmpEntity] = index
|
||||||
seis[entity] = nil
|
seis[entity] = nil
|
||||||
ses[#ses] = nil
|
ses[#ses] = nil
|
||||||
entityCount = entityCount - 1
|
|
||||||
onRemove = system.onRemove
|
onRemove = system.onRemove
|
||||||
if onRemove then
|
if onRemove then
|
||||||
onRemove(system, entity)
|
onRemove(system, entity)
|
||||||
@@ -501,12 +576,12 @@ function tiny_manageEntities(world)
|
|||||||
e2r[i] = nil
|
e2r[i] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- Add Entities
|
-- Add Entities
|
||||||
for i = 1, #e2a do
|
for i = 1, #e2a do
|
||||||
entity = e2a[i]
|
entity = e2a[i]
|
||||||
if not entities[entity] then
|
if not entities[entity] then
|
||||||
entities[entity] = true
|
entities[entity] = true
|
||||||
|
entityCount = entityCount + 1
|
||||||
|
|
||||||
for j = 1, #systems do
|
for j = 1, #systems do
|
||||||
system = systems[j]
|
system = systems[j]
|
||||||
@@ -518,7 +593,6 @@ function tiny_manageEntities(world)
|
|||||||
index = #ses + 1
|
index = #ses + 1
|
||||||
ses[index] = entity
|
ses[index] = entity
|
||||||
seis[entity] = index
|
seis[entity] = index
|
||||||
entityCount = entityCount + 1
|
|
||||||
onAdd = system.onAdd
|
onAdd = system.onAdd
|
||||||
if onAdd then
|
if onAdd then
|
||||||
onAdd(system, entity)
|
onAdd(system, entity)
|
||||||
@@ -533,25 +607,24 @@ function tiny_manageEntities(world)
|
|||||||
|
|
||||||
-- Update Entity count
|
-- Update Entity count
|
||||||
world.entityCount = entityCount
|
world.entityCount = entityCount
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Updates the World.
|
--- Updates the World by dt (delta time). Takes an optional parameter, `filter`,
|
||||||
-- Put this in your main loop.
|
-- which is a Filter that selects Systems from the World, and updates only those
|
||||||
-- @param world
|
-- Systems. If `filter` is not supplied, all Systems are updated. Put this
|
||||||
-- @param dt Delta time
|
-- function in your main loop.
|
||||||
function tiny.update(world, dt)
|
function tiny.update(world, dt, filter)
|
||||||
|
|
||||||
tiny_manageSystems(world)
|
tiny_manageSystems(world)
|
||||||
tiny_manageEntities(world)
|
tiny_manageEntities(world)
|
||||||
|
|
||||||
local systems = world.systems
|
local systems = world.systems
|
||||||
local system, update, onModify, entities
|
local system, update, interval, onModify
|
||||||
|
|
||||||
-- Iterate through Systems IN ORDER
|
-- Iterate through Systems IN ORDER
|
||||||
for i = 1, #systems do
|
for i = 1, #systems do
|
||||||
system = systems[i]
|
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.
|
-- Call the modify callback on Systems that have been modified.
|
||||||
onModify = system.onModify
|
onModify = system.onModify
|
||||||
@@ -559,10 +632,22 @@ function tiny.update(world, dt)
|
|||||||
onModify(system, dt)
|
onModify(system, dt)
|
||||||
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, dt)
|
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
|
end
|
||||||
|
|
||||||
system.modified = false
|
system.modified = false
|
||||||
@@ -571,7 +656,6 @@ function tiny.update(world, dt)
|
|||||||
end
|
end
|
||||||
|
|
||||||
--- Removes all Entities from the World.
|
--- Removes all Entities from the World.
|
||||||
-- @param world
|
|
||||||
function tiny.clearEntities(world)
|
function tiny.clearEntities(world)
|
||||||
for e in pairs(world.entities) do
|
for e in pairs(world.entities) do
|
||||||
tiny_removeEntity(world, e)
|
tiny_removeEntity(world, e)
|
||||||
@@ -579,7 +663,6 @@ function tiny.clearEntities(world)
|
|||||||
end
|
end
|
||||||
|
|
||||||
--- Removes all Systems from the World.
|
--- Removes all Systems from the World.
|
||||||
-- @param world
|
|
||||||
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
|
||||||
@@ -587,44 +670,56 @@ function tiny.clearSystems(world)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Gets count of Entities in World.
|
--- Gets number of Entities in the World.
|
||||||
-- @param world
|
|
||||||
function tiny.getEntityCount(world)
|
function tiny.getEntityCount(world)
|
||||||
return world.entityCount
|
return world.entityCount
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Gets count of Systems in World.
|
--- Gets number of Systems in World.
|
||||||
-- @param world
|
|
||||||
function tiny.getSystemCount(world)
|
function tiny.getSystemCount(world)
|
||||||
return #(world.systems)
|
return #(world.systems)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Gets the index of a System in the World. Lower indexed Systems are processed
|
--- Gets the index of the System in the World.
|
||||||
-- before higher indexed systems.
|
-- A simpler alternative is `system.index`.
|
||||||
-- @param world
|
|
||||||
-- @param system
|
|
||||||
function tiny.getSystemIndex(world, system)
|
function tiny.getSystemIndex(world, system)
|
||||||
return world.systemIndices[system]
|
return system.index
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Sets the index of a System in the World. Changes the order in
|
--- Sets the index of a System in the World, and returns the old index. Changes
|
||||||
-- which they Systems processed, because lower indexed Systems are processed
|
-- the order in which they Systems processed, because lower indexed Systems are
|
||||||
-- first.
|
-- processed first. Returns the old system.index.
|
||||||
-- @param world
|
|
||||||
-- @param system
|
|
||||||
-- @param index
|
|
||||||
function tiny.setSystemIndex(world, system, index)
|
function tiny.setSystemIndex(world, system, index)
|
||||||
local systemIndices = world.systemIndices
|
local oldIndex = system.index
|
||||||
local oldIndex = systemIndices[system]
|
|
||||||
local systems = world.systems
|
local systems = world.systems
|
||||||
local system = systems[oldIndex]
|
|
||||||
|
|
||||||
tremove(systems, oldIndex)
|
tremove(systems, oldIndex)
|
||||||
tinsert(systems, index, system)
|
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]] = i
|
systems[i].index = i
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return oldIndex
|
||||||
end
|
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
|
return tiny
|
||||||
|
|||||||
Reference in New Issue
Block a user