1
0
Fork 0
mirror of https://github.com/Reuh/ubiquitousse.git synced 2025-10-28 01:29:31 +00:00

Add callback, children, timer example systems

This commit is contained in:
Étienne Fildadut 2021-06-24 16:24:54 +02:00
parent 6ee98c097f
commit 9f4c03a136
3 changed files with 119 additions and 0 deletions

49
ecs/children.can Normal file
View file

@ -0,0 +1,49 @@
--- Children system
-- Allow to build a hierarchy between entities.
-- Children are stored directly in the .children entity table: they are added when their parent is added, and removed when it is removed from the world.
return {
name = "children",
filter = true,
default = {
parent = nil, -- reference to parent entity, if any
-- ... list of children
},
methods = {
--- Add a new entity to the world, using this entity as a parent.
add = :(c, o)
if not o.children then
o.children = {}
end
o.children.parent = c.entity
table.insert(c, o)
@world:add(o)
end,
--- Remove an entity from the world and from this entity's children.
remove = :(c, o)
@world:remove(o)
for i=#c, 1, -1 do
if c[i] == o then
table.remove(c, i)
break
end
end
o.children.parent = nil
end
},
onAdd = :(c)
for _, o in ipairs(c) do
o.children.parent = c.entity
@world:add(o)
end
end,
onRemove = :(c)
for _, o in ipairs(c) do
@world:remove(o)
o.children.parent = nil
end
if c.parent then
c.parent.children:remove(c.entity)
end
end
}