diff --git a/Sources/FirebladeECS/Nexus+Component.swift b/Sources/FirebladeECS/Nexus+Component.swift index 7d26565..98645a5 100644 --- a/Sources/FirebladeECS/Nexus+Component.swift +++ b/Sources/FirebladeECS/Nexus+Component.swift @@ -5,24 +5,24 @@ // Created by Christian Treffs on 13.10.17. // -extension Nexus { +public extension Nexus { - var numComponents: Int { + final var numComponents: Int { return componentsByType.reduce(0) { return $0 + $1.value.count } } - public func has(componentId: ComponentIdentifier, entityIdx: EntityIndex) -> Bool { + final func has(componentId: ComponentIdentifier, entityIdx: EntityIndex) -> Bool { guard let uniforms: UniformComponents = componentsByType[componentId] else { return false } return uniforms.has(entityIdx) } - public func count(components entityId: EntityIdentifier) -> Int { + final func count(components entityId: EntityIdentifier) -> Int { return componentIdsByEntity[entityId.index]?.count ?? 0 } - public func assign(component: Component, to entity: Entity) { + final func assign(component: Component, to entity: Entity) { let componentId: ComponentIdentifier = component.identifier let entityIdx: EntityIndex = entity.identifier.index let entityId: EntityIdentifier = entity.identifier @@ -54,35 +54,28 @@ extension Nexus { notify(ComponentAdded(component: componentId, toEntity: entity.identifier)) } - public func assign(component: C, to entity: Entity) where C: Component { + final func assign(component: C, to entity: Entity) where C: Component { assign(component: component, to: entity) } - public func get(component componentId: ComponentIdentifier, for entityId: EntityIdentifier) -> Component? { + final func get(component componentId: ComponentIdentifier, for entityId: EntityIdentifier) -> Component? { guard let uniformComponents: UniformComponents = componentsByType[componentId] else { return nil } return uniformComponents.get(at: entityId.index) } - public func get(for entityId: EntityIdentifier) -> C? where C: Component { + final func get(for entityId: EntityIdentifier) -> C? where C: Component { let componentId: ComponentIdentifier = C.identifier return get(componentId: componentId, entityIdx: entityId.index) } - private func get(componentId: ComponentIdentifier, entityIdx: EntityIndex) -> C? where C: Component { - guard let uniformComponents: UniformComponents = componentsByType[componentId] else { - return nil - } - return uniformComponents.get(at: entityIdx) as? C - } - - public func get(components entityId: EntityIdentifier) -> SparseComponentIdentifierSet? { + final func get(components entityId: EntityIdentifier) -> SparseComponentIdentifierSet? { return componentIdsByEntity[entityId.index] } @discardableResult - public func remove(component componentId: ComponentIdentifier, from entityId: EntityIdentifier) -> Bool { + final func remove(component componentId: ComponentIdentifier, from entityId: EntityIdentifier) -> Bool { let entityIdx: EntityIndex = entityId.index @@ -98,7 +91,7 @@ extension Nexus { } @discardableResult - public func clear(componentes entityId: EntityIdentifier) -> Bool { + final func clear(componentes entityId: EntityIdentifier) -> Bool { guard let allComponents: SparseComponentIdentifierSet = get(components: entityId) else { report("clearing components form entity \(entityId) with no components") @@ -108,3 +101,12 @@ extension Nexus { return removedAll } } + +private extension Nexus { + func get(componentId: ComponentIdentifier, entityIdx: EntityIndex) -> C? where C: Component { + guard let uniformComponents: UniformComponents = componentsByType[componentId] else { + return nil + } + return uniformComponents.get(at: entityIdx) as? C + } +} diff --git a/Sources/FirebladeECS/Nexus+Family.swift b/Sources/FirebladeECS/Nexus+Family.swift index c5a1f25..17a706d 100644 --- a/Sources/FirebladeECS/Nexus+Family.swift +++ b/Sources/FirebladeECS/Nexus+Family.swift @@ -7,6 +7,10 @@ public extension Nexus { + final var numFamilies: Int { + return familyMembersByTraits.keys.count + } + /// Gets or creates (new) family with given traits. /// /// - Parameters: