Optimize and cleanup Nexus
This commit is contained in:
parent
589a8c2ec1
commit
61d407e5db
|
|
@ -161,7 +161,7 @@ extension Family {
|
||||||
}
|
}
|
||||||
|
|
||||||
mutating func aggregateRelativesBreathFirst(_ parent: EntityIdentifier) {
|
mutating func aggregateRelativesBreathFirst(_ parent: EntityIdentifier) {
|
||||||
guard let children = nexus.parentChildrenMap[parent] else {
|
guard let children = nexus.childrenByParentEntity[parent] else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
children
|
children
|
||||||
|
|
|
||||||
|
|
@ -61,11 +61,13 @@ extension FamilyTraitSet: Hashable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension FamilyTraitSet: CustomStringConvertible, CustomDebugStringConvertible {
|
extension FamilyTraitSet: CustomStringConvertible {
|
||||||
@inlinable public var description: String {
|
@inlinable public var description: String {
|
||||||
return "<FamilyTraitSet [requiresAll:\(requiresAll.description) excludesAll:\(excludesAll.description)]>"
|
return "<FamilyTraitSet [requiresAll:\(requiresAll.description) excludesAll:\(excludesAll.description)]>"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension FamilyTraitSet: CustomDebugStringConvertible {
|
||||||
@inlinable public var debugDescription: String {
|
@inlinable public var debugDescription: String {
|
||||||
return "<FamilyTraitSet [requiresAll:\(requiresAll.debugDescription) excludesAll: \(excludesAll.debugDescription)]>"
|
return "<FamilyTraitSet [requiresAll:\(requiresAll.debugDescription) excludesAll: \(excludesAll.debugDescription)]>"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,7 @@ extension Nexus {
|
||||||
|
|
||||||
// add component instances to uniform component stores
|
// add component instances to uniform component stores
|
||||||
if componentsByType[componentId] == nil {
|
if componentsByType[componentId] == nil {
|
||||||
componentsByType[componentId] = ManagedContiguousArray<Component>()
|
componentsByType[componentId] = UnorderedSparseSet<Component>()
|
||||||
}
|
}
|
||||||
componentsByType[componentId]?.insert(component, at: entityId.id)
|
componentsByType[componentId]?.insert(component, at: entityId.id)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -64,11 +64,11 @@ extension Nexus {
|
||||||
|
|
||||||
final func add(entityWithId entityId: EntityIdentifier, toFamilyWithTraits traits: FamilyTraitSet) {
|
final func add(entityWithId entityId: EntityIdentifier, toFamilyWithTraits traits: FamilyTraitSet) {
|
||||||
precondition(familyMembersByTraits[traits] != nil)
|
precondition(familyMembersByTraits[traits] != nil)
|
||||||
familyMembersByTraits[traits].unsafelyUnwrapped.insert(entityId, at: entityId.id)
|
familyMembersByTraits[traits]!.insert(entityId, at: entityId.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
final func remove(entityWithId entityId: EntityIdentifier, fromFamilyWithTraits traits: FamilyTraitSet) {
|
final func remove(entityWithId entityId: EntityIdentifier, fromFamilyWithTraits traits: FamilyTraitSet) {
|
||||||
precondition(familyMembersByTraits[traits] != nil)
|
precondition(familyMembersByTraits[traits] != nil)
|
||||||
familyMembersByTraits[traits].unsafelyUnwrapped.remove(at: entityId.id)
|
familyMembersByTraits[traits]!.remove(at: entityId.id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,11 +8,11 @@
|
||||||
extension Nexus {
|
extension Nexus {
|
||||||
public final func addChild(_ child: Entity, to parent: Entity) -> Bool {
|
public final func addChild(_ child: Entity, to parent: Entity) -> Bool {
|
||||||
let inserted: Bool
|
let inserted: Bool
|
||||||
if parentChildrenMap[parent.identifier] == nil {
|
if childrenByParentEntity[parent.identifier] == nil {
|
||||||
parentChildrenMap[parent.identifier] = [child.identifier]
|
childrenByParentEntity[parent.identifier] = [child.identifier]
|
||||||
inserted = true
|
inserted = true
|
||||||
} else {
|
} else {
|
||||||
let (isNewMember, _) = parentChildrenMap[parent.identifier]!.insert(child.identifier)
|
let (isNewMember, _) = childrenByParentEntity[parent.identifier]!.insert(child.identifier)
|
||||||
inserted = isNewMember
|
inserted = isNewMember
|
||||||
}
|
}
|
||||||
if inserted {
|
if inserted {
|
||||||
|
|
@ -27,7 +27,7 @@ extension Nexus {
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
public final func removeChild(_ child: EntityIdentifier, from parent: EntityIdentifier) -> Bool {
|
public final func removeChild(_ child: EntityIdentifier, from parent: EntityIdentifier) -> Bool {
|
||||||
let removed: Bool = parentChildrenMap[parent]?.remove(child) != nil
|
let removed: Bool = childrenByParentEntity[parent]?.remove(child) != nil
|
||||||
if removed {
|
if removed {
|
||||||
delegate?.nexusEvent(ChildRemoved(parent: parent, child: child))
|
delegate?.nexusEvent(ChildRemoved(parent: parent, child: child))
|
||||||
}
|
}
|
||||||
|
|
@ -35,11 +35,11 @@ extension Nexus {
|
||||||
}
|
}
|
||||||
|
|
||||||
public final func removeAllChildren(from parent: Entity) {
|
public final func removeAllChildren(from parent: Entity) {
|
||||||
parentChildrenMap[parent.identifier]?.forEach { removeChild($0, from: parent.identifier) }
|
childrenByParentEntity[parent.identifier]?.forEach { removeChild($0, from: parent.identifier) }
|
||||||
return parentChildrenMap[parent.identifier] = nil
|
return childrenByParentEntity[parent.identifier] = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
public final func numChildren(for entity: Entity) -> Int {
|
public final func numChildren(for entity: Entity) -> Int {
|
||||||
return parentChildrenMap[entity.identifier]?.count ?? 0
|
return childrenByParentEntity[entity.identifier]?.count ?? 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,40 +6,44 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
public final class Nexus {
|
public final class Nexus {
|
||||||
public final weak var delegate: NexusEventDelegate?
|
|
||||||
|
|
||||||
/// Main entity storage.
|
/// Main entity storage.
|
||||||
/// Entities are tightly packed by EntityIdentifier.
|
/// Entities are tightly packed by EntityIdentifier.
|
||||||
@usableFromInline final var entityStorage: UnorderedSparseSet<Entity>
|
@usableFromInline final var entityStorage: UnorderedSparseSet<Entity>
|
||||||
|
|
||||||
|
/// Entity ids that are currently not used.
|
||||||
|
@usableFromInline final var freeEntities: [EntityIdentifier]
|
||||||
|
|
||||||
/// - Key: ComponentIdentifier aka component type.
|
/// - Key: ComponentIdentifier aka component type.
|
||||||
/// - Value: Array of component instances of same type (uniform).
|
/// - Value: Array of component instances of same type (uniform).
|
||||||
/// New component instances are appended.
|
/// New component instances are appended.
|
||||||
@usableFromInline final var componentsByType: [ComponentIdentifier: ManagedContiguousArray<Component>]
|
@usableFromInline final var componentsByType: [ComponentIdentifier: UnorderedSparseSet<Component>]
|
||||||
|
|
||||||
/// - Key: EntityIdentifier aka entity index
|
/// - Key: EntityIdentifier aka entity index
|
||||||
/// - Value: Set of unique component types (ComponentIdentifier).
|
/// - Value: Set of unique component types (ComponentIdentifier).
|
||||||
/// Each element is a component identifier associated with this entity.
|
/// Each element is a component identifier associated with this entity.
|
||||||
@usableFromInline final var componentIdsByEntity: [EntityIdentifier: Set<ComponentIdentifier>]
|
@usableFromInline final var componentIdsByEntity: [EntityIdentifier: Set<ComponentIdentifier>]
|
||||||
|
|
||||||
/// Entity ids that are currently not used.
|
/// - Key: A parent entity id.
|
||||||
@usableFromInline final var freeEntities: ContiguousArray<EntityIdentifier>
|
/// - Value: Adjacency Set of all associated children.
|
||||||
|
@usableFromInline final var childrenByParentEntity: [EntityIdentifier: Set<EntityIdentifier>]
|
||||||
|
|
||||||
/// - Key: FamilyTraitSet aka component types that make up one distinct family.
|
/// - Key: FamilyTraitSet aka component types that make up one distinct family.
|
||||||
/// - Value: Tightly packed EntityIdentifiers that represent the association of an entity to the family.
|
/// - Value: Tightly packed EntityIdentifiers that represent the association of an entity to the family.
|
||||||
@usableFromInline final var familyMembersByTraits: [FamilyTraitSet: UnorderedSparseSet<EntityIdentifier>]
|
@usableFromInline final var familyMembersByTraits: [FamilyTraitSet: UnorderedSparseSet<EntityIdentifier>]
|
||||||
|
|
||||||
/// - Key: A parent entity id.
|
public final weak var delegate: NexusEventDelegate?
|
||||||
/// - Value: Adjacency Set of all associated children.
|
|
||||||
@usableFromInline final var parentChildrenMap: [EntityIdentifier: Set<EntityIdentifier>]
|
|
||||||
|
|
||||||
public init() {
|
public init() {
|
||||||
entityStorage = UnorderedSparseSet<Entity>()
|
entityStorage = UnorderedSparseSet<Entity>()
|
||||||
componentsByType = [:]
|
componentsByType = [:]
|
||||||
componentIdsByEntity = [:]
|
componentIdsByEntity = [:]
|
||||||
freeEntities = ContiguousArray<EntityIdentifier>()
|
freeEntities = []
|
||||||
familyMembersByTraits = [:]
|
familyMembersByTraits = [:]
|
||||||
parentChildrenMap = [:]
|
childrenByParentEntity = [:]
|
||||||
|
}
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
public final func clear() {
|
public final func clear() {
|
||||||
|
|
@ -60,11 +64,7 @@ public final class Nexus {
|
||||||
componentsByType.removeAll()
|
componentsByType.removeAll()
|
||||||
componentIdsByEntity.removeAll()
|
componentIdsByEntity.removeAll()
|
||||||
familyMembersByTraits.removeAll()
|
familyMembersByTraits.removeAll()
|
||||||
parentChildrenMap.removeAll()
|
childrenByParentEntity.removeAll()
|
||||||
}
|
|
||||||
|
|
||||||
deinit {
|
|
||||||
clear()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -77,7 +77,7 @@ extension Nexus: Equatable {
|
||||||
lhs.freeEntities == rhs.freeEntities &&
|
lhs.freeEntities == rhs.freeEntities &&
|
||||||
lhs.familyMembersByTraits == rhs.familyMembersByTraits &&
|
lhs.familyMembersByTraits == rhs.familyMembersByTraits &&
|
||||||
lhs.componentsByType.keys == rhs.componentsByType.keys &&
|
lhs.componentsByType.keys == rhs.componentsByType.keys &&
|
||||||
lhs.parentChildrenMap == rhs.parentChildrenMap
|
lhs.childrenByParentEntity == rhs.childrenByParentEntity
|
||||||
// NOTE: components are not equatable (yet)
|
// NOTE: components are not equatable (yet)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue