|
|
@@ -23,7 +23,7 @@ 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-4" }
|
|
|
|
local tiny = { _VERSION = "1.1-7" }
|
|
|
|
|
|
|
|
|
|
|
|
-- Local versions of standard lua functions
|
|
|
|
-- Local versions of standard lua functions
|
|
|
|
local tinsert = table.insert
|
|
|
|
local tinsert = table.insert
|
|
|
@@ -206,12 +206,15 @@ end
|
|
|
|
-- * 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
|
|
|
|
-- * The `interval` field is an optional field that makes Systems update at
|
|
|
|
-- certain intervals using buffered time, regardless of World update frequency.
|
|
|
|
-- 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
|
|
|
|
-- For example, to make a System update once a second, set the System's interval
|
|
|
|
-- to 1.
|
|
|
|
-- to 1.
|
|
|
|
|
|
|
|
-- * The `index` field is the System's index in the World. Lower indexed
|
|
|
|
|
|
|
|
-- Systems are processed before higher indices. The `index` is a read only
|
|
|
|
|
|
|
|
-- field; to set the `index`, use `tiny.setSystemIndex(world, system)`.
|
|
|
|
-- * The `indices` field is a table of Entity keys to their indices in the
|
|
|
|
-- * 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
|
|
|
@@ -286,11 +289,11 @@ end
|
|
|
|
-- Systems process each entity individual, and are usually what is needed.
|
|
|
|
-- Systems process each entity individual, and are usually what is needed.
|
|
|
|
-- Processing Systems have three extra callbacks besides those inheritted from
|
|
|
|
-- Processing Systems have three extra callbacks besides those inheritted from
|
|
|
|
-- vanilla Systems.
|
|
|
|
-- vanilla Systems.
|
|
|
|
-- * `function system:preProcess(entities, dt)` - Called before iterating
|
|
|
|
--
|
|
|
|
-- over each Entity.
|
|
|
|
-- function system:preProcess(entities, dt) -- Called before iteration.
|
|
|
|
-- * `function system:process(entities, dt)` - Called for each Entity in
|
|
|
|
-- function system:process(entities, dt) -- Process each entity.
|
|
|
|
-- the System.
|
|
|
|
-- function system:postProcess(entity, dt) -- Called after iteration.
|
|
|
|
-- * `function system:postProcess(entity, dt)` - Called after iteration.
|
|
|
|
--
|
|
|
|
-- Processing Systems have their own `update` method, so don't implement a
|
|
|
|
-- Processing Systems have their own `update` method, so don't implement a
|
|
|
|
-- a custom `update` callback for Processing Systems.
|
|
|
|
-- a custom `update` callback for Processing Systems.
|
|
|
|
-- @see system
|
|
|
|
-- @see system
|
|
|
@@ -342,7 +345,8 @@ end
|
|
|
|
local worldMetaTable
|
|
|
|
local worldMetaTable
|
|
|
|
|
|
|
|
|
|
|
|
--- Creates a new World.
|
|
|
|
--- Creates a new World.
|
|
|
|
-- Can optionally add default Systems and Entities.
|
|
|
|
-- Can optionally add default Systems and Entities. Returns the new World along
|
|
|
|
|
|
|
|
-- with default Entities and Systems.
|
|
|
|
function tiny.world(...)
|
|
|
|
function tiny.world(...)
|
|
|
|
local ret = {
|
|
|
|
local ret = {
|
|
|
|
|
|
|
|
|
|
|
@@ -361,44 +365,43 @@ function tiny.world(...)
|
|
|
|
-- Set of Entities
|
|
|
|
-- Set of Entities
|
|
|
|
entities = {},
|
|
|
|
entities = {},
|
|
|
|
|
|
|
|
|
|
|
|
-- Number of Entities in World.
|
|
|
|
-- Number of Entities in World
|
|
|
|
entityCount = 0,
|
|
|
|
entityCount = 0,
|
|
|
|
|
|
|
|
|
|
|
|
-- List of System Data. A data element is a table with 4
|
|
|
|
-- List of Systems
|
|
|
|
-- keys: system, indices, entities, and active.
|
|
|
|
systems = {}
|
|
|
|
systems = {},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Table of Systems to System Indices
|
|
|
|
|
|
|
|
systemIndices = {}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tiny_add(ret, ...)
|
|
|
|
tiny_add(ret, ...)
|
|
|
|
tiny_manageSystems(ret)
|
|
|
|
tiny_manageSystems(ret)
|
|
|
|
tiny_manageEntities(ret)
|
|
|
|
tiny_manageEntities(ret)
|
|
|
|
|
|
|
|
|
|
|
|
return setmetatable(ret, worldMetaTable)
|
|
|
|
return setmetatable(ret, worldMetaTable), ...
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--- Adds an Entity to the world.
|
|
|
|
--- Adds an Entity to the world.
|
|
|
|
-- Also call this on Entities that have changed Components such that they
|
|
|
|
-- Also call this on Entities that have changed Components such that they
|
|
|
|
-- match different Filters.
|
|
|
|
-- match different Filters. Returns the Entity.
|
|
|
|
function tiny.addEntity(world, entity)
|
|
|
|
function tiny.addEntity(world, entity)
|
|
|
|
local e2a = world.entitiesToAdd
|
|
|
|
local e2a = world.entitiesToAdd
|
|
|
|
e2a[#e2a + 1] = entity
|
|
|
|
e2a[#e2a + 1] = entity
|
|
|
|
if world.entities[entity] then
|
|
|
|
if world.entities[entity] then
|
|
|
|
tiny_removeEntity(world, entity)
|
|
|
|
tiny_removeEntity(world, entity)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
tiny_addEntity = tiny.addEntity
|
|
|
|
tiny_addEntity = tiny.addEntity
|
|
|
|
|
|
|
|
|
|
|
|
--- Adds a System to the world.
|
|
|
|
--- Adds a System to the world. Returns the System.
|
|
|
|
function tiny.addSystem(world, system)
|
|
|
|
function tiny.addSystem(world, system)
|
|
|
|
local s2a = world.systemsToAdd
|
|
|
|
local s2a = world.systemsToAdd
|
|
|
|
s2a[#s2a + 1] = system
|
|
|
|
s2a[#s2a + 1] = system
|
|
|
|
|
|
|
|
return system
|
|
|
|
end
|
|
|
|
end
|
|
|
|
tiny_addSystem = tiny.addSystem
|
|
|
|
tiny_addSystem = tiny.addSystem
|
|
|
|
|
|
|
|
|
|
|
|
--- Shortcut for adding multiple Entities and Systems to the World.
|
|
|
|
--- Shortcut for adding multiple Entities and Systems to the World. Returns all
|
|
|
|
|
|
|
|
-- added Entities and Systems.
|
|
|
|
function tiny.add(world, ...)
|
|
|
|
function tiny.add(world, ...)
|
|
|
|
local obj
|
|
|
|
local obj
|
|
|
|
for i = 1, select("#", ...) do
|
|
|
|
for i = 1, select("#", ...) do
|
|
|
@@ -411,24 +414,28 @@ function tiny.add(world, ...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return ...
|
|
|
|
end
|
|
|
|
end
|
|
|
|
tiny_add = tiny.add
|
|
|
|
tiny_add = tiny.add
|
|
|
|
|
|
|
|
|
|
|
|
--- Removes an Entity to the World.
|
|
|
|
--- Removes an Entity to the World. Returns the Entity.
|
|
|
|
function tiny.removeEntity(world, entity)
|
|
|
|
function tiny.removeEntity(world, entity)
|
|
|
|
local e2r = world.entitiesToRemove
|
|
|
|
local e2r = world.entitiesToRemove
|
|
|
|
e2r[#e2r + 1] = entity
|
|
|
|
e2r[#e2r + 1] = entity
|
|
|
|
|
|
|
|
return entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
tiny_removeEntity = tiny.removeEntity
|
|
|
|
tiny_removeEntity = tiny.removeEntity
|
|
|
|
|
|
|
|
|
|
|
|
--- Removes a System from the world.
|
|
|
|
--- Removes a System from the world. Returns the System.
|
|
|
|
function tiny.removeSystem(world, system)
|
|
|
|
function tiny.removeSystem(world, system)
|
|
|
|
local s2r = world.systemsToRemove
|
|
|
|
local s2r = world.systemsToRemove
|
|
|
|
s2r[#s2r + 1] = system
|
|
|
|
s2r[#s2r + 1] = system
|
|
|
|
|
|
|
|
return system
|
|
|
|
end
|
|
|
|
end
|
|
|
|
tiny_removeSystem = tiny.removeSystem
|
|
|
|
tiny_removeSystem = tiny.removeSystem
|
|
|
|
|
|
|
|
|
|
|
|
--- Shortcut for removing multiple Entities and Systems from the World.
|
|
|
|
--- Shortcut for removing multiple Entities and Systems from the World. Returns
|
|
|
|
|
|
|
|
-- all removed Systems and Entities
|
|
|
|
function tiny.remove(world, ...)
|
|
|
|
function tiny.remove(world, ...)
|
|
|
|
local obj
|
|
|
|
local obj
|
|
|
|
for i = 1, select("#", ...) do
|
|
|
|
for i = 1, select("#", ...) do
|
|
|
@@ -441,6 +448,7 @@ function tiny.remove(world, ...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return ...
|
|
|
|
end
|
|
|
|
end
|
|
|
|
tiny_remove = tiny.remove
|
|
|
|
tiny_remove = tiny.remove
|
|
|
|
|
|
|
|
|
|
|
@@ -453,7 +461,6 @@ function tiny_manageSystems(world)
|
|
|
|
return
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local systemIndices = world.systemIndices
|
|
|
|
|
|
|
|
local entities = world.entities
|
|
|
|
local entities = world.entities
|
|
|
|
local systems = world.systems
|
|
|
|
local systems = world.systems
|
|
|
|
local system, index, filter
|
|
|
|
local system, index, filter
|
|
|
@@ -462,8 +469,8 @@ function tiny_manageSystems(world)
|
|
|
|
-- Remove Systems
|
|
|
|
-- Remove Systems
|
|
|
|
for i = 1, #s2r do
|
|
|
|
for i = 1, #s2r do
|
|
|
|
system = s2r[i]
|
|
|
|
system = s2r[i]
|
|
|
|
index = systemIndices[system]
|
|
|
|
index = system.index
|
|
|
|
if index then
|
|
|
|
if system.world == world then
|
|
|
|
onRemove = system.onRemove
|
|
|
|
onRemove = system.onRemove
|
|
|
|
if onRemove then
|
|
|
|
if onRemove then
|
|
|
|
entityList = system.entities
|
|
|
|
entityList = system.entities
|
|
|
@@ -471,10 +478,9 @@ function tiny_manageSystems(world)
|
|
|
|
onRemove(system, entityList[j])
|
|
|
|
onRemove(system, entityList[j])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
systemIndices[system] = nil
|
|
|
|
|
|
|
|
tremove(systems, index)
|
|
|
|
tremove(systems, index)
|
|
|
|
for j = index, #systems do
|
|
|
|
for j = index, #systems do
|
|
|
|
systemIndices[systems[j]] = j
|
|
|
|
systems[j].index = j
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
s2r[i] = nil
|
|
|
|
s2r[i] = nil
|
|
|
@@ -483,12 +489,13 @@ function tiny_manageSystems(world)
|
|
|
|
system.world = nil
|
|
|
|
system.world = nil
|
|
|
|
system.entities = nil
|
|
|
|
system.entities = nil
|
|
|
|
system.indices = nil
|
|
|
|
system.indices = nil
|
|
|
|
|
|
|
|
system.index = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Add Systems
|
|
|
|
-- Add Systems
|
|
|
|
for i = 1, #s2a do
|
|
|
|
for i = 1, #s2a do
|
|
|
|
system = s2a[i]
|
|
|
|
system = s2a[i]
|
|
|
|
if not systemIndices[system] then
|
|
|
|
if systems[system.index] ~= system then
|
|
|
|
entityList = {}
|
|
|
|
entityList = {}
|
|
|
|
entityIndices = {}
|
|
|
|
entityIndices = {}
|
|
|
|
system.entities = entityList
|
|
|
|
system.entities = entityList
|
|
|
@@ -499,7 +506,7 @@ function tiny_manageSystems(world)
|
|
|
|
system.modified = true
|
|
|
|
system.modified = true
|
|
|
|
system.world = world
|
|
|
|
system.world = world
|
|
|
|
index = #systems + 1
|
|
|
|
index = #systems + 1
|
|
|
|
systemIndices[system] = index
|
|
|
|
system.index = index
|
|
|
|
systems[index] = system
|
|
|
|
systems[index] = system
|
|
|
|
|
|
|
|
|
|
|
|
-- Try to add Entities
|
|
|
|
-- Try to add Entities
|
|
|
@@ -542,6 +549,7 @@ function tiny_manageEntities(world)
|
|
|
|
entity = e2r[i]
|
|
|
|
entity = e2r[i]
|
|
|
|
if entities[entity] then
|
|
|
|
if entities[entity] then
|
|
|
|
entities[entity] = nil
|
|
|
|
entities[entity] = nil
|
|
|
|
|
|
|
|
entityCount = entityCount - 1
|
|
|
|
|
|
|
|
|
|
|
|
for j = 1, #systems do
|
|
|
|
for j = 1, #systems do
|
|
|
|
system = systems[j]
|
|
|
|
system = systems[j]
|
|
|
@@ -556,7 +564,6 @@ function tiny_manageEntities(world)
|
|
|
|
seis[tmpEntity] = index
|
|
|
|
seis[tmpEntity] = index
|
|
|
|
seis[entity] = nil
|
|
|
|
seis[entity] = nil
|
|
|
|
ses[#ses] = nil
|
|
|
|
ses[#ses] = nil
|
|
|
|
entityCount = entityCount - 1
|
|
|
|
|
|
|
|
onRemove = system.onRemove
|
|
|
|
onRemove = system.onRemove
|
|
|
|
if onRemove then
|
|
|
|
if onRemove then
|
|
|
|
onRemove(system, entity)
|
|
|
|
onRemove(system, entity)
|
|
|
@@ -574,6 +581,7 @@ function tiny_manageEntities(world)
|
|
|
|
entity = e2a[i]
|
|
|
|
entity = e2a[i]
|
|
|
|
if not entities[entity] then
|
|
|
|
if not entities[entity] then
|
|
|
|
entities[entity] = true
|
|
|
|
entities[entity] = true
|
|
|
|
|
|
|
|
entityCount = entityCount + 1
|
|
|
|
|
|
|
|
|
|
|
|
for j = 1, #systems do
|
|
|
|
for j = 1, #systems do
|
|
|
|
system = systems[j]
|
|
|
|
system = systems[j]
|
|
|
@@ -585,7 +593,6 @@ function tiny_manageEntities(world)
|
|
|
|
index = #ses + 1
|
|
|
|
index = #ses + 1
|
|
|
|
ses[index] = entity
|
|
|
|
ses[index] = entity
|
|
|
|
seis[entity] = index
|
|
|
|
seis[entity] = index
|
|
|
|
entityCount = entityCount + 1
|
|
|
|
|
|
|
|
onAdd = system.onAdd
|
|
|
|
onAdd = system.onAdd
|
|
|
|
if onAdd then
|
|
|
|
if onAdd then
|
|
|
|
onAdd(system, entity)
|
|
|
|
onAdd(system, entity)
|
|
|
@@ -602,6 +609,14 @@ function tiny_manageEntities(world)
|
|
|
|
world.entityCount = entityCount
|
|
|
|
world.entityCount = entityCount
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--- 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)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--- Updates the World by dt (delta time). Takes an optional parameter, `filter`,
|
|
|
|
--- Updates the World by dt (delta time). Takes an optional parameter, `filter`,
|
|
|
|
-- which is a Filter that selects Systems from the World, and updates only those
|
|
|
|
-- 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
|
|
|
|
-- Systems. If `filter` is not supplied, all Systems are updated. Put this
|
|
|
@@ -625,7 +640,7 @@ function tiny.update(world, dt, filter)
|
|
|
|
onModify(system, dt)
|
|
|
|
onModify(system, dt)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--Update Systems that have an update method (most Systems)
|
|
|
|
-- Update Systems that have an update method (most Systems)
|
|
|
|
update = system.update
|
|
|
|
update = system.update
|
|
|
|
if update then
|
|
|
|
if update then
|
|
|
|
interval = system.interval
|
|
|
|
interval = system.interval
|
|
|
@@ -673,26 +688,24 @@ function tiny.getSystemCount(world)
|
|
|
|
return #(world.systems)
|
|
|
|
return #(world.systems)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--- Gets the index of a System in the World. Lower indexed Systems are processed
|
|
|
|
--- Gets the index of the System in the World.
|
|
|
|
-- before higher indexed systems.
|
|
|
|
-- A simpler alternative is `system.index`.
|
|
|
|
function tiny.getSystemIndex(world, system)
|
|
|
|
function tiny.getSystemIndex(world, system)
|
|
|
|
return world.systemIndices[system]
|
|
|
|
return system.index
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
--- Sets the index of a System in the World, and returns the old index. Changes
|
|
|
|
--- 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
|
|
|
|
-- the order in which they Systems processed, because lower indexed Systems are
|
|
|
|
-- processed first.
|
|
|
|
-- processed first. Returns the old system.index.
|
|
|
|
function tiny.setSystemIndex(world, system, index)
|
|
|
|
function tiny.setSystemIndex(world, system, index)
|
|
|
|
local systemIndices = world.systemIndices
|
|
|
|
local oldIndex = system.index
|
|
|
|
local oldIndex = systemIndices[system]
|
|
|
|
|
|
|
|
local systems = world.systems
|
|
|
|
local systems = world.systems
|
|
|
|
local system = systems[oldIndex]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tremove(systems, oldIndex)
|
|
|
|
tremove(systems, oldIndex)
|
|
|
|
tinsert(systems, index, system)
|
|
|
|
tinsert(systems, index, system)
|
|
|
|
|
|
|
|
|
|
|
|
for i = oldIndex, index, index >= oldIndex and 1 or -1 do
|
|
|
|
for i = oldIndex, index, index >= oldIndex and 1 or -1 do
|
|
|
|
systemIndices[systems[i]] = i
|
|
|
|
systems[i].index = i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
return oldIndex
|
|
|
|
return oldIndex
|
|
|
@@ -707,6 +720,7 @@ worldMetaTable = {
|
|
|
|
remove = tiny.remove,
|
|
|
|
remove = tiny.remove,
|
|
|
|
removeEntity = tiny.removeEntity,
|
|
|
|
removeEntity = tiny.removeEntity,
|
|
|
|
removeSystem = tiny.removeSystem,
|
|
|
|
removeSystem = tiny.removeSystem,
|
|
|
|
|
|
|
|
refresh = tiny.refresh,
|
|
|
|
update = tiny.update,
|
|
|
|
update = tiny.update,
|
|
|
|
clearEntities = tiny.clearEntities,
|
|
|
|
clearEntities = tiny.clearEntities,
|
|
|
|
clearSystems = tiny.clearSystems,
|
|
|
|
clearSystems = tiny.clearSystems,
|
|
|
|