Rework TypedFamilies to Family

This commit is contained in:
Christian Treffs 2019-08-21 10:24:20 +02:00
parent f56faaace5
commit af3864701e
17 changed files with 301 additions and 569 deletions

View File

@ -0,0 +1,32 @@
//
// Components1.swift
//
//
// Created by Christian Treffs on 21.08.19.
//
public typealias Family1<A: Component> = Family<Components1<A>>
public struct Components1<A>: ComponentsProviding where A: Component {
public let componentTypes: [Component.Type]
public init(_ components: (A.Type)) {
componentTypes = [A.self]
}
public static func getComponents(nexus: Nexus, entityId: EntityIdentifier) -> (A) {
let compA: A = nexus.get(unsafeComponentFor: entityId)
return (compA)
}
}
extension Nexus {
public func family<A>(
requires componentA: A.Type,
excludesAll excludedComponents: Component.Type...
) -> Family1<A> where A: Component {
return Family1<A>(nexus: self,
requiresAll: componentA,
excludesAll: excludedComponents)
}
}

View File

@ -0,0 +1,35 @@
//
// Components2.swift
//
//
// Created by Christian Treffs on 21.08.19.
//
public typealias Family2<A: Component, B: Component> = Family<Components2<A, B>>
public struct Components2<A, B>: ComponentsProviding where A: Component, B: Component {
public let componentTypes: [Component.Type]
public init(_ components: (A.Type, B.Type)) {
componentTypes = [ A.self, B.self]
}
public static func getComponents(nexus: Nexus, entityId: EntityIdentifier) -> (A, B) {
let compA: A = nexus.get(unsafeComponentFor: entityId)
let compB: B = nexus.get(unsafeComponentFor: entityId)
return (compA, compB)
}
}
extension Nexus {
public func family<A, B>(
requiresAll componentA: A.Type,
_ componentB: B.Type,
excludesAll excludedComponents: Component.Type...
) -> Family2<A, B> where A: Component, B: Component {
return Family2<A, B>(
nexus: self,
requiresAll: (componentA, componentB),
excludesAll: excludedComponents
)
}
}

View File

@ -0,0 +1,37 @@
//
// Components3.swift
//
//
// Created by Christian Treffs on 21.08.19.
//
public typealias Family3<A: Component, B: Component, C: Component> = Family<Components3<A, B, C>>
public struct Components3<A, B, C>: ComponentsProviding where A: Component, B: Component, C: Component {
public let componentTypes: [Component.Type]
public init(_ types: (A.Type, B.Type, C.Type)) {
componentTypes = [A.self, B.self, C.self]
}
public static func getComponents(nexus: Nexus, entityId: EntityIdentifier) -> (A, B, C) {
let compA: A = nexus.get(unsafeComponentFor: entityId)
let compB: B = nexus.get(unsafeComponentFor: entityId)
let compC: C = nexus.get(unsafeComponentFor: entityId)
return (compA, compB, compC)
}
}
extension Nexus {
public func family<A, B, C>(
requiresAll componentA: A.Type,
_ componentB: B.Type,
_ componentC: C.Type,
excludesAll excludedComponents: Component.Type...
) -> Family3<A, B, C> where A: Component, B: Component, C: Component {
return Family3(
nexus: self,
requiresAll: (componentA, componentB, componentC),
excludesAll: excludedComponents
)
}
}

View File

@ -0,0 +1,39 @@
//
// Components4.swift
//
//
// Created by Christian Treffs on 21.08.19.
//
public typealias Family4<A: Component, B: Component, C: Component, D: Component> = Family<Components4<A, B, C, D>>
public struct Components4<A, B, C, D>: ComponentsProviding where A: Component, B: Component, C: Component, D: Component {
public let componentTypes: [Component.Type]
public init(_ types: (A.Type, B.Type, C.Type, D.Type)) {
componentTypes = [A.self, B.self, C.self, D.self]
}
public static func getComponents(nexus: Nexus, entityId: EntityIdentifier) -> (A, B, C, D) {
let compA: A = nexus.get(unsafeComponentFor: entityId)
let compB: B = nexus.get(unsafeComponentFor: entityId)
let compC: C = nexus.get(unsafeComponentFor: entityId)
let compD: D = nexus.get(unsafeComponentFor: entityId)
return (compA, compB, compC, compD)
}
}
extension Nexus {
public func family<A, B, C, D>(
requiresAll componentA: A.Type,
_ componentB: B.Type,
_ componentC: C.Type,
_ componentD: D.Type,
excludesAll excludedComponents: Component.Type...
) -> Family4<A, B, C, D> where A: Component, B: Component, C: Component, D: Component {
return Family4(
nexus: self,
requiresAll: (componentA, componentB, componentC, componentD),
excludesAll: excludedComponents
)
}
}

