Skip to content

Commit

Permalink
improved the hasComponent function
Browse files Browse the repository at this point in the history
  • Loading branch information
untoldengine committed Jan 15, 2025
1 parent 52a7ba7 commit 150d550
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
8 changes: 6 additions & 2 deletions Sources/UntoldEngine/ECS/Scenes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,12 @@ func queryEntitiesWithComponentIds(_ componentTypes: [Int], in scene: Scene) ->
}.map(\.entityId)
}

func hasComponent(entity: EntityDesc, componentType: (some Any).Type) -> Bool {
func hasComponent(entityId: EntityID, componentType: (some Any).Type) -> Bool {
let entityIndex: EntityIndex = getEntityIndex(entityId)

let entityMask = scene.entities[Int(entityIndex)].mask

let componentId = getComponentId(for: componentType)

return entity.mask.test(componentId)
return entityMask.test(componentId)
}
13 changes: 13 additions & 0 deletions Tests/UntoldEngineTests/ECSTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,17 @@ final class ECSTests: XCTestCase {
let newVersion = getEntityVersion(newEntityId)
XCTAssertEqual(newVersion, initialVersion + 1, "Entity version should be incremented")
}

func testHasComponent() {
let entityId = createEntity()

_ = scene.assign(to: entityId, component: RenderComponent.self)

XCTAssertTrue(hasComponent(entityId: entityId, componentType: RenderComponent.self), "Should have component")

// remove component
scene.remove(component: RenderComponent.self, from: entityId)

XCTAssertFalse(hasComponent(entityId: entityId, componentType: RenderComponent.self), "Should not have component")
}
}

0 comments on commit 150d550

Please sign in to comment.