From 3c0ece24a50ea5ff4fad106773bd6e0d0034d3ba Mon Sep 17 00:00:00 2001 From: Quillraven Date: Fri, 30 Aug 2024 23:31:38 +0200 Subject: [PATCH] #153 add isMarkedForRemoval function --- .../kotlin/com/github/quillraven/fleks/entity.kt | 8 +++++++- .../kotlin/com/github/quillraven/fleks/EntityTest.kt | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/commonMain/kotlin/com/github/quillraven/fleks/entity.kt b/src/commonMain/kotlin/com/github/quillraven/fleks/entity.kt index d706aee..d59cf93 100644 --- a/src/commonMain/kotlin/com/github/quillraven/fleks/entity.kt +++ b/src/commonMain/kotlin/com/github/quillraven/fleks/entity.kt @@ -88,6 +88,12 @@ abstract class EntityComponentContext( * future calls to [World.entity]. */ fun Entity.remove() = componentService.world.minusAssign(this) + + /** + * Returns true, if and only if an [entity][Entity] will be removed at the end of the current [IteratingSystem]. + * This is the case, if it gets [removed][remove] during the system's iteration. + */ + fun Entity.isMarkedForRemoval() = this in componentService.world.entityService.delayedEntities } /** @@ -423,7 +429,7 @@ class EntityService( /** * The entities that get removed at the end of an [IteratingSystem] iteration. */ - private val delayedEntities = MutableEntityBag() + internal val delayedEntities = MutableEntityBag() /** * An optional [EntityHook] that gets called whenever an [entity][Entity] gets created and diff --git a/src/commonTest/kotlin/com/github/quillraven/fleks/EntityTest.kt b/src/commonTest/kotlin/com/github/quillraven/fleks/EntityTest.kt index 1560c43..92a0fc2 100644 --- a/src/commonTest/kotlin/com/github/quillraven/fleks/EntityTest.kt +++ b/src/commonTest/kotlin/com/github/quillraven/fleks/EntityTest.kt @@ -98,9 +98,11 @@ internal class EntityTest { val entity = testEntityService.create { } testEntityService.delayRemoval = true + with(testEntityService.world) { assertFalse(entity.isMarkedForRemoval()) } testEntityService -= entity assertEquals(1, testEntityService.numEntities) + with(testEntityService.world) { assertTrue(entity.isMarkedForRemoval()) } } @Test