-
Notifications
You must be signed in to change notification settings - Fork 0
Generic Entity Config
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):
For saving some time to write EntityConfig
data classes and also to make that process less error prone it is possible to use live templates in Intellij IDEA. Just open in Intellij Settings -> Live Templates. In the list choose Kotlin
and press the +
button at the top of the list box. Fill in below mentioned fields:
- Abbreviation:
fleksentityconfig
- Description:
creates a new EnityConfig data class for a Korge-fleks game
- Template text:
data class $ENTITY_CONFIG$(
override val name: String
) : EntityFactory.EntityConfig {
override val configureEntity = fun(world: World, entity: Entity) : Entity = with(world) {
entity.configure {
}
entity
}
init {
EntityFactory.register(this)
}
}
Then click on Define
and choose Kotlin from the drop-down menu. Finally press the Apply
button to save the changes.