2015-05-04 00:21:30 +00:00
|
|
|
--[[
|
2016-03-01 00:19:34 +00:00
|
|
|
Copyright (c) 2016 Calvin Rose
|
2015-05-04 00:21:30 +00:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
|
|
the Software without restriction, including without limitation the rights to
|
|
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
]]
|
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
--- @module tiny-ecs
|
|
|
|
-- @author Calvin Rose
|
2015-05-04 12:21:12 +00:00
|
|
|
-- @license MIT
|
|
|
|
-- @copyright 2015
|
2016-03-06 19:42:48 +00:00
|
|
|
local tiny = { _VERSION = "scm" }
|
2015-03-21 08:09:39 +00:00
|
|
|
|
2015-04-21 11:37:33 +00:00
|
|
|
-- Local versions of standard lua functions
|
2015-03-29 03:19:22 +00:00
|
|
|
local tinsert = table.insert
|
|
|
|
local tremove = table.remove
|
2015-05-03 15:05:50 +00:00
|
|
|
local tsort = table.sort
|
2015-03-21 08:09:39 +00:00
|
|
|
local setmetatable = setmetatable
|
2015-04-19 05:40:42 +00:00
|
|
|
local type = type
|
2015-05-05 14:03:04 +00:00
|
|
|
local select = select
|
2015-03-29 03:19:22 +00:00
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
-- Local versions of the library functions
|
|
|
|
local tiny_manageEntities
|
|
|
|
local tiny_manageSystems
|
2015-04-21 11:37:33 +00:00
|
|
|
local tiny_addEntity
|
|
|
|
local tiny_addSystem
|
2015-03-29 12:19:31 +00:00
|
|
|
local tiny_add
|
2015-04-21 11:37:33 +00:00
|
|
|
local tiny_removeEntity
|
|
|
|
local tiny_removeSystem
|
2015-03-29 12:19:31 +00:00
|
|
|
local tiny_remove
|
|
|
|
|
|
|
|
--- Filter functions.
|
|
|
|
-- A Filter is a function that selects which Entities apply to a System.
|
2015-05-04 12:21:12 +00:00
|
|
|
-- Filters take two parameters, the System and the Entity, and return a boolean
|
|
|
|
-- 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.
|
2015-06-18 20:02:16 +00:00
|
|
|
-- Filter's returned by tiny-ecs's Filter functions are immutable and can be
|
|
|
|
-- used by multiple Systems.
|
2015-05-04 12:21:12 +00:00
|
|
|
--
|
|
|
|
-- local f1 = tiny.requireAll("position", "velocity", "size")
|
2015-05-07 09:32:32 +00:00
|
|
|
-- local f2 = tiny.requireAny("position", "velocity", "size")
|
2015-05-04 12:21:12 +00:00
|
|
|
--
|
|
|
|
-- local e1 = {
|
|
|
|
-- position = {2, 3},
|
|
|
|
-- velocity = {3, 3},
|
|
|
|
-- size = {4, 4}
|
|
|
|
-- }
|
|
|
|
--
|
|
|
|
-- local entity2 = {
|
|
|
|
-- position = {4, 5},
|
|
|
|
-- size = {4, 4}
|
|
|
|
-- }
|
|
|
|
--
|
|
|
|
-- local e3 = {
|
|
|
|
-- position = {2, 3},
|
|
|
|
-- velocity = {3, 3}
|
|
|
|
-- }
|
|
|
|
--
|
|
|
|
-- 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
|
|
|
|
--
|
2015-05-05 14:03:04 +00:00
|
|
|
-- 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.
|
2015-05-07 09:32:32 +00:00
|
|
|
-- filter = tiny.requireAll("image", tiny.rejectAny("Player", "Enemy"))
|
2015-05-05 14:03:04 +00:00
|
|
|
--
|
2015-03-29 12:19:31 +00:00
|
|
|
-- @section Filter
|
|
|
|
|
2016-03-05 18:34:44 +00:00
|
|
|
-- A helper function to compile filters.
|
|
|
|
local filterJoin
|
2016-03-05 20:34:21 +00:00
|
|
|
|
|
|
|
-- A helper function to filters from string
|
|
|
|
local filterBuildString
|
|
|
|
|
2016-03-05 18:34:44 +00:00
|
|
|
do
|
2016-03-05 21:06:56 +00:00
|
|
|
|
2016-03-05 18:34:44 +00:00
|
|
|
local loadstring = loadstring or load
|
|
|
|
local function getchr(c)
|
|
|
|
return "\\" .. c:byte()
|
|
|
|
end
|
|
|
|
local function make_safe(text)
|
|
|
|
return ("%q"):format(text):gsub('\n', 'n'):gsub("[\128-\255]", getchr)
|
|
|
|
end
|
2016-03-05 20:34:21 +00:00
|
|
|
|
2016-03-05 21:35:06 +00:00
|
|
|
local function filterJoinRaw(prefix, seperator, ...)
|
2016-03-05 18:34:44 +00:00
|
|
|
local accum = {}
|
|
|
|
local build = {}
|
|
|
|
for i = 1, select('#', ...) do
|
|
|
|
local item = select(i, ...)
|
|
|
|
if type(item) == 'string' then
|
|
|
|
accum[#accum + 1] = ("(e[%s] ~= nil)"):format(make_safe(item))
|
|
|
|
elseif type(item) == 'function' then
|
|
|
|
build[#build + 1] = ('local subfilter_%d_ = select(%d, ...)'):format(i, i)
|
|
|
|
accum[#accum + 1] = ('(subfilter_%d_(system, e))'):format(i)
|
|
|
|
else
|
|
|
|
error 'Filter token must be a string or a filter function.'
|
2015-03-29 12:19:31 +00:00
|
|
|
end
|
|
|
|
end
|
2016-03-05 18:34:44 +00:00
|
|
|
local source = ('do %s\n return function(system, e) return %s(%s) end end'):format(
|
|
|
|
table.concat(build, '\n'),
|
|
|
|
prefix,
|
|
|
|
table.concat(accum, seperator)
|
|
|
|
)
|
|
|
|
local loader, err = loadstring(source)
|
|
|
|
if err then error(err) end
|
|
|
|
return loader(...)
|
2015-03-29 12:19:31 +00:00
|
|
|
end
|
2016-03-05 20:34:21 +00:00
|
|
|
|
2016-03-05 21:35:06 +00:00
|
|
|
function filterJoin(...)
|
|
|
|
local state, value = pcall(filterJoinRaw, ...)
|
|
|
|
if state then return value else return nil, value end
|
|
|
|
end
|
|
|
|
|
2016-03-05 20:34:21 +00:00
|
|
|
local function buildPart(str)
|
|
|
|
local accum = {}
|
|
|
|
local subParts = {}
|
|
|
|
str = str:gsub('%b()', function(p)
|
|
|
|
subParts[#subParts + 1] = buildPart(p:sub(2, -2))
|
|
|
|
return ('\255%d'):format(#subParts)
|
|
|
|
end)
|
|
|
|
for invert, part, sep in str:gmatch('(%!?)([^%|%&%!]+)([%|%&%!]?)') do
|
|
|
|
if part:match('^\255%d+$') then
|
|
|
|
local partIndex = tonumber(part:match(part:sub(2)))
|
|
|
|
accum[#accum + 1] = ('%s(%s)'):format(invert == '' and '' or 'not', subParts[partIndex])
|
|
|
|
else
|
|
|
|
accum[#accum + 1] = ("(e[%s] %s nil)"):format(make_safe(part), invert == '' and '~=' or '==')
|
|
|
|
end
|
|
|
|
if sep ~= '' then
|
|
|
|
accum[#accum + 1] = (sep == '|' and ' or ' or ' and ')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return table.concat(accum)
|
|
|
|
end
|
|
|
|
|
|
|
|
function filterBuildString(str)
|
2016-03-05 21:06:56 +00:00
|
|
|
local source = ("return function(_, e) return %s end"):format(buildPart(str))
|
2016-03-05 20:34:21 +00:00
|
|
|
local loader, err = loadstring(source)
|
2016-03-05 21:06:56 +00:00
|
|
|
if err then
|
|
|
|
error(err)
|
|
|
|
end
|
2016-03-05 20:34:21 +00:00
|
|
|
return loader()
|
|
|
|
end
|
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
end
|
|
|
|
|
2016-03-05 18:34:44 +00:00
|
|
|
--- Makes a Filter that selects Entities with all specified Components and
|
|
|
|
-- Filters.
|
|
|
|
function tiny.requireAll(...)
|
|
|
|
return filterJoin('', ' and ', ...)
|
|
|
|
end
|
|
|
|
|
2015-05-04 12:21:12 +00:00
|
|
|
--- Makes a Filter that selects Entities with at least one of the specified
|
|
|
|
-- Components and Filters.
|
2015-05-07 09:32:32 +00:00
|
|
|
function tiny.requireAny(...)
|
2016-03-05 18:34:44 +00:00
|
|
|
return filterJoin('', ' or ', ...)
|
2015-03-21 08:09:39 +00:00
|
|
|
end
|
|
|
|
|
2015-05-05 14:03:04 +00:00
|
|
|
--- Makes a Filter that rejects Entities with all specified Components and
|
|
|
|
-- Filters, and selects all other Entities.
|
|
|
|
function tiny.rejectAll(...)
|
2016-03-05 18:34:44 +00:00
|
|
|
return filterJoin('not', ' and ', ...)
|
2015-05-05 14:03:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Makes a Filter that rejects Entities with at least one of the specified
|
|
|
|
-- Components and Filters, and selects all other Entities.
|
2015-05-07 09:32:32 +00:00
|
|
|
function tiny.rejectAny(...)
|
2016-03-05 18:34:44 +00:00
|
|
|
return filterJoin('not', ' or ', ...)
|
2015-05-05 14:03:04 +00:00
|
|
|
end
|
|
|
|
|
2016-03-05 21:35:06 +00:00
|
|
|
--- Makes a Filter from a string. Syntax of `pattern` is as follows.
|
2016-03-05 20:34:21 +00:00
|
|
|
--
|
|
|
|
-- * Tokens are alphanumeric strings including underscores.
|
|
|
|
-- * Tokens can be separated by |, &, or surrounded by parentheses.
|
2016-03-05 21:35:06 +00:00
|
|
|
-- * Tokens can be prefixed with !, and are then operated on with a boolean 'not'.
|
2016-03-05 20:34:21 +00:00
|
|
|
--
|
|
|
|
-- Examples are best:
|
|
|
|
-- 'a|b|c' - Matches entities with an 'a' component OR a 'b' component or a 'c' component.
|
|
|
|
-- 'a&!b&c' - Matches entities with an 'a' component AND NOT a 'b' component AND a 'c' component.
|
|
|
|
-- 'a|(b&c&d)|e - Matches 'a' OR ('b' AND 'c' AND 'd') OR 'e'
|
2016-03-05 21:35:06 +00:00
|
|
|
-- @param pattern
|
|
|
|
function tiny.filter(pattern)
|
|
|
|
local state, value = pcall(filterBuildString, pattern)
|
|
|
|
if state then return value else return nil, value end
|
|
|
|
end
|
2016-03-05 20:34:21 +00:00
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
--- System functions.
|
2015-04-21 13:47:36 +00:00
|
|
|
-- A System is a wrapper around function callbacks for manipulating Entities.
|
2015-05-07 11:45:28 +00:00
|
|
|
-- Systems are implemented as tables that contain at least one method;
|
2015-04-27 13:48:27 +00:00
|
|
|
-- an update function that takes parameters like so:
|
2015-05-04 12:21:12 +00:00
|
|
|
--
|
|
|
|
-- * `function system:update(dt)`.
|
|
|
|
--
|
|
|
|
-- There are also a few other optional callbacks:
|
|
|
|
--
|
2015-06-17 02:39:10 +00:00
|
|
|
-- * `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.
|
2015-09-08 20:19:07 +00:00
|
|
|
-- * `function system:onAddToWorld(world)` - Called when the System is added
|
|
|
|
-- to the World, before any entities are added to the system.
|
|
|
|
-- * `function system:onRemoveFromWorld(world)` - Called when the System is
|
|
|
|
-- removed from the world, after all Entities are removed from the System.
|
2015-05-04 12:21:12 +00:00
|
|
|
--
|
2015-05-07 11:45:28 +00:00
|
|
|
-- 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:
|
2015-05-04 12:21:12 +00:00
|
|
|
-- system.filter = tiny.requireAll("a", "b", "c")
|
|
|
|
-- or
|
|
|
|
-- function system:filter(entity)
|
|
|
|
-- return entity.myRequiredComponentName ~= nil
|
|
|
|
-- end
|
|
|
|
--
|
2015-05-07 11:45:28 +00:00
|
|
|
-- 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.
|
|
|
|
--
|
2015-06-18 19:19:06 +00:00
|
|
|
-- * 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.
|
2015-05-07 11:45:28 +00:00
|
|
|
-- * 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.
|
2015-07-07 03:28:50 +00:00
|
|
|
-- * The `entities` field is an ordered list of Entities in the System. This
|
2015-05-07 11:45:28 +00:00
|
|
|
-- list can be used to quickly iterate through all Entities in a System.
|
2015-06-19 22:48:18 +00:00
|
|
|
-- * 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.
|
2015-07-07 03:28:50 +00:00
|
|
|
-- * 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)`.
|
2015-05-07 11:45:28 +00:00
|
|
|
-- * 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" }
|
|
|
|
|
2015-06-17 02:39:10 +00:00
|
|
|
-- Checks if a table is a System.
|
2015-05-07 11:45:28 +00:00
|
|
|
local function isSystem(table)
|
|
|
|
return table[systemTableKey]
|
|
|
|
end
|
|
|
|
|
2015-04-22 09:22:14 +00:00
|
|
|
-- Update function for all Processing Systems.
|
2015-05-04 01:09:34 +00:00
|
|
|
local function processingSystemUpdate(system, dt)
|
2015-04-22 09:22:14 +00:00
|
|
|
local preProcess = system.preProcess
|
|
|
|
local process = system.process
|
|
|
|
local postProcess = system.postProcess
|
2015-04-25 09:59:41 +00:00
|
|
|
|
2015-04-22 09:22:14 +00:00
|
|
|
if preProcess then
|
2015-05-04 01:09:34 +00:00
|
|
|
preProcess(system, dt)
|
2015-04-22 09:22:14 +00:00
|
|
|
end
|
2015-04-25 09:59:41 +00:00
|
|
|
|
2015-04-22 09:22:14 +00:00
|
|
|
if process then
|
2016-03-06 19:42:48 +00:00
|
|
|
if system.nocache then
|
|
|
|
local entities = system.world.entityList
|
|
|
|
local filter = system.filter
|
|
|
|
if filter then
|
|
|
|
for i = 1, #entities do
|
|
|
|
local entity = entities[i]
|
|
|
|
if filter(system, entity) then
|
|
|
|
process(system, entity, dt)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local entities = system.entities
|
|
|
|
for i = 1, #entities do
|
|
|
|
process(system, entities[i], dt)
|
|
|
|
end
|
2015-04-22 09:22:14 +00:00
|
|
|
end
|
|
|
|
end
|
2015-04-25 09:59:41 +00:00
|
|
|
|
2015-04-22 09:22:14 +00:00
|
|
|
if postProcess then
|
2015-05-04 01:09:34 +00:00
|
|
|
postProcess(system, dt)
|
2015-04-22 09:22:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-06 19:42:48 +00:00
|
|
|
-- Sorts Systems by a function system.sortDelegate(entity1, entity2) on modify.
|
2015-06-17 00:28:21 +00:00
|
|
|
local function sortedSystemOnModify(system, dt)
|
|
|
|
local entities = system.entities
|
2015-06-18 20:02:16 +00:00
|
|
|
local indices = system.indices
|
2015-06-17 00:28:21 +00:00
|
|
|
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
|
2016-03-06 19:42:48 +00:00
|
|
|
indices[entities[i]] = i
|
2015-06-17 00:28:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-19 22:48:18 +00:00
|
|
|
--- 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
|
2015-06-17 00:28:21 +00:00
|
|
|
end
|
|
|
|
|
2015-06-19 22:48:18 +00:00
|
|
|
--- Creates a new Processing System or Processing System class. Processing
|
2015-06-17 01:15:30 +00:00
|
|
|
-- Systems process each entity individual, and are usually what is needed.
|
2015-06-19 22:48:18 +00:00
|
|
|
-- Processing Systems have three extra callbacks besides those inheritted from
|
|
|
|
-- vanilla Systems.
|
2015-06-19 23:45:26 +00:00
|
|
|
--
|
2015-08-27 17:26:09 +00:00
|
|
|
-- function system:preProcess(dt) -- Called before iteration.
|
2015-08-27 17:32:51 +00:00
|
|
|
-- function system:process(entity, dt) -- Process each entity.
|
2015-08-27 17:26:09 +00:00
|
|
|
-- function system:postProcess(dt) -- Called after iteration.
|
2015-06-19 23:45:26 +00:00
|
|
|
--
|
2015-06-17 01:15:30 +00:00
|
|
|
-- Processing Systems have their own `update` method, so don't implement a
|
|
|
|
-- a custom `update` callback for Processing Systems.
|
2015-06-19 22:48:18 +00:00
|
|
|
-- @see system
|
|
|
|
function tiny.processingSystem(table)
|
|
|
|
table = table or {}
|
|
|
|
table[systemTableKey] = true
|
|
|
|
table.update = processingSystemUpdate
|
|
|
|
return table
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Creates a new Sorted System or Sorted System class. Sorted Systems sort
|
2015-06-17 01:15:30 +00:00
|
|
|
-- 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.
|
2015-06-19 22:48:18 +00:00
|
|
|
-- 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)
|
2015-06-17 00:28:21 +00:00
|
|
|
table = table or {}
|
|
|
|
table[systemTableKey] = true
|
2015-06-19 22:48:18 +00:00
|
|
|
table.onModify = sortedSystemOnModify
|
2015-06-17 00:28:21 +00:00
|
|
|
return table
|
|
|
|
end
|
|
|
|
|
2015-06-19 22:48:18 +00:00
|
|
|
--- Creates a new Sorted Processing System or Sorted Processing System class.
|
|
|
|
-- Sorted Processing Systems have both the aspects of Processing Systems and
|
|
|
|
-- Sorted Systems.
|
2015-06-17 03:18:33 +00:00
|
|
|
-- @see system
|
2015-06-19 22:48:18 +00:00
|
|
|
-- @see processingSystem
|
|
|
|
-- @see sortedSystem
|
|
|
|
function tiny.sortedProcessingSystem(table)
|
2015-06-17 03:18:33 +00:00
|
|
|
table = table or {}
|
|
|
|
table[systemTableKey] = true
|
|
|
|
table.update = processingSystemUpdate
|
2015-06-19 22:48:18 +00:00
|
|
|
table.onModify = sortedSystemOnModify
|
2015-06-17 03:18:33 +00:00
|
|
|
return table
|
|
|
|
end
|
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
--- World functions.
|
2015-05-04 12:21:12 +00:00
|
|
|
-- A World is a container that manages Entities and Systems. Typically, a
|
|
|
|
-- program uses one World at a time.
|
|
|
|
--
|
2015-05-07 11:45:28 +00:00
|
|
|
-- 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)`.
|
2015-03-29 12:19:31 +00:00
|
|
|
-- @section World
|
2015-03-21 08:09:39 +00:00
|
|
|
|
2015-05-07 11:45:28 +00:00
|
|
|
-- Forward declaration
|
|
|
|
local worldMetaTable
|
2015-04-22 09:22:14 +00:00
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
--- Creates a new World.
|
2015-07-07 23:05:56 +00:00
|
|
|
-- Can optionally add default Systems and Entities. Returns the new World along
|
|
|
|
-- with default Entities and Systems.
|
2015-03-29 12:19:31 +00:00
|
|
|
function tiny.world(...)
|
2015-08-24 16:52:11 +00:00
|
|
|
local ret = setmetatable({
|
2015-03-21 08:09:39 +00:00
|
|
|
|
2015-04-25 09:59:41 +00:00
|
|
|
-- List of Entities to add
|
|
|
|
entitiesToAdd = {},
|
|
|
|
|
|
|
|
-- List of Entities to remove
|
|
|
|
entitiesToRemove = {},
|
2015-03-22 04:07:42 +00:00
|
|
|
|
2015-08-27 18:31:45 +00:00
|
|
|
-- List of Entities to change
|
|
|
|
entitiesToChange = {},
|
|
|
|
|
2015-04-25 09:59:41 +00:00
|
|
|
-- List of Entities to add
|
|
|
|
systemsToAdd = {},
|
2015-03-31 11:55:12 +00:00
|
|
|
|
2015-04-25 09:59:41 +00:00
|
|
|
-- List of Entities to remove
|
|
|
|
systemsToRemove = {},
|
|
|
|
|
2015-04-27 13:48:27 +00:00
|
|
|
-- Set of Entities
|
2015-03-29 12:19:31 +00:00
|
|
|
entities = {},
|
2015-03-29 03:19:22 +00:00
|
|
|
|
2016-03-06 19:42:48 +00:00
|
|
|
-- List of Entities
|
|
|
|
entityList = {},
|
|
|
|
|
2015-07-07 03:28:50 +00:00
|
|
|
-- Number of Entities in World
|
2015-03-29 12:19:31 +00:00
|
|
|
entityCount = 0,
|
2015-03-21 08:09:39 +00:00
|
|
|
|
2015-07-07 03:28:50 +00:00
|
|
|
-- List of Systems
|
|
|
|
systems = {}
|
2015-08-24 16:52:11 +00:00
|
|
|
}, worldMetaTable)
|
2015-03-29 12:19:31 +00:00
|
|
|
|
2015-04-21 13:47:36 +00:00
|
|
|
tiny_add(ret, ...)
|
|
|
|
tiny_manageSystems(ret)
|
|
|
|
tiny_manageEntities(ret)
|
2015-03-22 04:07:42 +00:00
|
|
|
|
2015-08-24 16:52:11 +00:00
|
|
|
return ret, ...
|
2015-03-29 12:19:31 +00:00
|
|
|
end
|
|
|
|
|
2015-04-21 11:37:33 +00:00
|
|
|
--- Adds an Entity to the world.
|
2015-05-04 12:21:12 +00:00
|
|
|
-- Also call this on Entities that have changed Components such that they
|
2015-07-07 23:05:56 +00:00
|
|
|
-- match different Filters. Returns the Entity.
|
2015-04-21 11:37:33 +00:00
|
|
|
function tiny.addEntity(world, entity)
|
2015-04-25 09:59:41 +00:00
|
|
|
if world.entities[entity] then
|
2015-08-27 18:31:45 +00:00
|
|
|
local e2c = world.entitiesToChange
|
|
|
|
e2c[#e2c + 1] = entity
|
|
|
|
else
|
|
|
|
local e2a = world.entitiesToAdd
|
|
|
|
e2a[#e2a + 1] = entity
|
2015-04-25 09:59:41 +00:00
|
|
|
end
|
2015-07-07 02:01:06 +00:00
|
|
|
return entity
|
2015-04-21 11:37:33 +00:00
|
|
|
end
|
|
|
|
tiny_addEntity = tiny.addEntity
|
|
|
|
|
2015-07-07 23:05:56 +00:00
|
|
|
--- Adds a System to the world. Returns the System.
|
2015-04-21 11:37:33 +00:00
|
|
|
function tiny.addSystem(world, system)
|
2015-09-08 17:22:07 +00:00
|
|
|
assert(system.world == nil, "System already belongs to a World.")
|
2015-04-25 09:59:41 +00:00
|
|
|
local s2a = world.systemsToAdd
|
|
|
|
s2a[#s2a + 1] = system
|
2015-09-08 17:22:07 +00:00
|
|
|
system.world = world
|
2015-07-07 02:01:06 +00:00
|
|
|
return system
|
2015-04-21 11:37:33 +00:00
|
|
|
end
|
|
|
|
tiny_addSystem = tiny.addSystem
|
|
|
|
|
2015-07-07 23:05:56 +00:00
|
|
|
--- Shortcut for adding multiple Entities and Systems to the World. Returns all
|
|
|
|
-- added Entities and Systems.
|
2015-03-29 12:19:31 +00:00
|
|
|
function tiny.add(world, ...)
|
2015-05-05 14:03:04 +00:00
|
|
|
for i = 1, select("#", ...) do
|
2015-08-27 18:55:07 +00:00
|
|
|
local obj = select(i, ...)
|
2015-05-05 14:21:39 +00:00
|
|
|
if obj then
|
|
|
|
if isSystem(obj) then
|
|
|
|
tiny_addSystem(world, obj)
|
|
|
|
else -- Assume obj is an Entity
|
|
|
|
tiny_addEntity(world, obj)
|
|
|
|
end
|
2015-03-29 03:19:22 +00:00
|
|
|
end
|
2015-03-21 08:09:39 +00:00
|
|
|
end
|
2015-07-07 02:01:06 +00:00
|
|
|
return ...
|
2015-03-21 08:09:39 +00:00
|
|
|
end
|
2015-03-29 12:19:31 +00:00
|
|
|
tiny_add = tiny.add
|
2015-03-21 08:09:39 +00:00
|
|
|
|
2015-07-07 23:05:56 +00:00
|
|
|
--- Removes an Entity to the World. Returns the Entity.
|
2015-04-21 11:37:33 +00:00
|
|
|
function tiny.removeEntity(world, entity)
|
2015-04-25 09:59:41 +00:00
|
|
|
local e2r = world.entitiesToRemove
|
|
|
|
e2r[#e2r + 1] = entity
|
2015-07-07 02:01:06 +00:00
|
|
|
return entity
|
2015-04-21 11:37:33 +00:00
|
|
|
end
|
|
|
|
tiny_removeEntity = tiny.removeEntity
|
|
|
|
|
2015-07-07 23:05:56 +00:00
|
|
|
--- Removes a System from the world. Returns the System.
|
2015-04-21 11:37:33 +00:00
|
|
|
function tiny.removeSystem(world, system)
|
2015-09-08 17:22:07 +00:00
|
|
|
assert(system.world == world, "System does not belong to this World.")
|
2015-04-25 09:59:41 +00:00
|
|
|
local s2r = world.systemsToRemove
|
|
|
|
s2r[#s2r + 1] = system
|
2015-07-07 02:01:06 +00:00
|
|
|
return system
|
2015-04-21 11:37:33 +00:00
|
|
|
end
|
|
|
|
tiny_removeSystem = tiny.removeSystem
|
|
|
|
|
2015-07-07 23:05:56 +00:00
|
|
|
--- Shortcut for removing multiple Entities and Systems from the World. Returns
|
2015-07-29 16:40:13 +00:00
|
|
|
-- all removed Systems and Entities
|
2015-03-29 12:19:31 +00:00
|
|
|
function tiny.remove(world, ...)
|
2015-05-05 14:03:04 +00:00
|
|
|
for i = 1, select("#", ...) do
|
2015-08-27 18:55:07 +00:00
|
|
|
local obj = select(i, ...)
|
2015-05-05 14:21:39 +00:00
|
|
|
if obj then
|
|
|
|
if isSystem(obj) then
|
|
|
|
tiny_removeSystem(world, obj)
|
|
|
|
else -- Assume obj is an Entity
|
2015-09-08 17:36:27 +00:00
|
|
|
tiny_removeEntity(world, obj)
|
2015-05-05 14:21:39 +00:00
|
|
|
end
|
2015-03-29 03:19:22 +00:00
|
|
|
end
|
2015-03-21 08:09:39 +00:00
|
|
|
end
|
2015-07-07 02:01:06 +00:00
|
|
|
return ...
|
2015-03-21 08:09:39 +00:00
|
|
|
end
|
2015-03-29 12:19:31 +00:00
|
|
|
tiny_remove = tiny.remove
|
|
|
|
|
2015-05-03 15:05:50 +00:00
|
|
|
-- Adds and removes Systems that have been marked from the World.
|
|
|
|
function tiny_manageSystems(world)
|
2015-08-24 16:52:11 +00:00
|
|
|
local s2a, s2r = world.systemsToAdd, world.systemsToRemove
|
2015-04-25 09:59:41 +00:00
|
|
|
|
2015-08-24 16:52:11 +00:00
|
|
|
-- Early exit
|
|
|
|
if #s2a == 0 and #s2r == 0 then
|
|
|
|
return
|
|
|
|
end
|
2015-04-25 09:59:41 +00:00
|
|
|
|
2015-08-24 16:52:11 +00:00
|
|
|
world.systemsToAdd = {}
|
|
|
|
world.systemsToRemove = {}
|
|
|
|
|
2016-03-06 19:42:48 +00:00
|
|
|
local worldEntityList = world.entityList
|
2015-08-24 16:52:11 +00:00
|
|
|
local systems = world.systems
|
|
|
|
|
|
|
|
-- Remove Systems
|
|
|
|
for i = 1, #s2r do
|
2015-08-27 18:55:07 +00:00
|
|
|
local system = s2r[i]
|
|
|
|
local index = system.index
|
2015-09-08 17:36:27 +00:00
|
|
|
local onRemove = system.onRemove
|
2016-03-06 19:42:48 +00:00
|
|
|
if onRemove and not system.nocache then
|
2015-09-08 17:36:27 +00:00
|
|
|
local entityList = system.entities
|
|
|
|
for j = 1, #entityList do
|
|
|
|
onRemove(system, entityList[j])
|
2015-08-24 16:52:11 +00:00
|
|
|
end
|
2015-04-25 09:59:41 +00:00
|
|
|
end
|
2015-09-08 17:36:27 +00:00
|
|
|
tremove(systems, index)
|
|
|
|
for j = index, #systems do
|
|
|
|
systems[j].index = j
|
|
|
|
end
|
|
|
|
local onRemoveFromWorld = system.onRemoveFromWorld
|
|
|
|
if onRemoveFromWorld then
|
|
|
|
onRemoveFromWorld(system, world)
|
|
|
|
end
|
2015-08-24 16:52:11 +00:00
|
|
|
s2r[i] = nil
|
2015-03-21 08:09:39 +00:00
|
|
|
|
2015-08-24 16:52:11 +00:00
|
|
|
-- Clean up System
|
|
|
|
system.world = nil
|
|
|
|
system.entities = nil
|
|
|
|
system.indices = nil
|
|
|
|
system.index = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Add Systems
|
|
|
|
for i = 1, #s2a do
|
2015-08-27 18:55:07 +00:00
|
|
|
local system = s2a[i]
|
2016-03-06 19:42:48 +00:00
|
|
|
if systems[system.index or 0] ~= system then
|
|
|
|
if not system.nocache then
|
|
|
|
system.entities = {}
|
|
|
|
system.indices = {}
|
|
|
|
end
|
2015-08-24 16:52:11 +00:00
|
|
|
if system.active == nil then
|
|
|
|
system.active = true
|
|
|
|
end
|
|
|
|
system.modified = true
|
|
|
|
system.world = world
|
2015-08-27 18:55:07 +00:00
|
|
|
local index = #systems + 1
|
2015-08-24 16:52:11 +00:00
|
|
|
system.index = index
|
|
|
|
systems[index] = system
|
2015-09-08 17:36:27 +00:00
|
|
|
local onAddToWorld = system.onAddToWorld
|
|
|
|
if onAddToWorld then
|
|
|
|
onAddToWorld(system, world)
|
|
|
|
end
|
2015-08-24 16:52:11 +00:00
|
|
|
|
|
|
|
-- Try to add Entities
|
2016-03-06 19:42:48 +00:00
|
|
|
if not system.nocache then
|
|
|
|
local entityList = system.entities
|
|
|
|
local entityIndices = system.indices
|
|
|
|
local onAdd = system.onAdd
|
|
|
|
local filter = system.filter
|
|
|
|
if filter then
|
|
|
|
for j = 1, #worldEntityList do
|
|
|
|
local entity = worldEntityList[j]
|
|
|
|
if filter(system, entity) then
|
|
|
|
local entityIndex = #entityList + 1
|
|
|
|
entityList[entityIndex] = entity
|
|
|
|
entityIndices[entity] = entityIndex
|
|
|
|
if onAdd then
|
|
|
|
onAdd(system, entity)
|
|
|
|
end
|
2015-04-25 09:59:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-03-22 04:07:42 +00:00
|
|
|
end
|
2015-08-24 16:52:11 +00:00
|
|
|
s2a[i] = nil
|
|
|
|
end
|
2015-03-29 03:19:22 +00:00
|
|
|
end
|
|
|
|
|
2015-08-27 18:31:45 +00:00
|
|
|
-- Adds, removes, and changes Entities that have been marked.
|
2015-05-03 15:05:50 +00:00
|
|
|
function tiny_manageEntities(world)
|
2015-08-24 16:52:11 +00:00
|
|
|
|
2015-08-27 18:31:45 +00:00
|
|
|
local e2a = world.entitiesToAdd
|
|
|
|
local e2r = world.entitiesToRemove
|
|
|
|
local e2c = world.entitiesToChange
|
2015-04-25 09:59:41 +00:00
|
|
|
|
|
|
|
-- Early exit
|
2015-08-27 18:31:45 +00:00
|
|
|
if #e2a == 0 and #e2r == 0 and #e2c == 0 then
|
2015-04-25 09:59:41 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-08-27 18:31:45 +00:00
|
|
|
world.entitiesToChange = {}
|
2015-08-24 16:52:11 +00:00
|
|
|
world.entitiesToAdd = {}
|
|
|
|
world.entitiesToRemove = {}
|
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
local entities = world.entities
|
2016-03-06 19:42:48 +00:00
|
|
|
local entityList = world.entityList
|
2015-03-29 12:19:31 +00:00
|
|
|
local systems = world.systems
|
2015-04-21 11:37:33 +00:00
|
|
|
local entityCount = world.entityCount
|
2015-04-25 09:59:41 +00:00
|
|
|
|
2015-08-27 18:31:45 +00:00
|
|
|
-- Change Entities
|
|
|
|
for i = 1, #e2c do
|
2015-08-27 18:55:07 +00:00
|
|
|
local entity = e2c[i]
|
2015-08-27 18:31:45 +00:00
|
|
|
if entities[entity] then
|
|
|
|
for j = 1, #systems do
|
2015-08-27 18:55:07 +00:00
|
|
|
local system = systems[j]
|
2016-03-06 19:42:48 +00:00
|
|
|
if not system.nocache then
|
|
|
|
local ses = system.entities
|
|
|
|
local seis = system.indices
|
|
|
|
local index = seis[entity]
|
|
|
|
local filter = system.filter
|
|
|
|
if filter and filter(system, entity) then
|
|
|
|
if not index then
|
|
|
|
system.modified = true
|
|
|
|
index = #ses + 1
|
|
|
|
ses[index] = entity
|
|
|
|
seis[entity] = index
|
|
|
|
local onAdd = system.onAdd
|
|
|
|
if onAdd then
|
|
|
|
onAdd(system, entity)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif index then
|
2015-08-27 18:31:45 +00:00
|
|
|
system.modified = true
|
2016-03-06 19:42:48 +00:00
|
|
|
local tmpEntity = ses[#ses]
|
|
|
|
ses[index] = tmpEntity
|
|
|
|
seis[tmpEntity] = index
|
|
|
|
seis[entity] = nil
|
|
|
|
ses[#ses] = nil
|
|
|
|
local onRemove = system.onRemove
|
|
|
|
if onRemove then
|
|
|
|
onRemove(system, entity)
|
2015-08-27 18:31:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
e2c[i] = nil
|
|
|
|
end
|
|
|
|
|
2015-04-25 09:59:41 +00:00
|
|
|
-- Remove Entities
|
|
|
|
for i = 1, #e2r do
|
2015-08-27 18:55:07 +00:00
|
|
|
local entity = e2r[i]
|
2016-03-06 19:42:48 +00:00
|
|
|
e2r[i] = nil
|
|
|
|
local listIndex = entities[entity]
|
|
|
|
if listIndex then
|
|
|
|
-- Remove Entity from world state
|
|
|
|
local lastEntity = entityList[#entityList]
|
|
|
|
entities[lastEntity] = listIndex
|
2015-04-25 09:59:41 +00:00
|
|
|
entities[entity] = nil
|
2016-03-06 19:42:48 +00:00
|
|
|
entityList[listIndex] = lastEntity
|
|
|
|
entityList[#entityList] = nil
|
2015-06-21 18:42:03 +00:00
|
|
|
entityCount = entityCount - 1
|
2016-03-06 19:42:48 +00:00
|
|
|
-- Remove from cached systems
|
2015-04-25 09:59:41 +00:00
|
|
|
for j = 1, #systems do
|
2015-08-27 18:55:07 +00:00
|
|
|
local system = systems[j]
|
2016-03-06 19:42:48 +00:00
|
|
|
if not system.nocache then
|
|
|
|
local ses = system.entities
|
|
|
|
local seis = system.indices
|
|
|
|
local index = seis[entity]
|
|
|
|
if index then
|
|
|
|
system.modified = true
|
|
|
|
local tmpEntity = ses[#ses]
|
|
|
|
ses[index] = tmpEntity
|
|
|
|
seis[tmpEntity] = index
|
|
|
|
seis[entity] = nil
|
|
|
|
ses[#ses] = nil
|
|
|
|
local onRemove = system.onRemove
|
|
|
|
if onRemove then
|
|
|
|
onRemove(system, entity)
|
|
|
|
end
|
2015-03-29 09:07:39 +00:00
|
|
|
end
|
2015-03-23 14:58:26 +00:00
|
|
|
end
|
2015-03-21 08:09:39 +00:00
|
|
|
end
|
2015-04-25 09:59:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Add Entities
|
|
|
|
for i = 1, #e2a do
|
2015-08-27 18:55:07 +00:00
|
|
|
local entity = e2a[i]
|
2015-04-25 09:59:41 +00:00
|
|
|
if not entities[entity] then
|
2016-03-06 19:42:48 +00:00
|
|
|
local listIndex = #entityList + 1
|
|
|
|
entities[entity] = listIndex
|
|
|
|
entityList[listIndex] = entity
|
2015-06-21 18:42:03 +00:00
|
|
|
entityCount = entityCount + 1
|
2015-04-25 09:59:41 +00:00
|
|
|
for j = 1, #systems do
|
2015-08-27 18:55:07 +00:00
|
|
|
local system = systems[j]
|
2016-03-06 19:42:48 +00:00
|
|
|
if not system.nocache then
|
|
|
|
local ses = system.entities
|
|
|
|
local seis = system.indices
|
|
|
|
local filter = system.filter
|
|
|
|
if filter and filter(system, entity) then
|
|
|
|
system.modified = true
|
|
|
|
local index = #ses + 1
|
|
|
|
ses[index] = entity
|
|
|
|
seis[entity] = index
|
|
|
|
local onAdd = system.onAdd
|
|
|
|
if onAdd then
|
|
|
|
onAdd(system, entity)
|
|
|
|
end
|
2015-04-25 09:59:41 +00:00
|
|
|
end
|
2015-03-23 14:58:26 +00:00
|
|
|
end
|
2015-03-22 04:07:42 +00:00
|
|
|
end
|
2015-03-21 08:09:39 +00:00
|
|
|
end
|
2015-04-25 09:59:41 +00:00
|
|
|
e2a[i] = nil
|
2015-03-21 08:09:39 +00:00
|
|
|
end
|
|
|
|
|
2015-03-22 04:07:42 +00:00
|
|
|
-- Update Entity count
|
2015-04-21 11:37:33 +00:00
|
|
|
world.entityCount = entityCount
|
2015-03-29 03:19:22 +00:00
|
|
|
end
|
|
|
|
|
2015-07-29 16:40:13 +00:00
|
|
|
--- Manages Entities and Systems marked for deletion or addition. Call this
|
|
|
|
-- before modifying Systems and Entities outside of a call to `tiny.update`.
|
|
|
|
-- Do not call this within a call to `tiny.update`.
|
|
|
|
function tiny.refresh(world)
|
|
|
|
tiny_manageSystems(world)
|
|
|
|
tiny_manageEntities(world)
|
2016-03-06 19:42:48 +00:00
|
|
|
local systems = world.systems
|
|
|
|
for i = 1, #systems do
|
|
|
|
local system = systems[i]
|
|
|
|
if system.active then
|
|
|
|
local onModify = system.onModify
|
|
|
|
if onModify and system.modified then
|
|
|
|
onModify(system, 0)
|
|
|
|
end
|
|
|
|
system.modified = false
|
|
|
|
end
|
|
|
|
end
|
2015-07-29 16:40:13 +00:00
|
|
|
end
|
|
|
|
|
2015-06-17 02:39:10 +00:00
|
|
|
--- Updates the World by dt (delta time). Takes an optional parameter, `filter`,
|
2015-06-18 20:02:16 +00:00
|
|
|
-- 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.
|
2015-06-16 23:45:09 +00:00
|
|
|
function tiny.update(world, dt, filter)
|
2015-03-29 03:19:22 +00:00
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
tiny_manageSystems(world)
|
|
|
|
tiny_manageEntities(world)
|
2015-03-29 03:19:22 +00:00
|
|
|
|
2015-04-25 09:59:41 +00:00
|
|
|
local systems = world.systems
|
|
|
|
|
2015-03-22 04:07:42 +00:00
|
|
|
-- Iterate through Systems IN ORDER
|
2015-04-25 09:59:41 +00:00
|
|
|
for i = 1, #systems do
|
2015-08-27 18:55:07 +00:00
|
|
|
local system = systems[i]
|
2015-06-16 23:45:09 +00:00
|
|
|
if system.active and ((not filter) or filter(world, system)) then
|
2015-05-03 15:05:50 +00:00
|
|
|
|
|
|
|
-- Call the modify callback on Systems that have been modified.
|
2015-08-27 18:55:07 +00:00
|
|
|
local onModify = system.onModify
|
2015-05-04 01:09:34 +00:00
|
|
|
if onModify and system.modified then
|
|
|
|
onModify(system, dt)
|
2015-05-03 15:05:50 +00:00
|
|
|
end
|
|
|
|
|
2015-07-07 03:28:50 +00:00
|
|
|
-- Update Systems that have an update method (most Systems)
|
2015-08-27 18:55:07 +00:00
|
|
|
local update = system.update
|
2015-04-27 07:45:54 +00:00
|
|
|
if update then
|
2015-08-27 18:55:07 +00:00
|
|
|
local interval = system.interval
|
2015-06-19 22:48:18 +00:00
|
|
|
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
|
2015-04-27 07:45:54 +00:00
|
|
|
end
|
2015-05-03 15:05:50 +00:00
|
|
|
|
2015-05-04 01:09:34 +00:00
|
|
|
system.modified = false
|
2015-03-21 08:09:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
--- Removes all Entities from the World.
|
|
|
|
function tiny.clearEntities(world)
|
2016-03-06 19:42:48 +00:00
|
|
|
local el = world.entityList
|
|
|
|
for i = 1, #el do
|
|
|
|
tiny_removeEntity(world, el[i])
|
2015-03-23 14:58:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
--- Removes all Systems from the World.
|
|
|
|
function tiny.clearSystems(world)
|
2015-04-25 09:59:41 +00:00
|
|
|
local systems = world.systems
|
|
|
|
for i = #systems, 1, -1 do
|
2015-05-04 01:32:04 +00:00
|
|
|
tiny_removeSystem(world, systems[i])
|
2015-03-23 14:58:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-07 11:45:28 +00:00
|
|
|
--- Gets number of Entities in the World.
|
2015-03-31 09:57:55 +00:00
|
|
|
function tiny.getEntityCount(world)
|
|
|
|
return world.entityCount
|
|
|
|
end
|
|
|
|
|
2015-05-07 11:45:28 +00:00
|
|
|
--- Gets number of Systems in World.
|
2015-03-31 11:55:12 +00:00
|
|
|
function tiny.getSystemCount(world)
|
2015-04-25 09:59:41 +00:00
|
|
|
return #(world.systems)
|
2015-03-31 09:57:55 +00:00
|
|
|
end
|
|
|
|
|
2015-07-07 23:26:12 +00:00
|
|
|
--- Gets the index of the System in the World.
|
|
|
|
-- A simpler alternative is `system.index`.
|
|
|
|
function tiny.getSystemIndex(world, system)
|
|
|
|
return system.index
|
|
|
|
end
|
|
|
|
|
2015-06-17 02:39:10 +00:00
|
|
|
--- 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
|
2015-08-24 16:52:11 +00:00
|
|
|
-- processed first. Returns the old system.index.
|
2015-04-21 13:47:36 +00:00
|
|
|
function tiny.setSystemIndex(world, system, index)
|
2015-07-07 03:28:50 +00:00
|
|
|
local oldIndex = system.index
|
2015-04-21 13:47:36 +00:00
|
|
|
local systems = world.systems
|
2015-04-25 13:37:04 +00:00
|
|
|
|
2015-08-23 02:45:07 +00:00
|
|
|
if index < 0 then
|
|
|
|
index = tiny.getSystemCount(world) + 1 + index
|
|
|
|
end
|
|
|
|
|
2015-04-21 13:47:36 +00:00
|
|
|
tremove(systems, oldIndex)
|
2015-05-04 01:09:34 +00:00
|
|
|
tinsert(systems, index, system)
|
2015-04-25 13:37:04 +00:00
|
|
|
|
|
|
|
for i = oldIndex, index, index >= oldIndex and 1 or -1 do
|
2015-07-07 03:28:50 +00:00
|
|
|
systems[i].index = i
|
2015-04-25 13:37:04 +00:00
|
|
|
end
|
2015-05-07 11:45:28 +00:00
|
|
|
|
|
|
|
return oldIndex
|
2015-04-21 13:47:36 +00:00
|
|
|
end
|
|
|
|
|
2015-05-07 11:45:28 +00:00
|
|
|
-- Construct world metatable.
|
|
|
|
worldMetaTable = {
|
|
|
|
__index = {
|
|
|
|
add = tiny.add,
|
|
|
|
addEntity = tiny.addEntity,
|
|
|
|
addSystem = tiny.addSystem,
|
|
|
|
remove = tiny.remove,
|
|
|
|
removeEntity = tiny.removeEntity,
|
2015-06-14 14:17:18 +00:00
|
|
|
removeSystem = tiny.removeSystem,
|
2015-07-29 16:40:13 +00:00
|
|
|
refresh = tiny.refresh,
|
2015-05-07 11:45:28 +00:00
|
|
|
update = tiny.update,
|
|
|
|
clearEntities = tiny.clearEntities,
|
|
|
|
clearSystems = tiny.clearSystems,
|
|
|
|
getEntityCount = tiny.getEntityCount,
|
|
|
|
getSystemCount = tiny.getSystemCount,
|
2015-07-07 23:26:12 +00:00
|
|
|
getSystemIndex = tiny.getSystemIndex,
|
2015-05-07 11:45:28 +00:00
|
|
|
setSystemIndex = tiny.setSystemIndex
|
2015-08-24 16:52:11 +00:00
|
|
|
},
|
|
|
|
__tostring = function(self)
|
2015-08-27 18:55:07 +00:00
|
|
|
return "<tiny-ecs_World>"
|
2015-08-24 16:52:11 +00:00
|
|
|
end
|
2015-05-07 11:45:28 +00:00
|
|
|
}
|
|
|
|
|
2015-03-29 07:50:42 +00:00
|
|
|
return tiny
|