Remove abandoned code

This commit is contained in:
Christian Treffs 2020-04-30 19:50:46 +02:00
parent 2dfe0162ce
commit e1ac76513c
No known key found for this signature in database
GPG Key ID: 49A4B4B460BE3ED4
6 changed files with 0 additions and 372 deletions

View File

@ -1,37 +0,0 @@
//
// Component+Access.swift
//
//
// Created by Christian Treffs on 25.06.19.
//
#if swift(>=5.1)
@dynamicMemberLookup
public struct ReadableOnly<Comp> where Comp: Component {
@usableFromInline let component: Comp
public init(_ component: Comp) {
self.component = component
}
@inlinable
public subscript<C>(dynamicMember keyPath: KeyPath<Comp, C>) -> C {
return component[keyPath: keyPath]
}
}
@dynamicMemberLookup
public struct Writable<Comp> where Comp: Component {
@usableFromInline let component: Comp
public init(_ component: Comp) {
self.component = component
}
@inlinable
public subscript<C>(dynamicMember keyPath: ReferenceWritableKeyPath<Comp, C>) -> C {
nonmutating get { return component[keyPath: keyPath] }
nonmutating set { component[keyPath: keyPath] = newValue }
}
}
#endif

View File

@ -1,34 +0,0 @@
//
// Identifiable.swift
//
//
// Created by Christian Treffs on 05.10.19.
//
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#if swift(<5.1)
/// A class of types whose instances hold the value of an entity with stable identity.
public protocol Identifiable {
/// A type representing the stable identity of the entity associated with `self`.
associatedtype ID: Hashable
/// The stable identity of the entity associated with `self`.
var id: ID { get }
}
extension Identifiable where Self: AnyObject {
public var id: ObjectIdentifier {
return ObjectIdentifier(self)
}
}
#endif

View File

@ -1,103 +0,0 @@
//
// ManagedContiguousArray.swift
// FirebladeECS
//
// Created by Christian Treffs on 28.10.17.
//
public struct ManagedContiguousArray<Element> {
public typealias Index = Int
@usableFromInline let chunkSize: Int
@usableFromInline var size: Int = 0
@usableFromInline var store: ContiguousArray<Element?> = []
public init(minCount: Int = 4096) {
chunkSize = minCount
store = ContiguousArray<Element?>(repeating: nil, count: minCount)
}
@inline(__always)
public var count: Int {
return size
}
@discardableResult
@inlinable
public mutating func insert(_ element: Element, at index: Int) -> Bool {
if needsToGrow(index) {
grow(to: index)
}
if store[index] == nil {
size += 1
}
store[index] = element
return true
}
@inlinable
public func contains(_ index: Index) -> Bool {
if store.count <= index {
return false
}
return store[index] != nil
}
@inline(__always)
public func get(at index: Index) -> Element? {
return store[index]
}
@inline(__always)
public func get(unsafeAt index: Index) -> Element {
return store[index].unsafelyUnwrapped
}
@discardableResult
@inlinable
public mutating func remove(at index: Index) -> Bool {
if store[index] != nil {
size -= 1
}
store[index] = nil
if size == 0 {
clear()
}
return true
}
@inlinable
public mutating func clear(keepingCapacity: Bool = false) {
size = 0
store.removeAll(keepingCapacity: keepingCapacity)
}
@inlinable
func needsToGrow(_ index: Index) -> Bool {
return index > store.count - 1
}
@inlinable
mutating func grow(to index: Index) {
let newCapacity: Int = calculateCapacity(to: index)
let newCount: Int = newCapacity - store.count
store += ContiguousArray<Element?>(repeating: nil, count: newCount)
}
@inlinable
func calculateCapacity(to index: Index) -> Int {
let delta = Float(index) / Float(chunkSize)
let multiplier = Int(delta.rounded(.up)) + 1
return multiplier * chunkSize
}
}
// MARK: - Equatable
extension ManagedContiguousArray: Equatable where Element: Equatable {
public static func == (lhs: ManagedContiguousArray<Element>, rhs: ManagedContiguousArray<Element>) -> Bool {
return lhs.store == rhs.store
}
}
// MARK: - Codable
extension ManagedContiguousArray: Codable where Element: Codable { }

View File

