diff --git a/Sources/FirebladeECS/Nexus+Component.swift b/Sources/FirebladeECS/Nexus+Component.swift index ecd985e..e784e52 100644 --- a/Sources/FirebladeECS/Nexus+Component.swift +++ b/Sources/FirebladeECS/Nexus+Component.swift @@ -103,7 +103,7 @@ extension Nexus { } // relocate remaining indices pointing in the componentsByEntity map - if let remainingComponents = componentIdsByEntity[entityId.index] { + if let remainingComponents: ComponentIdentifiers = componentIdsByEntity[entityId.index] { // FIXME: may be expensive but is cheap for small entities for (index, compId) in remainingComponents.enumerated() { let cHash: EntityComponentHash = compId.hashValue(using: entityId.index) @@ -115,13 +115,14 @@ extension Nexus { return true } - public func clear(componentes entityId: EntityIdentifier) { - - guard let componentIds = get(components: entityId) else { return } - - componentIds.forEach { (componentId: ComponentIdentifier) in - remove(component: componentId, from: entityId) + @discardableResult + public func clear(componentes entityId: EntityIdentifier) -> Bool { + guard let allComponents: ComponentIdentifiers = get(components: entityId) else { + report("clearing components form entity \(entityId) with no components") + return true } + let removedAll: Bool = allComponents.reduce(true, { $0 && remove(component: $1, from: entityId) }) + return removedAll } } diff --git a/Sources/FirebladeECS/Nexus+Entity.swift b/Sources/FirebladeECS/Nexus+Entity.swift index 423d5c9..bcd89d7 100644 --- a/Sources/FirebladeECS/Nexus+Entity.swift +++ b/Sources/FirebladeECS/Nexus+Entity.swift @@ -64,7 +64,8 @@ extension Nexus { return false } - clear(componentes: entityId) + let cleared: Bool = clear(componentes: entityId) + assert(cleared, "Could not clear all components form entity \(entityId)") entity.invalidate() diff --git a/Sources/FirebladeECS/Nexus.swift b/Sources/FirebladeECS/Nexus.swift index c741e34..fc74fdb 100644 --- a/Sources/FirebladeECS/Nexus.swift +++ b/Sources/FirebladeECS/Nexus.swift @@ -39,6 +39,7 @@ public class Nexus { /// - Value: each element is an index pointing to the component identifier per entity in the componentIdsByEntity map var componentIdsByEntityLookup: [EntityComponentHash: ComponentIdsByEntityIndex] + /// - Values: entity ids that are currently not used var freeEntities: ContiguousArray public init() { diff --git a/Tests/FirebladeECSTests/NexusTests.swift b/Tests/FirebladeECSTests/NexusTests.swift index eed6e0f..45421cb 100644 --- a/Tests/FirebladeECSTests/NexusTests.swift +++ b/Tests/FirebladeECSTests/NexusTests.swift @@ -5,6 +5,7 @@ // Created by Christian Treffs on 09.10.17. // +import Darwin.C.stdlib import XCTest @testable import FirebladeECS