Add all components sequence to entity

This commit is contained in:
Christian Treffs 2020-05-26 15:03:31 +02:00
parent 65a69826b5
commit da7277b5e1
No known key found for this signature in database
GPG Key ID: 49A4B4B460BE3ED4
1 changed files with 31 additions and 0 deletions

View File

@ -125,6 +125,37 @@ public struct Entity {
public var numChildren: Int {
nexus.numChildren(for: self)
}
/// Returns an iterator over all components of this entity.
@inlinable
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>
@usableFromInline
init(nexus: Nexus, entityIdentifier: EntityIdentifier) {
if let comps = nexus.get(components: entityIdentifier) {
iterator = AnyIterator<Component>(comps.compactMap { nexus.get(component: $0, for: entityIdentifier) }.makeIterator())
} else {
iterator = AnyIterator { nil }
}
}
public mutating func next() -> Component? {
iterator.next()
}
}
}
extension Entity: Equatable {