Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import korlibs.korge.fleks.assets.data.SpriteFrames
import korlibs.korge.fleks.assets.data.SimpleTileSet
import korlibs.korge.fleks.assets.data.UNKNOWN
import korlibs.korge.fleks.assets.data.WorldMapData
import korlibs.korge.fleks.state.GameStateManager
import kotlin.collections.set


Expand Down Expand Up @@ -52,11 +53,13 @@ typealias TileSetsAssetType = MutableMap<String, Pair<AssetType, SimpleTileSet>>
* level editor. This can help to keep an overview of which assets are used in which area of the world and to avoid loading
* and unloading of assets.
*/
class AssetStore {
class AssetStore(
gameStateManager: GameStateManager
) {
// Handles loading of common and world cluster assets
val loader = AssetLoader(this)
// Data structure to keep track of loaded world map data (e.g. chunk meshes, level maps, grid vania, ...)
val worldMapData = WorldMapData()
val worldMapData = WorldMapData(this, gameStateManager)

var testing: Boolean = false // Set to true for unit tests on headless linux nodes on GitHub Actions runner

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,31 @@ class AssetLoader(
worldHeight = (commonChunkInfo.gridVaniaHeight * commonChunkInfo.chunkHeight * commonChunkInfo.tileSize).toFloat(),
gridVaniaWidth = commonChunkInfo.gridVaniaWidth,
gridVaniaHeight = commonChunkInfo.gridVaniaHeight,
levelChunkWidth = commonChunkInfo.chunkWidth,
levelChunkHeight = commonChunkInfo.chunkHeight,
chunkWidth = commonChunkInfo.chunkWidth,
chunkHeight = commonChunkInfo.chunkHeight,
tileSize = commonChunkInfo.tileSize,
collisionTiles = commonChunkInfo.collisionTiles,
collisionShapesBitmapSlice = collisionShapes
)
println("- Resources loaded in ${sw.elapsed}")

}

/**
* Function for loading all assets which are needed for a world chunk. This includes the tile maps and tile sets for
* the chunk and all assets which are needed for the entities of the chunk.
* Function for loading all assets which are needed for a list of world chunks. This includes the JSON file for the
* chunk data itself and any tile sets and other assets which are needed for the tile maps and game objects of the chunk.
*/
suspend fun loadWorldChunkAssets(worldName: String, chunkNumber: Int) {
suspend fun loadWorldChunkAssets(worldName: String, chunkList: List<Int>) {
// First load chunks and get list of asset clusters which need to be loaded.
loadChunkAssets(worldName, chunkNumber)
// TODO hardcoded for now
loadChunkAssets(worldName, 2)
loadChunkAssets(worldName, 3)
loadChunkAssets(worldName, 4)
loadChunkAssets(worldName, 5)
loadChunkAssets(worldName, 6)
loadChunkAssets(worldName, 7)
chunkList.forEach { chunk ->
loadWorldChunkAssets(worldName, chunk)
}
}

private suspend fun loadChunkAssets(worldName: String, chunkIndex: Int) {
/**
* Function for loading all assets which are needed for a world chunk. This includes the JSON file for the chunk data
* itself and any tile sets and other assets which are needed for the tile maps and game objects of the chunk.
*/
suspend fun loadWorldChunkAssets(worldName: String, chunkIndex: Int) {
val sw = Stopwatch().start()
print("INFO: AssetStore - Start loading world chunk '${worldName}/level_data/chunk_${chunkIndex}'... ")

Expand All @@ -90,8 +88,8 @@ class AssetLoader(
// compatible with the current version of the game.
val worldChunk: ChunkAssetInfo = configSerializer.json().decodeFromString(resourcesVfs["${worldName}/level_data/chunk_${chunkIndex}.json"].readString())
// Add world chunk
assetStore.worldMapData.chunkMeshes[chunkIndex] = worldChunk
assetStore.worldMapData.levelGridVania[worldChunk.chunkX, worldChunk.chunkY] = chunkIndex
assetStore.worldMapData.chunkLookUpTable[chunkIndex] = worldChunk
assetStore.worldMapData.chunkGridVania[worldChunk.chunkX, worldChunk.chunkY] = chunkIndex

println("- Resources loaded in ${sw.elapsed}")

Expand All @@ -106,7 +104,7 @@ class AssetLoader(
}

internal suspend fun loadClusterAssets(world: String? = null, clusterName: String, hotReloading: Boolean = false) {
// Keep track was loaded already to avoid reloading of assets which are already in memory.
// Keep track of already loaded assets to avoid reloading of assets which are already in memory
val clusterPath = world?.let { "${world}/${clusterName}" } ?: clusterName
if (loadedClusterAssets.contains(clusterPath)) {
//println("INFO: Asset cluster '$clusterPath' already loaded! No reload is happening!")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ data class CommonChunkInfo(
*
* @param entities List of entity configurations which are used to create entities in the world chunk.
* @param entitiesToBeSpawned List of entity names which shall be spawned automatically by the WorldChunkSystem.
* @param chunkX The x coordinate of the chunk within the Grid-vania world map.
* @param chunkY The y coordinate of the chunk within the Grid-vania world map.
* @param chunkX The x coordinate of the chunk within the grid-vania world map.
* @param chunkY The y coordinate of the chunk within the grid-vania world map.
* @param chunkTop The id of the neighbor chunk at the top of the chunk. -1 if there is no neighbor chunk at the top.
* @param chunkBottom The id of the neighbor chunk at the bottom of the chunk. -1 if there is no neighbor chunk at the bottom.
* @param chunkLeft The id of the neighbor chunk at the left of the chunk. -1 if there is no neighbor chunk at the left.
Expand Down
Loading
Loading