------------------------------- EcsController::ReparentEntity Change what parent an entity is linked to, also moving all of its children - if the parent is not set to INVALID_ENTITY_ID, the entity is linked to the parent and placed in the layer below
| 181 | // - if the parent is not set to INVALID_ENTITY_ID, the entity is linked to the parent and placed in the layer below |
| 182 | // |
| 183 | void EcsController::ReparentEntity(T_EntityId const entity, T_EntityId const newParent) |
| 184 | { |
| 185 | // get referred entity |
| 186 | EntityData& ent = m_Entities[entity]; |
| 187 | |
| 188 | // remove from current parent |
| 189 | RemoveEntityFromParent(entity, ent.parent); |
| 190 | |
| 191 | std::vector<RawComponentPtr> components; |
| 192 | T_CompTypeList compTypes = GetComponentsAndTypes(ent, components); |
| 193 | |
| 194 | // add to new parent, and get new layer |
| 195 | ent.parent = newParent; |
| 196 | if (ent.parent != INVALID_ENTITY_ID) |
| 197 | { |
| 198 | EntityData& parentEnt = m_Entities[ent.parent]; |
| 199 | parentEnt.children.push_back(entity); |
| 200 | |
| 201 | ent.layer = parentEnt.layer + 1u; |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | ent.layer = 0u; |
| 206 | } |
| 207 | |
| 208 | // #todo: handle cases in which the hierachy level didn't change |
| 209 | // the archetype will have the same signature but reside on a new hierachy layer |
| 210 | MoveArchetype(entity, ent, compTypes, components); |
| 211 | |
| 212 | // recursively reparent children to match hierachy layers |
| 213 | for (T_EntityId const childId : ent.children) |
| 214 | { |
| 215 | ReparentEntity(childId, entity); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | //----------------------------- |
| 220 | // EcsController::RemoveEntity |
no outgoing calls