View File

@ -0,0 +1,42 @@
//
// Components5.swift
//
//
// Created by Christian Treffs on 21.08.19.
//
public typealias Family5<A: Component, B: Component, C: Component, D: Component, E: Component> = Family<Components5<A, B, C, D, E>>
public struct Components5<A, B, C, D, E>: ComponentsProviding where A: Component, B: Component, C: Component, D: Component, E: Component {
public let componentTypes: [Component.Type]
public init(_ types: (A.Type, B.Type, C.Type, D.Type, E.Type)) {
componentTypes = [A.self, B.self, C.self, D.self, E.self]
}
public static func getComponents(nexus: Nexus, entityId: EntityIdentifier) -> (A, B, C, D, E) {
let compA: A = nexus.get(unsafeComponentFor: entityId)
let compB: B = nexus.get(unsafeComponentFor: entityId)
let compC: C = nexus.get(unsafeComponentFor: entityId)
let compD: D = nexus.get(unsafeComponentFor: entityId)
let compE: E = nexus.get(unsafeComponentFor: entityId)
return (compA, compB, compC, compD, compE)
}
}
extension Nexus {
// swiftlint:disable function_parameter_count
public func family<A, B, C, D, E>(
requiresAll componentA: A.Type,
_ componentB: B.Type,
_ componentC: C.Type,
_ componentD: D.Type,
_ componentE: E.Type,
excludesAll excludedComponents: Component.Type...
) -> Family5<A, B, C, D, E> where A: Component, B: Component, C: Component, D: Component, E: Component {
return Family5(
nexus: self,
requiresAll: (componentA, componentB, componentC, componentD, componentE),
excludesAll: excludedComponents
)
}
}

View File

@ -0,0 +1,14 @@
//
// ComponentsProviding.swift
//
//
// Created by Christian Treffs on 21.08.19.
//
public protocol ComponentsProviding {
associatedtype Components
associatedtype ComponentTypes
init(_ types: ComponentTypes)
var componentTypes: [Component.Type] { get }
static func getComponents(nexus: Nexus, entityId: EntityIdentifier) -> Components
}

View File

@ -0,0 +1,69 @@
//
// Family.swift
//
//
// Created by Christian Treffs on 21.08.19.
//
public struct Family<R> where R: ComponentsProviding {
@usableFromInline unowned let nexus: Nexus
public let traits: FamilyTraitSet
public init(nexus: Nexus, requiresAll: @autoclosure () -> (R.ComponentTypes), excludesAll: [Component.Type]) {
let required = R(requiresAll())
self.nexus = nexus
traits = FamilyTraitSet(requiresAll: required.componentTypes, excludesAll: excludesAll)
}
@inlinable public var memberIds: UnorderedSparseSet<EntityIdentifier> {
return nexus.members(withFamilyTraits: traits)
}
@inlinable public var count: Int {
return memberIds.count
}
@inlinable public var isEmpty: Bool {
return memberIds.isEmpty
}
@inlinable public var entities: FamilyEntities {
return FamilyEntities(nexus, memberIds)
}
@inlinable
public func canBecomeMember(_ entity: Entity) -> Bool {
return nexus.canBecomeMember(entity, in: traits)
}
@inlinable
public func isMember(_ entity: Entity) -> Bool {
return nexus.isMember(entity, in: traits)
}
}
extension Family {
public struct FamilyIterator: IteratorProtocol {
@usableFromInline var memberIdsIterator: UnorderedSparseSetIterator<EntityIdentifier>
@usableFromInline unowned let nexus: Nexus
public init(family: Family<R>) {
self.nexus = family.nexus
memberIdsIterator = family.memberIds.makeIterator()
}
public mutating func next() -> R.Components? {
guard let entityId: EntityIdentifier = memberIdsIterator.next() else {
return nil
}
return R.getComponents(nexus: nexus, entityId: entityId)
}
}
}
extension Family: Sequence {
__consuming public func makeIterator() -> FamilyIterator {
return FamilyIterator(family: self)
}
}

