|
|
@@ -23,14 +23,13 @@ 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-3" }
|
|
|
|
local tiny = { _VERSION = "1.1-4" }
|
|
|
|
|
|
|
|
|
|
|
|
-- 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 select = select
|
|
|
@@ -51,8 +50,8 @@ 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.requireAny` 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.requireAny("position", "velocity", "size")
|
|
|
|
-- local f2 = tiny.requireAny("position", "velocity", "size")
|
|
|
@@ -88,7 +87,6 @@ local tiny_remove
|
|
|
|
|
|
|
|
|
|
|
|
--- 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
|
|
|
@@ -110,7 +108,6 @@ 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.requireAny(...)
|
|
|
|
local components = {...}
|
|
|
|
local components = {...}
|
|
|
|
local len = #components
|
|
|
|
local len = #components
|
|
|
@@ -132,7 +129,6 @@ end
|
|
|
|
|
|
|
|
|
|
|
|
--- Makes a Filter that rejects Entities with all specified Components and
|
|
|
|
--- Makes a Filter that rejects Entities with all specified Components and
|
|
|
|
-- Filters, and selects all other Entities.
|
|
|
|
-- Filters, and selects all other Entities.
|
|
|
|
-- @param ... List of required Components and other Filters.
|
|
|
|
|
|
|
|
function tiny.rejectAll(...)
|
|
|
|
function tiny.rejectAll(...)
|
|
|
|
local components = {...}
|
|
|
|
local components = {...}
|
|
|
|
local len = #components
|
|
|
|
local len = #components
|
|
|
@@ -154,7 +150,6 @@ end
|
|
|
|
|
|
|
|
|
|
|
|
--- Makes a Filter that rejects Entities with at least one of the specified
|
|
|
|
--- Makes a Filter that rejects Entities with at least one of the specified
|
|
|
|
-- Components and Filters, and selects all other Entities.
|
|
|
|
-- Components and Filters, and selects all other Entities.
|
|
|
|
-- @param ... List of required Components and other Filters.
|
|
|
|
|
|
|
|
function tiny.rejectAny(...)
|
|
|
|
function tiny.rejectAny(...)
|
|
|
|
local components = {...}
|
|
|
|
local components = {...}
|
|
|
|
local len = #components
|
|
|
|
local len = #components
|
|
|
@@ -183,10 +178,15 @@ end
|
|
|
|
--
|
|
|
|
--
|
|
|
|
-- 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 convenient to use `tiny.requireAll` or `tiny.requireAny`,
|
|
|
|
-- 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
|
|
|
|
-- but one can write their own filters as well. Set the Filter of a System like
|
|
|
@@ -201,11 +201,17 @@ end
|
|
|
|
-- system is added to the World. A few are important, and few should be less
|
|
|
|
-- system is added to the World. A few are important, and few should be less
|
|
|
|
-- commonly used.
|
|
|
|
-- commonly used.
|
|
|
|
--
|
|
|
|
--
|
|
|
|
|
|
|
|
-- * The `world` field points to the World that the System belongs to. Useful
|
|
|
|
|
|
|
|
-- for adding and removing Entities from the world dynamically via the System.
|
|
|
|
-- * The `active` flag is whether or not the System is updated automatically.
|
|
|
|
-- * The `active` flag is whether or not the System is updated automatically.
|
|
|
|
-- Inactive Systems should be updated manually or not at all via
|
|
|
|
-- Inactive Systems should be updated manually or not at all via
|
|
|
|
-- `system:update(dt)`. Defaults to true.
|
|
|
|
-- `system:update(dt)`. Defaults to true.
|
|
|
|
-- * The 'entities' field is an ordered list of Entities in the System. This
|
|
|
|
-- * 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.
|
|
|
|
-- list can be used to quickly iterate through all Entities in a System.
|
|
|
|
|
|
|
|
-- * The `interval` field is an optional field that makes Systems update at
|
|
|
|
|
|
|
|
-- certain intervals using buffered time, regardless of World update frequency.
|
|
|
|
|
|
|
|
-- For example, to make a System update once a second, set the System's interval
|
|
|
|
|
|
|
|
-- to 1.
|
|
|
|
-- * The `indices` field is a table of Entity keys to their indices in the
|
|
|
|
-- * The `indices` field is a table of Entity keys to their indices in the
|
|
|
|
-- `entities` list. Most Systems can ignore this.
|
|
|
|
-- `entities` list. Most Systems can ignore this.
|
|
|
|
-- * The `modified` flag is an indicator if the System has been modified in
|
|
|
|
-- * The `modified` flag is an indicator if the System has been modified in
|
|
|
@@ -219,20 +225,11 @@ end
|
|
|
|
-- this key is considered a System rather than an Entity.
|
|
|
|
-- this key is considered a System rather than an Entity.
|
|
|
|
local systemTableKey = { "SYSTEM_TABLE_KEY" }
|
|
|
|
local systemTableKey = { "SYSTEM_TABLE_KEY" }
|
|
|
|
|
|
|
|
|
|
|
|
-- Check if tables are systems.
|
|
|
|
-- Checks if a table is a System.
|
|
|
|
local function isSystem(table)
|
|
|
|
local function isSystem(table)
|
|
|
|
return table[systemTableKey]
|
|
|
|
return table[systemTableKey]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--- Creates a default System.
|
|
|
|
|
|
|
|
-- @param table A table to be used as a System, or `nil` to create a new System.
|
|
|
|
|
|
|
|
-- @return A new System or System class
|
|
|
|
|
|
|
|
function tiny.system(table)
|
|
|
|
|
|
|
|
table = table or {}
|
|
|
|
|
|
|
|
table[systemTableKey] = true
|
|
|
|
|
|
|
|
return table
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Update function for all Processing Systems.
|
|
|
|
-- Update function for all Processing Systems.
|
|
|
|
local function processingSystemUpdate(system, dt)
|
|
|
|
local function processingSystemUpdate(system, dt)
|
|
|
|
local entities = system.entities
|
|
|
|
local entities = system.entities
|
|
|
@@ -258,24 +255,45 @@ 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)
|
|
|
|
-- A Processing System iterates through its Entities in no particluar order, and
|
|
|
|
local entities = system.entities
|
|
|
|
-- updates them individually. It has two important fields:
|
|
|
|
local indices = system.indices
|
|
|
|
--
|
|
|
|
local sortDelegate = system.sortDelegate
|
|
|
|
-- * `function system:process(entity, dt)`
|
|
|
|
if not sortDelegate then
|
|
|
|
-- * `function system:filter(entity)`
|
|
|
|
local compare = system.compare
|
|
|
|
--
|
|
|
|
sortDelegate = function(e1, e2)
|
|
|
|
-- There are also a few other optional callbacks, including the optional
|
|
|
|
return compare(system, e1, e2)
|
|
|
|
-- callbacks in `tiny.system`:
|
|
|
|
end
|
|
|
|
--
|
|
|
|
system.sortDelegate = sortDelegate
|
|
|
|
-- * `function system:preProcess(entities, dt)`
|
|
|
|
end
|
|
|
|
-- * `function system:postProcess(entities, dt)`
|
|
|
|
tsort(entities, sortDelegate)
|
|
|
|
--
|
|
|
|
for i = 1, #entities do
|
|
|
|
-- @param table A table to be used as a System, or `nil` to create a new
|
|
|
|
local entity = entities[i]
|
|
|
|
-- Processing System.
|
|
|
|
indices[entity] = i
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--- Creates a new System or System class from the supplied table. If `table` is
|
|
|
|
|
|
|
|
-- nil, creates a new table.
|
|
|
|
|
|
|
|
function tiny.system(table)
|
|
|
|
|
|
|
|
table = table or {}
|
|
|
|
|
|
|
|
table[systemTableKey] = true
|
|
|
|
|
|
|
|
return table
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--- Creates a new Processing System or Processing System class. Processing
|
|
|
|
|
|
|
|
-- Systems process each entity individual, and are usually what is needed.
|
|
|
|
|
|
|
|
-- Processing Systems have three extra callbacks besides those inheritted from
|
|
|
|
|
|
|
|
-- vanilla Systems.
|
|
|
|
|
|
|
|
-- * `function system:preProcess(entities, dt)` - Called before iterating
|
|
|
|
|
|
|
|
-- over each Entity.
|
|
|
|
|
|
|
|
-- * `function system:process(entities, dt)` - Called for each Entity in
|
|
|
|
|
|
|
|
-- the System.
|
|
|
|
|
|
|
|
-- * `function system:postProcess(entity, dt)` - Called after iteration.
|
|
|
|
|
|
|
|
-- Processing Systems have their own `update` method, so don't implement a
|
|
|
|
|
|
|
|
-- a custom `update` callback for Processing Systems.
|
|
|
|
-- @see system
|
|
|
|
-- @see system
|
|
|
|
-- @return A new Processing System or Processing System class
|
|
|
|
|
|
|
|
function tiny.processingSystem(table)
|
|
|
|
function tiny.processingSystem(table)
|
|
|
|
table = table or {}
|
|
|
|
table = table or {}
|
|
|
|
table[systemTableKey] = true
|
|
|
|
table[systemTableKey] = true
|
|
|
@@ -283,45 +301,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
|
|
|
|
-- @return A new Sorted System or Sorted System class
|
|
|
|
-- @see sortedSystem
|
|
|
|
function tiny.sortedSystem(table)
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
@@ -339,10 +343,7 @@ local worldMetaTable
|
|
|
|
|
|
|
|
|
|
|
|
--- Creates a new World.
|
|
|
|
--- Creates a new World.
|
|
|
|
-- Can optionally add default Systems and Entities.
|
|
|
|
-- Can optionally add default Systems and Entities.
|
|
|
|
-- @param ... Systems and Entities to add to the World
|
|
|
|
|
|
|
|
-- @return A new World
|
|
|
|
|
|
|
|
function tiny.world(...)
|
|
|
|
function tiny.world(...)
|
|
|
|
|
|
|
|
|
|
|
|
local ret = {
|
|
|
|
local ret = {
|
|
|
|
|
|
|
|
|
|
|
|
-- List of Entities to add
|
|
|
|
-- List of Entities to add
|
|
|
@@ -376,14 +377,11 @@ function tiny.world(...)
|
|
|
|
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.
|
|
|
|
-- @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
|
|
|
@@ -394,8 +392,6 @@ end
|
|
|
|
tiny_addEntity = tiny.addEntity
|
|
|
|
tiny_addEntity = tiny.addEntity
|
|
|
|
|
|
|
|
|
|
|
|
--- Adds a System to the world.
|
|
|
|
--- Adds a System to the world.
|
|
|
|
-- @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
|
|
|
@@ -403,10 +399,6 @@ 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.
|
|
|
|
-- @param world
|
|
|
|
|
|
|
|
-- @param ... Systems and Entities
|
|
|
|
|
|
|
|
-- @see addEntity
|
|
|
|
|
|
|
|
-- @see addSystem
|
|
|
|
|
|
|
|
function tiny.add(world, ...)
|
|
|
|
function tiny.add(world, ...)
|
|
|
|
local obj
|
|
|
|
local obj
|
|
|
|
for i = 1, select("#", ...) do
|
|
|
|
for i = 1, select("#", ...) do
|
|
|
@@ -423,8 +415,6 @@ end
|
|
|
|
tiny_add = tiny.add
|
|
|
|
tiny_add = tiny.add
|
|
|
|
|
|
|
|
|
|
|
|
--- Removes an Entity to the World.
|
|
|
|
--- Removes an Entity to the World.
|
|
|
|
-- @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
|
|
|
@@ -432,8 +422,6 @@ end
|
|
|
|
tiny_removeEntity = tiny.removeEntity
|
|
|
|
tiny_removeEntity = tiny.removeEntity
|
|
|
|
|
|
|
|
|
|
|
|
--- Removes a System from the world.
|
|
|
|
--- Removes a System from the world.
|
|
|
|
-- @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
|
|
|
@@ -441,10 +429,6 @@ 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.
|
|
|
|
-- @param world
|
|
|
|
|
|
|
|
-- @param ... Systems and Entities
|
|
|
|
|
|
|
|
-- @see removeEntity
|
|
|
|
|
|
|
|
-- @see removeSystem
|
|
|
|
|
|
|
|
function tiny.remove(world, ...)
|
|
|
|
function tiny.remove(world, ...)
|
|
|
|
local obj
|
|
|
|
local obj
|
|
|
|
for i = 1, select("#", ...) do
|
|
|
|
for i = 1, select("#", ...) do
|
|
|
@@ -462,7 +446,6 @@ 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
|
|
|
@@ -495,6 +478,11 @@ function tiny_manageSystems(world)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
s2r[i] = nil
|
|
|
|
s2r[i] = nil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Clean up System
|
|
|
|
|
|
|
|
system.world = nil
|
|
|
|
|
|
|
|
system.entities = nil
|
|
|
|
|
|
|
|
system.indices = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Add Systems
|
|
|
|
-- Add Systems
|
|
|
@@ -505,8 +493,11 @@ function tiny_manageSystems(world)
|
|
|
|
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
|
|
|
|
systemIndices[system] = index
|
|
|
|
systems[index] = system
|
|
|
|
systems[index] = system
|
|
|
@@ -529,12 +520,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
|
|
|
@@ -580,7 +569,6 @@ 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]
|
|
|
@@ -612,25 +600,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
|
|
|
@@ -641,7 +628,19 @@ function tiny.update(world, dt)
|
|
|
|
--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
|
|
|
@@ -650,7 +649,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)
|
|
|
@@ -658,7 +656,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
|
|
|
@@ -667,35 +664,24 @@ function tiny.clearSystems(world)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--- Gets number of Entities in the World.
|
|
|
|
--- Gets number of Entities in the World.
|
|
|
|
-- @param world
|
|
|
|
|
|
|
|
-- @return An integer
|
|
|
|
|
|
|
|
function tiny.getEntityCount(world)
|
|
|
|
function tiny.getEntityCount(world)
|
|
|
|
return world.entityCount
|
|
|
|
return world.entityCount
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--- Gets number of Systems in World.
|
|
|
|
--- Gets number of Systems in World.
|
|
|
|
-- @param world
|
|
|
|
|
|
|
|
-- @return An integer
|
|
|
|
|
|
|
|
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 a System in the World. Lower indexed Systems are processed
|
|
|
|
-- before higher indexed systems.
|
|
|
|
-- before higher indexed systems.
|
|
|
|
-- @param world
|
|
|
|
|
|
|
|
-- @param system
|
|
|
|
|
|
|
|
-- @return An integer between 1 and world:getSystemCount() inclusive
|
|
|
|
|
|
|
|
function tiny.getSystemIndex(world, system)
|
|
|
|
function tiny.getSystemIndex(world, system)
|
|
|
|
return world.systemIndices[system]
|
|
|
|
return world.systemIndices[system]
|
|
|
|
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.
|
|
|
|
-- @param world
|
|
|
|
|
|
|
|
-- @param system
|
|
|
|
|
|
|
|
-- @param index
|
|
|
|
|
|
|
|
-- @return Old index
|
|
|
|
|
|
|
|
function tiny.setSystemIndex(world, system, index)
|
|
|
|
function tiny.setSystemIndex(world, system, index)
|
|
|
|
local systemIndices = world.systemIndices
|
|
|
|
local systemIndices = world.systemIndices
|
|
|
|
local oldIndex = systemIndices[system]
|
|
|
|
local oldIndex = systemIndices[system]
|
|
|
|