Skip to content

Commit

Permalink
add entityBagOf and mutableEntityBagOf functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Quillraven committed Nov 24, 2024
1 parent a35160f commit 1bdf878
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ import kotlin.math.max
import kotlin.math.min
import kotlin.random.Random

/**
* Returns a new read-only [EntityBag] of given [entities].
*/
fun entityBagOf(vararg entities: Entity): EntityBag {
return MutableEntityBag(entities.size).apply {
entities.forEach { this += it }
}
}

/**
* Returns a new [MutableEntityBag] with the given [entities].
*/
fun mutableEntityBagOf(vararg entities: Entity): MutableEntityBag {
return MutableEntityBag(entities.size).apply {
entities.forEach { this += it }
}
}

interface EntityBag {
/**
* Returns the size of the [EntityBag].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,4 +626,26 @@ class EntityBagTest {
assertEquals(bagOf(testEntity1, testEntity2), testBag.take(2))
assertEquals(bagOf(testEntity1, testEntity2), testBag.take(3))
}

@Test
fun testEntityBagOf() {
var bag = entityBagOf(testEntity1, testEntity2)
assertEquals(2, bag.size)
assertTrue { testEntity1 in bag }
assertTrue { testEntity2 in bag }

bag = entityBagOf()
assertEquals(0, bag.size)
}

@Test
fun testMutableEntityBagOf() {
var bag = mutableEntityBagOf(testEntity1, testEntity2)
assertEquals(2, bag.size)
assertTrue { testEntity1 in bag }
assertTrue { testEntity2 in bag }

bag = mutableEntityBagOf()
assertEquals(0, bag.size)
}
}

0 comments on commit 1bdf878

Please sign in to comment.