mirror of
https://github.com/bakpakin/tiny-ecs.git
synced 2026-07-22 07:56:52 -06:00
Clean up System and Entity adding code. Add options for adding and removeing single Entities and Systems.
This commit is contained in:
@@ -5,6 +5,7 @@ local tiny = {}
|
|||||||
--- Tiny-ecs Version, a period-separated three number string like "1.2.3"
|
--- Tiny-ecs Version, a period-separated three number string like "1.2.3"
|
||||||
tiny._VERSION = "0.3.0"
|
tiny._VERSION = "0.3.0"
|
||||||
|
|
||||||
|
-- Local versions of standard lua functions
|
||||||
local tinsert = table.insert
|
local tinsert = table.insert
|
||||||
local tremove = table.remove
|
local tremove = table.remove
|
||||||
local tconcat = table.concat
|
local tconcat = table.concat
|
||||||
@@ -19,7 +20,11 @@ local tiny_system
|
|||||||
local tiny_manageEntities
|
local tiny_manageEntities
|
||||||
local tiny_manageSystems
|
local tiny_manageSystems
|
||||||
local tiny_updateSystem
|
local tiny_updateSystem
|
||||||
|
local tiny_addEntity
|
||||||
|
local tiny_addSystem
|
||||||
local tiny_add
|
local tiny_add
|
||||||
|
local tiny_removeEntity
|
||||||
|
local tiny_removeSystem
|
||||||
local tiny_remove
|
local tiny_remove
|
||||||
|
|
||||||
--- Filter functions.
|
--- Filter functions.
|
||||||
@@ -171,7 +176,27 @@ function tiny.world(...)
|
|||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Adds Entities and Systems to the World.
|
--- 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.
|
||||||
-- New objects will enter the World the next time World:update(dt) is called.
|
-- 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
|
-- Also call this method when an Entity has had its Components changed, such
|
||||||
-- that it matches different Filters.
|
-- that it matches different Filters.
|
||||||
@@ -179,35 +204,47 @@ end
|
|||||||
-- @param ... Systems and Entities
|
-- @param ... Systems and Entities
|
||||||
function tiny.add(world, ...)
|
function tiny.add(world, ...)
|
||||||
local args = {...}
|
local args = {...}
|
||||||
local entityStatus = world.entityStatus
|
|
||||||
local systemStatus = world.systemStatus
|
|
||||||
local systemIndices = world.systemIndices
|
|
||||||
local systemCount = world.systemCount
|
|
||||||
for _, obj in ipairs(args) do
|
for _, obj in ipairs(args) do
|
||||||
if getmetatable(obj) == systemMetaTable then
|
if getmetatable(obj) == systemMetaTable then
|
||||||
systemStatus[obj] = "add"
|
tiny_addSystem(world, obj)
|
||||||
systemCount = systemCount + 1
|
|
||||||
systemIndices[obj] = systemCount
|
|
||||||
else -- Assume obj is an Entity
|
else -- Assume obj is an Entity
|
||||||
entityStatus[obj] = "add"
|
tiny_addEntity(world, obj)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
tiny_add = tiny.add
|
tiny_add = tiny.add
|
||||||
|
|
||||||
--- Removes Entities and Systems from the World. Objects will exit the World the
|
--- Removes an Entity to the World.
|
||||||
-- next time World:update(dt) is called.
|
-- 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.
|
||||||
-- @param world
|
-- @param world
|
||||||
-- @param ... Systems and Entities
|
-- @param ... Systems and Entities
|
||||||
function tiny.remove(world, ...)
|
function tiny.remove(world, ...)
|
||||||
local args = {...}
|
local args = {...}
|
||||||
local entityStatus = world.entityStatus
|
|
||||||
local systemStatus = world.systemStatus
|
|
||||||
for _, obj in ipairs(args) do
|
for _, obj in ipairs(args) do
|
||||||
if getmetatable(obj) == systemMetaTable then
|
if getmetatable(obj) == systemMetaTable then
|
||||||
systemStatus[obj] = "remove"
|
tiny_removeSystem(world, obj)
|
||||||
else -- Assume obj is an Entity
|
else -- Assume obj is an Entity
|
||||||
entityStatus[obj] = "remove"
|
tiny_removeEntity(world, obj)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -254,15 +291,15 @@ function tiny.manageSystems(world)
|
|||||||
local systems = world.systems
|
local systems = world.systems
|
||||||
local systemStatus = world.systemStatus
|
local systemStatus = world.systemStatus
|
||||||
local activeSystems = world.activeSystems
|
local activeSystems = world.activeSystems
|
||||||
|
local systemCount = world.systemCount
|
||||||
-- Keep track of the number of Systems in the world
|
|
||||||
local deltaSystemCount = 0
|
|
||||||
|
|
||||||
for system, status in pairs(systemStatus) do
|
for system, status in pairs(systemStatus) do
|
||||||
|
|
||||||
if status == "add" then
|
if status == "add" then
|
||||||
local es = {}
|
local es = {}
|
||||||
systemEntities[system] = es
|
systemEntities[system] = es
|
||||||
|
systemIndices[system] = systemCount + 1
|
||||||
|
systemCount = systemCount + 1
|
||||||
systems[systemIndices[system]] = system
|
systems[systemIndices[system]] = system
|
||||||
activeSystems[system] = true
|
activeSystems[system] = true
|
||||||
local filter = system.filter
|
local filter = system.filter
|
||||||
@@ -271,9 +308,9 @@ function tiny.manageSystems(world)
|
|||||||
es[e] = filter(e) and true or nil
|
es[e] = filter(e) and true or nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
deltaSystemCount = deltaSystemCount + 1
|
|
||||||
elseif status == "remove" then
|
elseif status == "remove" then
|
||||||
local index = systemIndices[system]
|
local index = systemIndices[system]
|
||||||
|
systemCount = systemCount - 1
|
||||||
tremove(systems, index)
|
tremove(systems, index)
|
||||||
local onRemove = system.onRemove
|
local onRemove = system.onRemove
|
||||||
if onRemove then
|
if onRemove then
|
||||||
@@ -283,14 +320,13 @@ function tiny.manageSystems(world)
|
|||||||
end
|
end
|
||||||
systemEntities[system] = nil
|
systemEntities[system] = nil
|
||||||
activeSystems[system] = nil
|
activeSystems[system] = nil
|
||||||
deltaSystemCount = deltaSystemCount - 1
|
|
||||||
end
|
end
|
||||||
|
|
||||||
systemStatus[system] = nil
|
systemStatus[system] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Update the number of Systems in the World
|
-- Update the number of Systems in the World
|
||||||
world.systemCount = world.systemCount + deltaSystemCount
|
world.systemCount = systemCount
|
||||||
end
|
end
|
||||||
tiny_manageSystems = tiny.manageSystems
|
tiny_manageSystems = tiny.manageSystems
|
||||||
|
|
||||||
@@ -303,15 +339,13 @@ function tiny.manageEntities(world)
|
|||||||
local systemEntities = world.systemEntities
|
local systemEntities = world.systemEntities
|
||||||
local entities = world.entities
|
local entities = world.entities
|
||||||
local systems = world.systems
|
local systems = world.systems
|
||||||
|
local entityCount = world.entityCount
|
||||||
-- Keep track of the number of Entities in the World
|
|
||||||
local deltaEntityCount = 0
|
|
||||||
|
|
||||||
-- Add, remove, or change Entities
|
-- Add, remove, or change Entities
|
||||||
for e, s in pairs(entityStatus) do
|
for e, s in pairs(entityStatus) do
|
||||||
if s == "add" then
|
if s == "add" then
|
||||||
if not entities[e] then
|
if not entities[e] then
|
||||||
deltaEntityCount = deltaEntityCount + 1
|
entityCount = entityCount + 1
|
||||||
end
|
end
|
||||||
entities[e] = true
|
entities[e] = true
|
||||||
for sys, es in pairs(systemEntities) do
|
for sys, es in pairs(systemEntities) do
|
||||||
@@ -331,7 +365,7 @@ function tiny.manageEntities(world)
|
|||||||
end
|
end
|
||||||
elseif s == "remove" then
|
elseif s == "remove" then
|
||||||
if entities[e] then
|
if entities[e] then
|
||||||
deltaEntityCount = deltaEntityCount - 1
|
entityCount = entityCount - 1
|
||||||
end
|
end
|
||||||
entities[e] = nil
|
entities[e] = nil
|
||||||
for sys, es in pairs(systemEntities) do
|
for sys, es in pairs(systemEntities) do
|
||||||
@@ -346,7 +380,7 @@ function tiny.manageEntities(world)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Update Entity count
|
-- Update Entity count
|
||||||
world.entityCount = world.entityCount + deltaEntityCount
|
world.entityCount = entityCount
|
||||||
|
|
||||||
end
|
end
|
||||||
tiny_manageEntities = tiny.manageEntities
|
tiny_manageEntities = tiny.manageEntities
|
||||||
|
|||||||
Reference in New Issue
Block a user