mirror of
https://github.com/bakpakin/tiny-ecs.git
synced 2026-07-21 23:56:50 -06:00
Add more callbacks to Systems for adding and removing entities.
This commit is contained in:
@@ -170,18 +170,22 @@ end
|
|||||||
|
|
||||||
System.nextID = 0
|
System.nextID = 0
|
||||||
|
|
||||||
-- System(preupdate, update, [aspect])
|
-- System(preupdate, update, [aspect, addCallback, removeCallback])
|
||||||
|
|
||||||
-- Creates a new System with the given aspect and update callback. The update
|
-- Creates a new System with the given aspect and update callback. The update
|
||||||
-- callback should be a function of one parameter, an entity. If no aspect is
|
-- callback should be a function of one parameter, an entity. If no aspect is
|
||||||
-- provided the empty Aspect, which matches no Entity, is used. Preupdate is a
|
-- provided the empty Aspect, which matches no Entity, is used. Preupdate is a
|
||||||
-- function of no arguments that is called once per system update before the
|
-- function of no arguments that is called once per system update before the
|
||||||
-- entities are updated.
|
-- entities are updated. The add and remove callbacks are optional functions
|
||||||
function System:init(preupdate, update, aspect)
|
-- that are called when entities are added or removed from the system. They
|
||||||
|
-- should each take one argument - an Entity.
|
||||||
|
function System:init(preupdate, update, aspect, add, remove)
|
||||||
self.preupdate = preupdate
|
self.preupdate = preupdate
|
||||||
self.update = update
|
self.update = update
|
||||||
self.aspect = aspect or Aspect()
|
self.aspect = aspect or Aspect()
|
||||||
self.active = true
|
self.active = true
|
||||||
|
self.add = add
|
||||||
|
self.remove = remove
|
||||||
local id = System.nextID
|
local id = System.nextID
|
||||||
self.id = id
|
self.id = id
|
||||||
System.nextID = id + 1
|
System.nextID = id + 1
|
||||||
@@ -342,9 +346,18 @@ function World:update(dt)
|
|||||||
local sys = systemsToRemove[i]
|
local sys = systemsToRemove[i]
|
||||||
systemsToRemove[i] = nil
|
systemsToRemove[i] = nil
|
||||||
|
|
||||||
|
|
||||||
local id = sys.id
|
local id = sys.id
|
||||||
local sysIndex = systemIndices[id]
|
local sysIndex = systemIndices[id]
|
||||||
tremove(systems, sysIndex)
|
tremove(systems, sysIndex)
|
||||||
|
|
||||||
|
local removec = sys.remove
|
||||||
|
if removec then -- call 'remove' on all entities in the System
|
||||||
|
for eid in pairs(systemEntities[id]) do
|
||||||
|
removec(entities[eid])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
systemEntities[id] = nil
|
systemEntities[id] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -376,13 +389,23 @@ function World:update(dt)
|
|||||||
if s == "add" then
|
if s == "add" then
|
||||||
deltaEntityCount = deltaEntityCount + 1
|
deltaEntityCount = deltaEntityCount + 1
|
||||||
for sysID, eids in pairs(systemEntities) do
|
for sysID, eids in pairs(systemEntities) do
|
||||||
local a = systems[systemIndices[sysID]].aspect
|
local sys = systems[systemIndices[sysID]]
|
||||||
eids[eid] = a:matches(e) and true or nil
|
local matches = sys.aspect:matches(e) and true or nil
|
||||||
|
local addc = sys.add
|
||||||
|
if addc and matches and not eids[eid] then
|
||||||
|
addc(e)
|
||||||
|
end
|
||||||
|
eids[eid] = matches
|
||||||
end
|
end
|
||||||
elseif s == "remove" then
|
elseif s == "remove" then
|
||||||
deltaEntityCount = deltaEntityCount - 1
|
deltaEntityCount = deltaEntityCount - 1
|
||||||
entities[eid] = nil
|
entities[eid] = nil
|
||||||
for _, eids in pairs(systemEntities) do
|
for sysID, eids in pairs(systemEntities) do
|
||||||
|
local sys = systems[systemIndices[sysID]]
|
||||||
|
local removec = sys.remove
|
||||||
|
if removec then
|
||||||
|
removec(e)
|
||||||
|
end
|
||||||
eids[eid] = nil
|
eids[eid] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -413,4 +436,28 @@ function World:update(dt)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- World:clearEntities()
|
||||||
|
|
||||||
|
-- Removes all Entities from the World. When World:update(dt) is next called,
|
||||||
|
-- all Entities will be removed.
|
||||||
|
function World:clearEntities()
|
||||||
|
local status = self.status
|
||||||
|
for eid in pairs(self.entity) do
|
||||||
|
status[eid] = "remove"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- World:clearSystems()
|
||||||
|
|
||||||
|
-- Removes all Systems from the World. When World:update(dt) is next called,
|
||||||
|
-- all Systems will be removed.
|
||||||
|
function World:clearSystems()
|
||||||
|
local newSystemsToRemove = {}
|
||||||
|
local systems = self.systems
|
||||||
|
for i = 1, #systems do
|
||||||
|
newSystemsToRemove[i] = systems[i].id
|
||||||
|
end
|
||||||
|
self.systemsToRemove = newSystemsToRemove
|
||||||
|
end
|
||||||
|
|
||||||
return jojo
|
return jojo
|
||||||
|
|||||||
Reference in New Issue
Block a user