View File

@ -0,0 +1,30 @@
//
// FamilyEntities.swift
//
//
// Created by Christian Treffs on 21.08.19.
//
public struct FamilyEntities {
public let nexus: Nexus
public var memberIdsIterator: UnorderedSparseSetIterator<EntityIdentifier>
public init(_ nexus: Nexus, _ memberIds: UnorderedSparseSet<EntityIdentifier>) {
self.nexus = nexus
memberIdsIterator = memberIds.makeIterator()
}
}
extension FamilyEntities: IteratorProtocol {
public mutating func next() -> Entity? {
guard let entityId = memberIdsIterator.next() else {
return nil
}
return nexus.get(entity: entityId)
}
}
extension FamilyEntities: LazySequenceProtocol { }
// TODO: extension FamilyEntities: Equatable { }

View File

@ -1,84 +0,0 @@
//
// Nexus+TypedFamily.swift
// FirebladeECS
//
// Created by Christian Treffs on 29.09.18.
//
public extension Nexus {
func family<A>(
requires componentA: A.Type,
excludesAll excludedComponents: Component.Type...
) -> TypedFamily1<A> where A: Component {
return TypedFamily1(
self,
requiresAll: componentA,
excludesAll: excludedComponents
)
}
func family<A, B>(
requiresAll componentA: A.Type,
_ componentB: B.Type,
excludesAll excludedComponents: Component.Type...
) -> TypedFamily2<A, B> where A: Component, B: Component {
return TypedFamily2(
self,
requiresAll: componentA,
componentB,
excludesAll: excludedComponents
)
}
func family<A, B, C>(
requiresAll componentA: A.Type,
_ componentB: B.Type,
_ componentC: C.Type,
excludesAll excludedComponents: Component.Type...
) -> TypedFamily3<A, B, C> where A: Component, B: Component, C: Component {
return TypedFamily3(
self,
requiresAll: componentA,
componentB,
componentC,
excludesAll: excludedComponents
)
}
func family<A, B, C, D>(
requiresAll componentA: A.Type,
_ componentB: B.Type,
_ componentC: C.Type,
_ componentD: D.Type,
excludesAll excludedComponents: Component.Type...
) -> TypedFamily4<A, B, C, D> where A: Component, B: Component, C: Component, D: Component {
return TypedFamily4(
self,
requiresAll: componentA,
componentB,
componentC,
componentD,
excludesAll: excludedComponents
)
}
// swiftlint:disable function_parameter_count
func family<A, B, C, D, E>(
requiresAll componentA: A.Type,
_ componentB: B.Type,
_ componentC: C.Type,
_ componentD: D.Type,
_ componentE: E.Type,
excludesAll excludedComponents: Component.Type...
) -> TypedFamily5<A, B, C, D, E> where A: Component, B: Component, C: Component, D: Component, E: Component {
return TypedFamily5(
self,
requiresAll: componentA,
componentB,
componentC,
componentD,
componentE,
excludesAll: excludedComponents
)
}
}

View File

