mirror of
https://github.com/Reuh/ubiquitousse.git
synced 2025-10-27 09:09:30 +00:00
The component methods system was awkward and didn't give much benefit compared to just using methods on Systems. Plus now we really only have data in entities. Since we don't have component methods, the callback system had to be replaced; I integrated it with the default System methods since it's a relatively common behavior.
40 lines
1.1 KiB
Text
40 lines
1.1 KiB
Text
--- 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
|
|
}
|