Add TypedFamily1 + TypedFamily2

This commit is contained in:
Christian Treffs 2018-09-29 15:12:44 +02:00
parent 883e3e924e
commit af49e97115
5 changed files with 177 additions and 19 deletions

View File

@ -7,15 +7,31 @@
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...) -> TypedFamily<A, B, C> where A: Component, B: Component, C: Component {
return TypedFamily(self,
requiresAll: componentA,
componentB,
componentC,
excludesAll: excludedComponents)
excludesAll excludedComponents: Component.Type...) -> TypedFamily3<A, B, C> where A: Component, B: Component, C: Component {
return TypedFamily3(self,
requiresAll: componentA,
componentB,
componentC,
excludesAll: excludedComponents)
}
}

View File

@ -6,9 +6,12 @@
//
public protocol TypedFamilyProtocol: AnyObject {
associatedtype Members: FamilyMembersProtocol
var traits: FamilyTraitSet { get }
var nexus: Nexus? { get }
var memberIds: UniformEntityIdentifiers { get }
var members: Members { get set }
}
public extension TypedFamilyProtocol {
@ -17,14 +20,23 @@ public extension TypedFamilyProtocol {
}
}
public protocol FamilyMembersProtocol: LazySequenceProtocol {
associatedtype TypedFamily: TypedFamilyProtocol
var nexus: Nexus? { get }
var family: TypedFamily { get }
init(_ nexus: Nexus?, _ family: TypedFamily)
}
public protocol ComponentIteratorProtocol: IteratorProtocol {
associatedtype TypedFamily: TypedFamilyProtocol
var memberIds: UniformEntityIdentifiers { get }
var nexus: Nexus? { get }
var index: Int { get set }
}
public protocol FamilyMembersProtocol: LazySequenceProtocol {
var nexus: Nexus? { get }
init(_ nexus: Nexus?, _ family: TypedFamily)
}
internal extension ComponentIteratorProtocol {

View File

@ -0,0 +1,65 @@
//
// TypedFamily1.swift
// FirebladeECS
//
// Created by Christian Treffs on 29.09.18.
//
public final class TypedFamily1<A>: TypedFamilyProtocol where A: Component {
public private(set) weak var nexus: Nexus?
public let traits: FamilyTraitSet
public lazy var members: FamilyMembers1<A> = FamilyMembers1(nexus, self)
public init(_ nexus: Nexus, requiresAll compA: A.Type, excludesAll: [Component.Type]) {
self.nexus = nexus
traits = FamilyTraitSet(requiresAll: [compA], excludesAll: excludesAll)
defer {
nexus.onFamilyInit(traits: traits)
}
}
}
public struct FamilyMembers1<A>: FamilyMembersProtocol where A: Component {
public private(set) weak var nexus: Nexus?
public let family: TypedFamily1<A>
public init(_ nexus: Nexus?, _ family: TypedFamily1<A>) {
self.nexus = nexus
self.family = family
}
public func makeIterator() -> ComponentIterator1<A> {
return ComponentIterator1(nexus, family)
}
}
public struct ComponentIterator1<A>: ComponentIteratorProtocol where A: Component {
public private(set) weak var nexus: Nexus?
public let memberIds: UniformEntityIdentifiers
public var index: Int
public init(_ nexus: Nexus?, _ family: TypedFamily1<A>) {
self.nexus = nexus
memberIds = family.memberIds
index = memberIds.index(before: memberIds.startIndex)
}
public mutating func next() -> A? {
guard let entityId: EntityIdentifier = nextEntityId() else {
return nil
}
guard
let compA: A = nexus?.get(for: entityId)
else {
return nil
}
return compA
}
}

View File

@ -0,0 +1,66 @@
//
// TypedFamily2.swift
// FirebladeECS
//
// Created by Christian Treffs on 29.09.18.
//
public final class TypedFamily2<A, B>: TypedFamilyProtocol where A: Component, B: Component {
public private(set) weak var nexus: Nexus?
public let traits: FamilyTraitSet
public lazy var members: FamilyMembers2<A, B> = FamilyMembers2(nexus, self)
public init(_ nexus: Nexus, requiresAll compA: A.Type, _ compB: B.Type, excludesAll: [Component.Type]) {
self.nexus = nexus
traits = FamilyTraitSet(requiresAll: [compA, compB], excludesAll: excludesAll)
defer {
nexus.onFamilyInit(traits: traits)
}
}
}
public struct FamilyMembers2<A, B>: FamilyMembersProtocol where A: Component, B: Component {
public private(set) weak var nexus: Nexus?
public let family: TypedFamily2<A, B>
public init(_ nexus: Nexus?, _ family: TypedFamily2<A, B>) {
self.nexus = nexus
self.family = family
}
public func makeIterator() -> ComponentIterator2<A, B> {
return ComponentIterator2(nexus, family)
}
}
public struct ComponentIterator2<A, B>: ComponentIteratorProtocol where A: Component, B: Component {
public private(set) weak var nexus: Nexus?
public let memberIds: UniformEntityIdentifiers
public var index: Int
public init(_ nexus: Nexus?, _ family: TypedFamily2<A, B>) {
self.nexus = nexus
memberIds = family.memberIds
index = memberIds.index(before: memberIds.startIndex)
}
public mutating func next() -> (A, B)? {
guard let entityId: EntityIdentifier = nextEntityId() else {
return nil
}
guard
let compA: A = nexus?.get(for: entityId),
let compB: B = nexus?.get(for: entityId)
else {
return nil
}
return (compA, compB)
}
}

View File

@ -7,11 +7,10 @@
// swiftlint:disable large_tuple
public final class TypedFamily<A, B, C>: TypedFamilyProtocol where A: Component, B: Component, C: Component {
public final class TypedFamily3<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 lazy var members: FamilyMembers3<A, B, C> = FamilyMembers3(nexus, self)
public init(_ nexus: Nexus, requiresAll compA: A.Type, _ compB: B.Type, _ compC: C.Type, excludesAll: [Component.Type]) {
self.nexus = nexus
@ -23,28 +22,28 @@ public final class TypedFamily<A, B, C>: TypedFamilyProtocol where A: Component,
}
public struct FamilyMembers<A, B, C>: FamilyMembersProtocol where A: Component, B: Component, C: Component {
public struct FamilyMembers3<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 let family: TypedFamily3<A, B, C>
public init(_ nexus: Nexus?, _ family: TypedFamily<A, B, C>) {
public init(_ nexus: Nexus?, _ family: TypedFamily3<A, B, C>) {
self.nexus = nexus
self.family = family
}
public func makeIterator() -> ComponentIterator<A, B, C> {
return ComponentIterator(nexus, family)
public func makeIterator() -> ComponentIterator3<A, B, C> {
return ComponentIterator3(nexus, family)
}
}
public struct ComponentIterator<A, B, C>: ComponentIteratorProtocol where A: Component, B: Component, C: Component {
public struct ComponentIterator3<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>) {
public init(_ nexus: Nexus?, _ family: TypedFamily3<A, B, C>) {
self.nexus = nexus
memberIds = family.memberIds
index = memberIds.index(before: memberIds.startIndex)