Remove allComponents() and refine ComponentsIterator

This commit is contained in:
Christian Treffs 2020-10-20 14:38:47 +02:00
parent a8c0d94e44
commit c4d48202be
No known key found for this signature in database
GPG Key ID: 49A4B4B460BE3ED4
1 changed files with 5 additions and 13 deletions

View File

@ -107,29 +107,21 @@ public struct Entity {
public func makeComponentsIterator() -> ComponentsIterator { public func makeComponentsIterator() -> ComponentsIterator {
ComponentsIterator(nexus: nexus, entityIdentifier: identifier) ComponentsIterator(nexus: nexus, entityIdentifier: identifier)
} }
/// Returns a sequence of all componenents of this entity.
@inlinable
public func allComponents() -> AnySequence<Component> {
AnySequence { self.makeComponentsIterator() }
}
} }
extension Entity { extension Entity {
public struct ComponentsIterator: IteratorProtocol { public struct ComponentsIterator: IteratorProtocol {
private var iterator: AnyIterator<Component> private var iterator: IndexingIterator<([Component])>?
@usableFromInline @usableFromInline
init(nexus: Nexus, entityIdentifier: EntityIdentifier) { init(nexus: Nexus, entityIdentifier: EntityIdentifier) {
if let comps = nexus.get(components: entityIdentifier) { iterator = nexus.get(components: entityIdentifier)?
iterator = AnyIterator<Component>(comps.compactMap { nexus.get(unsafe: $0, for: entityIdentifier) }.makeIterator()) .map { nexus.get(unsafe: $0, for: entityIdentifier) }
} else { .makeIterator()
iterator = AnyIterator { nil }
}
} }
public mutating func next() -> Component? { public mutating func next() -> Component? {
iterator.next() iterator?.next()
} }
} }
} }