Implement first draft of parent child relation storage
This commit is contained in:
parent
f99f171a15
commit
8958d96687
|
|
@ -106,23 +106,24 @@ public struct Entity {
|
|||
/// - Parameter entity: The child entity.
|
||||
@discardableResult
|
||||
public func addChild(_ entity: Entity) -> Bool {
|
||||
return false
|
||||
return nexus.addChild(entity, to: self)
|
||||
}
|
||||
|
||||
/// Remove entity as child.
|
||||
/// - Parameter entity: The child entity.
|
||||
@discardableResult
|
||||
public func removeChild(_ entity: Entity) -> Bool {
|
||||
return false
|
||||
return nexus.removeChild(entity, from: self)
|
||||
}
|
||||
|
||||
/// Removes all children from this entity.
|
||||
public func removeAllChildren() {
|
||||
return nexus.removeAllChildren(from: self)
|
||||
}
|
||||
|
||||
/// Returns the number of children for this entity.
|
||||
public var numChildren: Int {
|
||||
return 0
|
||||
return nexus.numChildren(for: self)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -56,6 +56,8 @@ extension Nexus {
|
|||
return false
|
||||
}
|
||||
|
||||
removeAllChildren(from: entity)
|
||||
|
||||
if removeAll(componentes: entityId) {
|
||||
update(familyMembership: entityId)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
//
|
||||
// Nexus+SceneGraph.swift
|
||||
//
|
||||
//
|
||||
// Created by Christian Treffs on 30.09.19.
|
||||
//
|
||||
|
||||
extension Nexus {
|
||||
public final func addChild(_ child: Entity, to parent: Entity) -> Bool {
|
||||
let inserted: Bool
|
||||
if parentChildrenMap[parent.identifier] == nil {
|
||||
parentChildrenMap[parent.identifier] = [child.identifier]
|
||||
inserted = true
|
||||
} else {
|
||||
let (isNewMember, _) = parentChildrenMap[parent.identifier]!.insert(child.identifier)
|
||||
inserted = isNewMember
|
||||
}
|
||||
return inserted
|
||||
}
|
||||
|
||||
public final func removeChild(_ child: Entity, from parent: Entity) -> Bool {
|
||||
return parentChildrenMap[parent.identifier]?.remove(child.identifier) != nil
|
||||
}
|
||||
|
||||
public final func removeAllChildren(from parent: Entity) {
|
||||
return parentChildrenMap[parent.identifier] = nil
|
||||
}
|
||||
|
||||
public final func numChildren(for entity: Entity) -> Int {
|
||||
return parentChildrenMap[entity.identifier]?.count ?? 0
|
||||
}
|
||||
}
|
||||
|
|
@ -29,12 +29,15 @@ public final class Nexus {
|
|||
/// - Value: Tightly packed EntityIdentifiers that represent the association of an entity to the family.
|
||||
@usableFromInline final var familyMembersByTraits: [FamilyTraitSet: UnorderedSparseSet<EntityIdentifier>]
|
||||
|
||||
@usableFromInline final var parentChildrenMap: [EntityIdentifier: Set<EntityIdentifier>]
|
||||
|
||||
public init() {
|
||||
entityStorage = UnorderedSparseSet<Entity>()
|
||||
componentsByType = [:]
|
||||
componentIdsByEntity = [:]
|
||||
freeEntities = ContiguousArray<EntityIdentifier>()
|
||||
familyMembersByTraits = [:]
|
||||
parentChildrenMap = [:]
|
||||
}
|
||||
|
||||
public final func clear() {
|
||||
|
|
@ -55,6 +58,7 @@ public final class Nexus {
|
|||
componentsByType.removeAll()
|
||||
componentIdsByEntity.removeAll()
|
||||
familyMembersByTraits.removeAll()
|
||||
parentChildrenMap.removeAll()
|
||||
}
|
||||
|
||||
deinit {
|
||||
|
|
|
|||
Loading…
Reference in New Issue