Cleanups
This commit is contained in:
parent
4e15ef217e
commit
f7d2a5b3a2
|
|
@ -9,8 +9,8 @@ public protocol Component: class, TypeIdentifiable {}
|
|||
public typealias ComponentIdentifier = ObjectIdentifier
|
||||
|
||||
public extension Component {
|
||||
var identifier: ComponentIdentifier { return typeObjectIdentifier }
|
||||
static var identifier: ComponentIdentifier { return typeObjectIdentifier }
|
||||
static var identifier: ComponentIdentifier { return Self.typeObjectIdentifier }
|
||||
var identifier: ComponentIdentifier { return self.typeObjectIdentifier }
|
||||
}
|
||||
|
||||
/// TypeIdentifiable
|
||||
|
|
@ -22,6 +22,5 @@ public protocol TypeIdentifiable {
|
|||
|
||||
public extension TypeIdentifiable {
|
||||
static var typeObjectIdentifier: ObjectIdentifier { return ObjectIdentifier(Self.self) }
|
||||
|
||||
var typeObjectIdentifier: ObjectIdentifier { return Self.typeObjectIdentifier }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,45 +22,45 @@ public typealias TraitEntityIdHashSet = [TraitEntityIdHash: EntityIdInFamilyInde
|
|||
public typealias SparseComponentIdentifierSet = UnorderedSparseSet<ComponentIdentifier>
|
||||
|
||||
public protocol NexusDelegate: class {
|
||||
func nexusEventOccurred(_ event: ECSEvent)
|
||||
func nexusRecoverableErrorOccurred(_ message: String)
|
||||
func nexusEventOccurred(_ event: ECSEvent)
|
||||
func nexusRecoverableErrorOccurred(_ message: String)
|
||||
}
|
||||
|
||||
public class Nexus: Equatable {
|
||||
public weak var delegate: NexusDelegate?
|
||||
public weak var delegate: NexusDelegate?
|
||||
|
||||
/// - Index: index value matching entity identifier shifted to Int
|
||||
/// - Value: each element is a entity instance
|
||||
internal var entityStorage: Entities
|
||||
/// - Index: index value matching entity identifier shifted to Int
|
||||
/// - Value: each element is a entity instance
|
||||
internal var entityStorage: Entities
|
||||
|
||||
/// - Key: component type identifier
|
||||
/// - Value: each element is a component instance of the same type (uniform). New component instances are appended.
|
||||
internal var componentsByType: [ComponentIdentifier: UniformComponents]
|
||||
/// - Key: component type identifier
|
||||
/// - Value: each element is a component instance of the same type (uniform). New component instances are appended.
|
||||
internal var componentsByType: [ComponentIdentifier: UniformComponents]
|
||||
|
||||
/// - Key: entity id as index
|
||||
/// - Value: each element is a component identifier associated with this entity
|
||||
internal var componentIdsByEntity: [EntityIndex: SparseComponentIdentifierSet]
|
||||
/// - Key: entity id as index
|
||||
/// - Value: each element is a component identifier associated with this entity
|
||||
internal var componentIdsByEntity: [EntityIndex: SparseComponentIdentifierSet]
|
||||
|
||||
/// - Values: entity ids that are currently not used
|
||||
internal var freeEntities: ContiguousArray<EntityIdentifier>
|
||||
/// - Values: entity ids that are currently not used
|
||||
internal var freeEntities: ContiguousArray<EntityIdentifier>
|
||||
|
||||
//var familiesByTraitHash: [FamilyTraitSetHash: Family]
|
||||
internal var familyMembersByTraits: [FamilyTraitSet: UniformEntityIdentifiers]
|
||||
//var familiesByTraitHash: [FamilyTraitSetHash: Family]
|
||||
internal var familyMembersByTraits: [FamilyTraitSet: UniformEntityIdentifiers]
|
||||
|
||||
public init() {
|
||||
entityStorage = Entities()
|
||||
componentsByType = [:]
|
||||
componentIdsByEntity = [:]
|
||||
freeEntities = ContiguousArray<EntityIdentifier>()
|
||||
familyMembersByTraits = [:]
|
||||
}
|
||||
public init() {
|
||||
entityStorage = Entities()
|
||||
componentsByType = [:]
|
||||
componentIdsByEntity = [:]
|
||||
freeEntities = ContiguousArray<EntityIdentifier>()
|
||||
familyMembersByTraits = [:]
|
||||
}
|
||||
|
||||
public final func clear() {
|
||||
for entity: Entity in entityStorage {
|
||||
destroy(entity: entity)
|
||||
}
|
||||
|
||||
entityStorage.clear()
|
||||
entityStorage.removeAll()
|
||||
freeEntities.removeAll()
|
||||
|
||||
assert(entityStorage.isEmpty)
|
||||
|
|
@ -74,28 +74,28 @@ public class Nexus: Equatable {
|
|||
familyMembersByTraits.removeAll()
|
||||
}
|
||||
|
||||
deinit {
|
||||
deinit {
|
||||
clear()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Equatable
|
||||
public static func == (lhs: Nexus, rhs: Nexus) -> Bool {
|
||||
return lhs.entityStorage == rhs.entityStorage &&
|
||||
lhs.componentIdsByEntity == rhs.componentIdsByEntity &&
|
||||
lhs.freeEntities == rhs.freeEntities &&
|
||||
lhs.familyMembersByTraits == rhs.familyMembersByTraits &&
|
||||
lhs.componentsByType.count == rhs.componentsByType.count
|
||||
// TODO: components are not equatable yet
|
||||
lhs.componentIdsByEntity == rhs.componentIdsByEntity &&
|
||||
lhs.freeEntities == rhs.freeEntities &&
|
||||
lhs.familyMembersByTraits == rhs.familyMembersByTraits &&
|
||||
lhs.componentsByType.keys == rhs.componentsByType.keys
|
||||
// NOTE: components are not equatable (yet)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - nexus delegate
|
||||
extension Nexus {
|
||||
internal func notify(_ event: ECSEvent) {
|
||||
delegate?.nexusEventOccurred(event)
|
||||
}
|
||||
internal func notify(_ event: ECSEvent) {
|
||||
delegate?.nexusEventOccurred(event)
|
||||
}
|
||||
|
||||
internal func report(_ message: String) {
|
||||
delegate?.nexusRecoverableErrorOccurred(message)
|
||||
}
|
||||
internal func report(_ message: String) {
|
||||
delegate?.nexusRecoverableErrorOccurred(message)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
// Created by Christian Treffs on 30.10.17.
|
||||
//
|
||||
|
||||
open class UnorderedSparseSet<Element> {
|
||||
open class UnorderedSparseSet<Element>: MutableCollection, RandomAccessCollection {
|
||||
public typealias Index = Int
|
||||
public typealias Key = Int
|
||||
|
||||
|
|
@ -23,7 +23,7 @@ open class UnorderedSparseSet<Element> {
|
|||
}
|
||||
|
||||
deinit {
|
||||
clear()
|
||||
removeAll()
|
||||
}
|
||||
|
||||
public var count: Int { return dense.count }
|
||||
|
|
@ -92,11 +92,13 @@ open class UnorderedSparseSet<Element> {
|
|||
return removed
|
||||
}
|
||||
|
||||
public func clear(keepingCapacity: Bool = false) {
|
||||
@inlinable
|
||||
public func removeAll(keepingCapacity: Bool = false) {
|
||||
sparse.removeAll(keepingCapacity: keepingCapacity)
|
||||
dense.removeAll(keepingCapacity: keepingCapacity)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func makeIterator() -> UnorderedSparseSetIterator<Element> {
|
||||
return UnorderedSparseSetIterator<Element>(self)
|
||||
}
|
||||
|
|
@ -123,6 +125,42 @@ open class UnorderedSparseSet<Element> {
|
|||
|
||||
return (denseIndex, entry.element)
|
||||
}
|
||||
|
||||
// MARK: Collection
|
||||
@inlinable
|
||||
public func index(after index: Index) -> Int {
|
||||
return dense.index(after: index)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public subscript(position: Index) -> Element {
|
||||
get {
|
||||
guard let element: Element = get(at: position) else {
|
||||
fatalError("no element at index \(position)")
|
||||
}
|
||||
return element
|
||||
}
|
||||
|
||||
set(newValue) {
|
||||
insert(newValue, at: position)
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable public var startIndex: Index {
|
||||
return dense.startIndex
|
||||
}
|
||||
|
||||
@inlinable public var endIndex: Index {
|
||||
return dense.endIndex
|
||||
}
|
||||
}
|
||||
|
||||
extension UnorderedSparseSet.Entry: Equatable where UnorderedSparseSet.Element: Equatable { }
|
||||
|
||||
extension UnorderedSparseSet: Equatable where UnorderedSparseSet.Element: Equatable {
|
||||
public static func == (lhs: UnorderedSparseSet<Element>, rhs: UnorderedSparseSet<Element>) -> Bool {
|
||||
return lhs.dense == rhs.dense && lhs.sparse == rhs.sparse
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - UnorderedSparseSetIterator
|
||||
|
|
@ -137,44 +175,3 @@ public struct UnorderedSparseSetIterator<Element>: IteratorProtocol {
|
|||
return iterator.next()?.element
|
||||
}
|
||||
}
|
||||
|
||||
extension UnorderedSparseSet: MutableCollection, RandomAccessCollection {
|
||||
public func index(after index: Index) -> Int {
|
||||
return dense.index(after: index)
|
||||
}
|
||||
|
||||
public subscript(position: Index) -> Element {
|
||||
get {
|
||||
guard let element: Element = get(at: position) else {
|
||||
fatalError("no element at index \(position)")
|
||||
}
|
||||
return element
|
||||
}
|
||||
|
||||
set(newValue) {
|
||||
insert(newValue, at: position)
|
||||
}
|
||||
}
|
||||
|
||||
public var startIndex: Index {
|
||||
return dense.startIndex
|
||||
}
|
||||
|
||||
public var endIndex: Index {
|
||||
return dense.endIndex
|
||||
}
|
||||
}
|
||||
|
||||
extension UnorderedSparseSet.Entry: Equatable where UnorderedSparseSet.Element: Equatable {
|
||||
public static func == (lhs: UnorderedSparseSet.Entry, rhs: UnorderedSparseSet.Entry) -> Bool {
|
||||
return lhs.element == rhs.element && lhs.key == rhs.key
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Equatable
|
||||
extension UnorderedSparseSet: Equatable where UnorderedSparseSet.Element: Equatable {
|
||||
public static func == (lhs: UnorderedSparseSet<Element>, rhs: UnorderedSparseSet<Element>) -> Bool {
|
||||
return lhs.dense == rhs.dense &&
|
||||
lhs.sparse == rhs.sparse
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
// Created by Christian Treffs on 14.02.19.
|
||||
//
|
||||
|
||||
@testable import FirebladeECS
|
||||
import FirebladeECS
|
||||
import XCTest
|
||||
|
||||
class ComponentTests: XCTestCase {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
// Created by Christian Treffs on 21.10.17.
|
||||
//
|
||||
|
||||
@testable import FirebladeECS
|
||||
import FirebladeECS
|
||||
import XCTest
|
||||
|
||||
class ComponentTests: XCTestCase {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
// Created by Christian Treffs on 09.05.18.
|
||||
//
|
||||
|
||||
@testable import FirebladeECS
|
||||
import FirebladeECS
|
||||
import XCTest
|
||||
|
||||
class FamilyTraitsTests: XCTestCase {
|
||||
|
|
|
|||
|
|
@ -476,7 +476,7 @@ class SparseSetTests: XCTestCase {
|
|||
XCTAssertEqual(set.count, num)
|
||||
XCTAssertFalse(set.isEmpty)
|
||||
|
||||
set.clear()
|
||||
set.removeAll()
|
||||
|
||||
XCTAssertEqual(set.count, 0)
|
||||
XCTAssertTrue(set.isEmpty)
|
||||
|
|
|
|||
Loading…
Reference in New Issue