diff --git a/Sources/FirebladeECS/Component.swift b/Sources/FirebladeECS/Component.swift index e383056..b97f6d9 100644 --- a/Sources/FirebladeECS/Component.swift +++ b/Sources/FirebladeECS/Component.swift @@ -5,22 +5,12 @@ // Created by Christian Treffs on 08.10.17. // -public protocol Component: class, TypeIdentifiable {} -public typealias ComponentIdentifier = ObjectIdentifier +public protocol Component: class { + static var identifier: ComponentIdentifier { get } + var identifier: ComponentIdentifier { get } +} public extension Component { - static var identifier: ComponentIdentifier { return Self.typeObjectIdentifier } - var identifier: ComponentIdentifier { return self.typeObjectIdentifier } -} - -/// TypeIdentifiable -/// Identifies an object by it's meta type. -public protocol TypeIdentifiable { - static var typeObjectIdentifier: ObjectIdentifier { get } - var typeObjectIdentifier: ObjectIdentifier { get } -} - -public extension TypeIdentifiable { - static var typeObjectIdentifier: ObjectIdentifier { return ObjectIdentifier(Self.self) } - var typeObjectIdentifier: ObjectIdentifier { return Self.typeObjectIdentifier } + static var identifier: ComponentIdentifier { return ComponentIdentifier(Self.self) } + @inlinable var identifier: ComponentIdentifier { return Self.identifier } } diff --git a/Sources/FirebladeECS/ComponentIdentifier.swift b/Sources/FirebladeECS/ComponentIdentifier.swift new file mode 100644 index 0000000..2c3b394 --- /dev/null +++ b/Sources/FirebladeECS/ComponentIdentifier.swift @@ -0,0 +1,18 @@ +// +// ComponentIdentifier.swift +// +// +// Created by Christian Treffs on 20.08.19. +// + +/// Identifies a component by it's meta type +public struct ComponentIdentifier { + @usableFromInline let objectIdentifier: ObjectIdentifier + + init(_ type: T.Type) where T: Component { + self.objectIdentifier = ObjectIdentifier(type) + } +} + +extension ComponentIdentifier: Equatable { } +extension ComponentIdentifier: Hashable { } diff --git a/Tests/FirebladeECSTests/ComponentTests.swift b/Tests/FirebladeECSTests/ComponentTests.swift index c9f6a41..9ee80ee 100644 --- a/Tests/FirebladeECSTests/ComponentTests.swift +++ b/Tests/FirebladeECSTests/ComponentTests.swift @@ -11,13 +11,16 @@ import XCTest class ComponentTests: XCTestCase { func testComponentIdentifier() { - let p1 = Position(x: 1, y: 2) - XCTAssert(p1.identifier == Position.identifier) - - let v1 = Velocity(a: 3.14) - XCTAssert(v1.identifier == Velocity.identifier) - XCTAssert(v1.identifier != p1.identifier) - XCTAssert(Velocity.identifier != Position.identifier) + XCTAssertEqual(Position.identifier, Position.identifier) + XCTAssertEqual(Velocity.identifier, Velocity.identifier) + XCTAssertNotEqual(Velocity.identifier, Position.identifier) + + let p1 = Position(x: 1, y: 2) + let v1 = Velocity(a: 3.14) + XCTAssertEqual(p1.identifier, Position.identifier) + XCTAssertEqual(v1.identifier, Velocity.identifier) + XCTAssertNotEqual(v1.identifier, p1.identifier) + }