Add entity and component iterables to TypedFamily
This commit is contained in:
parent
73948fdb57
commit
ee2810bb61
|
|
@ -6,11 +6,15 @@
|
|||
//
|
||||
|
||||
public protocol TypedFamilyProtocol: AnyObject, Equatable, LazySequenceProtocol {
|
||||
associatedtype EntityComponentsSequence: EntityComponentsSequenceProtocol
|
||||
|
||||
var traits: FamilyTraitSet { get }
|
||||
var nexus: Nexus? { get }
|
||||
|
||||
var count: Int { get }
|
||||
|
||||
var entities: FamilyEntities { get }
|
||||
var entityAndComponents: EntityComponentsSequence { get }
|
||||
}
|
||||
|
||||
public extension TypedFamilyProtocol {
|
||||
|
|
@ -22,9 +26,14 @@ public extension TypedFamilyProtocol {
|
|||
return memberIds.count
|
||||
}
|
||||
|
||||
var entities: FamilyEntities {
|
||||
return FamilyEntities(nexus, memberIds)
|
||||
}
|
||||
|
||||
static func == <Other>(lhs: Self, rhs: Other) -> Bool where Other: TypedFamilyProtocol {
|
||||
return lhs.traits == rhs.traits && lhs.nexus == rhs.nexus
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public protocol ComponentIteratorProtocol: IteratorProtocol {
|
||||
|
|
@ -35,3 +44,31 @@ public protocol ComponentIteratorProtocol: IteratorProtocol {
|
|||
|
||||
init(_ nexus: Nexus?, _ family: TypedFamily)
|
||||
}
|
||||
|
||||
public protocol EntityComponentsSequenceProtocol: LazySequenceProtocol, IteratorProtocol {
|
||||
associatedtype TypedFamily: TypedFamilyProtocol
|
||||
|
||||
var memberIdsIterator: UnorderedSparseSetIterator<EntityIdentifier> { get }
|
||||
var nexus: Nexus? { get }
|
||||
|
||||
init(_ nexus: Nexus?, _ family: TypedFamily)
|
||||
}
|
||||
|
||||
public struct FamilyEntities: LazySequenceProtocol, IteratorProtocol {
|
||||
public private(set) weak var nexus: Nexus?
|
||||
public var memberIdsIterator: UnorderedSparseSetIterator<EntityIdentifier>
|
||||
|
||||
public init(_ nexus: Nexus?, _ memberIds: UniformEntityIdentifiers) {
|
||||
self.nexus = nexus
|
||||
memberIdsIterator = memberIds.makeIterator()
|
||||
}
|
||||
|
||||
public mutating func next() -> Entity? {
|
||||
guard let entityId = memberIdsIterator.next() else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return nexus?.get(entity: entityId)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,9 @@ public final class TypedFamily1<A>: TypedFamilyProtocol where A: Component {
|
|||
return ComponentIterator1(nexus, self)
|
||||
}
|
||||
|
||||
public var entityAndComponents: FamilyEntitiesAndComponents1<A> {
|
||||
return FamilyEntitiesAndComponents1(nexus, self)
|
||||
}
|
||||
}
|
||||
|
||||
public struct ComponentIterator1<A>: ComponentIteratorProtocol where A: Component {
|
||||
|
|
@ -43,3 +46,28 @@ public struct ComponentIterator1<A>: ComponentIteratorProtocol where A: Componen
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
public struct FamilyEntitiesAndComponents1<A>: EntityComponentsSequenceProtocol where A: Component {
|
||||
public private(set) weak var nexus: Nexus?
|
||||
public var memberIdsIterator: UnorderedSparseSetIterator<EntityIdentifier>
|
||||
|
||||
public init(_ nexus: Nexus?, _ family: TypedFamily1<A>) {
|
||||
self.nexus = nexus
|
||||
memberIdsIterator = family.memberIds.makeIterator()
|
||||
}
|
||||
|
||||
public mutating func next() -> (Entity, A)? {
|
||||
guard let entityId = memberIdsIterator.next() else {
|
||||
return nil
|
||||
}
|
||||
|
||||
guard
|
||||
let entity = nexus?.get(entity: entityId),
|
||||
let compA: A = nexus?.get(for: entityId)
|
||||
else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return (entity, compA)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@
|
|||
// Created by Christian Treffs on 29.09.18.
|
||||
//
|
||||
|
||||
// swiftlint:disable large_tuple
|
||||
|
||||
public final class TypedFamily2<A, B>: TypedFamilyProtocol where A: Component, B: Component {
|
||||
|
||||
public private(set) weak var nexus: Nexus?
|
||||
|
|
@ -22,6 +24,10 @@ public final class TypedFamily2<A, B>: TypedFamilyProtocol where A: Component, B
|
|||
return ComponentIterator2(nexus, self)
|
||||
}
|
||||
|
||||
public var entityAndComponents: FamilyEntitiesAndComponents2<A, B> {
|
||||
return FamilyEntitiesAndComponents2<A, B>(nexus, self)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public struct ComponentIterator2<A, B>: ComponentIteratorProtocol where A: Component, B: Component {
|
||||
|
|
@ -50,3 +56,29 @@ public struct ComponentIterator2<A, B>: ComponentIteratorProtocol where A: Compo
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
public struct FamilyEntitiesAndComponents2<A, B>: EntityComponentsSequenceProtocol where A: Component, B: Component {
|
||||
public private(set) weak var nexus: Nexus?
|
||||
public var memberIdsIterator: UnorderedSparseSetIterator<EntityIdentifier>
|
||||
|
||||
public init(_ nexus: Nexus?, _ family: TypedFamily2<A, B>) {
|
||||
self.nexus = nexus
|
||||
memberIdsIterator = family.memberIds.makeIterator()
|
||||
}
|
||||
|
||||
public mutating func next() -> (Entity, A, B)? {
|
||||
guard let entityId = memberIdsIterator.next() else {
|
||||
return nil
|
||||
}
|
||||
|
||||
guard
|
||||
let entity = nexus?.get(entity: entityId),
|
||||
let compA: A = nexus?.get(for: entityId),
|
||||
let compB: B = nexus?.get(for: entityId)
|
||||
else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return (entity, compA, compB)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,10 @@ public final class TypedFamily3<A, B, C>: TypedFamilyProtocol where A: Component
|
|||
return ComponentIterator3(nexus, self)
|
||||
}
|
||||
|
||||
public var entityAndComponents: FamilyEntitiesAndComponents3<A, B, C> {
|
||||
return FamilyEntitiesAndComponents3(nexus, self)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public struct ComponentIterator3<A, B, C>: ComponentIteratorProtocol where A: Component, B: Component, C: Component {
|
||||
|
|
@ -52,3 +56,30 @@ public struct ComponentIterator3<A, B, C>: ComponentIteratorProtocol where A: Co
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
public struct FamilyEntitiesAndComponents3<A, B, C>: EntityComponentsSequenceProtocol where A: Component, B: Component, C: Component {
|
||||
public private(set) weak var nexus: Nexus?
|
||||
public var memberIdsIterator: UnorderedSparseSetIterator<EntityIdentifier>
|
||||
|
||||
public init(_ nexus: Nexus?, _ family: TypedFamily3<A, B, C>) {
|
||||
self.nexus = nexus
|
||||
memberIdsIterator = family.memberIds.makeIterator()
|
||||
}
|
||||
|
||||
public mutating func next() -> (Entity, A, B, C)? {
|
||||
guard let entityId = memberIdsIterator.next() else {
|
||||
return nil
|
||||
}
|
||||
|
||||
guard
|
||||
let entity = nexus?.get(entity: entityId),
|
||||
let compA: A = nexus?.get(for: entityId),
|
||||
let compB: B = nexus?.get(for: entityId),
|
||||
let compC: C = nexus?.get(for: entityId)
|
||||
else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return (entity, compA, compB, compC)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,9 @@ public final class TypedFamily4<A, B, C, D>: TypedFamilyProtocol where A: Compon
|
|||
return ComponentIterator4(nexus, self)
|
||||
}
|
||||
|
||||
public var entityAndComponents: FamilyEntitiesAndComponents4<A, B, C, D> {
|
||||
return FamilyEntitiesAndComponents4(nexus, self)
|
||||
}
|
||||
}
|
||||
|
||||
public struct ComponentIterator4<A, B, C, D>: ComponentIteratorProtocol where A: Component, B: Component, C: Component, D: Component {
|
||||
|
|
@ -53,3 +56,31 @@ public struct ComponentIterator4<A, B, C, D>: ComponentIteratorProtocol where A:
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
public struct FamilyEntitiesAndComponents4<A, B, C, D>: EntityComponentsSequenceProtocol where A: Component, B: Component, C: Component, D: Component {
|
||||
public private(set) weak var nexus: Nexus?
|
||||
public var memberIdsIterator: UnorderedSparseSetIterator<EntityIdentifier>
|
||||
|
||||
public init(_ nexus: Nexus?, _ family: TypedFamily4<A, B, C, D>) {
|
||||
self.nexus = nexus
|
||||
memberIdsIterator = family.memberIds.makeIterator()
|
||||
}
|
||||
|
||||
public mutating func next() -> (Entity, A, B, C, D)? {
|
||||
guard let entityId = memberIdsIterator.next() else {
|
||||
return nil
|
||||
}
|
||||
|
||||
guard
|
||||
let entity = nexus?.get(entity: entityId),
|
||||
let compA: A = nexus?.get(for: entityId),
|
||||
let compB: B = nexus?.get(for: entityId),
|
||||
let compC: C = nexus?.get(for: entityId),
|
||||
let compD: D = nexus?.get(for: entityId)
|
||||
else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return (entity, compA, compB, compC, compD)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,10 @@ public final class TypedFamily5<A, B, C, D, E>: TypedFamilyProtocol where A: Com
|
|||
return ComponentIterator5(nexus, self)
|
||||
}
|
||||
|
||||
public var entityAndComponents: FamilyEntitiesAndComponents5<A, B, C, D, E> {
|
||||
return FamilyEntitiesAndComponents5(nexus, self)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public struct ComponentIterator5<A, B, C, D, E>: ComponentIteratorProtocol where A: Component, B: Component, C: Component, D: Component, E: Component {
|
||||
|
|
@ -54,3 +58,32 @@ public struct ComponentIterator5<A, B, C, D, E>: ComponentIteratorProtocol where
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
public struct FamilyEntitiesAndComponents5<A, B, C, D, E>: EntityComponentsSequenceProtocol where A: Component, B: Component, C: Component, D: Component, E: Component {
|
||||
public private(set) weak var nexus: Nexus?
|
||||
public var memberIdsIterator: UnorderedSparseSetIterator<EntityIdentifier>
|
||||
|
||||
public init(_ nexus: Nexus?, _ family: TypedFamily5<A, B, C, D, E>) {
|
||||
self.nexus = nexus
|
||||
memberIdsIterator = family.memberIds.makeIterator()
|
||||
}
|
||||
|
||||
public mutating func next() -> (Entity, A, B, C, D, E)? {
|
||||
guard let entityId = memberIdsIterator.next() else {
|
||||
return nil
|
||||
}
|
||||
|
||||
guard
|
||||
let entity = nexus?.get(entity: entityId),
|
||||
let compA: A = nexus?.get(for: entityId),
|
||||
let compB: B = nexus?.get(for: entityId),
|
||||
let compC: C = nexus?.get(for: entityId),
|
||||
let compD: D = nexus?.get(for: entityId),
|
||||
let compE: E = nexus?.get(for: entityId)
|
||||
else {
|
||||
return nil
|
||||
}
|
||||
|
||||
return (entity, compA, compB, compC, compD, compE)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue