Naming and optimizations

This commit is contained in:
Christian Treffs 2019-08-21 14:02:57 +02:00
parent c4d213a4e2
commit bd20408bf6
14 changed files with 50 additions and 50 deletions

View File

@ -5,7 +5,7 @@
// 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
public let traits: FamilyTraitSet

View File

@ -1,13 +1,13 @@
//
// Components1.swift
// Family1.swift
//
//
// 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 init(_ components: (A.Type)) {

View File

@ -1,13 +1,13 @@
//
// Components2.swift
// Family2.swift
//
//
// 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 init(_ components: (A.Type, B.Type)) {

View File

@ -1,13 +1,13 @@
//
// Components3.swift
// Family3.swift
//
//
// 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 init(_ types: (A.Type, B.Type, C.Type)) {
componentTypes = [A.self, B.self, C.self]

View File

@ -1,13 +1,13 @@
//
// Components4.swift
// Family4.swift
//
//
// 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 init(_ types: (A.Type, B.Type, C.Type, D.Type)) {
componentTypes = [A.self, B.self, C.self, D.self]

View File

@ -1,13 +1,13 @@
//
// Components5.swift
// Family5.swift
//
//
// 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 init(_ types: (A.Type, B.Type, C.Type, D.Type, E.Type)) {
componentTypes = [A.self, B.self, C.self, D.self, E.self]

View File

@ -1,11 +1,11 @@
//
// ComponentsProviding.swift
// FamilyRequirementsManaging.swift
//
//
// Created by Christian Treffs on 21.08.19.
//
public protocol ComponentsProviding {
public protocol FamilyRequirementsManaging {
associatedtype Components
associatedtype ComponentTypes
associatedtype EntityAndComponents

View File

@ -27,7 +27,7 @@ public extension Nexus {
/// test if component is already assigned
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)")
return
}
@ -46,37 +46,37 @@ public extension Nexus {
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 {
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 {
return nil
}
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
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
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)
/// components are guaranteed to be reference tyes so unsafeDowncast is applicable here
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]
}
@ -89,14 +89,14 @@ public extension Nexus {
update(familyMembership: entityId)
delegate?.nexusEventOccurred(ComponentRemoved(component: componentId, from: entityId))
delegate?.nexusEvent(ComponentRemoved(component: componentId, from: entityId))
return true
}
@discardableResult
final func removeAll(componentes entityId: EntityIdentifier) -> Bool {
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
}
var iter = allComponents.makeIterator()
@ -108,8 +108,8 @@ public extension Nexus {
}
}
private extension Nexus {
final func get<C>(componentId: ComponentIdentifier, entityId: EntityIdentifier) -> C? where C: Component {
extension Nexus {
@inlinable final func get<C>(componentId: ComponentIdentifier, entityId: EntityIdentifier) -> C? where C: Component {
guard let uniformComponents = componentsByType[componentId] else {
return nil
}

View File

@ -18,7 +18,7 @@ extension Nexus {
let newEntityIdentifier: EntityIdentifier = nextEntityId()
let newEntity = Entity(nexus: self, id: newEntityIdentifier)
entityStorage.insert(newEntity, at: newEntityIdentifier.index)
delegate?.nexusEventOccurred(EntityCreated(entityId: newEntityIdentifier))
delegate?.nexusEvent(EntityCreated(entityId: newEntityIdentifier))
return newEntity
}
@ -51,7 +51,7 @@ extension Nexus {
let entityId: EntityIdentifier = entity.identifier
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
}
@ -61,7 +61,7 @@ extension Nexus {
freeEntities.append(entityId)
delegate?.nexusEventOccurred(EntityDestroyed(entityId: entityId))
delegate?.nexusEvent(EntityDestroyed(entityId: entityId))
return true
}
}

View File

@ -49,12 +49,12 @@ extension Nexus {
switch (isMatch, isMember) {
case (true, false):
add(entityWithId: entityId, toFamilyWithTraits: traits)
delegate?.nexusEventOccurred(FamilyMemberAdded(member: entityId, toFamily: traits))
delegate?.nexusEvent(FamilyMemberAdded(member: entityId, toFamily: traits))
return
case (false, true):
remove(entityWithId: entityId, fromFamilyWithTraits: traits)
delegate?.nexusEventOccurred(FamilyMemberRemoved(member: entityId, from: traits))
delegate?.nexusEvent(FamilyMemberRemoved(member: entityId, from: traits))
return
default:

View File

@ -6,6 +6,6 @@
//
public protocol NexusEventDelegate: class {
func nexusEventOccurred(_ event: ECSEvent)
func nexusRecoverableErrorOccurred(_ message: String)
func nexusEvent(_ event: NexusEvent)
func nexusNonFatalError(_ message: String)
}

View File

@ -1,48 +1,48 @@
//
// Events.swift
// NexusEvents.swift
// FirebladeECS
//
// 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 struct EntityDestroyed: ECSEvent {
public struct EntityDestroyed: NexusEvent {
public let entityId: EntityIdentifier
}
public struct ComponentAdded: ECSEvent {
public struct ComponentAdded: NexusEvent {
public let component: ComponentIdentifier
public let toEntity: EntityIdentifier
}
public struct ComponentUpdated: ECSEvent {
public struct ComponentUpdated: NexusEvent {
public let atEnity: EntityIdentifier
}
public struct ComponentRemoved: ECSEvent {
public struct ComponentRemoved: NexusEvent {
public let component: ComponentIdentifier
public let from: EntityIdentifier
}
public struct FamilyMemberAdded: ECSEvent {
public struct FamilyMemberAdded: NexusEvent {
public let member: EntityIdentifier
public let toFamily: FamilyTraitSet
}
public struct FamilyMemberRemoved: ECSEvent {
public struct FamilyMemberRemoved: NexusEvent {
public let member: EntityIdentifier
public let from: FamilyTraitSet
}
public struct FamilyCreated: ECSEvent {
public struct FamilyCreated: NexusEvent {
public let family: FamilyTraitSet
}
public struct FamilyDestroyed: ECSEvent {
public struct FamilyDestroyed: NexusEvent {
public let family: FamilyTraitSet
}

View File

@ -46,7 +46,7 @@ class Color: Component {
}
class ExampleSystem {
private let family: Family<Components2<Position, Velocity>>
private let family: Family2<Position, Velocity>
init(nexus: Nexus) {
family = nexus.family(requiresAll: Position.self, Velocity.self, excludesAll: EmptyComponent.self)

View File

@ -54,7 +54,7 @@ final class SingleGameState: SingleComponent {
class ExampleSystem {
private let family: Family<Components2<Position, Velocity>>
private let family: Family2<Position, Velocity>
init(nexus: Nexus) {
family = nexus.family(requiresAll: Position.self, Velocity.self, excludesAll: EmptyComponent.self)
@ -92,7 +92,7 @@ class ColorSystem {
}
class PositionSystem {
let positions: Family<Components1<Position>>
let positions: Family1<Position>
var velocity: Double = 4.0