Add createMember() method to families

This commit is contained in:
Christian Treffs 2020-07-21 17:02:16 +02:00
parent 8ddc249b7b
commit f48fd3c2e7
No known key found for this signature in database
GPG Key ID: 49A4B4B460BE3ED4
7 changed files with 37 additions and 0 deletions

View File

@ -184,3 +184,17 @@ extension Family {
}
extension Family.RelativesIterator: LazySequenceProtocol { }
// MARK: - member creation
extension Family {
/// Create a new entity with components required by this family.
///
/// Since the created entity will meet the requirements of this family it
/// will automatically become member of this family.
/// - Parameter components: The components required by this family.
/// - Returns: The newly created entity.
@discardableResult
public func createMember(with components: R.Components) -> Entity {
R.createMember(nexus: nexus, components: components)
}
}

View File

@ -30,6 +30,10 @@ public struct Requires1<A>: FamilyRequirementsManaging where A: Component {
let childCompA: A = nexus.get(unsafeComponentFor: childId)
return (parent: parentCompA, child: childCompA)
}
public static func createMember(nexus: Nexus, components: A) -> Entity {
nexus.createEntity(with: components)
}
}
extension Nexus {

View File

@ -15,6 +15,7 @@ public struct Requires2<A, B>: FamilyRequirementsManaging where A: Component, B:
public init(_ components: (A.Type, B.Type)) {
componentTypes = [ A.self, B.self]
}
public static func components(nexus: Nexus, entityId: EntityIdentifier) -> (A, B) {
let compA: A = nexus.get(unsafeComponentFor: entityId)
let compB: B = nexus.get(unsafeComponentFor: entityId)
@ -35,6 +36,10 @@ public struct Requires2<A, B>: FamilyRequirementsManaging where A: Component, B:
let ccB: B = nexus.get(unsafeComponentFor: childId)
return (parent: (pcA, pcB), child: (ccA, ccB))
}
public static func createMember(nexus: Nexus, components: (A, B)) -> Entity {
nexus.createEntity(with: components.0, components.1)
}
}
extension Nexus {

View File

@ -40,6 +40,10 @@ public struct Requires3<A, B, C>: FamilyRequirementsManaging where A: Component,
let ccC: C = nexus.get(unsafeComponentFor: childId)
return (parent: (pcA, pcB, pcC), child: (ccA, ccB, ccC))
}
public static func createMember(nexus: Nexus, components: (A, B, C)) -> Entity {
nexus.createEntity(with: components.0, components.1, components.2)
}
}
extension Nexus {

View File

@ -45,6 +45,10 @@ public struct Requires4<A, B, C, D>: FamilyRequirementsManaging where A: Compone
let ccD: D = nexus.get(unsafeComponentFor: childId)
return (parent: (pcA, pcB, pcC, pcD), child: (ccA, ccB, ccC, ccD))
}
public static func createMember(nexus: Nexus, components: (A, B, C, D)) -> Entity {
nexus.createEntity(with: components.0, components.1, components.2, components.3)
}
}
extension Nexus {

View File

@ -50,6 +50,10 @@ public struct Requires5<A, B, C, D, E>: FamilyRequirementsManaging where A: Comp
return (parent: (pcA, pcB, pcC, pcD, pcE),
child: (ccA, ccB, ccC, ccD, ccE))
}
public static func createMember(nexus: Nexus, components: (A, B, C, D, E)) -> Entity {
nexus.createEntity(with: components.0, components.1, components.2, components.3, components.4)
}
}
extension Nexus {

View File

@ -18,4 +18,6 @@ public protocol FamilyRequirementsManaging {
static func components(nexus: Nexus, entityId: EntityIdentifier) -> Components
static func entityAndComponents(nexus: Nexus, entityId: EntityIdentifier) -> EntityAndComponents
static func relativesDescending(nexus: Nexus, parentId: EntityIdentifier, childId: EntityIdentifier) -> RelativesDescending
static func createMember(nexus: Nexus, components: Components) -> Entity
}