From ce471b072276ce3e4c6dcf9beb9c876d522cb7de Mon Sep 17 00:00:00 2001 From: Quillraven Date: Thu, 19 Dec 2024 16:53:04 +0100 Subject: [PATCH] add plusAssign(family) to MutableEntityBag --- .../quillraven/fleks/collection/entityBag.kt | 8 ++++++++ .../fleks/collection/EntityBagTest.kt | 17 +++++++++++++++++ 2 files changed, 25 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 43de639..da50d3d 100644 --- a/src/commonMain/kotlin/com/github/quillraven/fleks/collection/entityBag.kt +++ b/src/commonMain/kotlin/com/github/quillraven/fleks/collection/entityBag.kt @@ -3,6 +3,7 @@ package com.github.quillraven.fleks.collection import com.github.quillraven.fleks.Entity +import com.github.quillraven.fleks.Family import kotlin.math.max import kotlin.math.min import kotlin.random.Random @@ -470,6 +471,13 @@ class MutableEntityBag( entities.forEach { plusAssign(it) } } + /** + * Adds all entities of the [family] to the bag. If the [capacity] is not sufficient then a resize is happening. + */ + operator fun plusAssign(family: Family) { + family.entities.forEach { plusAssign(it) } + } + /** * Removes all [entities] 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 5985cf1..53a56e7 100644 --- a/src/commonTest/kotlin/com/github/quillraven/fleks/collection/EntityBagTest.kt +++ b/src/commonTest/kotlin/com/github/quillraven/fleks/collection/EntityBagTest.kt @@ -1,6 +1,7 @@ package com.github.quillraven.fleks.collection import com.github.quillraven.fleks.Entity +import com.github.quillraven.fleks.Family import com.github.quillraven.fleks.configureWorld import kotlin.test.* @@ -661,6 +662,22 @@ class EntityBagTest { assertTrue { testEntity2 in bag } } + @Test + fun `test plusAssign of a family`() { + val bag = mutableEntityBagOf() + val testWorld = configureWorld { } + val toAdd = Family(world = testWorld).apply { + onEntityAdded(testEntity1, BitArray()) + onEntityAdded(testEntity2, BitArray()) + } + + bag += toAdd + + assertEquals(2, bag.size) + assertTrue { testEntity1 in bag } + assertTrue { testEntity2 in bag } + } + @Test fun `test minusAssign of an EntityBag`() { val bag = mutableEntityBagOf()