Naming and optimizations
This commit is contained in:
parent
c4d213a4e2
commit
bd20408bf6
|
|
@ -5,7 +5,7 @@
|
||||||
// Created by Christian Treffs on 21.08.19.
|
// Created by Christian Treffs on 21.08.19.
|
||||||
//
|
//
|
||||||
|
|
||||||
public struct Family<R> where R: ComponentsProviding {
|
public struct Family<R> where R: FamilyRequirementsManaging {
|
||||||
@usableFromInline unowned let nexus: Nexus
|
@usableFromInline unowned let nexus: Nexus
|
||||||
public let traits: FamilyTraitSet
|
public let traits: FamilyTraitSet
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
//
|
//
|
||||||
// Components1.swift
|
// Family1.swift
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// Created by Christian Treffs on 21.08.19.
|
// Created by Christian Treffs on 21.08.19.
|
||||||
//
|
//
|
||||||
|
|
||||||
public typealias Family1<A: Component> = Family<Components1<A>>
|
public typealias Family1<A: Component> = Family<Requires1<A>>
|
||||||
|
|
||||||
public struct Components1<A>: ComponentsProviding where A: Component {
|
public struct Requires1<A>: FamilyRequirementsManaging where A: Component {
|
||||||
public let componentTypes: [Component.Type]
|
public let componentTypes: [Component.Type]
|
||||||
|
|
||||||
public init(_ components: (A.Type)) {
|
public init(_ components: (A.Type)) {
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
//
|
//
|
||||||
// Components2.swift
|
// Family2.swift
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// Created by Christian Treffs on 21.08.19.
|
// Created by Christian Treffs on 21.08.19.
|
||||||
//
|
//
|
||||||
|
|
||||||
public typealias Family2<A: Component, B: Component> = Family<Components2<A, B>>
|
public typealias Family2<A: Component, B: Component> = Family<Requires2<A, B>>
|
||||||
|
|
||||||
public struct Components2<A, B>: ComponentsProviding where A: Component, B: Component {
|
public struct Requires2<A, B>: FamilyRequirementsManaging where A: Component, B: Component {
|
||||||
public let componentTypes: [Component.Type]
|
public let componentTypes: [Component.Type]
|
||||||
|
|
||||||
public init(_ components: (A.Type, B.Type)) {
|
public init(_ components: (A.Type, B.Type)) {
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
//
|
//
|
||||||
// Components3.swift
|
// Family3.swift
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// Created by Christian Treffs on 21.08.19.
|
// Created by Christian Treffs on 21.08.19.
|
||||||
//
|
//
|
||||||
|
|
||||||
public typealias Family3<A: Component, B: Component, C: Component> = Family<Components3<A, B, C>>
|
public typealias Family3<A: Component, B: Component, C: Component> = Family<Requires3<A, B, C>>
|
||||||
|
|
||||||
public struct Components3<A, B, C>: ComponentsProviding where A: Component, B: Component, C: Component {
|
public struct Requires3<A, B, C>: FamilyRequirementsManaging where A: Component, B: Component, C: Component {
|
||||||
public let componentTypes: [Component.Type]
|
public let componentTypes: [Component.Type]
|
||||||
public init(_ types: (A.Type, B.Type, C.Type)) {
|
public init(_ types: (A.Type, B.Type, C.Type)) {
|
||||||
componentTypes = [A.self, B.self, C.self]
|
componentTypes = [A.self, B.self, C.self]
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
//
|
//
|
||||||
// Components4.swift
|
// Family4.swift
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// Created by Christian Treffs on 21.08.19.
|
// Created by Christian Treffs on 21.08.19.
|
||||||
//
|
//
|
||||||
|
|
||||||
public typealias Family4<A: Component, B: Component, C: Component, D: Component> = Family<Components4<A, B, C, D>>
|
public typealias Family4<A: Component, B: Component, C: Component, D: Component> = Family<Requires4<A, B, C, D>>
|
||||||
|
|
||||||
public struct Components4<A, B, C, D>: ComponentsProviding where A: Component, B: Component, C: Component, D: Component {
|
public struct Requires4<A, B, C, D>: FamilyRequirementsManaging where A: Component, B: Component, C: Component, D: Component {
|
||||||
public let componentTypes: [Component.Type]
|
public let componentTypes: [Component.Type]
|
||||||
public init(_ types: (A.Type, B.Type, C.Type, D.Type)) {
|
public init(_ types: (A.Type, B.Type, C.Type, D.Type)) {
|
||||||
componentTypes = [A.self, B.self, C.self, D.self]
|
componentTypes = [A.self, B.self, C.self, D.self]
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
//
|
//
|
||||||
// Components5.swift
|
// Family5.swift
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// Created by Christian Treffs on 21.08.19.
|
// Created by Christian Treffs on 21.08.19.
|
||||||
//
|
//
|
||||||
|
|
||||||
public typealias Family5<A: Component, B: Component, C: Component, D: Component, E: Component> = Family<Components5<A, B, C, D, E>>
|
public typealias Family5<A: Component, B: Component, C: Component, D: Component, E: Component> = Family<Requires5<A, B, C, D, E>>
|
||||||
|
|
||||||
public struct Components5<A, B, C, D, E>: ComponentsProviding where A: Component, B: Component, C: Component, D: Component, E: Component {
|
public struct Requires5<A, B, C, D, E>: FamilyRequirementsManaging where A: Component, B: Component, C: Component, D: Component, E: Component {
|
||||||
public let componentTypes: [Component.Type]
|
public let componentTypes: [Component.Type]
|
||||||
public init(_ types: (A.Type, B.Type, C.Type, D.Type, E.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]
|
componentTypes = [A.self, B.self, C.self, D.self, E.self]
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
//
|
//
|
||||||
// ComponentsProviding.swift
|
// FamilyRequirementsManaging.swift
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// Created by Christian Treffs on 21.08.19.
|
// Created by Christian Treffs on 21.08.19.
|
||||||
//
|
//
|
||||||
|
|
||||||
public protocol ComponentsProviding {
|
public protocol FamilyRequirementsManaging {
|
||||||
associatedtype Components
|
associatedtype Components
|
||||||
associatedtype ComponentTypes
|
associatedtype ComponentTypes
|
||||||
associatedtype EntityAndComponents
|
associatedtype EntityAndComponents
|
||||||
|
|
@ -27,7 +27,7 @@ public extension Nexus {
|
||||||
|
|
||||||
/// test if component is already assigned
|
/// test if component is already assigned
|
||||||
guard !has(componentId: componentId, entityId: entityId) else {
|
guard !has(componentId: componentId, entityId: entityId) else {
|
||||||
delegate?.nexusRecoverableErrorOccurred("ComponentAdd collision: \(entityId) already has a component \(component)")
|
delegate?.nexusNonFatalError("ComponentAdd collision: \(entityId) already has a component \(component)")
|
||||||
assertionFailure("ComponentAdd collision: \(entityId) already has a component \(component)")
|
assertionFailure("ComponentAdd collision: \(entityId) already has a component \(component)")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -46,37 +46,37 @@ public extension Nexus {
|
||||||
|
|
||||||
update(familyMembership: entityId)
|
update(familyMembership: entityId)
|
||||||
|
|
||||||
delegate?.nexusEventOccurred(ComponentAdded(component: componentId, toEntity: entity.identifier))
|
delegate?.nexusEvent(ComponentAdded(component: componentId, toEntity: entity.identifier))
|
||||||
}
|
}
|
||||||
|
|
||||||
final func assign<C>(component: C, to entity: Entity) where C: Component {
|
final func assign<C>(component: C, to entity: Entity) where C: Component {
|
||||||
assign(component: component, to: entity)
|
assign(component: component, to: entity)
|
||||||
}
|
}
|
||||||
|
|
||||||
final func get(component componentId: ComponentIdentifier, for entityId: EntityIdentifier) -> Component? {
|
@inlinable final func get(component componentId: ComponentIdentifier, for entityId: EntityIdentifier) -> Component? {
|
||||||
guard let uniformComponents = componentsByType[componentId] else {
|
guard let uniformComponents = componentsByType[componentId] else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return uniformComponents.get(at: entityId.index)
|
return uniformComponents.get(at: entityId.index)
|
||||||
}
|
}
|
||||||
|
|
||||||
final func get(unsafeComponent componentId: ComponentIdentifier, for entityId: EntityIdentifier) -> Component {
|
@inlinable final func get(unsafeComponent 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
final func get<C>(for entityId: EntityIdentifier) -> C? where C: Component {
|
@inlinable final func get<C>(for entityId: EntityIdentifier) -> C? where C: Component {
|
||||||
let componentId: ComponentIdentifier = C.identifier
|
let componentId: ComponentIdentifier = C.identifier
|
||||||
return get(componentId: componentId, entityId: entityId)
|
return get(componentId: componentId, entityId: entityId)
|
||||||
}
|
}
|
||||||
|
|
||||||
final func get<C>(unsafeComponentFor entityId: EntityIdentifier) -> C where C: Component {
|
@inlinable final func get<C>(unsafeComponentFor entityId: EntityIdentifier) -> C where C: Component {
|
||||||
let component: Component = get(unsafeComponent: C.identifier, for: entityId)
|
let component: Component = get(unsafeComponent: C.identifier, for: entityId)
|
||||||
/// components are guaranteed to be reference tyes so unsafeDowncast is applicable here
|
/// components are guaranteed to be reference tyes so unsafeDowncast is applicable here
|
||||||
return unsafeDowncast(component, to: C.self)
|
return unsafeDowncast(component, to: C.self)
|
||||||
}
|
}
|
||||||
|
|
||||||
final func get(components entityId: EntityIdentifier) -> Set<ComponentIdentifier>? {
|
@inlinable final func get(components entityId: EntityIdentifier) -> Set<ComponentIdentifier>? {
|
||||||
return componentIdsByEntity[entityId]
|
return componentIdsByEntity[entityId]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -89,14 +89,14 @@ public extension Nexus {
|
||||||
|
|
||||||
update(familyMembership: entityId)
|
update(familyMembership: entityId)
|
||||||
|
|
||||||
delegate?.nexusEventOccurred(ComponentRemoved(component: componentId, from: entityId))
|
delegate?.nexusEvent(ComponentRemoved(component: componentId, from: entityId))
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@discardableResult
|
@discardableResult
|
||||||
final func removeAll(componentes entityId: EntityIdentifier) -> Bool {
|
final func removeAll(componentes entityId: EntityIdentifier) -> Bool {
|
||||||
guard let allComponents = get(components: entityId) else {
|
guard let allComponents = get(components: entityId) else {
|
||||||
delegate?.nexusRecoverableErrorOccurred("clearing components form entity \(entityId) with no components")
|
delegate?.nexusNonFatalError("clearing components form entity \(entityId) with no components")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
var iter = allComponents.makeIterator()
|
var iter = allComponents.makeIterator()
|
||||||
|
|
@ -108,8 +108,8 @@ public extension Nexus {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private extension Nexus {
|
extension Nexus {
|
||||||
final func get<C>(componentId: ComponentIdentifier, entityId: EntityIdentifier) -> C? where C: Component {
|
@inlinable final func get<C>(componentId: ComponentIdentifier, entityId: EntityIdentifier) -> C? where C: Component {
|
||||||
guard let uniformComponents = componentsByType[componentId] else {
|
guard let uniformComponents = componentsByType[componentId] else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ extension Nexus {
|
||||||
let newEntityIdentifier: EntityIdentifier = nextEntityId()
|
let newEntityIdentifier: EntityIdentifier = nextEntityId()
|
||||||
let newEntity = Entity(nexus: self, id: newEntityIdentifier)
|
let newEntity = Entity(nexus: self, id: newEntityIdentifier)
|
||||||
entityStorage.insert(newEntity, at: newEntityIdentifier.index)
|
entityStorage.insert(newEntity, at: newEntityIdentifier.index)
|
||||||
delegate?.nexusEventOccurred(EntityCreated(entityId: newEntityIdentifier))
|
delegate?.nexusEvent(EntityCreated(entityId: newEntityIdentifier))
|
||||||
return newEntity
|
return newEntity
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -51,7 +51,7 @@ extension Nexus {
|
||||||
let entityId: EntityIdentifier = entity.identifier
|
let entityId: EntityIdentifier = entity.identifier
|
||||||
|
|
||||||
guard entityStorage.remove(at: entityId.index) != nil else {
|
guard entityStorage.remove(at: entityId.index) != nil else {
|
||||||
delegate?.nexusRecoverableErrorOccurred("EntityRemove failure: no entity \(entityId) to remove")
|
delegate?.nexusNonFatalError("EntityRemove failure: no entity \(entityId) to remove")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -61,7 +61,7 @@ extension Nexus {
|
||||||
|
|
||||||
freeEntities.append(entityId)
|
freeEntities.append(entityId)
|
||||||
|
|
||||||
delegate?.nexusEventOccurred(EntityDestroyed(entityId: entityId))
|
delegate?.nexusEvent(EntityDestroyed(entityId: entityId))
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -49,12 +49,12 @@ extension Nexus {
|
||||||
switch (isMatch, isMember) {
|
switch (isMatch, isMember) {
|
||||||
case (true, false):
|
case (true, false):
|
||||||
add(entityWithId: entityId, toFamilyWithTraits: traits)
|
add(entityWithId: entityId, toFamilyWithTraits: traits)
|
||||||
delegate?.nexusEventOccurred(FamilyMemberAdded(member: entityId, toFamily: traits))
|
delegate?.nexusEvent(FamilyMemberAdded(member: entityId, toFamily: traits))
|
||||||
return
|
return
|
||||||
|
|
||||||
case (false, true):
|
case (false, true):
|
||||||
remove(entityWithId: entityId, fromFamilyWithTraits: traits)
|
remove(entityWithId: entityId, fromFamilyWithTraits: traits)
|
||||||
delegate?.nexusEventOccurred(FamilyMemberRemoved(member: entityId, from: traits))
|
delegate?.nexusEvent(FamilyMemberRemoved(member: entityId, from: traits))
|
||||||
return
|
return
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,6 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
public protocol NexusEventDelegate: class {
|
public protocol NexusEventDelegate: class {
|
||||||
func nexusEventOccurred(_ event: ECSEvent)
|
func nexusEvent(_ event: NexusEvent)
|
||||||
func nexusRecoverableErrorOccurred(_ message: String)
|
func nexusNonFatalError(_ message: String)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,48 +1,48 @@
|
||||||
//
|
//
|
||||||
// Events.swift
|
// NexusEvents.swift
|
||||||
// FirebladeECS
|
// FirebladeECS
|
||||||
//
|
//
|
||||||
// Created by Christian Treffs on 08.10.17.
|
// Created by Christian Treffs on 08.10.17.
|
||||||
//
|
//
|
||||||
|
|
||||||
public protocol ECSEvent {}
|
public protocol NexusEvent {}
|
||||||
|
|
||||||
public struct EntityCreated: ECSEvent {
|
public struct EntityCreated: NexusEvent {
|
||||||
public let entityId: EntityIdentifier
|
public let entityId: EntityIdentifier
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct EntityDestroyed: ECSEvent {
|
public struct EntityDestroyed: NexusEvent {
|
||||||
public let entityId: EntityIdentifier
|
public let entityId: EntityIdentifier
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct ComponentAdded: ECSEvent {
|
public struct ComponentAdded: NexusEvent {
|
||||||
public let component: ComponentIdentifier
|
public let component: ComponentIdentifier
|
||||||
public let toEntity: EntityIdentifier
|
public let toEntity: EntityIdentifier
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct ComponentUpdated: ECSEvent {
|
public struct ComponentUpdated: NexusEvent {
|
||||||
public let atEnity: EntityIdentifier
|
public let atEnity: EntityIdentifier
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct ComponentRemoved: ECSEvent {
|
public struct ComponentRemoved: NexusEvent {
|
||||||
public let component: ComponentIdentifier
|
public let component: ComponentIdentifier
|
||||||
public let from: EntityIdentifier
|
public let from: EntityIdentifier
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct FamilyMemberAdded: ECSEvent {
|
public struct FamilyMemberAdded: NexusEvent {
|
||||||
public let member: EntityIdentifier
|
public let member: EntityIdentifier
|
||||||
public let toFamily: FamilyTraitSet
|
public let toFamily: FamilyTraitSet
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct FamilyMemberRemoved: ECSEvent {
|
public struct FamilyMemberRemoved: NexusEvent {
|
||||||
public let member: EntityIdentifier
|
public let member: EntityIdentifier
|
||||||
public let from: FamilyTraitSet
|
public let from: FamilyTraitSet
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct FamilyCreated: ECSEvent {
|
public struct FamilyCreated: NexusEvent {
|
||||||
public let family: FamilyTraitSet
|
public let family: FamilyTraitSet
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct FamilyDestroyed: ECSEvent {
|
public struct FamilyDestroyed: NexusEvent {
|
||||||
public let family: FamilyTraitSet
|
public let family: FamilyTraitSet
|
||||||
}
|
}
|
||||||
|
|
@ -46,7 +46,7 @@ class Color: Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
class ExampleSystem {
|
class ExampleSystem {
|
||||||
private let family: Family<Components2<Position, Velocity>>
|
private let family: Family2<Position, Velocity>
|
||||||
|
|
||||||
init(nexus: Nexus) {
|
init(nexus: Nexus) {
|
||||||
family = nexus.family(requiresAll: Position.self, Velocity.self, excludesAll: EmptyComponent.self)
|
family = nexus.family(requiresAll: Position.self, Velocity.self, excludesAll: EmptyComponent.self)
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,7 @@ final class SingleGameState: SingleComponent {
|
||||||
|
|
||||||
|
|
||||||
class ExampleSystem {
|
class ExampleSystem {
|
||||||
private let family: Family<Components2<Position, Velocity>>
|
private let family: Family2<Position, Velocity>
|
||||||
|
|
||||||
init(nexus: Nexus) {
|
init(nexus: Nexus) {
|
||||||
family = nexus.family(requiresAll: Position.self, Velocity.self, excludesAll: EmptyComponent.self)
|
family = nexus.family(requiresAll: Position.self, Velocity.self, excludesAll: EmptyComponent.self)
|
||||||
|
|
@ -92,7 +92,7 @@ class ColorSystem {
|
||||||
}
|
}
|
||||||
|
|
||||||
class PositionSystem {
|
class PositionSystem {
|
||||||
let positions: Family<Components1<Position>>
|
let positions: Family1<Position>
|
||||||
|
|
||||||
var velocity: Double = 4.0
|
var velocity: Double = 4.0
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue