Skip to content

Generic Entity Config

Marko Koschak edited this page May 22, 2024 · 10 revisions

KorGE-Fleks uses generic entity config classes for creating game objects dynamically out of some given configuration. That configuration comes usually out of a level map object layer from Tiled or LDtk.

Some existing entity config data classes can be found in korlibs.korge.fleks.entity.config.

For creating your own entity config data classes start with following code snippet and check the explanation below.

Entity config data classes are derived from EntityConfig and contain 4 sections as shown below:

import com.github.quillraven.fleks.*
import korlibs.korge.fleks.entity.*

data class MyEntityConfig(
    override val name: String,  // (1) Name For the specific game object which is characterized by below config values

    // (2) Specific game object config which shall be changeable from outside
    val <property1>: String,
    val <property2>: Int,
    ...

) : EntityFactory.EntityConfig {

    // The actual (anonymous) configure function as a value object
    override val configureEntity = fun(world: World, entity: Entity) : Entity = with(world) {
        entity.configure {
            // (3) Build up entity with components for specific game object as needed
            it += SomeComponent(
                      <property1> = this@MyEntityConnfig.<property1>,  // Take over specific game object config
                      <property2> = this@MyEntityConnfig.<property2>
                  )
            it += AnotherComponent(staticValue = 42f)  // Static configuration for game object
            ...
        }
        return entity
    }

    // (4) Init block which registers the entity config in the EntityFactory
    init {
        EntityFactory.register(this)
    }
}

Section (1) and (4) can be just taken over as is.

Section (2):

Section (3):

Clone this wiki locally