Skip to content

Commit

Permalink
(#40) fix issue that families are not updated correctly during system…
Browse files Browse the repository at this point in the history
… creation

* fix issue that families are not updated correctly during system creation
* do not sign SNAPSHOT releases
* refactor setting family of a system
  • Loading branch information
Quillraven authored May 12, 2022
1 parent 039d24c commit 27c3591
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 32 deletions.
6 changes: 6 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ publishing {
}
}

// only sign if version is not a SNAPSHOT release.
// this makes it easier to publish to mavenLocal and test the packed version.
tasks.withType<Sign>().configureEach {
onlyIf { !project.version.toString().endsWith("SNAPSHOT") }
}

signing {
useInMemoryPgpKeys(System.getenv("SIGNING_KEY"), System.getenv("SIGNING_PASSWORD"))
sign(publishing.publications[publicationName])
Expand Down
7 changes: 7 additions & 0 deletions src/main/kotlin/com/github/quillraven/fleks/entity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ class EntityService(
return newEntity
}

/**
* Notifies all registered [EntityListener].
*/
internal fun notifyAll() {
forEach { e -> listeners.forEach { l -> l.onEntityCfgChanged(e, cmpMasks[e.id]) } }
}

/**
* Updates an [entity] with the given [configuration].
* Notifies any registered [EntityListener].
Expand Down
43 changes: 11 additions & 32 deletions src/main/kotlin/com/github/quillraven/fleks/system.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.github.quillraven.fleks

import com.github.quillraven.fleks.collection.BitArray
import com.github.quillraven.fleks.collection.EntityComparator
import java.lang.reflect.Field
import kotlin.reflect.KClass

/**
Expand Down Expand Up @@ -33,7 +32,7 @@ data class Fixed(val step: Float) : Interval
*/
abstract class IntervalSystem(
val interval: Interval = EachFrame,
var enabled: Boolean = true
var enabled: Boolean = true,
) {
/**
* Returns the [world][World] to which this system belongs.
Expand Down Expand Up @@ -133,7 +132,7 @@ abstract class IteratingSystem(
* Returns the [family][Family] of this system.
* This reference gets updated by the [SystemService] when the system gets created via reflection.
*/
private lateinit var family: Family
internal lateinit var family: Family

/**
* Returns the [entityService][EntityService] of this system.
Expand Down Expand Up @@ -167,7 +166,7 @@ abstract class IteratingSystem(
* a FleksNoSuchComponentException. To avoid that you could check if an entity really has the component
* before accessing it but that is redundant in context of a family.
*
* To avoid these kinds of problems, entity removals are delayed until the end of the iteration. This also means
* To avoid these kinds of issues, entity removals are delayed until the end of the iteration. This also means
* that a removed entity of this family will still be part of the [onTickEntity] for the current iteration.
*/
override fun onTick() {
Expand Down Expand Up @@ -245,17 +244,20 @@ class SystemService(
val sysType = systemTypes[sysIdx]
val newSystem = newInstance(sysType, cmpService, injectables)

if (IteratingSystem::class.java.isAssignableFrom(sysType.java)) {
if (newSystem is IteratingSystem) {
// set family reference of newly created iterating system
@Suppress("UNCHECKED_CAST")
val family = family(sysType as KClass<out IteratingSystem>, entityService, cmpService, allFamilies)
val famField = field(newSystem, "family")
famField.isAccessible = true
famField.set(newSystem, family)
newSystem.family = family(sysType as KClass<out IteratingSystem>, entityService, cmpService, allFamilies)
}

newSystem
}

// Families are created above together with its system. This can have the side effect that they are
// not updated correctly if entities get created in a system's init block because the family might not exist
// at that time.
// Therefore, we need to notify them one time after all families are available to update them correctly.
entityService.notifyAll()
}

/**
Expand Down Expand Up @@ -325,29 +327,6 @@ class SystemService(
return family
}

/**
* Returns a [Field] of name [fieldName] of the given [system].
*
* @throws [FleksSystemCreationException] if the [system] does not have a [Field] of name [fieldName].
*/
private fun field(system: IntervalSystem, fieldName: String): Field {
var sysClass: Class<*> = system::class.java
var classField: Field? = null
while (classField == null) {
try {
classField = sysClass.getDeclaredField(fieldName)
} catch (e: NoSuchFieldException) {
val supC = sysClass.superclass ?: throw FleksSystemCreationException(
system::class,
"No '$fieldName' field found"
)
sysClass = supC
}

}
return classField
}

/**
* Returns the specified [system][IntervalSystem].
*
Expand Down
26 changes: 26 additions & 0 deletions src/test/kotlin/com/github/quillraven/fleks/SystemTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@ private class SystemTestIteratingSystemQualifiedInjectable(
override fun onTickEntity(entity: Entity) = Unit
}

@AnyOf([SystemTestComponent::class])
private class SystemTestEntityCreation : IteratingSystem() {
var numTicks = 0

init {
world.entity { add<SystemTestComponent>() }
}

override fun onTickEntity(entity: Entity) {
++numTicks
}
}

internal class SystemTest {
private fun systemService(
systemTypes: List<KClass<out IntervalSystem>>,
Expand Down Expand Up @@ -413,4 +426,17 @@ internal class SystemTest {

assertEquals(1, service.system<SystemTestIntervalSystemEachFrame>().numDisposes)
}

@Test
fun `create entity during system init`() {
// this test verifies that entities that are created in a system's init block
// are correctly added to families
val world = World {}

val service = systemService(listOf(SystemTestEntityCreation::class), world = world)
service.update()

val system = service.system<SystemTestEntityCreation>()
assertEquals(1, system.numTicks)
}
}

0 comments on commit 27c3591

Please sign in to comment.