Extract TypedFamily protocols

This commit is contained in:
Christian Treffs 2018-09-29 14:46:45 +02:00
parent 8c3301bd1d
commit 883e3e924e
2 changed files with 86 additions and 62 deletions

View File

@ -5,58 +5,30 @@
// Created by Christian Treffs on 29.09.18.
//
// swiftlint:disable large_tuple
public protocol TypedFamilyProtocol: AnyObject {
var traits: FamilyTraitSet { get }
var nexus: Nexus? { get }
var memberIds: UniformEntityIdentifiers { get }
}
public final class TypedFamily<A, B, C> where A: Component, B: Component, C: Component {
internal weak var nexus: Nexus?
public let traits: FamilyTraitSet
internal 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)
defer {
nexus.onFamilyInit(traits: traits)
}
}
public private(set) lazy var members: FamilyMembers<A, B, C> = FamilyMembers(nexus, self)
internal final var memberIds: UniformEntityIdentifiers {
public extension TypedFamilyProtocol {
var memberIds: UniformEntityIdentifiers {
return nexus?.members(withFamilyTraits: traits) ?? UniformEntityIdentifiers()
}
}
public struct FamilyMembers<A, B, C>: LazySequenceProtocol where A: Component, B: Component, C: Component {
private weak var nexus: Nexus?
public let family: TypedFamily<A, B, C>
internal init(_ nexus: Nexus?, _ family: TypedFamily<A, B, C>) {
self.nexus = nexus
self.family = family
}
public func makeIterator() -> ComponentIterator<A, B, C> {
return ComponentIterator(nexus, family)
}
public protocol ComponentIteratorProtocol: IteratorProtocol {
var memberIds: UniformEntityIdentifiers { get }
var nexus: Nexus? { get }
var index: Int { get set }
}
public struct ComponentIterator<A, B, C>: IteratorProtocol where A: Component, B: Component, C: Component {
public protocol FamilyMembersProtocol: LazySequenceProtocol {
var nexus: Nexus? { get }
}
private let memberIds: UniformEntityIdentifiers
private weak var nexus: Nexus?
private var index: Int
public init(_ nexus: Nexus?, _ family: TypedFamily<A, B, C>) {
self.nexus = nexus
memberIds = family.memberIds
index = memberIds.index(before: memberIds.startIndex)
}
private mutating func nextIndex() -> Int? {
internal extension ComponentIteratorProtocol {
internal mutating func nextIndex() -> Int? {
let nextIndex: Int = memberIds.index(after: index)
guard nextIndex < memberIds.endIndex else {
return nil
@ -66,28 +38,11 @@ public struct ComponentIterator<A, B, C>: IteratorProtocol where A: Component, B
return nextIndex
}
private mutating func nextEntityId() -> EntityIdentifier? {
internal mutating func nextEntityId() -> EntityIdentifier? {
guard let index: Int = nextIndex() else {
return nil
}
return memberIds[index]
}
public mutating func next() -> (A, B, C)? {
guard let entityId: EntityIdentifier = nextEntityId() 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)
}
}

View File

@ -0,0 +1,69 @@
//
// TypedFamily3.swift
// FirebladeECS
//
// Created by Christian Treffs on 29.09.18.
//
// swiftlint:disable large_tuple
public final class TypedFamily<A, B, C>: TypedFamilyProtocol where A: Component, B: Component, C: Component {
public private(set) weak var nexus: Nexus?
public let traits: FamilyTraitSet
public private(set) lazy var members: FamilyMembers<A, B, C> = FamilyMembers(nexus, self)
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)
defer {
nexus.onFamilyInit(traits: traits)
}
}
}
public struct FamilyMembers<A, B, C>: FamilyMembersProtocol where A: Component, B: Component, C: Component {
public private(set) weak var nexus: Nexus?
public let family: TypedFamily<A, B, C>
public init(_ nexus: Nexus?, _ family: TypedFamily<A, B, C>) {
self.nexus = nexus
self.family = family
}
public func makeIterator() -> ComponentIterator<A, B, C> {
return ComponentIterator(nexus, family)
}
}
public struct ComponentIterator<A, B, C>: ComponentIteratorProtocol where A: Component, B: Component, C: Component {
public private(set) weak var nexus: Nexus?
public let memberIds: UniformEntityIdentifiers
public var index: Int
public init(_ nexus: Nexus?, _ family: TypedFamily<A, B, C>) {
self.nexus = nexus
memberIds = family.memberIds
index = memberIds.index(before: memberIds.startIndex)
}
public mutating func next() -> (A, B, C)? {
guard let entityId: EntityIdentifier = nextEntityId() 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)
}
}