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
Étienne Reuh Fildadut 9d2e886609 ecs: removed .entity in components, components do not need to be tables, pass entity as a new argument in several callbacks, remove System.methods, add System:callback, System:emit and System:reorder, add System.w, improve documentation
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.
2021-12-26 18:43:40 +01:00

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
}