@ -1,107 +0,0 @@
//
// Nexus+Codable.swift
//
//
// Created by Christian Treffs on 05.10.19.
//
extension Nexus: Codable {
public enum CodingKeys: String, CodingKey {
case version
case entities
case freeEntities
case childrenByParent
case componentsByType
case familyMembersByTraits
case componentIdsByEntity
public enum Components: String, CodingKey {
case componentId
case components
}
public enum SparseSet: String, CodingKey {
case dense
case sparse
public enum Entry: String, CodingKey {
case key
case element
}
}
}
}
// MARK: - Encodable
extension Nexus {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(Nexus.version, forKey: .version)
try container.encode(entityStorage, forKey: .entities)
try container.encode(freeEntities, forKey: .freeEntities)
try container.encode(childrenByParentEntity, forKey: .childrenByParent)
try container.encode(familyMembersByTraits, forKey: .familyMembersByTraits)
try container.encode(componentIdsByEntity, forKey: .componentIdsByEntity)
// encode componentsByType
var contComponentsByType = container.nestedUnkeyedContainer(forKey: .componentsByType)
for (stableId, components) in componentsByType {
var contComponents = contComponentsByType.nestedContainer(keyedBy: CodingKeys.Components.self)
try contComponents.encode(stableId, forKey: .componentId)
var compSparseSet = contComponents.nestedContainer(keyedBy: CodingKeys.SparseSet.self, forKey: .components)
try compSparseSet.encode(components.sparse, forKey: .sparse)
var denseContainer = compSparseSet.nestedUnkeyedContainer(forKey: .dense)
try components.dense.forEach { (entry: UnorderedSparseSet<Component>.Entry) in
var entryContainer = denseContainer.nestedContainer(keyedBy: CodingKeys.SparseSet.Entry.self)
try entryContainer.encode(entry.key, forKey: .key)
try entry.element.encode(to: entryContainer.superEncoder(forKey: .element))
}
}
}
}
// MARK: - Decodable
extension Nexus {
public convenience init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let version = try container.decode(String.self, forKey: .version)
if version != Nexus.version {
throw Error.versionMismatch(required: Nexus.version, provided: version)
}
let entityStorage = try container.decode(UnorderedSparseSet<EntityIdentifier>.self, forKey: .entities)
let freeEntities = try container.decode([EntityIdentifier].self, forKey: .freeEntities)
let childrenByParentEntity = try container.decode([EntityIdentifier: Set<EntityIdentifier>].self, forKey: .childrenByParent)
let familyMembersByTraits = try container.decode([FamilyTraitSet: UnorderedSparseSet<EntityIdentifier>].self, forKey: .familyMembersByTraits)
let componentIdsByEntity = try container.decode([EntityIdentifier: Set<ComponentIdentifier>].self, forKey: .componentIdsByEntity)
// decode componentsByType
var contComponentsByType = try container.nestedUnkeyedContainer(forKey: .componentsByType)
var componentsByType = [ComponentIdentifier: UnorderedSparseSet<Component>]()
for _ in 0..<(contComponentsByType.count ?? 0) {
let contComponents = try contComponentsByType.nestedContainer(keyedBy: CodingKeys.Components.self)
let stableId = try contComponents.decode(ComponentIdentifier.self, forKey: .componentId)
let compSparseSet = try contComponents.nestedContainer(keyedBy: CodingKeys.SparseSet.self, forKey: .components)
let sparse = try compSparseSet.decode([Int: Int].self, forKey: .sparse)
var denseContainer = try compSparseSet.nestedUnkeyedContainer(forKey: .dense)
var dense = ContiguousArray<UnorderedSparseSet<Component>.Entry>()
for _ in 0..<(denseContainer.count ?? 0) {
let entryContainer = try denseContainer.nestedContainer(keyedBy: CodingKeys.SparseSet.Entry.self)
let key = try entryContainer.decode(UnorderedSparseSet<Component>.Key.self, forKey: .key)
let element: Component = try Nexus.componentDecoderMap[stableId]!(entryContainer.superDecoder(forKey: .element))
dense.append(UnorderedSparseSet<Component>.Entry(key: key, element: element))
}
componentsByType[stableId] = UnorderedSparseSet<Component>(sparse: sparse, dense: dense)
}
self.init(entityStorage: entityStorage,
componentsByType: componentsByType,
componentsByEntity: componentIdsByEntity,
freeEntities: freeEntities,
familyMembersByTraits: familyMembersByTraits,
childrenByParentEntity: childrenByParentEntity)
}
}

View File

@ -1,41 +0,0 @@
//
// AccessTests.swift
//
//
// Created by Christian Treffs on 25.06.19.
//
import FirebladeECS
import XCTest
#if swift(>=5.1)
class AccessTests: XCTestCase {
func testReadOnly() {
let pos = Position(x: 1, y: 2)
let readable = ReadableOnly<Position>(pos)
XCTAssertEqual(readable.x, 1)
XCTAssertEqual(readable.y, 2)
// readable.x = 3 // does not work and that's correct!
}
func testWrite() {
let pos = Position(x: 1, y: 2)
let writable = Writable<Position>(pos)
XCTAssertEqual(writable.x, 1)
XCTAssertEqual(writable.y, 2)
writable.x = 3
XCTAssertEqual(writable.x, 3)
XCTAssertEqual(pos.x, 3)
XCTAssertEqual(writable.y, 2)
XCTAssertEqual(pos.y, 2)
}
}
#endif

View File

@ -1,50 +0,0 @@
//
// NexusCodingTests.swift
// FirebladeECSTests
//
// Created by Christian Treffs on 05.10.19.
//
import XCTest
import FirebladeECS
class NexusCodingTests: XCTestCase {
var nexus: Nexus!
override func setUp() {
super.setUp()
nexus = Nexus()
let e0 = nexus.createEntity(with: Position(x: 1, y: 2), Name(name: "Entity0"))
let e1 = nexus.createEntity(with: Position(x: 1, y: 2), Name(name: "Entity1"))
let e2 = nexus.createEntity(with: Velocity(a: 2.34), Name(name: "Entity1"))
nexus.destroy(entity: e0)
let e3 = nexus.createEntity(with: Velocity(a: 5.67), Name(name: "Entity3"))
e1.addChild(e2)
e1.addChild(e3)
_ = nexus.family(requiresAll: Position.self, Name.self, excludesAll: EmptyComponent.self)
}
override func tearDown() {
super.tearDown()
nexus = nil
}
func testEncodeDecodeNexusJSON() {
let encoder = JSONEncoder()
let decoder = JSONDecoder()
var data: Data!
XCTAssertNoThrow(data = try encoder.encode(nexus))
XCTAssertGreaterThan(data.count, 0)
var nexus2: Nexus!
XCTAssertNoThrow(nexus2 = try decoder.decode(Nexus.self, from: data))
XCTAssertEqual(nexus2, nexus)
var data2: Data!
XCTAssertNoThrow(data2 = try encoder.encode(nexus2))
XCTAssertGreaterThan(data2.count, 0)
}
}