2015-03-29 12:19:31 +00:00
|
|
|
--- @module tiny-ecs
|
|
|
|
-- @author Calvin Rose
|
|
|
|
local tiny = {}
|
|
|
|
|
2015-03-31 11:55:12 +00:00
|
|
|
--- Tiny-ecs Version, a period-separated three number string like "1.2.3"
|
2015-04-21 13:47:36 +00:00
|
|
|
tiny._VERSION = "1.0.0"
|
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
|
|
|
|
local tconcat = table.concat
|
|
|
|
local pairs = pairs
|
|
|
|
local ipairs = ipairs
|
2015-03-21 08:09:39 +00:00
|
|
|
local setmetatable = setmetatable
|
2015-03-29 03:19:22 +00:00
|
|
|
local getmetatable = getmetatable
|
2015-04-19 05:40:42 +00:00
|
|
|
local type = type
|
2015-03-29 03:19:22 +00:00
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
-- Local versions of the library functions
|
|
|
|
local tiny_system
|
|
|
|
local tiny_manageEntities
|
|
|
|
local tiny_manageSystems
|
|
|
|
local tiny_updateSystem
|
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.
|
|
|
|
-- @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
|
|
|
|
function tiny.requireAll(...)
|
|
|
|
local components = {...}
|
|
|
|
local len = #components
|
2015-04-21 13:47:36 +00:00
|
|
|
return function(_, e)
|
2015-03-29 12:19:31 +00:00
|
|
|
local c
|
|
|
|
for i = 1, len do
|
|
|
|
c = components[i]
|
2015-04-19 05:40:42 +00:00
|
|
|
if type(c) == 'function' then
|
2015-04-21 13:47:36 +00:00
|
|
|
if not c(_, e) then
|
2015-04-19 05:40:42 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
elseif e[c] == nil then
|
2015-03-29 12:19:31 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
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(...)
|
|
|
|
local components = {...}
|
|
|
|
local len = #components
|
2015-04-21 13:47:36 +00:00
|
|
|
return function(_, e)
|
2015-03-29 12:19:31 +00:00
|
|
|
local c
|
|
|
|
for i = 1, len do
|
|
|
|
c = components[i]
|
2015-04-19 05:40:42 +00:00
|
|
|
if type(c) == 'function' then
|
2015-04-21 13:47:36 +00:00
|
|
|
if c(_, e) then
|
2015-04-19 05:40:42 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
elseif e[c] ~= nil then
|
2015-03-29 12:19:31 +00:00
|
|
|
return true
|
|
|
|
end
|
2015-03-22 02:41:34 +00:00
|
|
|
end
|
2015-03-29 12:19:31 +00:00
|
|
|
return false
|
2015-03-22 02:41:34 +00:00
|
|
|
end
|
2015-03-21 08:09:39 +00:00
|
|
|
end
|
|
|
|
|
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-03-29 12:19:31 +00:00
|
|
|
-- @section System
|
2015-03-21 08:09:39 +00:00
|
|
|
|
2015-04-21 13:47:36 +00:00
|
|
|
-- 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 = {}
|
|
|
|
|
|
|
|
-- Check if tables are systems.
|
|
|
|
local function isSystem(table)
|
|
|
|
return table[systemTableKey]
|
2015-03-22 02:41:34 +00:00
|
|
|
end
|
|
|
|
|
2015-04-21 13:47:36 +00:00
|
|
|
--- Marks a table conforming to the System interface as a System recognized by
|
|
|
|
-- tiny-ecs. Systems are tables that contain at least one field, an update
|
|
|
|
-- function that takes parameters like so:
|
|
|
|
-- `function system:update(world, entities, dt)`. The `world` is the World the
|
|
|
|
-- System belongs to, `entities` is an unordered table of Entities,
|
|
|
|
-- with Entities as the 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.
|
|
|
|
function tiny.system(table)
|
|
|
|
if table == nil then
|
|
|
|
table = {}
|
|
|
|
end
|
|
|
|
table[systemTableKey] = true
|
|
|
|
return table
|
2015-03-21 08:09:39 +00:00
|
|
|
end
|
2015-04-21 13:47:36 +00:00
|
|
|
tiny_system = tiny.system
|
2015-03-21 08:09:39 +00:00
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
local worldMetaTable = { __index = tiny }
|
2015-03-21 08:09:39 +00:00
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
--- 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).`
|
|
|
|
-- @section World
|
2015-03-21 08:09:39 +00:00
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
--- Creates a new World.
|
|
|
|
-- Can optionally add default Systems and Entities.
|
|
|
|
-- @param ... Systems and Entities to add to the World
|
|
|
|
-- @return A new World
|
|
|
|
function tiny.world(...)
|
2015-03-21 08:09:39 +00:00
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
local ret = {
|
2015-03-21 08:09:39 +00:00
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
-- Table of Entities to status
|
2015-03-31 11:55:12 +00:00
|
|
|
-- Statuses: remove, add
|
|
|
|
entityStatus = {},
|
2015-03-22 04:07:42 +00:00
|
|
|
|
2015-03-31 11:55:12 +00:00
|
|
|
-- Table of Systems to status
|
|
|
|
-- Statuses: remove, add
|
|
|
|
systemStatus = {},
|
|
|
|
|
|
|
|
-- Set of Entities -- active are true, inactive are false
|
2015-03-29 12:19:31 +00:00
|
|
|
entities = {},
|
2015-03-29 03:19:22 +00:00
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
-- Number of Entities in World.
|
|
|
|
entityCount = 0,
|
2015-03-21 08:09:39 +00:00
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
-- Number of Systems in World.
|
|
|
|
systemCount = 0,
|
2015-03-28 15:11:36 +00:00
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
-- List of Systems
|
|
|
|
systems = {},
|
2015-03-21 08:09:39 +00:00
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
-- Table of Systems to whether or not they are active.
|
|
|
|
activeSystems = {},
|
2015-03-22 04:07:42 +00:00
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
-- Table of Systems to System Indices
|
|
|
|
systemIndices = {},
|
2015-03-22 04:07:42 +00:00
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
-- Table of Systems to Sets of matching Entities
|
2015-03-31 11:55:12 +00:00
|
|
|
systemEntities = {}
|
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-04-21 13:47:36 +00:00
|
|
|
return setmetatable(ret, worldMetaTable)
|
2015-03-21 08:09:39 +00:00
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
end
|
|
|
|
|
2015-04-21 11:37:33 +00:00
|
|
|
--- 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.
|
|
|
|
-- @param world
|
|
|
|
-- @param entity
|
|
|
|
function tiny.addEntity(world, entity)
|
|
|
|
world.entityStatus[entity] = "add"
|
|
|
|
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)
|
|
|
|
world.systemStatus[system] = "add"
|
|
|
|
end
|
|
|
|
tiny_addSystem = tiny.addSystem
|
|
|
|
|
|
|
|
--- Shortcut for adding multiple Entities and Systems to the World.
|
2015-03-29 12:19:31 +00:00
|
|
|
-- 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
|
|
|
|
function tiny.add(world, ...)
|
2015-03-22 00:15:44 +00:00
|
|
|
local args = {...}
|
2015-03-29 03:19:22 +00:00
|
|
|
for _, obj in ipairs(args) do
|
2015-04-21 13:47:36 +00:00
|
|
|
if isSystem(obj) then
|
2015-04-21 11:37:33 +00:00
|
|
|
tiny_addSystem(world, obj)
|
2015-03-29 03:19:22 +00:00
|
|
|
else -- Assume obj is an Entity
|
2015-04-21 11:37:33 +00:00
|
|
|
tiny_addEntity(world, obj)
|
2015-03-29 03:19:22 +00:00
|
|
|
end
|
2015-03-21 08:09:39 +00:00
|
|
|
end
|
|
|
|
end
|
2015-03-29 12:19:31 +00:00
|
|
|
tiny_add = tiny.add
|
2015-03-21 08:09:39 +00:00
|
|
|
|
2015-04-21 11:37:33 +00:00
|
|
|
--- 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)
|
|
|
|
world.entityStatus[entity] = "remove"
|
|
|
|
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)
|
|
|
|
world.systemStatus[system] = "remove"
|
|
|
|
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.
|
2015-03-29 12:19:31 +00:00
|
|
|
-- @param world
|
|
|
|
-- @param ... Systems and Entities
|
|
|
|
function tiny.remove(world, ...)
|
2015-03-22 00:15:44 +00:00
|
|
|
local args = {...}
|
2015-03-29 03:19:22 +00:00
|
|
|
for _, obj in ipairs(args) do
|
2015-04-21 13:47:36 +00:00
|
|
|
if isSystem(obj) then
|
2015-04-21 11:37:33 +00:00
|
|
|
tiny_removeSystem(world, obj)
|
2015-03-31 11:55:12 +00:00
|
|
|
else -- Assume obj is an Entity
|
2015-04-21 11:37:33 +00:00
|
|
|
tiny_removeEntity(world, obj)
|
2015-03-29 03:19:22 +00:00
|
|
|
end
|
2015-03-21 08:09:39 +00:00
|
|
|
end
|
|
|
|
end
|
2015-03-29 12:19:31 +00:00
|
|
|
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)
|
2015-04-21 13:47:36 +00:00
|
|
|
local es = world.systemEntities[system]
|
|
|
|
system:update(world, es, dt)
|
2015-03-24 10:01:21 +00:00
|
|
|
end
|
2015-03-29 12:19:31 +00:00
|
|
|
tiny_updateSystem = tiny.updateSystem
|
2015-03-21 08:09:39 +00:00
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
--- 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)
|
2015-03-22 04:07:42 +00:00
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
local systemEntities = world.systemEntities
|
|
|
|
local systemIndices = world.systemIndices
|
|
|
|
local entities = world.entities
|
|
|
|
local systems = world.systems
|
2015-03-31 11:55:12 +00:00
|
|
|
local systemStatus = world.systemStatus
|
2015-03-29 12:19:31 +00:00
|
|
|
local activeSystems = world.activeSystems
|
2015-04-21 11:37:33 +00:00
|
|
|
local systemCount = world.systemCount
|
2015-03-29 03:19:22 +00:00
|
|
|
|
2015-03-31 11:55:12 +00:00
|
|
|
for system, status in pairs(systemStatus) do
|
2015-03-29 03:19:22 +00:00
|
|
|
|
2015-03-31 11:55:12 +00:00
|
|
|
if status == "add" then
|
|
|
|
local es = {}
|
|
|
|
systemEntities[system] = es
|
2015-04-21 11:37:33 +00:00
|
|
|
systemIndices[system] = systemCount + 1
|
|
|
|
systemCount = systemCount + 1
|
2015-04-03 11:59:02 +00:00
|
|
|
systems[systemIndices[system]] = system
|
2015-03-31 11:55:12 +00:00
|
|
|
activeSystems[system] = true
|
|
|
|
local filter = system.filter
|
2015-04-21 13:47:36 +00:00
|
|
|
local onAdd = system.onAdd
|
2015-03-31 11:55:12 +00:00
|
|
|
if filter then
|
|
|
|
for e in pairs(entities) do
|
2015-04-21 13:47:36 +00:00
|
|
|
local added = filter(system, e) and true or nil
|
|
|
|
es[e] = added
|
|
|
|
if added and onAdd then
|
|
|
|
onAdd(system, e)
|
|
|
|
end
|
2015-03-31 11:55:12 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif status == "remove" then
|
|
|
|
local index = systemIndices[system]
|
2015-04-21 11:37:33 +00:00
|
|
|
systemCount = systemCount - 1
|
2015-03-31 11:55:12 +00:00
|
|
|
tremove(systems, index)
|
|
|
|
local onRemove = system.onRemove
|
2015-03-29 12:19:31 +00:00
|
|
|
if onRemove then
|
2015-03-29 03:19:22 +00:00
|
|
|
for e in pairs(systemEntities[sys]) do
|
2015-03-29 12:19:31 +00:00
|
|
|
onRemove(e)
|
2015-03-29 03:19:22 +00:00
|
|
|
end
|
|
|
|
end
|
2015-03-31 11:55:12 +00:00
|
|
|
systemEntities[system] = nil
|
|
|
|
activeSystems[system] = nil
|
2015-03-23 14:58:26 +00:00
|
|
|
end
|
2015-03-21 08:09:39 +00:00
|
|
|
|
2015-03-31 11:55:12 +00:00
|
|
|
systemStatus[system] = nil
|
2015-03-22 04:07:42 +00:00
|
|
|
end
|
|
|
|
|
2015-03-29 03:19:22 +00:00
|
|
|
-- Update the number of Systems in the World
|
2015-04-21 11:37:33 +00:00
|
|
|
world.systemCount = systemCount
|
2015-03-29 03:19:22 +00:00
|
|
|
end
|
2015-03-29 12:19:31 +00:00
|
|
|
tiny_manageSystems = tiny.manageSystems
|
2015-03-29 03:19:22 +00:00
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
--- 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)
|
2015-03-29 03:19:22 +00:00
|
|
|
|
2015-03-31 11:55:12 +00:00
|
|
|
local entityStatus = world.entityStatus
|
2015-03-29 12:19:31 +00:00
|
|
|
local systemEntities = world.systemEntities
|
|
|
|
local entities = world.entities
|
|
|
|
local systems = world.systems
|
2015-04-21 11:37:33 +00:00
|
|
|
local entityCount = world.entityCount
|
2015-03-22 04:07:42 +00:00
|
|
|
|
|
|
|
-- Add, remove, or change Entities
|
2015-03-31 11:55:12 +00:00
|
|
|
for e, s in pairs(entityStatus) do
|
2015-03-21 08:09:39 +00:00
|
|
|
if s == "add" then
|
2015-04-19 05:40:42 +00:00
|
|
|
if not entities[e] then
|
2015-04-21 11:37:33 +00:00
|
|
|
entityCount = entityCount + 1
|
2015-04-19 05:40:42 +00:00
|
|
|
end
|
2015-03-31 11:55:12 +00:00
|
|
|
entities[e] = true
|
2015-04-21 13:47:36 +00:00
|
|
|
for system, es in pairs(systemEntities) do
|
|
|
|
local filter = system.filter
|
2015-03-29 09:07:39 +00:00
|
|
|
if filter then
|
2015-04-21 13:47:36 +00:00
|
|
|
local matches = filter(system, e) and true or nil
|
|
|
|
local onRemove = system.onRemove
|
2015-04-19 06:22:51 +00:00
|
|
|
if not matches and es[e] and onRemove then
|
2015-04-21 13:47:36 +00:00
|
|
|
onRemove(system, e)
|
2015-04-19 06:22:51 +00:00
|
|
|
end
|
2015-04-21 13:47:36 +00:00
|
|
|
local onAdd = system.onAdd
|
2015-03-29 12:19:31 +00:00
|
|
|
if onAdd and matches and not es[e] then
|
2015-04-21 13:47:36 +00:00
|
|
|
onAdd(system, e)
|
2015-03-29 09:07:39 +00:00
|
|
|
end
|
|
|
|
es[e] = matches
|
2015-03-23 14:58:26 +00:00
|
|
|
end
|
2015-03-21 08:09:39 +00:00
|
|
|
end
|
2015-03-22 04:07:42 +00:00
|
|
|
elseif s == "remove" then
|
2015-04-19 05:40:42 +00:00
|
|
|
if entities[e] then
|
2015-04-21 11:37:33 +00:00
|
|
|
entityCount = entityCount - 1
|
2015-04-19 05:40:42 +00:00
|
|
|
end
|
2015-03-28 14:54:34 +00:00
|
|
|
entities[e] = nil
|
2015-04-21 13:47:36 +00:00
|
|
|
for system, es in pairs(systemEntities) do
|
|
|
|
local onRemove = system.onRemove
|
2015-03-29 12:19:31 +00:00
|
|
|
if es[e] and onRemove then
|
2015-04-21 13:47:36 +00:00
|
|
|
onRemove(system, e)
|
2015-03-23 14:58:26 +00:00
|
|
|
end
|
2015-03-28 14:54:34 +00:00
|
|
|
es[e] = nil
|
2015-03-22 04:07:42 +00:00
|
|
|
end
|
2015-03-21 08:09:39 +00:00
|
|
|
end
|
2015-03-31 11:55:12 +00:00
|
|
|
entityStatus[e] = 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-22 02:41:34 +00:00
|
|
|
|
2015-03-29 03:19:22 +00:00
|
|
|
end
|
2015-03-29 12:19:31 +00:00
|
|
|
tiny_manageEntities = tiny.manageEntities
|
2015-03-29 03:19:22 +00:00
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
--- Updates the World.
|
|
|
|
-- Frees Entities that have been marked for freeing, adds
|
2015-03-29 03:19:22 +00:00
|
|
|
-- entities that have been marked for adding, etc.
|
2015-03-29 12:19:31 +00:00
|
|
|
-- @param world
|
|
|
|
-- @param dt Delta time
|
|
|
|
function tiny.update(world, dt)
|
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-03-22 04:07:42 +00:00
|
|
|
-- Iterate through Systems IN ORDER
|
2015-03-29 12:19:31 +00:00
|
|
|
for _, s in ipairs(world.systems) do
|
|
|
|
if world.activeSystems[s] then
|
|
|
|
tiny_updateSystem(world, s, dt)
|
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.
|
|
|
|
-- When World:update(dt) is next called,
|
2015-03-23 14:58:26 +00:00
|
|
|
-- all Entities will be removed.
|
2015-03-29 12:19:31 +00:00
|
|
|
-- @param world
|
|
|
|
function tiny.clearEntities(world)
|
2015-03-31 11:55:12 +00:00
|
|
|
local entityStatus = world.entityStatus
|
2015-03-29 12:19:31 +00:00
|
|
|
for e in pairs(world.entities) do
|
2015-03-31 11:55:12 +00:00
|
|
|
entityStatus[e] = "remove"
|
2015-03-23 14:58:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-29 12:19:31 +00:00
|
|
|
--- Removes all Systems from the World.
|
|
|
|
-- When World:update(dt) is next called,
|
2015-03-23 14:58:26 +00:00
|
|
|
-- all Systems will be removed.
|
2015-03-29 12:19:31 +00:00
|
|
|
-- @param world
|
|
|
|
function tiny.clearSystems(world)
|
2015-03-31 11:55:12 +00:00
|
|
|
local systemStatus = world.systemStatus
|
|
|
|
for _, s in ipairs(world.systems) do
|
|
|
|
systemStatus[s] = "remove"
|
2015-03-23 14:58:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-31 09:57:55 +00:00
|
|
|
--- Gets count of Entities in World.
|
|
|
|
-- @param world
|
|
|
|
function tiny.getEntityCount(world)
|
|
|
|
return world.entityCount
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Gets count of Systems in World.
|
|
|
|
-- @param world
|
2015-03-31 11:55:12 +00:00
|
|
|
function tiny.getSystemCount(world)
|
2015-03-31 09:57:55 +00:00
|
|
|
return world.systemCount
|
|
|
|
end
|
|
|
|
|
2015-04-21 13:47:36 +00:00
|
|
|
--- Gets the index of a System in the world. Lower indexed Systems are processed
|
|
|
|
-- before higher indexed systems.
|
|
|
|
-- @param world
|
|
|
|
-- @param system
|
|
|
|
function tiny.getSystemIndex(world, system)
|
|
|
|
return world.systemIndices[system]
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Sets the index of a System in the world. Changes the order in
|
|
|
|
-- which they Systems processed, because lower indexed Systems are processed
|
|
|
|
-- first.
|
|
|
|
-- @param world
|
|
|
|
-- @param system
|
|
|
|
-- @param index
|
|
|
|
function tiny.setSystemIndex(world, system, index)
|
|
|
|
local oldIndex = world.systemIndices[system]
|
|
|
|
local systems = world.systems
|
|
|
|
tremove(systems, oldIndex)
|
|
|
|
tinsert(systems, index, system)
|
|
|
|
world.systemIndices[system] = index
|
|
|
|
end
|
|
|
|
|
2015-03-31 11:55:12 +00:00
|
|
|
--- Activates Systems in the World.
|
|
|
|
-- Activated Systems will be update whenever tiny.update(world, dt) is called.
|
2015-03-29 12:19:31 +00:00
|
|
|
-- @param world
|
2015-03-31 11:55:12 +00:00
|
|
|
-- @param ... Systems to activate. The Systems must already be added to the
|
|
|
|
-- World.
|
|
|
|
function tiny.activate(world, ...)
|
|
|
|
local args = {...}
|
|
|
|
for _, obj in ipairs(args) do
|
|
|
|
world.activeSystems[obj] = 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 _, obj in ipairs(args) do
|
|
|
|
world.activeSystems[obj] = nil
|
|
|
|
end
|
2015-03-29 09:07:39 +00:00
|
|
|
end
|
|
|
|
|
2015-03-29 07:50:42 +00:00
|
|
|
return tiny
|