diff --git a/Sources/FirebladeECS/Components1.swift b/Sources/FirebladeECS/Components1.swift new file mode 100644 index 0000000..5b9e33d --- /dev/null +++ b/Sources/FirebladeECS/Components1.swift @@ -0,0 +1,32 @@ +// +// Components1.swift +// +// +// Created by Christian Treffs on 21.08.19. +// + +public typealias Family1 = Family> + +public struct Components1: 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( + requires componentA: A.Type, + excludesAll excludedComponents: Component.Type... + ) -> Family1 where A: Component { + return Family1(nexus: self, + requiresAll: componentA, + excludesAll: excludedComponents) + } +} diff --git a/Sources/FirebladeECS/Components2.swift b/Sources/FirebladeECS/Components2.swift new file mode 100644 index 0000000..daf866b --- /dev/null +++ b/Sources/FirebladeECS/Components2.swift @@ -0,0 +1,35 @@ +// +// Components2.swift +// +// +// Created by Christian Treffs on 21.08.19. +// + +public typealias Family2 = Family> + +public struct Components2: 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( + requiresAll componentA: A.Type, + _ componentB: B.Type, + excludesAll excludedComponents: Component.Type... + ) -> Family2 where A: Component, B: Component { + return Family2( + nexus: self, + requiresAll: (componentA, componentB), + excludesAll: excludedComponents + ) + } +} diff --git a/Sources/FirebladeECS/Components3.swift b/Sources/FirebladeECS/Components3.swift new file mode 100644 index 0000000..d5b25ba --- /dev/null +++ b/Sources/FirebladeECS/Components3.swift @@ -0,0 +1,37 @@ +// +// Components3.swift +// +// +// Created by Christian Treffs on 21.08.19. +// + +public typealias Family3 = Family> + +public struct Components3: 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( + requiresAll componentA: A.Type, + _ componentB: B.Type, + _ componentC: C.Type, + excludesAll excludedComponents: Component.Type... + ) -> Family3 where A: Component, B: Component, C: Component { + return Family3( + nexus: self, + requiresAll: (componentA, componentB, componentC), + excludesAll: excludedComponents + ) + } +} diff --git a/Sources/FirebladeECS/Components4.swift b/Sources/FirebladeECS/Components4.swift new file mode 100644 index 0000000..d981a13 --- /dev/null +++ b/Sources/FirebladeECS/Components4.swift @@ -0,0 +1,39 @@ +// +// Components4.swift +// +// +// Created by Christian Treffs on 21.08.19. +// + +public typealias Family4 = Family> + +public struct Components4: 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( + requiresAll componentA: A.Type, + _ componentB: B.Type, + _ componentC: C.Type, + _ componentD: D.Type, + excludesAll excludedComponents: Component.Type... + ) -> Family4 where A: Component, B: Component, C: Component, D: Component { + return Family4( + nexus: self, + requiresAll: (componentA, componentB, componentC, componentD), + excludesAll: excludedComponents + ) + } +} diff --git a/Sources/FirebladeECS/Components5.swift b/Sources/FirebladeECS/Components5.swift new file mode 100644 index 0000000..27966f7 --- /dev/null +++ b/Sources/FirebladeECS/Components5.swift @@ -0,0 +1,42 @@ +// +// Components5.swift +// +// +// Created by Christian Treffs on 21.08.19. +// + +public typealias Family5 = Family> + +public struct Components5: 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( + requiresAll componentA: A.Type, + _ componentB: B.Type, + _ componentC: C.Type, + _ componentD: D.Type, + _ componentE: E.Type, + excludesAll excludedComponents: Component.Type... + ) -> Family5 where A: Component, B: Component, C: Component, D: Component, E: Component { + return Family5( + nexus: self, + requiresAll: (componentA, componentB, componentC, componentD, componentE), + excludesAll: excludedComponents + ) + } +} diff --git a/Sources/FirebladeECS/ComponentsProviding.swift b/Sources/FirebladeECS/ComponentsProviding.swift new file mode 100644 index 0000000..10f4494 --- /dev/null +++ b/Sources/FirebladeECS/ComponentsProviding.swift @@ -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 +} diff --git a/Sources/FirebladeECS/Family.swift b/Sources/FirebladeECS/Family.swift new file mode 100644 index 0000000..45cba8d --- /dev/null +++ b/Sources/FirebladeECS/Family.swift @@ -0,0 +1,69 @@ +// +// Family.swift +// +// +// Created by Christian Treffs on 21.08.19. +// + +public struct Family 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 { + 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 + @usableFromInline unowned let nexus: Nexus + + public init(family: Family) { + 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) + } +} diff --git a/Sources/FirebladeECS/FamilyEntities.swift b/Sources/FirebladeECS/FamilyEntities.swift new file mode 100644 index 0000000..025bfd5 --- /dev/null +++ b/Sources/FirebladeECS/FamilyEntities.swift @@ -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 + + public init(_ nexus: Nexus, _ memberIds: UnorderedSparseSet) { + 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 { } diff --git a/Sources/FirebladeECS/Nexus+TypedFamily.swift b/Sources/FirebladeECS/Nexus+TypedFamily.swift deleted file mode 100644 index b008a3f..0000000 --- a/Sources/FirebladeECS/Nexus+TypedFamily.swift +++ /dev/null @@ -1,84 +0,0 @@ -// -// Nexus+TypedFamily.swift -// FirebladeECS -// -// Created by Christian Treffs on 29.09.18. -// - -public extension Nexus { - func family( - requires componentA: A.Type, - excludesAll excludedComponents: Component.Type... - ) -> TypedFamily1 where A: Component { - return TypedFamily1( - self, - requiresAll: componentA, - excludesAll: excludedComponents - ) - } - - func family( - requiresAll componentA: A.Type, - _ componentB: B.Type, - excludesAll excludedComponents: Component.Type... - ) -> TypedFamily2 where A: Component, B: Component { - return TypedFamily2( - self, - requiresAll: componentA, - componentB, - excludesAll: excludedComponents - ) - } - - func family( - requiresAll componentA: A.Type, - _ componentB: B.Type, - _ componentC: C.Type, - excludesAll excludedComponents: Component.Type... - ) -> TypedFamily3 where A: Component, B: Component, C: Component { - return TypedFamily3( - self, - requiresAll: componentA, - componentB, - componentC, - excludesAll: excludedComponents - ) - } - - func family( - requiresAll componentA: A.Type, - _ componentB: B.Type, - _ componentC: C.Type, - _ componentD: D.Type, - excludesAll excludedComponents: Component.Type... - ) -> TypedFamily4 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( - requiresAll componentA: A.Type, - _ componentB: B.Type, - _ componentC: C.Type, - _ componentD: D.Type, - _ componentE: E.Type, - excludesAll excludedComponents: Component.Type... - ) -> TypedFamily5 where A: Component, B: Component, C: Component, D: Component, E: Component { - return TypedFamily5( - self, - requiresAll: componentA, - componentB, - componentC, - componentD, - componentE, - excludesAll: excludedComponents - ) - } -} diff --git a/Sources/FirebladeECS/TypedFamily.swift b/Sources/FirebladeECS/TypedFamily.swift deleted file mode 100644 index 4fa2472..0000000 --- a/Sources/FirebladeECS/TypedFamily.swift +++ /dev/null @@ -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 { 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 { - 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 == (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 { get } - - init(_ nexus: Nexus, _ family: TypedFamily) -} - -public protocol EntityComponentsSequenceProtocol: LazySequenceProtocol, IteratorProtocol { - associatedtype TypedFamily: TypedFamilyProtocol - - var memberIdsIterator: UnorderedSparseSetIterator { get } - - init(_ nexus: Nexus, _ family: TypedFamily) -} - -public struct FamilyEntities: LazySequenceProtocol, IteratorProtocol { - public let nexus: Nexus - public var memberIdsIterator: UnorderedSparseSetIterator - - public init(_ nexus: Nexus, _ memberIds: UnorderedSparseSet) { - self.nexus = nexus - memberIdsIterator = memberIds.makeIterator() - } - - public mutating func next() -> Entity? { - guard let entityId = memberIdsIterator.next() else { - return nil - } - - return nexus.get(entity: entityId) - } -} diff --git a/Sources/FirebladeECS/TypedFamily1.swift b/Sources/FirebladeECS/TypedFamily1.swift deleted file mode 100644 index e91603f..0000000 --- a/Sources/FirebladeECS/TypedFamily1.swift +++ /dev/null @@ -1,64 +0,0 @@ -// -// TypedFamily1.swift -// FirebladeECS -// -// Created by Christian Treffs on 29.09.18. -// - -public struct TypedFamily1: 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 { - return ComponentIterator1(nexus, self) - } - - public var entityAndComponents: FamilyEntitiesAndComponents1 { - return FamilyEntitiesAndComponents1(nexus, self) - } -} - -public struct ComponentIterator1: ComponentIteratorProtocol where A: Component { - public let nexus: Nexus - public var memberIdsIterator: UnorderedSparseSetIterator - - public init(_ nexus: Nexus, _ family: TypedFamily1) { - 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: EntityComponentsSequenceProtocol where A: Component { - public let nexus: Nexus - public var memberIdsIterator: UnorderedSparseSetIterator - - public init(_ nexus: Nexus, _ family: TypedFamily1) { - 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) - } -} diff --git a/Sources/FirebladeECS/TypedFamily2.swift b/Sources/FirebladeECS/TypedFamily2.swift deleted file mode 100644 index ff94922..0000000 --- a/Sources/FirebladeECS/TypedFamily2.swift +++ /dev/null @@ -1,78 +0,0 @@ -// -// TypedFamily2.swift -// FirebladeECS -// -// Created by Christian Treffs on 29.09.18. -// - -// swiftlint:disable large_tuple - -public struct TypedFamily2: 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 { - return ComponentIterator2(nexus, self) - } - - public var entityAndComponents: FamilyEntitiesAndComponents2 { - return FamilyEntitiesAndComponents2(nexus, self) - } -} - -public struct ComponentIterator2: ComponentIteratorProtocol where A: Component, B: Component { - public let nexus: Nexus - public var memberIdsIterator: UnorderedSparseSetIterator - - public init(_ nexus: Nexus, _ family: TypedFamily2) { - 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: EntityComponentsSequenceProtocol where A: Component, B: Component { - public let nexus: Nexus - public var memberIdsIterator: UnorderedSparseSetIterator - - public init(_ nexus: Nexus, _ family: TypedFamily2) { - 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) - } -} diff --git a/Sources/FirebladeECS/TypedFamily3.swift b/Sources/FirebladeECS/TypedFamily3.swift deleted file mode 100644 index 3e7a5a2..0000000 --- a/Sources/FirebladeECS/TypedFamily3.swift +++ /dev/null @@ -1,80 +0,0 @@ -// -// TypedFamily3.swift -// FirebladeECS -// -// Created by Christian Treffs on 29.09.18. -// - -// swiftlint:disable large_tuple - -public struct TypedFamily3: 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 { - return ComponentIterator3(nexus, self) - } - - public var entityAndComponents: FamilyEntitiesAndComponents3 { - return FamilyEntitiesAndComponents3(nexus, self) - } -} - -public struct ComponentIterator3: ComponentIteratorProtocol where A: Component, B: Component, C: Component { - public let nexus: Nexus - public var memberIdsIterator: UnorderedSparseSetIterator - - public init(_ nexus: Nexus, _ family: TypedFamily3) { - 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: EntityComponentsSequenceProtocol where A: Component, B: Component, C: Component { - public let nexus: Nexus - public var memberIdsIterator: UnorderedSparseSetIterator - - public init(_ nexus: Nexus, _ family: TypedFamily3) { - 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) - } -} diff --git a/Sources/FirebladeECS/TypedFamily4.swift b/Sources/FirebladeECS/TypedFamily4.swift deleted file mode 100644 index b0e3801..0000000 --- a/Sources/FirebladeECS/TypedFamily4.swift +++ /dev/null @@ -1,82 +0,0 @@ -// -// TypedFamily4.swift -// FirebladeECS -// -// Created by Christian Treffs on 29.09.18. -// - -// swiftlint:disable large_tuple - -public struct TypedFamily4: 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 { - return ComponentIterator4(nexus, self) - } - - public var entityAndComponents: FamilyEntitiesAndComponents4 { - return FamilyEntitiesAndComponents4(nexus, self) - } -} - -public struct ComponentIterator4: ComponentIteratorProtocol where A: Component, B: Component, C: Component, D: Component { - public let nexus: Nexus - public var memberIdsIterator: UnorderedSparseSetIterator - - public init(_ nexus: Nexus, _ family: TypedFamily4) { - 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: EntityComponentsSequenceProtocol where A: Component, B: Component, C: Component, D: Component { - public let nexus: Nexus - public var memberIdsIterator: UnorderedSparseSetIterator - - public init(_ nexus: Nexus, _ family: TypedFamily4) { - 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) - } -} diff --git a/Sources/FirebladeECS/TypedFamily5.swift b/Sources/FirebladeECS/TypedFamily5.swift deleted file mode 100644 index 399e622..0000000 --- a/Sources/FirebladeECS/TypedFamily5.swift +++ /dev/null @@ -1,84 +0,0 @@ -// -// TypedFamily5.swift -// FirebladeECS -// -// Created by Christian Treffs on 29.09.18. -// - -// swiftlint:disable large_tuple - -public struct TypedFamily5: 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 { - return ComponentIterator5(nexus, self) - } - - public var entityAndComponents: FamilyEntitiesAndComponents5 { - return FamilyEntitiesAndComponents5(nexus, self) - } -} - -public struct ComponentIterator5: ComponentIteratorProtocol where A: Component, B: Component, C: Component, D: Component, E: Component { - public let nexus: Nexus - public var memberIdsIterator: UnorderedSparseSetIterator - - public init(_ nexus: Nexus, _ family: TypedFamily5) { - 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: EntityComponentsSequenceProtocol where A: Component, B: Component, C: Component, D: Component, E: Component { - public let nexus: Nexus - public var memberIdsIterator: UnorderedSparseSetIterator - - public init(_ nexus: Nexus, _ family: TypedFamily5) { - 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) - } -} diff --git a/Tests/FirebladeECSPerformanceTests/Base.swift b/Tests/FirebladeECSPerformanceTests/Base.swift index 63db0ff..5ad6700 100644 --- a/Tests/FirebladeECSPerformanceTests/Base.swift +++ b/Tests/FirebladeECSPerformanceTests/Base.swift @@ -46,7 +46,7 @@ class Color: Component { } class ExampleSystem { - private let family: TypedFamily2 + private let family: Family> init(nexus: Nexus) { family = nexus.family(requiresAll: Position.self, Velocity.self, excludesAll: EmptyComponent.self) diff --git a/Tests/FirebladeECSTests/Base.swift b/Tests/FirebladeECSTests/Base.swift index 0c122a7..630b109 100644 --- a/Tests/FirebladeECSTests/Base.swift +++ b/Tests/FirebladeECSTests/Base.swift @@ -54,7 +54,7 @@ final class SingleGameState: SingleComponent { class ExampleSystem { - private let family: TypedFamily2 + private let family: Family> 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 + let positions: Family> var velocity: Double = 4.0