|
|
|
@ -1,14 +1,39 @@
|
|
|
|
|
--[[
|
|
|
|
|
Copyright (c) 2015 Calvin Rose
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
]]
|
|
|
|
|
|
|
|
|
|
--- @module tiny-ecs
|
|
|
|
|
-- @author Calvin Rose
|
|
|
|
|
local tiny = { _VERSION = "1.0-2" }
|
|
|
|
|
-- @license MIT
|
|
|
|
|
-- @copyright 2015
|
|
|
|
|
local tiny = { _VERSION = "1.1-2" }
|
|
|
|
|
|
|
|
|
|
-- 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
|
|
|
|
@ -22,20 +47,57 @@ local tiny_remove
|
|
|
|
|
|
|
|
|
|
--- Filter functions.
|
|
|
|
|
-- A Filter is a function that selects which Entities apply to a System.
|
|
|
|
|
-- 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.
|
|
|
|
|
-- Filter's returned by `tiny.requireAll` and `tiny.requireAny` are immutable
|
|
|
|
|
-- and can be used by multiple Systems.
|
|
|
|
|
--
|
|
|
|
|
-- local f1 = tiny.requireAll("position", "velocity", "size")
|
|
|
|
|
-- local f2 = tiny.requireAny("position", "velocity", "size")
|
|
|
|
|
--
|
|
|
|
|
-- 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
|
|
|
|
|
--
|
|
|
|
|
-- 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 filters Entities with specified Components.
|
|
|
|
|
-- An Entity must have all Components to match the filter.
|
|
|
|
|
-- @param ... List of Components
|
|
|
|
|
--- 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
|
|
|
|
|
return function(_, e)
|
|
|
|
|
return function(system, e)
|
|
|
|
|
local c
|
|
|
|
|
for i = 1, len do
|
|
|
|
|
c = components[i]
|
|
|
|
|
if type(c) == 'function' then
|
|
|
|
|
if not c(_, e) then
|
|
|
|
|
if not c(system, e) then
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
elseif e[c] == nil then
|
|
|
|
@ -46,18 +108,18 @@ function tiny.requireAll(...)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Makes a Filter that filters Entities with specified Components.
|
|
|
|
|
-- An Entity must have at least one specified Component to match the filter.
|
|
|
|
|
-- @param ... List of Components
|
|
|
|
|
function tiny.requireOne(...)
|
|
|
|
|
--- 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.requireAny(...)
|
|
|
|
|
local components = {...}
|
|
|
|
|
local len = #components
|
|
|
|
|
return function(_, e)
|
|
|
|
|
return function(system, e)
|
|
|
|
|
local c
|
|
|
|
|
for i = 1, len do
|
|
|
|
|
c = components[i]
|
|
|
|
|
if type(c) == 'function' then
|
|
|
|
|
if c(_, e) then
|
|
|
|
|
if c(system, e) then
|
|
|
|
|
return true
|
|
|
|
|
end
|
|
|
|
|
elseif e[c] ~= nil then
|
|
|
|
@ -68,8 +130,89 @@ function tiny.requireOne(...)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Makes a Filter that rejects Entities with all specified Components and
|
|
|
|
|
-- Filters, and selects all other Entities.
|
|
|
|
|
-- @param ... List of required Components and other Filters.
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
--- Makes a Filter that rejects Entities with at least one of the specified
|
|
|
|
|
-- Components and Filters, and selects all other Entities.
|
|
|
|
|
-- @param ... List of required Components and other Filters.
|
|
|
|
|
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)`
|
|
|
|
|
--
|
|
|
|
|
-- 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
|
|
|
|
|
--
|
|
|
|
|
-- 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 `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 `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
|
|
|
|
@ -81,34 +224,25 @@ local function isSystem(table)
|
|
|
|
|
return table[systemTableKey]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Creates a System. Systems are tables that contain at least one field;
|
|
|
|
|
-- an update function that takes parameters like so:
|
|
|
|
|
-- `function system:update(entities, dt)`. `entities` is an unordered table of
|
|
|
|
|
-- Entities with Entities as KEYS, and `dt` is the delta time. There are also a
|
|
|
|
|
-- few other optional callbacks:
|
|
|
|
|
-- `function system:filter(entity)` - returns a boolean,
|
|
|
|
|
-- `function system:onAdd(entity)` - returns nil,
|
|
|
|
|
-- `function system:onRemove(entity)` - returns nil.
|
|
|
|
|
-- For Filters, it is conveient to use `tiny.requireAll` or `tiny.requireOne`,
|
|
|
|
|
-- but one can write their own filters as well.
|
|
|
|
|
--- Creates a default System.
|
|
|
|
|
-- @param table A table to be used as a System, or `nil` to create a new System.
|
|
|
|
|
-- @return A new System or System class
|
|
|
|
|
function tiny.system(table)
|
|
|
|
|
if table == nil then
|
|
|
|
|
table = {}
|
|
|
|
|
end
|
|
|
|
|
table = table or {}
|
|
|
|
|
table[systemTableKey] = true
|
|
|
|
|
return table
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Update function for all Processing Systems.
|
|
|
|
|
local function processingSystemUpdate(system, entities, dt)
|
|
|
|
|
local function processingSystemUpdate(system, dt)
|
|
|
|
|
local entities = system.entities
|
|
|
|
|
local preProcess = system.preProcess
|
|
|
|
|
local process = system.process
|
|
|
|
|
local postProcess = system.postProcess
|
|
|
|
|
local entity
|
|
|
|
|
|
|
|
|
|
if preProcess then
|
|
|
|
|
preProcess(system, entities, dt)
|
|
|
|
|
preProcess(system, dt)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if process then
|
|
|
|
@ -120,41 +254,88 @@ local function processingSystemUpdate(system, entities, dt)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if postProcess then
|
|
|
|
|
postProcess(system, entities, dt)
|
|
|
|
|
postProcess(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)`, and `function
|
|
|
|
|
-- system:filter(entity)`. `entities` is Entities,
|
|
|
|
|
-- and `dt` is the delta time. There are also a few other
|
|
|
|
|
-- optional callbacks:
|
|
|
|
|
-- `function system:preProcess(entities, dt)` - returns nil,
|
|
|
|
|
-- `function system:postProcess(entities, dt)` - returns nil,
|
|
|
|
|
-- `function system:onAdd(entity)` - returns nil,
|
|
|
|
|
-- `function system:onRemove(entity)` - returns nil.
|
|
|
|
|
-- For Filters, it is conveient to use `tiny.requireAll` or `tiny.requireOne`,
|
|
|
|
|
-- but one can write their own filters as well.
|
|
|
|
|
--- 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.
|
|
|
|
|
-- @see system
|
|
|
|
|
-- @return A new Processing System or Processing System class
|
|
|
|
|
function tiny.processingSystem(table)
|
|
|
|
|
if table == nil then
|
|
|
|
|
table = {}
|
|
|
|
|
end
|
|
|
|
|
table = table or {}
|
|
|
|
|
table[systemTableKey] = true
|
|
|
|
|
table.update = processingSystemUpdate
|
|
|
|
|
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
|
|
|
|
|
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.
|
|
|
|
|
-- @see system
|
|
|
|
|
-- @see processingSystem
|
|
|
|
|
-- @return A new Sorted System or Sorted System class
|
|
|
|
|
function tiny.sortedSystem(table)
|
|
|
|
|
table = table or {}
|
|
|
|
|
table[systemTableKey] = true
|
|
|
|
|
table.update = processingSystemUpdate
|
|
|
|
|
table.onModify = sortedSystemOnModify
|
|
|
|
|
table.sort = sortedSystemOnModify
|
|
|
|
|
return table
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- World functions.
|
|
|
|
|
-- A World is a container that manages Entities and Systems. 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).`
|
|
|
|
|
-- A World is a container that manages Entities and Systems. Typically, a
|
|
|
|
|
-- program uses one World at a time.
|
|
|
|
|
--
|
|
|
|
|
-- 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.
|
|
|
|
@ -199,9 +380,8 @@ function tiny.world(...)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Adds an Entity to the world.
|
|
|
|
|
-- The new Entity will enter the world next time World:update is called.
|
|
|
|
|
-- Also call this on Entities that have changed Components such that it
|
|
|
|
|
-- matches different systems.
|
|
|
|
|
-- Also call this on Entities that have changed Components such that they
|
|
|
|
|
-- match different Filters.
|
|
|
|
|
-- @param world
|
|
|
|
|
-- @param entity
|
|
|
|
|
function tiny.addEntity(world, entity)
|
|
|
|
@ -214,7 +394,6 @@ end
|
|
|
|
|
tiny_addEntity = tiny.addEntity
|
|
|
|
|
|
|
|
|
|
--- Adds a System to the world.
|
|
|
|
|
-- The new System will enter the world next time World:update is called.
|
|
|
|
|
-- @param world
|
|
|
|
|
-- @param system
|
|
|
|
|
function tiny.addSystem(world, system)
|
|
|
|
@ -224,27 +403,26 @@ end
|
|
|
|
|
tiny_addSystem = tiny.addSystem
|
|
|
|
|
|
|
|
|
|
--- Shortcut for adding multiple Entities and Systems to the World.
|
|
|
|
|
-- New objects will enter the World the next time World:update(dt) is called.
|
|
|
|
|
-- Also call this method when an Entity has had its Components changed, such
|
|
|
|
|
-- that it matches different Filters.
|
|
|
|
|
-- @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.
|
|
|
|
|
-- The Entity will exit the World next time World:update is called.
|
|
|
|
|
-- Also call this on Entities that have changed Components such that it
|
|
|
|
|
-- matches different systems.
|
|
|
|
|
-- @param world
|
|
|
|
|
-- @param entity
|
|
|
|
|
function tiny.removeEntity(world, entity)
|
|
|
|
@ -254,7 +432,6 @@ end
|
|
|
|
|
tiny_removeEntity = tiny.removeEntity
|
|
|
|
|
|
|
|
|
|
--- Removes a System from the world.
|
|
|
|
|
-- The System will exit the World next time World:update is called.
|
|
|
|
|
-- @param world
|
|
|
|
|
-- @param system
|
|
|
|
|
function tiny.removeSystem(world, system)
|
|
|
|
@ -264,34 +441,27 @@ end
|
|
|
|
|
tiny_removeSystem = tiny.removeSystem
|
|
|
|
|
|
|
|
|
|
--- Shortcut for removing multiple Entities and Systems from the World.
|
|
|
|
|
-- Objects will exit the World the next time World:update(dt) is called.
|
|
|
|
|
-- @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
|
|
|
|
|
|
|
|
|
|
--- Updates a System.
|
|
|
|
|
-- @param world
|
|
|
|
|
-- @param system A System in the World to update
|
|
|
|
|
-- @param dt Delta time
|
|
|
|
|
function tiny.updateSystem(world, system, dt)
|
|
|
|
|
local es = world.systemEntities[system]
|
|
|
|
|
system:update(es, dt)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Adds and removes Systems that have been marked from the World.
|
|
|
|
|
-- The user of this library should seldom if ever call this.
|
|
|
|
|
-- @param world
|
|
|
|
|
function tiny.manageSystems(world)
|
|
|
|
|
-- Adds and removes Systems that have been marked from the World.
|
|
|
|
|
function tiny_manageSystems(world)
|
|
|
|
|
|
|
|
|
|
local s2a, s2r = world.systemsToAdd, world.systemsToRemove
|
|
|
|
|
|
|
|
|
@ -303,18 +473,17 @@ function tiny.manageSystems(world)
|
|
|
|
|
local systemIndices = world.systemIndices
|
|
|
|
|
local entities = world.entities
|
|
|
|
|
local systems = world.systems
|
|
|
|
|
|
|
|
|
|
local system, systemData, index, filter, entityList, entityIndices, entityIndex, onRemove, onAdd
|
|
|
|
|
local system, index, filter
|
|
|
|
|
local entityList, entityIndices, entityIndex, onRemove, onAdd
|
|
|
|
|
|
|
|
|
|
-- Remove Systems
|
|
|
|
|
for i = 1, #s2r do
|
|
|
|
|
system = s2r[i]
|
|
|
|
|
index = systemIndices[system]
|
|
|
|
|
if index then
|
|
|
|
|
systemData = systems[index]
|
|
|
|
|
onRemove = system.onRemove
|
|
|
|
|
if onRemove then
|
|
|
|
|
entityList = systemData.entities
|
|
|
|
|
entityList = system.entities
|
|
|
|
|
for j = 1, #entityList do
|
|
|
|
|
onRemove(system, entityList[j])
|
|
|
|
|
end
|
|
|
|
@ -322,7 +491,7 @@ function tiny.manageSystems(world)
|
|
|
|
|
systemIndices[system] = nil
|
|
|
|
|
tremove(systems, index)
|
|
|
|
|
for j = index, #systems do
|
|
|
|
|
systemIndices[systems[j].system] = j
|
|
|
|
|
systemIndices[systems[j]] = j
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
s2r[i] = nil
|
|
|
|
@ -334,10 +503,13 @@ function tiny.manageSystems(world)
|
|
|
|
|
if not systemIndices[system] then
|
|
|
|
|
entityList = {}
|
|
|
|
|
entityIndices = {}
|
|
|
|
|
systemData = { system = system, entities = entityList, indices = entityIndices, active = true }
|
|
|
|
|
system.entities = entityList
|
|
|
|
|
system.indices = entityIndices
|
|
|
|
|
system.active = true
|
|
|
|
|
system.modified = true
|
|
|
|
|
index = #systems + 1
|
|
|
|
|
systemIndices[system] = index
|
|
|
|
|
systems[index] = systemData
|
|
|
|
|
systems[index] = system
|
|
|
|
|
|
|
|
|
|
-- Try to add Entities
|
|
|
|
|
onAdd = system.onAdd
|
|
|
|
@ -359,12 +531,9 @@ function tiny.manageSystems(world)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
tiny_manageSystems = tiny.manageSystems
|
|
|
|
|
|
|
|
|
|
--- Adds and removes Entities that have been marked.
|
|
|
|
|
-- The user of this library should seldom if ever call this.
|
|
|
|
|
-- @param world
|
|
|
|
|
function tiny.manageEntities(world)
|
|
|
|
|
-- Adds and removes Entities that have been marked.
|
|
|
|
|
function tiny_manageEntities(world)
|
|
|
|
|
|
|
|
|
|
local e2a, e2r = world.entitiesToAdd, world.entitiesToRemove
|
|
|
|
|
|
|
|
|
@ -376,7 +545,8 @@ function tiny.manageEntities(world)
|
|
|
|
|
local entities = world.entities
|
|
|
|
|
local systems = world.systems
|
|
|
|
|
local entityCount = world.entityCount
|
|
|
|
|
local entity, system, systemData, index, onRemove, onAdd, ses, seis, filter, tmpEntity
|
|
|
|
|
local entity, system, index
|
|
|
|
|
local onRemove, onAdd, ses, seis, filter, tmpEntity
|
|
|
|
|
|
|
|
|
|
-- Remove Entities
|
|
|
|
|
for i = 1, #e2r do
|
|
|
|
@ -385,13 +555,13 @@ function tiny.manageEntities(world)
|
|
|
|
|
entities[entity] = nil
|
|
|
|
|
|
|
|
|
|
for j = 1, #systems do
|
|
|
|
|
systemData = systems[j]
|
|
|
|
|
system = systemData.system
|
|
|
|
|
ses = systemData.entities
|
|
|
|
|
seis = systemData.indices
|
|
|
|
|
system = systems[j]
|
|
|
|
|
ses = system.entities
|
|
|
|
|
seis = system.indices
|
|
|
|
|
index = seis[entity]
|
|
|
|
|
|
|
|
|
|
if index then
|
|
|
|
|
system.modified = true
|
|
|
|
|
tmpEntity = ses[#ses]
|
|
|
|
|
ses[index] = tmpEntity
|
|
|
|
|
seis[tmpEntity] = index
|
|
|
|
@ -418,12 +588,12 @@ function tiny.manageEntities(world)
|
|
|
|
|
entities[entity] = true
|
|
|
|
|
|
|
|
|
|
for j = 1, #systems do
|
|
|
|
|
systemData = systems[j]
|
|
|
|
|
system = systemData.system
|
|
|
|
|
ses = systemData.entities
|
|
|
|
|
seis = systemData.indices
|
|
|
|
|
system = systems[j]
|
|
|
|
|
ses = system.entities
|
|
|
|
|
seis = system.indices
|
|
|
|
|
filter = system.filter
|
|
|
|
|
if filter and filter(system, entity) then
|
|
|
|
|
system.modified = true
|
|
|
|
|
index = #ses + 1
|
|
|
|
|
ses[index] = entity
|
|
|
|
|
seis[entity] = index
|
|
|
|
@ -444,11 +614,9 @@ function tiny.manageEntities(world)
|
|
|
|
|
world.entityCount = entityCount
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
tiny_manageEntities = tiny.manageEntities
|
|
|
|
|
|
|
|
|
|
--- Updates the World.
|
|
|
|
|
-- Frees Entities that have been marked for freeing, adds
|
|
|
|
|
-- entities that have been marked for adding, etc.
|
|
|
|
|
-- Put this in your main loop.
|
|
|
|
|
-- @param world
|
|
|
|
|
-- @param dt Delta time
|
|
|
|
|
function tiny.update(world, dt)
|
|
|
|
@ -457,20 +625,31 @@ function tiny.update(world, dt)
|
|
|
|
|
tiny_manageEntities(world)
|
|
|
|
|
|
|
|
|
|
local systems = world.systems
|
|
|
|
|
local systemData
|
|
|
|
|
local system, update, onModify, entities
|
|
|
|
|
|
|
|
|
|
-- Iterate through Systems IN ORDER
|
|
|
|
|
for i = 1, #systems do
|
|
|
|
|
systemData = systems[i]
|
|
|
|
|
if systemData.active then
|
|
|
|
|
systemData.system:update(systemData.entities, dt)
|
|
|
|
|
system = systems[i]
|
|
|
|
|
if system.active then
|
|
|
|
|
|
|
|
|
|
-- Call the modify callback on Systems that have been modified.
|
|
|
|
|
onModify = system.onModify
|
|
|
|
|
if onModify and system.modified then
|
|
|
|
|
onModify(system, dt)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--Update Systems that have an update method (most Systems)
|
|
|
|
|
update = system.update
|
|
|
|
|
if update then
|
|
|
|
|
update(system, dt)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
system.modified = false
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Removes all Entities from the World.
|
|
|
|
|
-- When World:update(dt) is next called,
|
|
|
|
|
-- all Entities will be removed.
|
|
|
|
|
-- @param world
|
|
|
|
|
function tiny.clearEntities(world)
|
|
|
|
|
for e in pairs(world.entities) do
|
|
|
|
@ -479,80 +658,76 @@ function tiny.clearEntities(world)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Removes all Systems from the World.
|
|
|
|
|
-- When World:update(dt) is next called,
|
|
|
|
|
-- all Systems will be removed.
|
|
|
|
|
-- @param world
|
|
|
|
|
function tiny.clearSystems(world)
|
|
|
|
|
local systems = world.systems
|
|
|
|
|
for i = #systems, 1, -1 do
|
|
|
|
|
tiny_removeSystem(world, systems[i].system)
|
|
|
|
|
tiny_removeSystem(world, systems[i])
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Gets count of Entities in World.
|
|
|
|
|
--- Gets number of Entities in the World.
|
|
|
|
|
-- @param world
|
|
|
|
|
-- @return An integer
|
|
|
|
|
function tiny.getEntityCount(world)
|
|
|
|
|
return world.entityCount
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Gets count of Systems in World.
|
|
|
|
|
--- Gets number of Systems in World.
|
|
|
|
|
-- @param world
|
|
|
|
|
-- @return An integer
|
|
|
|
|
function tiny.getSystemCount(world)
|
|
|
|
|
return #(world.systems)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Gets the index of a System in the world. Lower indexed Systems are processed
|
|
|
|
|
--- Gets the index of a System in the World. Lower indexed Systems are processed
|
|
|
|
|
-- before higher indexed systems.
|
|
|
|
|
-- @param world
|
|
|
|
|
-- @param system
|
|
|
|
|
-- @return An integer between 1 and world:getSystemCount() inclusive
|
|
|
|
|
function tiny.getSystemIndex(world, system)
|
|
|
|
|
return world.systemIndices[system]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Sets the index of a System in the world. Changes the order in
|
|
|
|
|
--- 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
|
|
|
|
|
-- @return Old index
|
|
|
|
|
function tiny.setSystemIndex(world, system, index)
|
|
|
|
|
local systemIndices = world.systemIndices
|
|
|
|
|
local oldIndex = systemIndices[system]
|
|
|
|
|
local systems = world.systems
|
|
|
|
|
local systemData = systems[oldIndex]
|
|
|
|
|
local system = systems[oldIndex]
|
|
|
|
|
|
|
|
|
|
tremove(systems, oldIndex)
|
|
|
|
|
tinsert(systems, index, systemData)
|
|
|
|
|
tinsert(systems, index, system)
|
|
|
|
|
|
|
|
|
|
for i = oldIndex, index, index >= oldIndex and 1 or -1 do
|
|
|
|
|
systemIndices[systems[i].system] = i
|
|
|
|
|
systemIndices[systems[i]] = i
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return oldIndex
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Activates Systems in the World.
|
|
|
|
|
-- Activated Systems will be update whenever tiny.update(world, dt) is called.
|
|
|
|
|
-- @param world
|
|
|
|
|
-- @param ... Systems to activate. The Systems must already be added to the
|
|
|
|
|
-- World.
|
|
|
|
|
function tiny.activate(world, ...)
|
|
|
|
|
local args = {...}
|
|
|
|
|
for _, system in ipairs(args) do
|
|
|
|
|
world.systems[world.systemIndices[system]].active = true
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
--- Deactivates Systems in the World.
|
|
|
|
|
-- Deactivated Systems must be update manually, and will not update when the
|
|
|
|
|
-- rest of World updates. They will, however, process new Entities added while
|
|
|
|
|
-- the System is deactivated.
|
|
|
|
|
-- @param world
|
|
|
|
|
-- @param ... Systems to deactivate. The Systems must already be added to the
|
|
|
|
|
-- World.
|
|
|
|
|
function tiny.deactivate(world, ...)
|
|
|
|
|
local args = {...}
|
|
|
|
|
for _, system in ipairs(args) do
|
|
|
|
|
world.systems[world.systemIndices[system]].active = false
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
-- Construct world metatable.
|
|
|
|
|
worldMetaTable = {
|
|
|
|
|
__index = {
|
|
|
|
|
add = tiny.add,
|
|
|
|
|
addEntity = tiny.addEntity,
|
|
|
|
|
addSystem = tiny.addSystem,
|
|
|
|
|
remove = tiny.remove,
|
|
|
|
|
removeEntity = tiny.removeEntity,
|
|
|
|
|
update = tiny.update,
|
|
|
|
|
clearEntities = tiny.clearEntities,
|
|
|
|
|
clearSystems = tiny.clearSystems,
|
|
|
|
|
getEntityCount = tiny.getEntityCount,
|
|
|
|
|
getSystemCount = tiny.getSystemCount,
|
|
|
|
|
getSystemIndex = tiny.getSystemIndex,
|
|
|
|
|
setSystemIndex = tiny.setSystemIndex
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tiny
|
|
|
|
|