Refine component getter and setter
This commit is contained in:
parent
bcf934ff7e
commit
8821648d7f
|
|
@ -8,12 +8,12 @@
|
||||||
extension Entity {
|
extension Entity {
|
||||||
@inlinable
|
@inlinable
|
||||||
public func get<C>() -> C? where C: Component {
|
public func get<C>() -> C? where C: Component {
|
||||||
nexus.get(for: identifier)
|
nexus.get(safe: identifier)
|
||||||
}
|
}
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
public func get<A>(component compType: A.Type = A.self) -> A? where A: Component {
|
public func get<A>(component compType: A.Type = A.self) -> A? where A: Component {
|
||||||
nexus.get(for: identifier)
|
nexus.get(safe: identifier)
|
||||||
}
|
}
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
|
|
@ -33,7 +33,7 @@ extension Entity {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get or set component instance by type via subscript.
|
/// Get or set component instance by type via subscript.
|
||||||
///
|
///
|
||||||
/// **Behavior:**
|
/// **Behavior:**
|
||||||
/// - If `Comp` is a component type that is currently *not* assigned to this entity,
|
/// - If `Comp` is a component type that is currently *not* assigned to this entity,
|
||||||
/// the new instance will be assigned to this entity.
|
/// the new instance will be assigned to this entity.
|
||||||
|
|
@ -57,7 +57,6 @@ extension Entity {
|
||||||
|
|
||||||
/// Get the value of a component using the key Path to the property in the component.
|
/// Get the value of a component using the key Path to the property in the component.
|
||||||
/// - Parameter componentKeyPath: The `KeyPath` to the property of the given component.
|
/// - Parameter componentKeyPath: The `KeyPath` to the property of the given component.
|
||||||
/// - Returns: If `Comp` is assigned to this entity the value at the given `KeyPath` is returned, nil otherwise.
|
|
||||||
@inlinable
|
@inlinable
|
||||||
public func get<Comp, Value>(valueAt componentKeyPath: KeyPath<Comp, Value>) -> Value where Comp: Component {
|
public func get<Comp, Value>(valueAt componentKeyPath: KeyPath<Comp, Value>) -> Value where Comp: Component {
|
||||||
self.get(component: Comp.self)![keyPath: componentKeyPath]
|
self.get(component: Comp.self)![keyPath: componentKeyPath]
|
||||||
|
|
@ -88,7 +87,7 @@ extension Entity {
|
||||||
return nexus.assign(component: newInstance, entityId: identifier)
|
return nexus.assign(component: newInstance, entityId: identifier)
|
||||||
}
|
}
|
||||||
|
|
||||||
get(component: Comp.self).unsafelyUnwrapped[keyPath: componentKeyPath] = newValue
|
get(component: Comp.self)![keyPath: componentKeyPath] = newValue
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -122,7 +122,7 @@ extension Entity {
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
init(nexus: Nexus, entityIdentifier: EntityIdentifier) {
|
init(nexus: Nexus, entityIdentifier: EntityIdentifier) {
|
||||||
if let comps = nexus.get(components: entityIdentifier) {
|
if let comps = nexus.get(components: entityIdentifier) {
|
||||||
iterator = AnyIterator<Component>(comps.compactMap { nexus.get(component: $0, for: entityIdentifier) }.makeIterator())
|
iterator = AnyIterator<Component>(comps.compactMap { nexus.get(unsafe: $0, for: entityIdentifier) }.makeIterator())
|
||||||
} else {
|
} else {
|
||||||
iterator = AnyIterator { nil }
|
iterator = AnyIterator { nil }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,13 +23,13 @@ public struct Requires1<Comp1>: FamilyRequirementsManaging where Comp1: Componen
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func components(nexus: Nexus, entityId: EntityIdentifier) -> (Comp1) {
|
public static func components(nexus: Nexus, entityId: EntityIdentifier) -> (Comp1) {
|
||||||
let comp1: Comp1 = nexus.get(unsafeComponentFor: entityId)
|
let comp1: Comp1 = nexus.get(unsafe: entityId)
|
||||||
return (comp1)
|
return (comp1)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func entityAndComponents(nexus: Nexus, entityId: EntityIdentifier) -> (Entity, Comp1) {
|
public static func entityAndComponents(nexus: Nexus, entityId: EntityIdentifier) -> (Entity, Comp1) {
|
||||||
let entity = Entity(nexus: nexus, id: entityId)
|
let entity = Entity(nexus: nexus, id: entityId)
|
||||||
let comp1: Comp1 = nexus.get(unsafeComponentFor: entityId)
|
let comp1: Comp1 = nexus.get(unsafe: entityId)
|
||||||
return (entity, comp1)
|
return (entity, comp1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -116,15 +116,15 @@ public struct Requires2<Comp1, Comp2>: FamilyRequirementsManaging where Comp1: C
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func components(nexus: Nexus, entityId: EntityIdentifier) -> (Comp1, Comp2) {
|
public static func components(nexus: Nexus, entityId: EntityIdentifier) -> (Comp1, Comp2) {
|
||||||
let comp1: Comp1 = nexus.get(unsafeComponentFor: entityId)
|
let comp1: Comp1 = nexus.get(unsafe: entityId)
|
||||||
let comp2: Comp2 = nexus.get(unsafeComponentFor: entityId)
|
let comp2: Comp2 = nexus.get(unsafe: entityId)
|
||||||
return (comp1, comp2)
|
return (comp1, comp2)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func entityAndComponents(nexus: Nexus, entityId: EntityIdentifier) -> (Entity, Comp1, Comp2) {
|
public static func entityAndComponents(nexus: Nexus, entityId: EntityIdentifier) -> (Entity, Comp1, Comp2) {
|
||||||
let entity = Entity(nexus: nexus, id: entityId)
|
let entity = Entity(nexus: nexus, id: entityId)
|
||||||
let comp1: Comp1 = nexus.get(unsafeComponentFor: entityId)
|
let comp1: Comp1 = nexus.get(unsafe: entityId)
|
||||||
let comp2: Comp2 = nexus.get(unsafeComponentFor: entityId)
|
let comp2: Comp2 = nexus.get(unsafe: entityId)
|
||||||
return (entity, comp1, comp2)
|
return (entity, comp1, comp2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -215,17 +215,17 @@ public struct Requires3<Comp1, Comp2, Comp3>: FamilyRequirementsManaging where C
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func components(nexus: Nexus, entityId: EntityIdentifier) -> (Comp1, Comp2, Comp3) {
|
public static func components(nexus: Nexus, entityId: EntityIdentifier) -> (Comp1, Comp2, Comp3) {
|
||||||
let comp1: Comp1 = nexus.get(unsafeComponentFor: entityId)
|
let comp1: Comp1 = nexus.get(unsafe: entityId)
|
||||||
let comp2: Comp2 = nexus.get(unsafeComponentFor: entityId)
|
let comp2: Comp2 = nexus.get(unsafe: entityId)
|
||||||
let comp3: Comp3 = nexus.get(unsafeComponentFor: entityId)
|
let comp3: Comp3 = nexus.get(unsafe: entityId)
|
||||||
return (comp1, comp2, comp3)
|
return (comp1, comp2, comp3)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func entityAndComponents(nexus: Nexus, entityId: EntityIdentifier) -> (Entity, Comp1, Comp2, Comp3) {
|
public static func entityAndComponents(nexus: Nexus, entityId: EntityIdentifier) -> (Entity, Comp1, Comp2, Comp3) {
|
||||||
let entity = Entity(nexus: nexus, id: entityId)
|
let entity = Entity(nexus: nexus, id: entityId)
|
||||||
let comp1: Comp1 = nexus.get(unsafeComponentFor: entityId)
|
let comp1: Comp1 = nexus.get(unsafe: entityId)
|
||||||
let comp2: Comp2 = nexus.get(unsafeComponentFor: entityId)
|
let comp2: Comp2 = nexus.get(unsafe: entityId)
|
||||||
let comp3: Comp3 = nexus.get(unsafeComponentFor: entityId)
|
let comp3: Comp3 = nexus.get(unsafe: entityId)
|
||||||
return (entity, comp1, comp2, comp3)
|
return (entity, comp1, comp2, comp3)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -320,19 +320,19 @@ public struct Requires4<Comp1, Comp2, Comp3, Comp4>: FamilyRequirementsManaging
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func components(nexus: Nexus, entityId: EntityIdentifier) -> (Comp1, Comp2, Comp3, Comp4) {
|
public static func components(nexus: Nexus, entityId: EntityIdentifier) -> (Comp1, Comp2, Comp3, Comp4) {
|
||||||
let comp1: Comp1 = nexus.get(unsafeComponentFor: entityId)
|
let comp1: Comp1 = nexus.get(unsafe: entityId)
|
||||||
let comp2: Comp2 = nexus.get(unsafeComponentFor: entityId)
|
let comp2: Comp2 = nexus.get(unsafe: entityId)
|
||||||
let comp3: Comp3 = nexus.get(unsafeComponentFor: entityId)
|
let comp3: Comp3 = nexus.get(unsafe: entityId)
|
||||||
let comp4: Comp4 = nexus.get(unsafeComponentFor: entityId)
|
let comp4: Comp4 = nexus.get(unsafe: entityId)
|
||||||
return (comp1, comp2, comp3, comp4)
|
return (comp1, comp2, comp3, comp4)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func entityAndComponents(nexus: Nexus, entityId: EntityIdentifier) -> (Entity, Comp1, Comp2, Comp3, Comp4) {
|
public static func entityAndComponents(nexus: Nexus, entityId: EntityIdentifier) -> (Entity, Comp1, Comp2, Comp3, Comp4) {
|
||||||
let entity = Entity(nexus: nexus, id: entityId)
|
let entity = Entity(nexus: nexus, id: entityId)
|
||||||
let comp1: Comp1 = nexus.get(unsafeComponentFor: entityId)
|
let comp1: Comp1 = nexus.get(unsafe: entityId)
|
||||||
let comp2: Comp2 = nexus.get(unsafeComponentFor: entityId)
|
let comp2: Comp2 = nexus.get(unsafe: entityId)
|
||||||
let comp3: Comp3 = nexus.get(unsafeComponentFor: entityId)
|
let comp3: Comp3 = nexus.get(unsafe: entityId)
|
||||||
let comp4: Comp4 = nexus.get(unsafeComponentFor: entityId)
|
let comp4: Comp4 = nexus.get(unsafe: entityId)
|
||||||
return (entity, comp1, comp2, comp3, comp4)
|
return (entity, comp1, comp2, comp3, comp4)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -431,21 +431,21 @@ public struct Requires5<Comp1, Comp2, Comp3, Comp4, Comp5>: FamilyRequirementsMa
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func components(nexus: Nexus, entityId: EntityIdentifier) -> (Comp1, Comp2, Comp3, Comp4, Comp5) {
|
public static func components(nexus: Nexus, entityId: EntityIdentifier) -> (Comp1, Comp2, Comp3, Comp4, Comp5) {
|
||||||
let comp1: Comp1 = nexus.get(unsafeComponentFor: entityId)
|
let comp1: Comp1 = nexus.get(unsafe: entityId)
|
||||||
let comp2: Comp2 = nexus.get(unsafeComponentFor: entityId)
|
let comp2: Comp2 = nexus.get(unsafe: entityId)
|
||||||
let comp3: Comp3 = nexus.get(unsafeComponentFor: entityId)
|
let comp3: Comp3 = nexus.get(unsafe: entityId)
|
||||||
let comp4: Comp4 = nexus.get(unsafeComponentFor: entityId)
|
let comp4: Comp4 = nexus.get(unsafe: entityId)
|
||||||
let comp5: Comp5 = nexus.get(unsafeComponentFor: entityId)
|
let comp5: Comp5 = nexus.get(unsafe: entityId)
|
||||||
return (comp1, comp2, comp3, comp4, comp5)
|
return (comp1, comp2, comp3, comp4, comp5)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func entityAndComponents(nexus: Nexus, entityId: EntityIdentifier) -> (Entity, Comp1, Comp2, Comp3, Comp4, Comp5) {
|
public static func entityAndComponents(nexus: Nexus, entityId: EntityIdentifier) -> (Entity, Comp1, Comp2, Comp3, Comp4, Comp5) {
|
||||||
let entity = Entity(nexus: nexus, id: entityId)
|
let entity = Entity(nexus: nexus, id: entityId)
|
||||||
let comp1: Comp1 = nexus.get(unsafeComponentFor: entityId)
|
let comp1: Comp1 = nexus.get(unsafe: entityId)
|
||||||
let comp2: Comp2 = nexus.get(unsafeComponentFor: entityId)
|
let comp2: Comp2 = nexus.get(unsafe: entityId)
|
||||||
let comp3: Comp3 = nexus.get(unsafeComponentFor: entityId)
|
let comp3: Comp3 = nexus.get(unsafe: entityId)
|
||||||
let comp4: Comp4 = nexus.get(unsafeComponentFor: entityId)
|
let comp4: Comp4 = nexus.get(unsafe: entityId)
|
||||||
let comp5: Comp5 = nexus.get(unsafeComponentFor: entityId)
|
let comp5: Comp5 = nexus.get(unsafe: entityId)
|
||||||
return (entity, comp1, comp2, comp3, comp4, comp5)
|
return (entity, comp1, comp2, comp3, comp4, comp5)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -548,23 +548,23 @@ public struct Requires6<Comp1, Comp2, Comp3, Comp4, Comp5, Comp6>: FamilyRequire
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func components(nexus: Nexus, entityId: EntityIdentifier) -> (Comp1, Comp2, Comp3, Comp4, Comp5, Comp6) {
|
public static func components(nexus: Nexus, entityId: EntityIdentifier) -> (Comp1, Comp2, Comp3, Comp4, Comp5, Comp6) {
|
||||||
let comp1: Comp1 = nexus.get(unsafeComponentFor: entityId)
|
let comp1: Comp1 = nexus.get(unsafe: entityId)
|
||||||
let comp2: Comp2 = nexus.get(unsafeComponentFor: entityId)
|
let comp2: Comp2 = nexus.get(unsafe: entityId)
|
||||||
let comp3: Comp3 = nexus.get(unsafeComponentFor: entityId)
|
let comp3: Comp3 = nexus.get(unsafe: entityId)
|
||||||
let comp4: Comp4 = nexus.get(unsafeComponentFor: entityId)
|
let comp4: Comp4 = nexus.get(unsafe: entityId)
|
||||||
let comp5: Comp5 = nexus.get(unsafeComponentFor: entityId)
|
let comp5: Comp5 = nexus.get(unsafe: entityId)
|
||||||
let comp6: Comp6 = nexus.get(unsafeComponentFor: entityId)
|
let comp6: Comp6 = nexus.get(unsafe: entityId)
|
||||||
return (comp1, comp2, comp3, comp4, comp5, comp6)
|
return (comp1, comp2, comp3, comp4, comp5, comp6)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func entityAndComponents(nexus: Nexus, entityId: EntityIdentifier) -> (Entity, Comp1, Comp2, Comp3, Comp4, Comp5, Comp6) {
|
public static func entityAndComponents(nexus: Nexus, entityId: EntityIdentifier) -> (Entity, Comp1, Comp2, Comp3, Comp4, Comp5, Comp6) {
|
||||||
let entity = Entity(nexus: nexus, id: entityId)
|
let entity = Entity(nexus: nexus, id: entityId)
|
||||||
let comp1: Comp1 = nexus.get(unsafeComponentFor: entityId)
|
let comp1: Comp1 = nexus.get(unsafe: entityId)
|
||||||
let comp2: Comp2 = nexus.get(unsafeComponentFor: entityId)
|
let comp2: Comp2 = nexus.get(unsafe: entityId)
|
||||||
let comp3: Comp3 = nexus.get(unsafeComponentFor: entityId)
|
let comp3: Comp3 = nexus.get(unsafe: entityId)
|
||||||
let comp4: Comp4 = nexus.get(unsafeComponentFor: entityId)
|
let comp4: Comp4 = nexus.get(unsafe: entityId)
|
||||||
let comp5: Comp5 = nexus.get(unsafeComponentFor: entityId)
|
let comp5: Comp5 = nexus.get(unsafe: entityId)
|
||||||
let comp6: Comp6 = nexus.get(unsafeComponentFor: entityId)
|
let comp6: Comp6 = nexus.get(unsafe: entityId)
|
||||||
return (entity, comp1, comp2, comp3, comp4, comp5, comp6)
|
return (entity, comp1, comp2, comp3, comp4, comp5, comp6)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -671,25 +671,25 @@ public struct Requires7<Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7>: Family
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func components(nexus: Nexus, entityId: EntityIdentifier) -> (Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7) {
|
public static func components(nexus: Nexus, entityId: EntityIdentifier) -> (Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7) {
|
||||||
let comp1: Comp1 = nexus.get(unsafeComponentFor: entityId)
|
let comp1: Comp1 = nexus.get(unsafe: entityId)
|
||||||
let comp2: Comp2 = nexus.get(unsafeComponentFor: entityId)
|
let comp2: Comp2 = nexus.get(unsafe: entityId)
|
||||||
let comp3: Comp3 = nexus.get(unsafeComponentFor: entityId)
|
let comp3: Comp3 = nexus.get(unsafe: entityId)
|
||||||
let comp4: Comp4 = nexus.get(unsafeComponentFor: entityId)
|
let comp4: Comp4 = nexus.get(unsafe: entityId)
|
||||||
let comp5: Comp5 = nexus.get(unsafeComponentFor: entityId)
|
let comp5: Comp5 = nexus.get(unsafe: entityId)
|
||||||
let comp6: Comp6 = nexus.get(unsafeComponentFor: entityId)
|
let comp6: Comp6 = nexus.get(unsafe: entityId)
|
||||||
let comp7: Comp7 = nexus.get(unsafeComponentFor: entityId)
|
let comp7: Comp7 = nexus.get(unsafe: entityId)
|
||||||
return (comp1, comp2, comp3, comp4, comp5, comp6, comp7)
|
return (comp1, comp2, comp3, comp4, comp5, comp6, comp7)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func entityAndComponents(nexus: Nexus, entityId: EntityIdentifier) -> (Entity, Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7) {
|
public static func entityAndComponents(nexus: Nexus, entityId: EntityIdentifier) -> (Entity, Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7) {
|
||||||
let entity = Entity(nexus: nexus, id: entityId)
|
let entity = Entity(nexus: nexus, id: entityId)
|
||||||
let comp1: Comp1 = nexus.get(unsafeComponentFor: entityId)
|
let comp1: Comp1 = nexus.get(unsafe: entityId)
|
||||||
let comp2: Comp2 = nexus.get(unsafeComponentFor: entityId)
|
let comp2: Comp2 = nexus.get(unsafe: entityId)
|
||||||
let comp3: Comp3 = nexus.get(unsafeComponentFor: entityId)
|
let comp3: Comp3 = nexus.get(unsafe: entityId)
|
||||||
let comp4: Comp4 = nexus.get(unsafeComponentFor: entityId)
|
let comp4: Comp4 = nexus.get(unsafe: entityId)
|
||||||
let comp5: Comp5 = nexus.get(unsafeComponentFor: entityId)
|
let comp5: Comp5 = nexus.get(unsafe: entityId)
|
||||||
let comp6: Comp6 = nexus.get(unsafeComponentFor: entityId)
|
let comp6: Comp6 = nexus.get(unsafe: entityId)
|
||||||
let comp7: Comp7 = nexus.get(unsafeComponentFor: entityId)
|
let comp7: Comp7 = nexus.get(unsafe: entityId)
|
||||||
return (entity, comp1, comp2, comp3, comp4, comp5, comp6, comp7)
|
return (entity, comp1, comp2, comp3, comp4, comp5, comp6, comp7)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -800,27 +800,27 @@ public struct Requires8<Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7, Comp8>:
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func components(nexus: Nexus, entityId: EntityIdentifier) -> (Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7, Comp8) {
|
public static func components(nexus: Nexus, entityId: EntityIdentifier) -> (Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7, Comp8) {
|
||||||
let comp1: Comp1 = nexus.get(unsafeComponentFor: entityId)
|
let comp1: Comp1 = nexus.get(unsafe: entityId)
|
||||||
let comp2: Comp2 = nexus.get(unsafeComponentFor: entityId)
|
let comp2: Comp2 = nexus.get(unsafe: entityId)
|
||||||
let comp3: Comp3 = nexus.get(unsafeComponentFor: entityId)
|
let comp3: Comp3 = nexus.get(unsafe: entityId)
|
||||||
let comp4: Comp4 = nexus.get(unsafeComponentFor: entityId)
|
let comp4: Comp4 = nexus.get(unsafe: entityId)
|
||||||
let comp5: Comp5 = nexus.get(unsafeComponentFor: entityId)
|
let comp5: Comp5 = nexus.get(unsafe: entityId)
|
||||||
let comp6: Comp6 = nexus.get(unsafeComponentFor: entityId)
|
let comp6: Comp6 = nexus.get(unsafe: entityId)
|
||||||
let comp7: Comp7 = nexus.get(unsafeComponentFor: entityId)
|
let comp7: Comp7 = nexus.get(unsafe: entityId)
|
||||||
let comp8: Comp8 = nexus.get(unsafeComponentFor: entityId)
|
let comp8: Comp8 = nexus.get(unsafe: entityId)
|
||||||
return (comp1, comp2, comp3, comp4, comp5, comp6, comp7, comp8)
|
return (comp1, comp2, comp3, comp4, comp5, comp6, comp7, comp8)
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func entityAndComponents(nexus: Nexus, entityId: EntityIdentifier) -> (Entity, Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7, Comp8) {
|
public static func entityAndComponents(nexus: Nexus, entityId: EntityIdentifier) -> (Entity, Comp1, Comp2, Comp3, Comp4, Comp5, Comp6, Comp7, Comp8) {
|
||||||
let entity = Entity(nexus: nexus, id: entityId)
|
let entity = Entity(nexus: nexus, id: entityId)
|
||||||
let comp1: Comp1 = nexus.get(unsafeComponentFor: entityId)
|
let comp1: Comp1 = nexus.get(unsafe: entityId)
|
||||||
let comp2: Comp2 = nexus.get(unsafeComponentFor: entityId)
|
let comp2: Comp2 = nexus.get(unsafe: entityId)
|
||||||
let comp3: Comp3 = nexus.get(unsafeComponentFor: entityId)
|
let comp3: Comp3 = nexus.get(unsafe: entityId)
|
||||||
let comp4: Comp4 = nexus.get(unsafeComponentFor: entityId)
|
let comp4: Comp4 = nexus.get(unsafe: entityId)
|
||||||
let comp5: Comp5 = nexus.get(unsafeComponentFor: entityId)
|
let comp5: Comp5 = nexus.get(unsafe: entityId)
|
||||||
let comp6: Comp6 = nexus.get(unsafeComponentFor: entityId)
|
let comp6: Comp6 = nexus.get(unsafe: entityId)
|
||||||
let comp7: Comp7 = nexus.get(unsafeComponentFor: entityId)
|
let comp7: Comp7 = nexus.get(unsafe: entityId)
|
||||||
let comp8: Comp8 = nexus.get(unsafeComponentFor: entityId)
|
let comp8: Comp8 = nexus.get(unsafe: entityId)
|
||||||
return (entity, comp1, comp2, comp3, comp4, comp5, comp6, comp7, comp8)
|
return (entity, comp1, comp2, comp3, comp4, comp5, comp6, comp7, comp8)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,33 +34,32 @@ extension Nexus {
|
||||||
}
|
}
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
public final func get(component componentId: ComponentIdentifier, for entityId: EntityIdentifier) -> Component? {
|
public final func get(safe componentId: ComponentIdentifier, for entityId: EntityIdentifier) -> Component? {
|
||||||
guard let uniformComponents = componentsByType[componentId], uniformComponents.contains(entityId.index) else {
|
guard let uniformComponents = componentsByType[componentId], uniformComponents.contains(entityId.index) else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return uniformComponents.get(at: entityId.index)
|
return uniformComponents.get(at: entityId.index)
|
||||||
}
|
}
|
||||||
|
|
||||||
@inlinable
|
|
||||||
public final func get<C>(componentId: ComponentIdentifier, entityId: EntityIdentifier) -> C? where C: Component {
|
|
||||||
get(component: componentId, for: entityId) as? C
|
|
||||||
}
|
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
public final func get(unsafeComponent componentId: ComponentIdentifier, for entityId: EntityIdentifier) -> Component {
|
public final func get(unsafe componentId: ComponentIdentifier, for entityId: EntityIdentifier) -> Component {
|
||||||
let uniformComponents = componentsByType[componentId].unsafelyUnwrapped
|
let uniformComponents = componentsByType[componentId].unsafelyUnwrapped
|
||||||
return uniformComponents.get(unsafeAt: entityId.index)
|
return uniformComponents.get(unsafeAt: entityId.index)
|
||||||
}
|
}
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
public final func get<C>(for entityId: EntityIdentifier) -> C? where C: Component {
|
public final func get<C>(safe componentId: ComponentIdentifier, for entityId: EntityIdentifier) -> C? where C: Component {
|
||||||
let componentId: ComponentIdentifier = C.identifier
|
get(safe: componentId, for: entityId) as? C
|
||||||
return get(componentId: componentId, entityId: entityId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
public final func get<C>(unsafeComponentFor entityId: EntityIdentifier) -> C where C: Component {
|
public final func get<C>(safe entityId: EntityIdentifier) -> C? where C: Component {
|
||||||
let component: Component = get(unsafeComponent: C.identifier, for: entityId)
|
get(safe: C.identifier, for: entityId)
|
||||||
|
}
|
||||||
|
|
||||||
|
@inlinable
|
||||||
|
public final func get<C>(unsafe entityId: EntityIdentifier) -> C where C: Component {
|
||||||
|
let component: Component = get(unsafe: C.identifier, for: entityId)
|
||||||
// components are guaranteed to be reference types so unsafeDowncast is applicable here
|
// components are guaranteed to be reference types so unsafeDowncast is applicable here
|
||||||
return unsafeDowncast(component, to: C.self)
|
return unsafeDowncast(component, to: C.self)
|
||||||
}
|
}
|
||||||
|
|
@ -96,6 +95,4 @@ extension Nexus {
|
||||||
}
|
}
|
||||||
return removedAll
|
return removedAll
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ extension Single where A: SingleComponent {
|
||||||
// Since we guarantee that the component will always be present by managing the complete lifecycle of the entity
|
// Since we guarantee that the component will always be present by managing the complete lifecycle of the entity
|
||||||
// and component assignment we may unsafelyUnwrap here.
|
// and component assignment we may unsafelyUnwrap here.
|
||||||
// Since components will always be of reference type (class) we may use unsafeDowncast here for performance reasons.
|
// Since components will always be of reference type (class) we may use unsafeDowncast here for performance reasons.
|
||||||
nexus.get(unsafeComponentFor: entityId)
|
nexus.get(unsafe: entityId)
|
||||||
}
|
}
|
||||||
|
|
||||||
public var entity: Entity {
|
public var entity: Entity {
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ public struct Requires{{ idx }}<{{ CompParams }}>: FamilyRequirementsManaging wh
|
||||||
|
|
||||||
public static func components(nexus: Nexus, entityId: EntityIdentifier) -> ({{ CompParams }}) {
|
public static func components(nexus: Nexus, entityId: EntityIdentifier) -> ({{ CompParams }}) {
|
||||||
{% for comp in components %}
|
{% for comp in components %}
|
||||||
let {{ comp|lowercase }}: {{ comp }} = nexus.get(unsafeComponentFor: entityId)
|
let {{ comp|lowercase }}: {{ comp }} = nexus.get(unsafe: entityId)
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
return ({{ CompsLowercased }})
|
return ({{ CompsLowercased }})
|
||||||
}
|
}
|
||||||
|
|
@ -54,7 +54,7 @@ public struct Requires{{ idx }}<{{ CompParams }}>: FamilyRequirementsManaging wh
|
||||||
public static func entityAndComponents(nexus: Nexus, entityId: EntityIdentifier) -> (Entity, {{ CompParams }}) {
|
public static func entityAndComponents(nexus: Nexus, entityId: EntityIdentifier) -> (Entity, {{ CompParams }}) {
|
||||||
let entity: Entity = Entity(nexus: nexus, id: entityId)
|
let entity: Entity = Entity(nexus: nexus, id: entityId)
|
||||||
{% for comp in components %}
|
{% for comp in components %}
|
||||||
let {{ comp|lowercase }}: {{ comp }} = nexus.get(unsafeComponentFor: entityId)
|
let {{ comp|lowercase }}: {{ comp }} = nexus.get(unsafe: entityId)
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
return (entity, {{ CompsLowercased }})
|
return (entity, {{ CompsLowercased }})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -107,7 +107,6 @@ class EntityTests: XCTestCase {
|
||||||
XCTAssertEqual(entity[Position.self]?.x, 1234)
|
XCTAssertEqual(entity[Position.self]?.x, 1234)
|
||||||
XCTAssertEqual(entity[Velocity.self]?.a, 123.0)
|
XCTAssertEqual(entity[Velocity.self]?.a, 123.0)
|
||||||
|
|
||||||
|
|
||||||
// remove position component
|
// remove position component
|
||||||
entity[Position.self] = nil
|
entity[Position.self] = nil
|
||||||
XCTAssertNil(entity[Position.self])
|
XCTAssertNil(entity[Position.self])
|
||||||
|
|
@ -117,7 +116,7 @@ class EntityTests: XCTestCase {
|
||||||
XCTAssertTrue(entity[Position.self] === pos)
|
XCTAssertTrue(entity[Position.self] === pos)
|
||||||
entity[Position.self] = nil // remove position component
|
entity[Position.self] = nil // remove position component
|
||||||
XCTAssertNil(entity[Position.self])
|
XCTAssertNil(entity[Position.self])
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue