--- 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 to add when the entity is added to the world children = {}, -- [children]=true,children... map+list of children currently in the entity children (don't set this yourself) }, onAdd = :(c, e) if c.parent then -- add to parent let parentcc = c.parent.children.children table.insert(parentcc, e) parentcc[e] = true end for _, o in ipairs(c) do -- add children if not o.children then o.children = {} end o.children.parent = e @world:add(o) end end, onRemove = :(c, e) for i=#c.children, 1, -1 do -- remove children @world:remove(c.children[i]) end if c.parent then -- remove from parent let parentcc = c.parent.children.children for i=#parentcc, 1, -1 do if parentcc[i] == e then table.remove(parentcc, i) break end end parentcc[e] = nil end end }