@ -1,94 +0,0 @@
//
// TypedFamily.swift
// FirebladeECS
//
// Created by Christian Treffs on 29.09.18.
//
// Note: implemenetation ideas
// * define sort order of entities
// * define read/write access
// * set size and storage constraints
public protocol TypedFamilyProtocol: Equatable, Sequence {
associatedtype EntityComponentsSequence: EntityComponentsSequenceProtocol
var traits: FamilyTraitSet { get }
var nexus: Nexus { get }
var count: Int { get }
var isEmpty: Bool { get }
var memberIds: UnorderedSparseSet<EntityIdentifier> { get }
var entities: FamilyEntities { get }
var entityAndComponents: EntityComponentsSequence { get }
func canBecomeMember(_ entity: Entity) -> Bool
func isMember(_ entity: Entity) -> Bool
}
public extension TypedFamilyProtocol {
@inlinable
func canBecomeMember(_ entity: Entity) -> Bool {
return nexus.canBecomeMember(entity, in: traits)
}
@inlinable
func isMember(_ entity: Entity) -> Bool {
return nexus.isMember(entity, in: traits)
}
@inlinable var memberIds: UnorderedSparseSet<EntityIdentifier> {
return nexus.members(withFamilyTraits: traits)
}
@inlinable var count: Int {
return memberIds.count
}
@inlinable var isEmpty: Bool {
return memberIds.isEmpty
}
@inlinable 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 {
associatedtype TypedFamily: TypedFamilyProtocol
var memberIdsIterator: UnorderedSparseSetIterator<EntityIdentifier> { get }
init(_ nexus: Nexus, _ family: TypedFamily)
}
public protocol EntityComponentsSequenceProtocol: LazySequenceProtocol, IteratorProtocol {
associatedtype TypedFamily: TypedFamilyProtocol
var memberIdsIterator: UnorderedSparseSetIterator<EntityIdentifier> { get }
init(_ nexus: Nexus, _ family: TypedFamily)
}
public struct FamilyEntities: LazySequenceProtocol, IteratorProtocol {
public let nexus: Nexus
public var memberIdsIterator: UnorderedSparseSetIterator<EntityIdentifier>
public init(_ nexus: Nexus, _ memberIds: UnorderedSparseSet<EntityIdentifier>) {
self.nexus = nexus
memberIdsIterator = memberIds.makeIterator()
}
public mutating func next() -> Entity? {
guard let entityId = memberIdsIterator.next() else {
return nil
}
return nexus.get(entity: entityId)
}
}

View File

@ -1,64 +0,0 @@
//
// TypedFamily1.swift
// FirebladeECS
//
// Created by Christian Treffs on 29.09.18.
//
public struct TypedFamily1<A>: TypedFamilyProtocol where A: Component {
public let nexus: Nexus
public let traits: FamilyTraitSet
public init(_ nexus: Nexus, requiresAll compA: A.Type, excludesAll: [Component.Type]) {
self.nexus = nexus
traits = FamilyTraitSet(requiresAll: [compA], excludesAll: excludesAll)
nexus.onFamilyInit(traits: traits)
}
public func makeIterator() -> ComponentIterator1<A> {
return ComponentIterator1(nexus, self)
}
public var entityAndComponents: FamilyEntitiesAndComponents1<A> {
return FamilyEntitiesAndComponents1(nexus, self)
}
}
public struct ComponentIterator1<A>: ComponentIteratorProtocol where A: Component {
public let 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() -> A? {
guard let entityId = memberIdsIterator.next() else {
return nil
}
return nexus.get(for: entityId)
}
}
public struct FamilyEntitiesAndComponents1<A>: EntityComponentsSequenceProtocol where A: Component {
public let 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(),
let entity = nexus.get(entity: entityId),
let compA: A = nexus.get(for: entityId)
else {
return nil
}
return (entity, compA)
}
}

View File

@ -1,78 +0,0 @@
//
// TypedFamily2.swift
// FirebladeECS
//
// Created by Christian Treffs on 29.09.18.
//
// swiftlint:disable large_tuple
public struct TypedFamily2<A, B>: TypedFamilyProtocol where A: Component, B: Component {
public let nexus: Nexus
public let traits: FamilyTraitSet
public init(_ nexus: Nexus, requiresAll compA: A.Type, _ compB: B.Type, excludesAll: [Component.Type]) {
self.nexus = nexus
traits = FamilyTraitSet(requiresAll: [compA, compB], excludesAll: excludesAll)
nexus.onFamilyInit(traits: traits)
}
public func makeIterator() -> ComponentIterator2<A, 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 {
public let 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() -> (A, B)? {
guard let entityId: EntityIdentifier = memberIdsIterator.next() else {
return nil
}
guard
let compA: A = nexus.get(for: entityId),
let compB: B = nexus.get(for: entityId)
else {
return nil
}
return (compA, compB)
}
}
public struct FamilyEntitiesAndComponents2<A, B>: EntityComponentsSequenceProtocol where A: Component, B: Component {
public let 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)
}
}

View File

