diff --git a/ReadMe.md b/ReadMe.md index 1250f3c..2f2e5cf 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -1,7 +1,7 @@ # Fleks [![LTS](https://img.shields.io/badge/LTS-2.4-orange.svg)](https://search.maven.org/artifact/io.github.quillraven.fleks/Fleks/2.4/jar) -[![Snapshot](https://img.shields.io/badge/Snapshot-2.4--SNAPSHOT-orange.svg)](https://s01.oss.sonatype.org/#nexus-search;gav~io.github.quillraven.fleks~~2.4-SNAPSHOT~~) +[![Snapshot](https://img.shields.io/badge/Snapshot-2.5--SNAPSHOT-orange.svg)](https://s01.oss.sonatype.org/#nexus-search;gav~io.github.quillraven.fleks~~2.5-SNAPSHOT~~) [![Build Master](https://img.shields.io/github/actions/workflow/status/quillraven/fleks/build.yml?branch=master)](https://github.com/Quillraven/fleks/actions) [![Kotlin](https://img.shields.io/badge/Kotlin-1.9.0-red.svg)](http://kotlinlang.org/) diff --git a/build.gradle.kts b/build.gradle.kts index 5783b99..a06e398 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -12,7 +12,7 @@ plugins { } group = "io.github.quillraven.fleks" -version = "2.4" +version = "2.5-SNAPSHOT" kotlin { jvm { diff --git a/src/commonMain/kotlin/com/github/quillraven/fleks/component.kt b/src/commonMain/kotlin/com/github/quillraven/fleks/component.kt index 81be5e3..6c815d8 100644 --- a/src/commonMain/kotlin/com/github/quillraven/fleks/component.kt +++ b/src/commonMain/kotlin/com/github/quillraven/fleks/component.kt @@ -4,7 +4,6 @@ import com.github.quillraven.fleks.collection.Bag import com.github.quillraven.fleks.collection.bag import kotlin.math.max import kotlin.native.concurrent.ThreadLocal -import kotlin.reflect.KClass /** * A class that assigns a unique [id] per type of [Component] starting from 0. @@ -68,7 +67,7 @@ interface Component { */ class ComponentsHolder>( private val world: World, - private val name: String, + private val type: ComponentType<*>, private var components: Array, ) { /** @@ -128,7 +127,7 @@ class ComponentsHolder>( * @throws [FleksNoSuchEntityComponentException] if the [entity] does not have such a component. */ operator fun get(entity: Entity): T { - return components[entity.id] ?: throw FleksNoSuchEntityComponentException(entity, name) + return components[entity.id] ?: throw FleksNoSuchEntityComponentException(entity, componentName()) } /** @@ -143,8 +142,15 @@ class ComponentsHolder>( operator fun contains(entity: Entity): Boolean = components.size > entity.id && components[entity.id] != null + /** + * Returns the simplified component name of a [ComponentType]. + * The default toString() format is 'package.Component$Companion'. + * This method returns 'Component' without package and companion. + */ + private fun componentName(): String = type::class.toString().substringAfterLast(".").substringBefore("$") + override fun toString(): String { - return "ComponentsHolder($name)" + return "ComponentsHolder(type=${componentName()}, id=${type.id})" } } @@ -162,11 +168,6 @@ class ComponentService { @PublishedApi internal val holdersBag = bag>() - // Format of toString() is package.Component$Companion - // This method returns 'Component' which is the short name of the component class. - fun KClass<*>.toComponentName(): String = - this.toString().substringAfterLast(".").substringBefore("$") - /** * Returns a [ComponentsHolder] for the given [componentType]. This function is only * used by [World.loadSnapshot] where we don't have the correct type information @@ -174,11 +175,8 @@ class ComponentService { */ fun wildcardHolder(componentType: ComponentType<*>): ComponentsHolder<*> { if (holdersBag.hasNoValueAtIndex(componentType.id)) { - // We cannot use simpleName here because it returns "Companion". - // Therefore, we do some string manipulation to get the name of the component correctly. - // Format of toString() is package.Component$Companion - val name = componentType::class.toComponentName() - holdersBag[componentType.id] = ComponentsHolder(world, name, Array?>(world.capacity) { null }) + holdersBag[componentType.id] = + ComponentsHolder(world, componentType, Array?>(world.capacity) { null }) } return holdersBag[componentType.id] } @@ -190,8 +188,7 @@ class ComponentService { @Suppress("UNCHECKED_CAST") inline fun > holder(componentType: ComponentType): ComponentsHolder { if (holdersBag.hasNoValueAtIndex(componentType.id)) { - val name = T::class.simpleName ?: T::class.toComponentName() - holdersBag[componentType.id] = ComponentsHolder(world, name, Array(world.capacity) { null }) + holdersBag[componentType.id] = ComponentsHolder(world, componentType, Array(world.capacity) { null }) } return holdersBag[componentType.id] as ComponentsHolder }