From 9150e5a163f799c047f71f08a19b714f1959e6d1 Mon Sep 17 00:00:00 2001 From: Quillraven Date: Wed, 11 Dec 2024 07:47:10 +0100 Subject: [PATCH] add plusAssign/minusAssign of EntityBag to MutableEntityBag --- .../quillraven/fleks/collection/entityBag.kt | 14 +++++++++ .../fleks/collection/EntityBagTest.kt | 30 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/commonMain/kotlin/com/github/quillraven/fleks/collection/entityBag.kt b/src/commonMain/kotlin/com/github/quillraven/fleks/collection/entityBag.kt index 97b4c6c..43de639 100644 --- a/src/commonMain/kotlin/com/github/quillraven/fleks/collection/entityBag.kt +++ b/src/commonMain/kotlin/com/github/quillraven/fleks/collection/entityBag.kt @@ -463,6 +463,20 @@ class MutableEntityBag( } } + /** + * Adds all [entities] to the bag. If the [capacity] is not sufficient then a resize is happening. + */ + operator fun plusAssign(entities: EntityBag) { + entities.forEach { plusAssign(it) } + } + + /** + * Removes all [entities] of the bag. + */ + operator fun minusAssign(entities: EntityBag) { + entities.forEach { minusAssign(it) } + } + /** * Resets [size] to zero and clears any [entity][Entity] of the bag. */ diff --git a/src/commonTest/kotlin/com/github/quillraven/fleks/collection/EntityBagTest.kt b/src/commonTest/kotlin/com/github/quillraven/fleks/collection/EntityBagTest.kt index 676440a..5985cf1 100644 --- a/src/commonTest/kotlin/com/github/quillraven/fleks/collection/EntityBagTest.kt +++ b/src/commonTest/kotlin/com/github/quillraven/fleks/collection/EntityBagTest.kt @@ -648,4 +648,34 @@ class EntityBagTest { bag = mutableEntityBagOf() assertEquals(0, bag.size) } + + @Test + fun `test plusAssign of an EntityBag`() { + val bag = mutableEntityBagOf() + val toAdd = entityBagOf(testEntity1, testEntity2) + + bag += toAdd + + assertEquals(2, bag.size) + assertTrue { testEntity1 in bag } + assertTrue { testEntity2 in bag } + } + + @Test + fun `test minusAssign of an EntityBag`() { + val bag = mutableEntityBagOf() + val toRemove = entityBagOf(testEntity1, testEntity2) + + // remove of empty bag does nothing + bag -= toRemove + assertEquals(0, bag.size) + + bag += testEntity2 + bag += testEntity1 + assertEquals(2, bag.size) + bag -= toRemove + assertEquals(0, bag.size) + assertFalse { testEntity1 in bag } + assertFalse { testEntity2 in bag } + } }