Merge branch 'feature/destroy-members' into develop
This commit is contained in:
commit
4c15fcf823
|
|
@ -17,14 +17,16 @@ public struct Family<R> where R: FamilyRequirementsManaging {
|
|||
nexus.onFamilyInit(traits: traits)
|
||||
}
|
||||
|
||||
@inlinable public var memberIds: UnorderedSparseSet<EntityIdentifier, EntityIdentifier.Idx> {
|
||||
@inlinable var memberIds: UnorderedSparseSet<EntityIdentifier, EntityIdentifier.Idx> {
|
||||
nexus.members(withFamilyTraits: traits)
|
||||
}
|
||||
|
||||
/// Returns the number of family member entities.
|
||||
@inlinable public var count: Int {
|
||||
memberIds.count
|
||||
}
|
||||
|
||||
/// True if this family has no members; false otherwise.
|
||||
@inlinable public var isEmpty: Bool {
|
||||
memberIds.isEmpty
|
||||
}
|
||||
|
|
@ -38,6 +40,13 @@ public struct Family<R> where R: FamilyRequirementsManaging {
|
|||
public func isMember(_ entity: Entity) -> Bool {
|
||||
nexus.isMember(entity, in: traits)
|
||||
}
|
||||
|
||||
/// Destroy all member entities of this family.
|
||||
/// - Returns: True if entities where destroyed successfully, false otherwise.
|
||||
@discardableResult
|
||||
public func destroyMembers() -> Bool {
|
||||
entities.reduce(!isEmpty) { $0 && nexus.destroy(entity: $1) }
|
||||
}
|
||||
}
|
||||
|
||||
extension Family: Equatable {
|
||||
|
|
@ -48,7 +57,7 @@ extension Family: Equatable {
|
|||
}
|
||||
|
||||
extension Family: Sequence {
|
||||
__consuming public func makeIterator() -> ComponentsIterator {
|
||||
public func makeIterator() -> ComponentsIterator {
|
||||
ComponentsIterator(family: self)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -185,6 +185,33 @@ class FamilyTests: XCTestCase {
|
|||
XCTAssertEqual(family.memberIds.count, count + (count / 2))
|
||||
}
|
||||
|
||||
func testFamilyDestroyMembers() {
|
||||
let family = nexus.family(requiresAll: Position.self, Color.self)
|
||||
|
||||
family.createMember(with: (Position(x: 1, y: 2), Color(r: 1, g: 2, b: 3)))
|
||||
family.createMember(with: (Position(x: 3, y: 4), Color(r: 4, g: 5, b: 6)))
|
||||
nexus.createEntity(with: Name(name: "anotherEntity"))
|
||||
|
||||
XCTAssertEqual(nexus.numFamilies, 1)
|
||||
XCTAssertEqual(nexus.numComponents, 5)
|
||||
XCTAssertEqual(nexus.numEntities, 3)
|
||||
XCTAssertEqual(family.count, 2)
|
||||
|
||||
XCTAssertTrue(family.destroyMembers())
|
||||
|
||||
XCTAssertEqual(nexus.numFamilies, 1)
|
||||
XCTAssertEqual(nexus.numComponents, 1)
|
||||
XCTAssertEqual(nexus.numEntities, 1)
|
||||
XCTAssertEqual(family.count, 0)
|
||||
|
||||
XCTAssertFalse(family.destroyMembers())
|
||||
|
||||
XCTAssertEqual(nexus.numFamilies, 1)
|
||||
XCTAssertEqual(nexus.numComponents, 1)
|
||||
XCTAssertEqual(nexus.numEntities, 1)
|
||||
XCTAssertEqual(family.count, 0)
|
||||
}
|
||||
|
||||
func testFamilyCreateMembers() {
|
||||
let position = Position(x: 0, y: 1)
|
||||
let name = Name(name: "SomeName")
|
||||
|
|
|
|||
|
|
@ -194,6 +194,7 @@ extension FamilyTests {
|
|||
("testFamilyBulkDestroy", testFamilyBulkDestroy),
|
||||
("testFamilyCreateMembers", testFamilyCreateMembers),
|
||||
("testFamilyCreation", testFamilyCreation),
|
||||
("testFamilyDestroyMembers", testFamilyDestroyMembers),
|
||||
("testFamilyExchange", testFamilyExchange),
|
||||
("testFamilyLateMember", testFamilyLateMember),
|
||||
("testFamilyMemberBasicIteration", testFamilyMemberBasicIteration),
|
||||
|
|
|
|||
Loading…
Reference in New Issue