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 {
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 {
public struct ComponentsIterator: IteratorProtocol {
private var iterator: AnyIterator<Component>
private var iterator: IndexingIterator<([Component])>?
@usableFromInline
init(nexus: Nexus, entityIdentifier: EntityIdentifier) {
if let comps = nexus.get(components: entityIdentifier) {
iterator = AnyIterator<Component>(comps.compactMap { nexus.get(unsafe: $0, for: entityIdentifier) }.makeIterator())
} else {
iterator = AnyIterator { nil }
}
iterator = nexus.get(components: entityIdentifier)?
.map { nexus.get(unsafe: $0, for: entityIdentifier) }
.makeIterator()
}
public mutating func next() -> Component? {
iterator.next()
iterator?.next()
}
}
}