Skip to content

Commit

Permalink
Fix Archetype.entities returning empty slots in backing array
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Sep 24, 2023
1 parent 604800e commit 5ba05a9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.mineinabyss.geary.datatypes

import com.mineinabyss.geary.helpers.toGeary

private const val initialSize: Int = 16
private const val growFactor: Int = 2

class IdList : Iterable<ULong> {
class IdList {
var backingArr = ULongArray(initialSize)
var size = 0
val lastIndex get() = size - 1
Expand All @@ -25,7 +27,7 @@ class IdList : Iterable<ULong> {
return backingArr[--size]
}

override fun iterator(): Iterator<ULong> {
return backingArr.iterator()
fun getEntities(): Sequence<Entity> {
return backingArr.asSequence().take(size).map { it.toGeary() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ data class Archetype(
private val eventRunner get() = archetypes.eventRunner


val entities: List<Entity> get() = ids.map { it.toGeary() }
val entities: Sequence<Entity> get() = ids.getEntities()

/** The entity ids in this archetype. Indices are the same as [componentData]'s sub-lists. */
private val ids: IdList = IdList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package com.mineinabyss.geary.datatypes

import com.mineinabyss.geary.components.relations.InstanceOf
import com.mineinabyss.geary.components.relations.Persists
import com.mineinabyss.geary.helpers.componentId
import com.mineinabyss.geary.helpers.entity
import com.mineinabyss.geary.helpers.getArchetype
import com.mineinabyss.geary.helpers.*
import com.mineinabyss.geary.helpers.tests.GearyTest
import com.mineinabyss.geary.modules.archetypes
import com.mineinabyss.geary.systems.accessors.RelationWithData
Expand Down Expand Up @@ -136,6 +134,20 @@ internal class GearyEntityTests : GearyTest() {
entity.getAllPersisting().shouldContainExactly("Test")
}

@Nested
inner class ChildTest {
@Test
fun `should handle single parent child relation correctly when using addParent`() {
val parent = entity()
val child = entity {
addParent(parent)
}

parent.children.shouldContainExactly(child)
child.parents.shouldContainExactly(parent)
}
}

@Nested
inner class RelationTest {
@Test
Expand Down

0 comments on commit 5ba05a9

Please sign in to comment.