Merge branch 'feature/entity-components' into develop

This commit is contained in:
Christian Treffs 2020-05-26 15:09:55 +02:00
commit 37565e38a6
No known key found for this signature in database
GPG Key ID: 49A4B4B460BE3ED4
9 changed files with 61 additions and 14 deletions

View File

@ -125,6 +125,37 @@ public struct Entity {
public var numChildren: Int {
nexus.numChildren(for: self)
}
/// Returns an iterator over all components of this entity.
@inlinable
public func makeComponentsIterator() -> ComponentsIterator {
ComponentsIterator(nexus: nexus, entityIdentifier: identifier)
}
/// Returns a sequence of all componenents of this entity.
@inlinable
public func allComponents() -> AnySequence<Component> {
AnySequence { self.makeComponentsIterator() }
}
}
extension Entity {
public struct ComponentsIterator: IteratorProtocol {
private var iterator: AnyIterator<Component>
@usableFromInline
init(nexus: Nexus, entityIdentifier: EntityIdentifier) {
if let comps = nexus.get(components: entityIdentifier) {
iterator = AnyIterator<Component>(comps.compactMap { nexus.get(component: $0, for: entityIdentifier) }.makeIterator())
} else {
iterator = AnyIterator { nil }
}
}
public mutating func next() -> Component? {
iterator.next()
}
}
}
extension Entity: Equatable {

View File

@ -5,7 +5,6 @@
// Created by Christian Treffs on 22.10.17.
//
#if DEBUG
@testable import FirebladeECS
import XCTest
@ -22,5 +21,33 @@ class EntityTests: XCTestCase {
XCTAssertEqual(max, EntityIdentifier.invalid)
XCTAssertEqual(max.id, Int(UInt32.max))
}
func testAllComponentsOfEntity() {
let nexus = Nexus()
let pos = Position(x: 1, y: 2)
let name = Name(name: "Hello")
let vel = Velocity(a: 1.234)
let entity = nexus.createEntity()
entity.assign(pos)
entity.assign(name)
entity.assign(vel)
let expectedComponents: [Component] = [pos, name, vel]
let allComponents = entity.allComponents()
XCTAssertTrue(allComponents.elementsEqualUnordered(expectedComponents) { $0 === $1 })
}
}
extension Sequence {
func elementsEqualUnordered<OtherSequence>(_ other: OtherSequence, by areEquivalent: (Element, OtherSequence.Element) throws -> Bool) rethrows -> Bool where OtherSequence: Sequence {
for element in self {
if try !other.contains(where: { try areEquivalent(element, $0) }) {
return false
}
}
return true
}
}
#endif

View File

@ -5,7 +5,6 @@
// Created by Christian Treffs on 09.10.17.
//
#if DEBUG
@testable import FirebladeECS
import XCTest
@ -186,4 +185,3 @@ class FamilyTests: XCTestCase {
XCTAssertEqual(family.memberIds.count, count + (count / 2))
}
}
#endif

View File

@ -5,7 +5,6 @@
// Created by Christian Treffs on 16.10.17.
//
#if DEBUG
@testable import FirebladeECS
import XCTest
@ -65,4 +64,3 @@ class HashingTests: XCTestCase {
}
}
#endif

View File

@ -5,7 +5,6 @@
// Created by Christian Treffs on 09.10.17.
//
#if DEBUG
@testable import FirebladeECS
import XCTest
@ -156,4 +155,3 @@ class NexusTests: XCTestCase {
XCTAssert(pB.y != pA.y)
}
}
#endif

View File

@ -5,7 +5,6 @@
// Created by Christian Treffs on 13.02.19.
//
#if DEBUG
@testable import FirebladeECS
import XCTest
@ -63,4 +62,3 @@ class SingleTests: XCTestCase {
XCTAssertTrue(singleGame === single.component)
}
}
#endif

View File

@ -5,7 +5,6 @@
// Created by Christian Treffs on 31.10.17.
//
#if DEBUG
@testable import FirebladeECS
import XCTest
@ -540,4 +539,3 @@ class SparseSetTests: XCTestCase {
XCTAssertEqual(mapped, ["C", "A", "B"])
}
}
#endif

View File

@ -5,7 +5,6 @@
// Created by Christian Treffs on 10.05.18.
//
#if DEBUG
@testable import FirebladeECS
import XCTest
@ -125,4 +124,3 @@ class SystemsTests: XCTestCase {
}
}
}
#endif

View File

@ -25,6 +25,7 @@ extension EntityTests {
// `swift test --generate-linuxmain`
// to regenerate.
static let __allTests__EntityTests = [
("testAllComponentsOfEntity", testAllComponentsOfEntity),
("testEntityIdentifierAndIndex", testEntityIdentifierAndIndex)
]
}