mirror of
https://github.com/bakpakin/tiny-ecs.git
synced 2024-10-12 19:44:17 +00:00
Add Sorted Systems.
Remove tiny.manageSystems and tiny.manageEntities from API. Add onModify System callback.
This commit is contained in:
parent
0f86eae571
commit
fb345c7718
103
tiny.lua
103
tiny.lua
@ -1,10 +1,11 @@
|
|||||||
--- @module tiny-ecs
|
--- @module tiny-ecs
|
||||||
-- @author Calvin Rose
|
-- @author Calvin Rose
|
||||||
local tiny = { _VERSION = "1.0-3" }
|
local tiny = { _VERSION = "1.1-1" }
|
||||||
|
|
||||||
-- 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 pairs = pairs
|
local pairs = pairs
|
||||||
local ipairs = ipairs
|
local ipairs = ipairs
|
||||||
local setmetatable = setmetatable
|
local setmetatable = setmetatable
|
||||||
@ -88,7 +89,8 @@ end
|
|||||||
-- few other optional callbacks:
|
-- few other optional callbacks:
|
||||||
-- `function system:filter(entity)` - returns a boolean,
|
-- `function system:filter(entity)` - returns a boolean,
|
||||||
-- `function system:onAdd(entity)` - returns nil,
|
-- `function system:onAdd(entity)` - returns nil,
|
||||||
-- `function system:onRemove(entity)` - returns nil.
|
-- `function system:onRemove(entity)` - returns nil,
|
||||||
|
-- `function system:onModify(systemData, dt)` - returns nil.
|
||||||
-- For Filters, it is conveient to use `tiny.requireAll` or `tiny.requireOne`,
|
-- For Filters, it is conveient to use `tiny.requireAll` or `tiny.requireOne`,
|
||||||
-- but one can write their own filters as well.
|
-- but one can write their own filters as well.
|
||||||
-- @param table A table to be used as a System, or `nil` to create a new System.
|
-- @param table A table to be used as a System, or `nil` to create a new System.
|
||||||
@ -133,7 +135,8 @@ end
|
|||||||
-- `function system:preProcess(entities, dt)` - returns nil,
|
-- `function system:preProcess(entities, dt)` - returns nil,
|
||||||
-- `function system:postProcess(entities, dt)` - returns nil,
|
-- `function system:postProcess(entities, dt)` - returns nil,
|
||||||
-- `function system:onAdd(entity)` - returns nil,
|
-- `function system:onAdd(entity)` - returns nil,
|
||||||
-- `function system:onRemove(entity)` - returns nil.
|
-- `function system:onRemove(entity)` - returns nil,
|
||||||
|
-- `function system:onModify(systemData, dt)` - returns nil.
|
||||||
-- For Filters, it is conveient to use `tiny.requireAll` or `tiny.requireOne`,
|
-- For Filters, it is conveient to use `tiny.requireAll` or `tiny.requireOne`,
|
||||||
-- but one can write their own filters as well.
|
-- but one can write their own filters as well.
|
||||||
-- @param table A table to be used as a System, or `nil` to create a new
|
-- @param table A table to be used as a System, or `nil` to create a new
|
||||||
@ -147,10 +150,55 @@ function tiny.processingSystem(table)
|
|||||||
return table
|
return table
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Sorts Systems by a function system.sort(entity1, entity2) on modify.
|
||||||
|
local function sortedSystemOnModify(system, systemData, dt)
|
||||||
|
local system = systemData.system
|
||||||
|
local entities = systemData.entities
|
||||||
|
local entityIndices = systemData.entityIndices
|
||||||
|
local sortDelegate = systemData.sortDelegate
|
||||||
|
if not sortDelegate then
|
||||||
|
local sort = system.sort
|
||||||
|
sortDelegate = function(e1, e2)
|
||||||
|
sort(system, e1, e2)
|
||||||
|
end
|
||||||
|
systemData.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 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,
|
||||||
|
-- `function system:sort(entity1, entity2)` - returns boolean.
|
||||||
|
-- For Filters, it is conveient to use `tiny.requireAll` or `tiny.requireOne`,
|
||||||
|
-- but one can write their own filters as well.
|
||||||
|
-- @param table A table to be used as a System, or `nil` to create a new
|
||||||
|
-- Processing System.
|
||||||
|
function tiny.sortedSystem(table)
|
||||||
|
if table == nil then
|
||||||
|
table = {}
|
||||||
|
end
|
||||||
|
table[systemTableKey] = true
|
||||||
|
table.update = processingSystemUpdate
|
||||||
|
table.onModify = sortedSystemOnModify
|
||||||
|
return table
|
||||||
|
end
|
||||||
|
|
||||||
--- World functions.
|
--- World functions.
|
||||||
-- A World is a container that manages Entities and Systems. The tiny-ecs module
|
-- 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
|
-- is set to be the `__index` of all World tables, so the often clearer syntax
|
||||||
-- World:method can be used for any function in the library. For example,
|
-- 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).`
|
-- `tiny.add(world, e1, e2, e3)` is the same as `world:add(e1, e2, e3).`
|
||||||
-- @section World
|
-- @section World
|
||||||
|
|
||||||
@ -288,10 +336,8 @@ function tiny.updateSystem(world, system, dt)
|
|||||||
system:update(es, dt)
|
system:update(es, dt)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Adds and removes Systems that have been marked from the World.
|
-- Adds and removes Systems that have been marked from the World.
|
||||||
-- The user of this library should seldom if ever call this.
|
function tiny_manageSystems(world)
|
||||||
-- @param world
|
|
||||||
function tiny.manageSystems(world)
|
|
||||||
|
|
||||||
local s2a, s2r = world.systemsToAdd, world.systemsToRemove
|
local s2a, s2r = world.systemsToAdd, world.systemsToRemove
|
||||||
|
|
||||||
@ -303,8 +349,8 @@ function tiny.manageSystems(world)
|
|||||||
local systemIndices = world.systemIndices
|
local systemIndices = world.systemIndices
|
||||||
local entities = world.entities
|
local entities = world.entities
|
||||||
local systems = world.systems
|
local systems = world.systems
|
||||||
|
local system, systemData, index, filter
|
||||||
local system, systemData, index, filter, entityList, entityIndices, entityIndex, onRemove, onAdd
|
local entityList, entityIndices, entityIndex, onRemove, onAdd
|
||||||
|
|
||||||
-- Remove Systems
|
-- Remove Systems
|
||||||
for i = 1, #s2r do
|
for i = 1, #s2r do
|
||||||
@ -334,7 +380,13 @@ function tiny.manageSystems(world)
|
|||||||
if not systemIndices[system] then
|
if not systemIndices[system] then
|
||||||
entityList = {}
|
entityList = {}
|
||||||
entityIndices = {}
|
entityIndices = {}
|
||||||
systemData = { system = system, entities = entityList, indices = entityIndices, active = true }
|
systemData = {
|
||||||
|
system = system,
|
||||||
|
entities = entityList,
|
||||||
|
indices = entityIndices,
|
||||||
|
active = true,
|
||||||
|
modified = true
|
||||||
|
}
|
||||||
index = #systems + 1
|
index = #systems + 1
|
||||||
systemIndices[system] = index
|
systemIndices[system] = index
|
||||||
systems[index] = systemData
|
systems[index] = systemData
|
||||||
@ -359,12 +411,9 @@ function tiny.manageSystems(world)
|
|||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
tiny_manageSystems = tiny.manageSystems
|
|
||||||
|
|
||||||
--- Adds and removes Entities that have been marked.
|
-- Adds and removes Entities that have been marked.
|
||||||
-- The user of this library should seldom if ever call this.
|
function tiny_manageEntities(world)
|
||||||
-- @param world
|
|
||||||
function tiny.manageEntities(world)
|
|
||||||
|
|
||||||
local e2a, e2r = world.entitiesToAdd, world.entitiesToRemove
|
local e2a, e2r = world.entitiesToAdd, world.entitiesToRemove
|
||||||
|
|
||||||
@ -376,7 +425,8 @@ function tiny.manageEntities(world)
|
|||||||
local entities = world.entities
|
local entities = world.entities
|
||||||
local systems = world.systems
|
local systems = world.systems
|
||||||
local entityCount = world.entityCount
|
local entityCount = world.entityCount
|
||||||
local entity, system, systemData, index, onRemove, onAdd, ses, seis, filter, tmpEntity
|
local entity, system, systemData, index
|
||||||
|
local onRemove, onAdd, ses, seis, filter, tmpEntity
|
||||||
|
|
||||||
-- Remove Entities
|
-- Remove Entities
|
||||||
for i = 1, #e2r do
|
for i = 1, #e2r do
|
||||||
@ -392,6 +442,7 @@ function tiny.manageEntities(world)
|
|||||||
index = seis[entity]
|
index = seis[entity]
|
||||||
|
|
||||||
if index then
|
if index then
|
||||||
|
systemData.modified = true
|
||||||
tmpEntity = ses[#ses]
|
tmpEntity = ses[#ses]
|
||||||
ses[index] = tmpEntity
|
ses[index] = tmpEntity
|
||||||
seis[tmpEntity] = index
|
seis[tmpEntity] = index
|
||||||
@ -424,6 +475,7 @@ function tiny.manageEntities(world)
|
|||||||
seis = systemData.indices
|
seis = systemData.indices
|
||||||
filter = system.filter
|
filter = system.filter
|
||||||
if filter and filter(system, entity) then
|
if filter and filter(system, entity) then
|
||||||
|
systemData.modified = true
|
||||||
index = #ses + 1
|
index = #ses + 1
|
||||||
ses[index] = entity
|
ses[index] = entity
|
||||||
seis[entity] = index
|
seis[entity] = index
|
||||||
@ -444,7 +496,6 @@ function tiny.manageEntities(world)
|
|||||||
world.entityCount = entityCount
|
world.entityCount = entityCount
|
||||||
|
|
||||||
end
|
end
|
||||||
tiny_manageEntities = tiny.manageEntities
|
|
||||||
|
|
||||||
--- Updates the World.
|
--- Updates the World.
|
||||||
-- Frees Entities that have been marked for freeing, adds
|
-- Frees Entities that have been marked for freeing, adds
|
||||||
@ -457,17 +508,29 @@ function tiny.update(world, dt)
|
|||||||
tiny_manageEntities(world)
|
tiny_manageEntities(world)
|
||||||
|
|
||||||
local systems = world.systems
|
local systems = world.systems
|
||||||
local systemData, system, update
|
local systemData, system, update, onModify, entities
|
||||||
|
|
||||||
-- Iterate through Systems IN ORDER
|
-- Iterate through Systems IN ORDER
|
||||||
for i = 1, #systems do
|
for i = 1, #systems do
|
||||||
systemData = systems[i]
|
systemData = systems[i]
|
||||||
if systemData.active then
|
if systemData.active then
|
||||||
system = systemData.system
|
system = systemData.system
|
||||||
|
|
||||||
|
-- Call the modify callback on Systems that have been modified.
|
||||||
|
if systemData.modified then
|
||||||
|
onModify = system.onModify
|
||||||
|
if onModify then
|
||||||
|
onModify(system, systemData, dt)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
--Update Systems that have an update method (most Systems)
|
||||||
update = system.update
|
update = system.update
|
||||||
if update then
|
if update then
|
||||||
update(system, systemData.entities, dt)
|
update(system, systemData.entities, dt)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
systemData.modified = false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user