Skip to content

Commit

Permalink
Load entity configs from LDtk level map
Browse files Browse the repository at this point in the history
  • Loading branch information
jobe-m committed Aug 12, 2024
1 parent 1c858b4 commit 73a30e3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package korlibs.korge.fleks.assets

import entities.config.common.*
import korlibs.audio.format.*
import korlibs.audio.sound.*
import korlibs.datastructure.*
Expand All @@ -14,6 +15,7 @@ import korlibs.korge.ldtk.*
import korlibs.korge.ldtk.view.*
import korlibs.time.Stopwatch
import kotlin.collections.set
import kotlin.reflect.*


/**
Expand Down Expand Up @@ -68,14 +70,26 @@ class AssetStore {
}

fun getLdtkWorld(name: String) : LDTKWorld =
if (ldtkWorld.contains(name)) {
ldtkWorld[name]!!.second
} else error("AssetStore: LDtkWorld '$name' not found!")
if (ldtkWorld.contains(name)) ldtkWorld[name]!!.second
else error("AssetStore: LDtkWorld '$name' not found!")

fun getLdtkLevel(ldtkWorld: LDTKWorld, levelName: String) : Level =
if (ldtkWorld.levelsByName.contains(levelName)) {
ldtkWorld.levelsByName[levelName]!!.level
} else error("AssetStore: LDtkLevel '$levelName' not found!")
if (ldtkWorld.levelsByName.contains(levelName)) ldtkWorld.levelsByName[levelName]!!.level
else error("AssetStore: LDtkLevel '$levelName' not found!")

fun loadEntitiesForLdtkLevel(worldName: String, levelName: String) {
// Create all game objects and save their entity config in the EntityFactory
val entityLayer = getLdtkLevel(getLdtkWorld(worldName), levelName).layerInstances?.firstOrNull { it.entityInstances.isNotEmpty() }
entityLayer?.entityInstances?.forEach {
println("INFO: Entity config loaded with ID '${it.defUid}' (${it.identifier})")
GameObjectConfig(
name = "uid_${it.defUid}",
ldtkIdentifier = it.identifier,
x = it.px[0].toFloat(),
y = it.px[1].toFloat()
)
}
}

fun getNinePatch(name: String) : NinePatchBmpSlice =
if (images.contains(name)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kotlinx.serialization.Serializable
@Serializable @SerialName("Info")
data class InfoComponent(
var name: String = "noName",
var ldtkIdentifier: String = "",
var entityId: Int = -1,

// internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ data class LdtkLevelMapComponent(

val assetStore: AssetStore = this.inject(name = "AssetStore")

val ldtkLevelMapComponent = entity[LdtkLevelMapComponent]
val ldtkLevel = assetStore.getLdtkLevel(assetStore.getLdtkWorld(ldtkLevelMapComponent.worldName), ldtkLevelMapComponent.levelName)
val ldtkLevel = assetStore.getLdtkLevel(assetStore.getLdtkWorld(worldName), levelName)
width = ldtkLevel.pxWid.toFloat()
height = ldtkLevel.pxHei.toFloat()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ data class LevelMapConfig(
override val name: String,

private val mapType: TileMapType,
private val assetName: String = "", // Used with Tiled based maps
private val worldName: String = "", // Used with LDtk based maps
private val assetName: String = "", // Used with LDtk and Tiled based maps
private val levelName: String = "", // Used with LDtk based maps
private val layerTag: RenderLayerTag,
private val x: Float = 0f,
Expand All @@ -34,7 +33,7 @@ data class LevelMapConfig(
override fun World.entityConfigure(entity: Entity) : Entity {
entity.configure {
when (mapType) {
TileMapType.LDTK -> it += LdtkLevelMapComponent(worldName, levelName)
TileMapType.LDTK -> it += LdtkLevelMapComponent(assetName, levelName)
TileMapType.TILED -> it += TiledLevelMapComponent(assetName)
}
it += PositionComponent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class ObjectRenderSystem(
}
}
}
// Rendering path for text
// Rendering path for text (not optimized - no caching)
else if (entity has TextFieldComponent) {
val (text, fontName, textRangeStart, textRangeEnd, width, height, wordWrap, horizontalAlign, verticalAlign) = entity[TextFieldComponent]
val offset: Point = Point(offsetX, offsetY)
Expand Down Expand Up @@ -146,6 +146,7 @@ class ObjectRenderSystem(
}
}
}
// Rendering path for 9-patch graphic (not optimized - no caching)
else if (entity has NinePatchComponent) {
val (name, width, height) = entity[NinePatchComponent]
val ninePatch = assetStore.getNinePatch(name)
Expand Down

0 comments on commit 73a30e3

Please sign in to comment.