Remove allComponents() and refine ComponentsIterator
This commit is contained in:
parent
a8c0d94e44
commit
c4d48202be
|
|
@ -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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue