Fix swiftlint warnings

This commit is contained in:
Christian Treffs 2019-09-30 20:50:12 +02:00
parent 28bad38ea9
commit 29cd66b947
14 changed files with 66 additions and 42 deletions

View File

@ -14,7 +14,8 @@ public struct ReadableOnly<Comp> where Comp: Component {
self.component = component
}
@inlinable public subscript<C>(dynamicMember keyPath: KeyPath<Comp, C>) -> C {
@inlinable
public subscript<C>(dynamicMember keyPath: KeyPath<Comp, C>) -> C {
return component[keyPath: keyPath]
}
}
@ -27,7 +28,8 @@ public struct Writable<Comp> where Comp: Component {
self.component = component
}
@inlinable public subscript<C>(dynamicMember keyPath: ReferenceWritableKeyPath<Comp, C>) -> C {
@inlinable
public subscript<C>(dynamicMember keyPath: ReferenceWritableKeyPath<Comp, C>) -> C {
nonmutating get { return component[keyPath: keyPath] }
nonmutating set { component[keyPath: keyPath] = newValue }
}

View File

@ -14,7 +14,7 @@ public protocol Component: class {
var identifier: ComponentIdentifier { get }
}
public extension Component {
static var identifier: ComponentIdentifier { return ComponentIdentifier(Self.self) }
@inlinable var identifier: ComponentIdentifier { return Self.identifier }
extension Component {
public static var identifier: ComponentIdentifier { return ComponentIdentifier(Self.self) }
@inlinable public var identifier: ComponentIdentifier { return Self.identifier }
}

View File

@ -5,19 +5,19 @@
// Created by Christian Treffs on 22.10.17.
//
public extension Entity {
extension Entity {
@inlinable
func get<C>() -> C? where C: Component {
public func get<C>() -> C? where C: Component {
return nexus.get(for: identifier)
}
@inlinable
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 {
return nexus.get(for: identifier)
}
@inlinable
func get<A, B>(components _: A.Type, _: B.Type) -> (A?, B?) where A: Component, B: Component {
public func get<A, B>(components _: A.Type, _: B.Type) -> (A?, B?) where A: Component, B: Component {
let compA: A? = get(component: A.self)
let compB: B? = get(component: B.self)
return (compA, compB)
@ -25,7 +25,7 @@ public extension Entity {
// swiftlint:disable large_tuple
@inlinable
func get<A, B, C>(components _: A.Type, _: B.Type, _: C.Type) -> (A?, B?, C?) where A: Component, B: Component, C: Component {
public func get<A, B, C>(components _: A.Type, _: B.Type, _: C.Type) -> (A?, B?, C?) where A: Component, B: Component, C: Component {
let compA: A? = get(component: A.self)
let compB: B? = get(component: B.self)
let compC: C? = get(component: C.self)

View File

@ -25,7 +25,8 @@ extension EntityIdentifier: Equatable { }
extension EntityIdentifier: Hashable { }
extension EntityIdentifier: Comparable {
@inlinable public static func < (lhs: EntityIdentifier, rhs: EntityIdentifier) -> Bool {
@inlinable
public static func < (lhs: EntityIdentifier, rhs: EntityIdentifier) -> Bool {
return lhs.index < rhs.index
}
}

View File

@ -19,6 +19,7 @@ public struct Requires2<A, B>: FamilyRequirementsManaging where A: Component, B:
return (compA, compB)
}
// swiftlint:disable:next large_tuple
public static func entityAndComponents(nexus: Nexus, entityId: EntityIdentifier) -> (Entity, A, B) {
let entity: Entity = nexus.get(unsafeEntity: entityId)
let compA: A = nexus.get(unsafeComponentFor: entityId)

View File

@ -5,10 +5,13 @@
// Created by Christian Treffs on 21.08.19.
//
// swiftlint:disable large_tuple
public typealias Family3<A: Component, B: Component, C: Component> = Family<Requires3<A, B, C>>
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

@ -5,10 +5,13 @@
// Created by Christian Treffs on 21.08.19.
//
// swiftlint:disable large_tuple
public typealias Family4<A: Component, B: Component, C: Component, D: Component> = Family<Requires4<A, B, C, D>>
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

@ -5,10 +5,13 @@
// Created by Christian Treffs on 21.08.19.
//
// swiftlint:disable large_tuple
public typealias Family5<A: Component, B: Component, C: Component, D: Component, E: Component> = Family<Requires5<A, B, C, D, E>>
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

@ -9,8 +9,11 @@ public protocol FamilyRequirementsManaging {
associatedtype Components
associatedtype ComponentTypes
associatedtype EntityAndComponents
init(_ types: ComponentTypes)
var componentTypes: [Component.Type] { get }
static func components(nexus: Nexus, entityId: EntityIdentifier) -> Components
static func entityAndComponents(nexus: Nexus, entityId: EntityIdentifier) -> EntityAndComponents
}

View File

@ -5,23 +5,23 @@
// Created by Christian Treffs on 13.10.17.
//
public extension Nexus {
final var numComponents: Int {
extension Nexus {
public final var numComponents: Int {
return componentsByType.reduce(0) { $0 + $1.value.count }
}
final func has(componentId: ComponentIdentifier, entityId: EntityIdentifier) -> Bool {
public final func has(componentId: ComponentIdentifier, entityId: EntityIdentifier) -> Bool {
guard let uniforms = componentsByType[componentId] else {
return false
}
return uniforms.contains(entityId.index)
}
final func count(components entityId: EntityIdentifier) -> Int {
public final func count(components entityId: EntityIdentifier) -> Int {
return componentIdsByEntity[entityId]?.count ?? 0
}
final func assign(component: Component, to entity: Entity) {
public final func assign(component: Component, to entity: Entity) {
let componentId: ComponentIdentifier = component.identifier
let entityId: EntityIdentifier = entity.identifier
@ -49,39 +49,44 @@ public extension Nexus {
delegate?.nexusEvent(ComponentAdded(component: componentId, toEntity: entity.identifier))
}
final func assign<C>(component: C, to entity: Entity) where C: Component {
public final func assign<C>(component: C, to entity: Entity) where C: Component {
assign(component: component, to: entity)
}
@inlinable final func get(component componentId: ComponentIdentifier, for entityId: EntityIdentifier) -> Component? {
@inlinable
public final func get(component componentId: ComponentIdentifier, for entityId: EntityIdentifier) -> Component? {
guard let uniformComponents = componentsByType[componentId] else {
return nil
}
return uniformComponents.get(at: entityId.index)
}
@inlinable final func get(unsafeComponent componentId: ComponentIdentifier, for entityId: EntityIdentifier) -> Component {
@inlinable
public final func get(unsafeComponent componentId: ComponentIdentifier, for entityId: EntityIdentifier) -> Component {
let uniformComponents = componentsByType[componentId].unsafelyUnwrapped
return uniformComponents.get(unsafeAt: entityId.index)
}
@inlinable final func get<C>(for entityId: EntityIdentifier) -> C? where C: Component {
@inlinable
public final func get<C>(for entityId: EntityIdentifier) -> C? where C: Component {
let componentId: ComponentIdentifier = C.identifier
return get(componentId: componentId, entityId: entityId)
}
@inlinable final func get<C>(unsafeComponentFor entityId: EntityIdentifier) -> C where C: Component {
@inlinable
public 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)
}
@inlinable final func get(components entityId: EntityIdentifier) -> Set<ComponentIdentifier>? {
@inlinable
public final func get(components entityId: EntityIdentifier) -> Set<ComponentIdentifier>? {
return componentIdsByEntity[entityId]
}
@discardableResult
final func remove(component componentId: ComponentIdentifier, from entityId: EntityIdentifier) -> Bool {
public final func remove(component componentId: ComponentIdentifier, from entityId: EntityIdentifier) -> Bool {
// delete component instance
componentsByType[componentId]?.remove(at: entityId.index)
// unasign component from entity
@ -94,7 +99,7 @@ public extension Nexus {
}
@discardableResult
final func removeAll(componentes entityId: EntityIdentifier) -> Bool {
public final func removeAll(componentes entityId: EntityIdentifier) -> Bool {
guard let allComponents = get(components: entityId) else {
delegate?.nexusNonFatalError("clearing components form entity \(entityId) with no components")
return false
@ -106,10 +111,9 @@ public extension Nexus {
}
return removedAll
}
}
extension Nexus {
@inlinable final func get<C>(componentId: ComponentIdentifier, entityId: EntityIdentifier) -> C? where C: Component {
@inlinable
public final func get<C>(componentId: ComponentIdentifier, entityId: EntityIdentifier) -> C? where C: Component {
guard let uniformComponents = componentsByType[componentId] else {
return nil
}

View File

@ -6,7 +6,8 @@
//
extension Nexus {
@inlinable internal func nextEntityId() -> EntityIdentifier {
@inlinable
internal func nextEntityId() -> EntityIdentifier {
guard let nextReused: EntityIdentifier = freeEntities.popLast() else {
return EntityIdentifier(UInt32(entityStorage.count))
}

View File

@ -5,12 +5,12 @@
// Created by Christian Treffs on 13.10.17.
//
public extension Nexus {
final var numFamilies: Int {
extension Nexus {
public final var numFamilies: Int {
return familyMembersByTraits.keys.count
}
func canBecomeMember(_ entity: Entity, in traits: FamilyTraitSet) -> Bool {
public func canBecomeMember(_ entity: Entity, in traits: FamilyTraitSet) -> Bool {
guard let componentIds = componentIdsByEntity[entity.identifier] else {
assertionFailure("no component set defined for entity: \(entity)")
return false
@ -18,19 +18,19 @@ public extension Nexus {
return traits.isMatch(components: componentIds)
}
func members(withFamilyTraits traits: FamilyTraitSet) -> UnorderedSparseSet<EntityIdentifier> {
public func members(withFamilyTraits traits: FamilyTraitSet) -> UnorderedSparseSet<EntityIdentifier> {
return familyMembersByTraits[traits] ?? UnorderedSparseSet<EntityIdentifier>()
}
func isMember(_ entity: Entity, in family: FamilyTraitSet) -> Bool {
public func isMember(_ entity: Entity, in family: FamilyTraitSet) -> Bool {
return isMember(entity.identifier, in: family)
}
func isMember(_ entityId: EntityIdentifier, in family: FamilyTraitSet) -> Bool {
public func isMember(_ entityId: EntityIdentifier, in family: FamilyTraitSet) -> Bool {
return isMember(entity: entityId, inFamilyWithTraits: family)
}
func isMember(entity entityId: EntityIdentifier, inFamilyWithTraits traits: FamilyTraitSet) -> Bool {
public func isMember(entity entityId: EntityIdentifier, inFamilyWithTraits traits: FamilyTraitSet) -> Bool {
return members(withFamilyTraits: traits).contains(entityId.index)
}
}

View File

@ -64,7 +64,8 @@ public final class Nexus {
// MARK: - Equatable
extension Nexus: Equatable {
@inlinable public static func == (lhs: Nexus, rhs: Nexus) -> Bool {
@inlinable
public static func == (lhs: Nexus, rhs: Nexus) -> Bool {
return lhs.entityStorage == rhs.entityStorage &&
lhs.componentIdsByEntity == rhs.componentIdsByEntity &&
lhs.freeEntities == rhs.freeEntities &&

View File

@ -9,8 +9,8 @@ public protocol SingleComponent: Component {
init()
}
public extension Nexus {
func single<S>(_ component: S.Type) -> Single<S> where S: SingleComponent {
extension Nexus {
public func single<S>(_ component: S.Type) -> Single<S> where S: SingleComponent {
let family = self.family(requires: S.self)
precondition(family.count <= 1, "Singleton count of \(S.self) must be 0 or 1: \(family.count)")
let entityId: EntityIdentifier
@ -23,21 +23,23 @@ public extension Nexus {
}
}
public struct Single<A>: Equatable where A: SingleComponent {
public struct Single<A> where A: SingleComponent {
public let nexus: Nexus
public let traits: FamilyTraitSet
public let entityId: EntityIdentifier
}
public extension Single where A: SingleComponent {
@inlinable var component: A {
extension Single: Equatable { }
extension Single where A: SingleComponent {
@inlinable public var component: A {
/// 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.
/// Since components will allways be of reference type (class) we may use unsafeDowncast here for performance reasons.
return nexus.get(unsafeComponentFor: entityId)
}
var entity: Entity {
public var entity: Entity {
return nexus.get(entity: entityId).unsafelyUnwrapped
}
}