@ -1,80 +0,0 @@
//
// TypedFamily3.swift
// FirebladeECS
//
// Created by Christian Treffs on 29.09.18.
//
// swiftlint:disable large_tuple
public struct TypedFamily3<A, B, C>: TypedFamilyProtocol where A: Component, B: Component, C: Component {
public let nexus: Nexus
public let traits: FamilyTraitSet
public init(_ nexus: Nexus, requiresAll compA: A.Type, _ compB: B.Type, _ compC: C.Type, excludesAll: [Component.Type]) {
self.nexus = nexus
traits = FamilyTraitSet(requiresAll: [compA, compB, compC], excludesAll: excludesAll)
nexus.onFamilyInit(traits: traits)
}
public func makeIterator() -> ComponentIterator3<A, B, C> {
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 {
public let 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() -> (A, B, C)? {
guard let entityId: EntityIdentifier = memberIdsIterator.next() else {
return nil
}
guard
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 (compA, compB, compC)
}
}
public struct FamilyEntitiesAndComponents3<A, B, C>: EntityComponentsSequenceProtocol where A: Component, B: Component, C: Component {
public let 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)
}
}

View File

@ -1,82 +0,0 @@
//
// TypedFamily4.swift
// FirebladeECS
//
// Created by Christian Treffs on 29.09.18.
//
// swiftlint:disable large_tuple
public struct TypedFamily4<A, B, C, D>: TypedFamilyProtocol where A: Component, B: Component, C: Component, D: Component {
public let nexus: Nexus
public let traits: FamilyTraitSet
public init(_ nexus: Nexus, requiresAll compA: A.Type, _ compB: B.Type, _ compC: C.Type, _ compD: D.Type, excludesAll: [Component.Type]) {
self.nexus = nexus
traits = FamilyTraitSet(requiresAll: [compA, compB, compC, compD], excludesAll: excludesAll)
nexus.onFamilyInit(traits: traits)
}
public func makeIterator() -> ComponentIterator4<A, B, C, D> {
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 {
public let 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() -> (A, B, C, D)? {
guard let entityId: EntityIdentifier = memberIdsIterator.next() else {
return nil
}
guard
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 (compA, compB, compC, compD)
}
}
public struct FamilyEntitiesAndComponents4<A, B, C, D>: EntityComponentsSequenceProtocol where A: Component, B: Component, C: Component, D: Component {
public let 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)
}
}

View File

@ -1,84 +0,0 @@
//
// TypedFamily5.swift
// FirebladeECS
//
// Created by Christian Treffs on 29.09.18.
//
// swiftlint:disable large_tuple
public struct TypedFamily5<A, B, C, D, E>: TypedFamilyProtocol where A: Component, B: Component, C: Component, D: Component, E: Component {
public let nexus: Nexus
public let traits: FamilyTraitSet
public init(_ nexus: Nexus, requiresAll compA: A.Type, _ compB: B.Type, _ compC: C.Type, _ compD: D.Type, _ compE: E.Type, excludesAll: [Component.Type]) {
self.nexus = nexus
traits = FamilyTraitSet(requiresAll: [compA, compB, compC, compD, compE], excludesAll: excludesAll)
nexus.onFamilyInit(traits: traits)
}
public func makeIterator() -> ComponentIterator5<A, B, C, D, E> {
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 {
public let 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() -> (A, B, C, D, E)? {
guard let entityId: EntityIdentifier = memberIdsIterator.next() else {
return nil
}
guard
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 (compA, compB, compC, compD, compE)
}
}
public struct FamilyEntitiesAndComponents5<A, B, C, D, E>: EntityComponentsSequenceProtocol where A: Component, B: Component, C: Component, D: Component, E: Component {
public let 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)
}
}

View File

@ -46,7 +46,7 @@ class Color: Component {
}
class ExampleSystem {
private let family: TypedFamily2<Position, Velocity>
private let family: Family<Components2<Position, Velocity>>
init(nexus: Nexus) {
family = nexus.family(requiresAll: Position.self, Velocity.self, excludesAll: EmptyComponent.self)

View File

@ -54,7 +54,7 @@ final class SingleGameState: SingleComponent {
class ExampleSystem {
private let family: TypedFamily2<Position, Velocity>
private let family: Family<Components2<Position, Velocity>>
init(nexus: Nexus) {
family = nexus.family(requiresAll: Position.self, Velocity.self, excludesAll: EmptyComponent.self)
@ -92,7 +92,7 @@ class ColorSystem {
}
class PositionSystem {
let positions: TypedFamily1<Position>
let positions: Family<Components1<Position>>
var velocity: Double = 4.0