mirror of
https://github.com/bakpakin/tiny-ecs.git
synced 2026-07-21 23:56:50 -06:00
Add onAddToWorld and onRemoveFromWorld callbacks to systems. Also, adding or removing systems to a world twice raises an error.
This commit is contained in:
@@ -449,7 +449,7 @@ function tiny.remove(world, ...)
|
||||
if isSystem(obj) then
|
||||
tiny_removeSystem(world, obj)
|
||||
else -- Assume obj is an Entity
|
||||
tiny_removeEntity(world, obj)
|
||||
tiny_removeEntity(world, obj)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -476,19 +476,21 @@ function tiny_manageSystems(world)
|
||||
for i = 1, #s2r do
|
||||
local system = s2r[i]
|
||||
local index = system.index
|
||||
if system.world == world then
|
||||
local onRemove = system.onRemove
|
||||
if onRemove then
|
||||
local entityList = system.entities
|
||||
for j = 1, #entityList do
|
||||
onRemove(system, entityList[j])
|
||||
end
|
||||
end
|
||||
tremove(systems, index)
|
||||
for j = index, #systems do
|
||||
systems[j].index = j
|
||||
local onRemove = system.onRemove
|
||||
if onRemove then
|
||||
local entityList = system.entities
|
||||
for j = 1, #entityList do
|
||||
onRemove(system, entityList[j])
|
||||
end
|
||||
end
|
||||
tremove(systems, index)
|
||||
for j = index, #systems do
|
||||
systems[j].index = j
|
||||
end
|
||||
local onRemoveFromWorld = system.onRemoveFromWorld
|
||||
if onRemoveFromWorld then
|
||||
onRemoveFromWorld(system, world)
|
||||
end
|
||||
s2r[i] = nil
|
||||
|
||||
-- Clean up System
|
||||
@@ -514,6 +516,10 @@ function tiny_manageSystems(world)
|
||||
local index = #systems + 1
|
||||
system.index = index
|
||||
systems[index] = system
|
||||
local onAddToWorld = system.onAddToWorld
|
||||
if onAddToWorld then
|
||||
onAddToWorld(system, world)
|
||||
end
|
||||
|
||||
-- Try to add Entities
|
||||
local onAdd = system.onAdd
|
||||
|
||||
Reference in New Issue
Block a user