Skip to content

Commit

Permalink
core: update AssetProvider to allow for prepared to be invoked multip…
Browse files Browse the repository at this point in the history
…le times (#266)
  • Loading branch information
LeHaine authored Jul 20, 2024
1 parent b516b13 commit a31bc74
Showing 1 changed file with 2 additions and 11 deletions.
13 changes: 2 additions & 11 deletions core/src/commonMain/kotlin/com/littlekt/AssetProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,32 +51,23 @@ open class AssetProvider(val context: Context) {
val assets: Map<KClass<*>, Map<VfsFile, GameAsset<*>>>
get() = _assets

/**
* Holds the current state of assets being prepared.
*
* @see prepare
*/
var prepared = false
protected set

/** A lambda that is invoked once all assets have been loaded and prepared. */
var onFullyLoaded: (() -> Unit)? = null

/** Determines if it has been fully loaded. */
val fullyLoaded: Boolean
get() = totalAssetsLoading.value == 0 && prepared
get() = totalAssetsLoading.value == 0 && assetsToPrepare.isEmpty()

private var job: Job? = null

/** Updates to check if all assets have been loaded, and if so, prepare them. */
fun update() {
if (totalAssetsLoading.value > 0) return
if (!prepared && job?.isActive != true) {
if (job?.isActive != true) {
job =
KtScope.launch {
assetsToPrepare.fastForEach { it.prepare() }
assetsToPrepare.clear()
prepared = true
onFullyLoaded?.invoke()
}
}
Expand Down

0 comments on commit a31bc74

Please sign in to comment.