1
0
Fork 0
mirror of https://github.com/Reuh/ubiquitousse.git synced 2025-10-27 17:19:31 +00:00
ubiquitousse/ecs/children.can

40 lines
1.2 KiB
Text

--- Children system.
-- Allows to build a hierarchy between entities.
--
-- An entity's parent entity is stored in its `parent` component.
-- An entity's children are stored in its `children` component (list of children entities).
--
-- You can set theses values before adding the entity to the world; when you add the entity it will add itself
-- to its parent children list and add all its children to the world.
--
-- If you remove an entity from the world, it will also remove all its children from the world.
return {
name = "children",
filter = true,
onAdd = :(e)
if not e.children then e.children = {} end
if e.parent then -- add to parent
let parentchildren = e.parent.children
table.insert(parentchildren, e)
end
for _, o in ipairs(e.children) do -- add predefined children
o.parent = e
@world:add(o)
end
end,
onRemove = :(e)
for i=#e.children, 1, -1 do -- remove children
@world:remove(e.children[i])
end
if e.parent then -- remove from parent
let parentchildren = e.parent.children
for i=#parentchildren, 1, -1 do
if parentchildren[i] == e then
table.remove(parentchildren, i)
break
end
end
end
end
}