mirror of
https://github.com/Reuh/ubiquitousse.git
synced 2025-10-27 17:19:31 +00:00
Simplify ecs.children
This commit is contained in:
parent
74571c9be4
commit
7e0c41bb04
1 changed files with 25 additions and 25 deletions
|
|
@ -1,40 +1,40 @@
|
||||||
--- Children system
|
--- Children system.
|
||||||
-- Allow to build a hierarchy between entities.
|
-- Allows 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.
|
--
|
||||||
|
-- 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 {
|
return {
|
||||||
name = "children",
|
name = "children",
|
||||||
filter = true,
|
filter = true,
|
||||||
default = {
|
onAdd = :(e)
|
||||||
parent = nil, -- reference to parent entity, if any
|
if not e.children then e.children = {} end
|
||||||
-- ... list of children to add when the entity is added to the world
|
if e.parent then -- add to parent
|
||||||
children = {}, -- [children]=true,children... map+list of children currently in the entity children (don't set this yourself)
|
let parentchildren = e.parent.children
|
||||||
},
|
table.insert(parentchildren, e)
|
||||||
onAdd = :(c, e)
|
|
||||||
if c.parent then -- add to parent
|
|
||||||
let parentcc = c.parent.children.children
|
|
||||||
table.insert(parentcc, e)
|
|
||||||
parentcc[e] = true
|
|
||||||
end
|
end
|
||||||
for _, o in ipairs(c) do -- add children
|
for _, o in ipairs(e.children) do -- add predefined children
|
||||||
if not o.children then o.children = {} end
|
o.parent = e
|
||||||
o.children.parent = e
|
|
||||||
@world:add(o)
|
@world:add(o)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
onRemove = :(c, e)
|
onRemove = :(e)
|
||||||
for i=#c.children, 1, -1 do -- remove children
|
for i=#e.children, 1, -1 do -- remove children
|
||||||
@world:remove(c.children[i])
|
@world:remove(e.children[i])
|
||||||
end
|
end
|
||||||
if c.parent then -- remove from parent
|
if e.parent then -- remove from parent
|
||||||
let parentcc = c.parent.children.children
|
let parentchildren = e.parent.children
|
||||||
for i=#parentcc, 1, -1 do
|
for i=#parentchildren, 1, -1 do
|
||||||
if parentcc[i] == e then
|
if parentchildren[i] == e then
|
||||||
table.remove(parentcc, i)
|
table.remove(parentchildren, i)
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
parentcc[e] = nil
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue