Skip to content

Commit

Permalink
Update asset store and some entity configs
Browse files Browse the repository at this point in the history
  • Loading branch information
jobe-m committed Jul 26, 2024
1 parent ab37760 commit 04a9339
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import kotlinx.serialization.*
*/
@Serializable @SerialName("AssetModel")
data class AssetModel(
val type: AssetType,
val folderName: String = "", // default is empty string
val folder: String = "",
val sounds: MutableMap<String, String> = mutableMapOf(),
val backgrounds: MutableMap<String, ParallaxConfig> = mutableMapOf(),
val images: MutableMap<String, ImageDataConfig> = mutableMapOf(),
Expand All @@ -30,39 +29,6 @@ data class AssetModel(
val fileName: String,
val type: TileMapType
)

fun addSound(id: String, fileName: String) {
sounds[id] = fileName
}

fun addBackground(id: String, config: ParallaxConfig) {
backgrounds[id] = config
}

fun addImage(id: String, fileName: String, layers: String? = null) {
images[id] = ImageDataConfig(fileName, layers)
}

fun addFont(id: String, fileName: String) {
fonts[id] = fileName
}

fun addTileMap(id: String, fileName: String, type: TileMapType) {
tileMaps[id] = TileMapConfig(fileName, type)
}
}

/**
* Call this function inside KorGE scenes to load additional assets into an already created [AssetStore] object.
*/
suspend fun AssetStore.loadAssets(type: AssetType, folderName: String, hotReloading: Boolean = false, cfg: AssetModel.() -> Unit) {
val assetModel = AssetModel(type, folderName).apply(cfg)
this.loadAssets(assetModel, hotReloading)
if (hotReloading) {
this.configureResourceDirWatcher {
addAssetWatcher(type) {}
}
}
}

enum class AssetType { COMMON, WORLD, LEVEL, SPECIAL }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ResourceDirWatcherConfiguration(
assetUpdater.enabled = true
assetUpdater.toBeEnabled = false

resourcesVfs[assetConfig.folderName].apply {
resourcesVfs[assetConfig.folder].apply {
if (stat().isDirectory) {
println("Add watcher for '${path}'")
watch {
Expand All @@ -121,12 +121,12 @@ class ResourceDirWatcherConfiguration(
// Check filename
if (file.fullName.contains(config.value.removeSuffix(".fnt")) && !reloading) {
reloading = true // save that reloading is in progress
println("Reloading ${assetConfig.folderName}/${config.value} for changes in ${file.fullName} ... ")
println("Reloading ${assetConfig.folder}/${config.value} for changes in ${file.fullName} ... ")

launchImmediately(context = assetReloadContext) {
delay(500)
val assetName = config.key
assetStore.fonts[assetName] = Pair(assetUpdater.type, resourcesVfs[assetConfig.folderName + "/" + config.value].readBitmapFont(atlas = null))
assetStore.fonts[assetName] = Pair(assetUpdater.type, resourcesVfs[assetConfig.folder + "/" + config.value].readBitmapFont(atlas = null))
}

delay(100)
Expand All @@ -145,11 +145,11 @@ class ResourceDirWatcherConfiguration(
val assetName = config.key
assetStore.images[assetName] = Pair(assetUpdater.type,
if (config.value.layers == null) {
resourcesVfs[assetConfig.folderName + "/" + config.value.fileName].readImageDataContainer(ASE.toProps(), atlas = null)
resourcesVfs[assetConfig.folder + "/" + config.value.fileName].readImageDataContainer(ASE.toProps(), atlas = null)
} else {
val props = ASE.toProps()
props.setExtra("layers", config.value.layers)
resourcesVfs[assetConfig.folderName + "/" + config.value.fileName].readImageDataContainer(props, atlas = null)
resourcesVfs[assetConfig.folder + "/" + config.value.fileName].readImageDataContainer(props, atlas = null)
}
)

Expand All @@ -169,7 +169,7 @@ class ResourceDirWatcherConfiguration(
// Give aseprite more time to finish writing the files
delay(100)
val assetName = config.key
assetStore.backgrounds[assetName] = Pair(assetUpdater.type, resourcesVfs[assetConfig.folderName + "/" + config.value.aseName].readParallaxDataContainer(config.value, ASE, atlas = null))
assetStore.backgrounds[assetName] = Pair(assetUpdater.type, resourcesVfs[assetConfig.folder + "/" + config.value.aseName].readParallaxDataContainer(config.value, ASE, atlas = null))

println("\nTriggering asset change for: $assetName")
// Guard period until reloading is activated again - this is used for debouncing watch messages
Expand All @@ -190,8 +190,8 @@ class ResourceDirWatcherConfiguration(
delay(500)
val assetName = config.key
when (config.value.type) {
TileMapType.LDTK -> assetStore.ldtkWorld[assetName] = Pair(assetUpdater.type, resourcesVfs[assetConfig.folderName + "/" + config.value.fileName].readLDTKWorld(extrude = true))
TileMapType.TILED -> assetStore.tiledMaps[assetName] = Pair(assetUpdater.type, resourcesVfs[assetConfig.folderName + "/" + config.value.fileName].readTiledMap())
TileMapType.LDTK -> assetStore.ldtkWorld[assetName] = Pair(assetUpdater.type, resourcesVfs[assetConfig.folder + "/" + config.value.fileName].readLDTKWorld(extrude = true))
TileMapType.TILED -> assetStore.tiledMaps[assetName] = Pair(assetUpdater.type, resourcesVfs[assetConfig.folder + "/" + config.value.fileName].readTiledMap())
}

println("\nTriggering asset change for: $assetName")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class AssetStore {
val specialAtlas: MutableAtlasUnit = MutableAtlasUnit(1024, 2048, border = 1)

// @Volatile
internal var commonAssetConfig: AssetModel = AssetModel(type = AssetType.COMMON)
internal var currentWorldAssetConfig: AssetModel = AssetModel(type = AssetType.WORLD)
internal var currentLevelAssetConfig: AssetModel = AssetModel(type = AssetType.LEVEL)
internal var specialAssetConfig: AssetModel = AssetModel(type = AssetType.SPECIAL)
internal var commonAssetConfig: AssetModel = AssetModel()
internal var currentWorldAssetConfig: AssetModel = AssetModel()
internal var currentLevelAssetConfig: AssetModel = AssetModel()
internal var specialAssetConfig: AssetModel = AssetModel()

internal var tiledMaps: MutableMap<String, Pair<AssetType, TiledMap>> = mutableMapOf()
internal var ldtkWorld: MutableMap<String, Pair<AssetType, LDTKWorld>> = mutableMapOf()
Expand Down Expand Up @@ -100,33 +100,32 @@ class AssetStore {
else error("AssetStore: Cannot find font '$name'!")
}

suspend fun loadAssets(assetConfig: AssetModel, hotReloading: Boolean) {
val type: AssetType = assetConfig.type
suspend fun loadAssets(type: AssetType, assetConfig: AssetModel, hotReloading: Boolean) {
var assetLoaded = false
val atlas = when (type) {
AssetType.COMMON -> {
prepareCurrentAssets(assetConfig, commonAssetConfig, hotReloading)?.also { config ->
prepareCurrentAssets(type, assetConfig, commonAssetConfig, hotReloading)?.also { config ->
commonAssetConfig = config
assetLoaded = true
}
commonAtlas
}
AssetType.WORLD -> {
prepareCurrentAssets(assetConfig, currentWorldAssetConfig, hotReloading)?.also { config ->
prepareCurrentAssets(type, assetConfig, currentWorldAssetConfig, hotReloading)?.also { config ->
currentWorldAssetConfig = config
assetLoaded = true
}
worldAtlas
}
AssetType.LEVEL -> {
prepareCurrentAssets(assetConfig, currentLevelAssetConfig, hotReloading)?.also { config ->
prepareCurrentAssets(type, assetConfig, currentLevelAssetConfig, hotReloading)?.also { config ->
currentLevelAssetConfig = config
assetLoaded = true
}
levelAtlas
}
AssetType.SPECIAL -> {
prepareCurrentAssets(assetConfig, specialAssetConfig, hotReloading)?.also { config ->
prepareCurrentAssets(type, assetConfig, specialAssetConfig, hotReloading)?.also { config ->
specialAssetConfig = config
assetLoaded = true
}
Expand All @@ -137,66 +136,72 @@ class AssetStore {
if (assetLoaded) {

val sw = Stopwatch().start()
println("AssetStore: Start loading [${type.name}] resources from '${assetConfig.folderName}'...")
println("AssetStore: Start loading [${type.name}] resources from '${assetConfig.folder}'...")

// Update maps of music, images, ...
assetConfig.tileMaps.forEach { tileMap ->
when (tileMap.value.type) {
TileMapType.LDTK -> ldtkWorld[tileMap.key] = Pair(type, resourcesVfs[assetConfig.folderName + "/" + tileMap.value.fileName].readLDTKWorld(extrude = true))
TileMapType.TILED -> tiledMaps[tileMap.key] = Pair(type, resourcesVfs[assetConfig.folderName + "/" + tileMap.value.fileName].readTiledMap(atlas = atlas))
TileMapType.LDTK -> ldtkWorld[tileMap.key] = Pair(type, resourcesVfs[assetConfig.folder + "/" + tileMap.value.fileName].readLDTKWorld(extrude = true))
TileMapType.TILED -> tiledMaps[tileMap.key] = Pair(type, resourcesVfs[assetConfig.folder + "/" + tileMap.value.fileName].readTiledMap(atlas = atlas))
}
}

assetConfig.sounds.forEach { sound ->
val soundFile = resourcesVfs[assetConfig.folderName + "/" + sound.value].readMusic()
val soundFile = resourcesVfs[assetConfig.folder + "/" + sound.value].readMusic()
val soundChannel = soundFile.play()
// val soundChannel = resourcesVfs[assetConfig.assetFolderName + "/" + sound.value].readSound().play()
soundChannel.pause()
sounds[sound.key] = Pair(type, soundChannel)
}
assetConfig.backgrounds.forEach { background ->
backgrounds[background.key] = Pair(type, resourcesVfs[assetConfig.folderName + "/" + background.value.aseName].readParallaxDataContainer(background.value, ASE, atlas = atlas))
backgrounds[background.key] = Pair(type, resourcesVfs[assetConfig.folder + "/" + background.value.aseName].readParallaxDataContainer(background.value, ASE, atlas = atlas))
}
assetConfig.images.forEach { image ->
images[image.key] = Pair(
type,
if (image.value.layers == null) {
resourcesVfs[assetConfig.folderName + "/" + image.value.fileName].readImageDataContainer(ASE.toProps(), atlas = atlas)
resourcesVfs[assetConfig.folder + "/" + image.value.fileName].readImageDataContainer(ASE.toProps(), atlas = atlas)
} else {
val props = ASE.toProps() // TODO check -- ImageDecodingProps(it.value.fileName, extra = ExtraTypeCreate())
props.setExtra("layers", image.value.layers)
resourcesVfs[assetConfig.folderName + "/" + image.value.fileName].readImageDataContainer(props, atlas)
resourcesVfs[assetConfig.folder + "/" + image.value.fileName].readImageDataContainer(props, atlas)
}
)
}
assetConfig.fonts.forEach { font ->
// TODO there is a bug with drawing spaces from font when the font is added to an atlas - remove atlas for now
fonts[font.key] = Pair(type, resourcesVfs[assetConfig.folderName + "/" + font.value].readBitmapFont()) // readBitmapFont(atlas = atlas))
fonts[font.key] = Pair(type, resourcesVfs[assetConfig.folder + "/" + font.value].readBitmapFont()) // readBitmapFont(atlas = atlas))
}

println("Assets: Loaded resources in ${sw.elapsed}")

if (hotReloading) {
configureResourceDirWatcher {
addAssetWatcher(type) {}
}
}
}
}

private fun prepareCurrentAssets(newAssetConfig: AssetModel, currentAssetConfig: AssetModel, hotReloading: Boolean): AssetModel? =
when (currentAssetConfig.folderName) {
private fun prepareCurrentAssets(type: AssetType, newAssetConfig: AssetModel, currentAssetConfig: AssetModel, hotReloading: Boolean): AssetModel? =
when (currentAssetConfig.folder) {
"" -> {
// Just load new assets
newAssetConfig
}
newAssetConfig.folderName -> {
newAssetConfig.folder -> {
if (hotReloading) {
// removeAssets(newAssetConfig.type)
println("INFO: Reload ${newAssetConfig.type} assets '${newAssetConfig.folderName}'.")
println("INFO: Reload $type assets '${newAssetConfig.folder}'.")
newAssetConfig
} else {
println("INFO: ${newAssetConfig.type} assets '${newAssetConfig.folderName}' already loaded! No reload is happening!")
println("INFO: $type assets '${newAssetConfig.folder}' already loaded! No reload is happening!")
null
}
}
else -> {
println("INFO: Remove old ${newAssetConfig.type} assets and load new ones!")
removeAssets(newAssetConfig.type)
println("INFO: Remove old $type assets and load new ones!")
removeAssets(type)
newAssetConfig
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import kotlinx.serialization.*
* Entity config for a dialog box which appears on the dialog layer in front of any game play.
* Dialog Box is rendered on indexLayer 100 - 102 in foreground on [FG_DIALOGS][RenderLayerTag.FG_DIALOGS] layer.
*/
@Serializable
@Serializable @SerialName("DialogBox")
data class DialogBoxConfig(
override val name: String,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import korlibs.korge.fleks.entity.*
import korlibs.korge.fleks.entity.EntityFactory.EntityConfig
import korlibs.korge.fleks.tags.*
import korlibs.korge.fleks.utils.*
import kotlinx.serialization.Serializable
import kotlinx.serialization.*


@Serializable
@Serializable @SerialName("RichText")
data class RichTextConfig(
override val name: String,

Expand Down

0 comments on commit 04a9339

Please sign in to comment.