Skip to content

Commit

Permalink
Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
jobe-m committed Mar 15, 2024
1 parent 8b361ff commit df86e85
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ val onDrawableFamilyAdded: FamilyHook = { entity ->
val ldtkLevelMapComponent = entity[LdtkLevelMapComponent]
val ldtkWorld = AssetStore.getLdtkWorld(ldtkLevelMapComponent.worldName)
val ldtkLevel = AssetStore.getLdtkLevel(ldtkWorld, ldtkLevelMapComponent.levelName)
val view = LDTKLevelView(level = LDTKLevel(
world = ldtkWorld,
level = ldtkLevel
))
val view = LDTKLevelView(
level = LDTKLevel(
world = ldtkWorld,
level = ldtkLevel
)
)
width = ldtkLevel.pxWid.toDouble()
height = ldtkLevel.pxHei.toDouble()
view
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,50 @@ import kotlin.concurrent.*
import kotlin.math.max


class KorgeViewCache {
companion object {
// Korge specific internal objects which we do not want to store in the components - they are accessed by entity id the same way as components
@Volatile
private var views: Array<View?>? = null
private val VIEWS: Array<View?> get() = views ?: error("KorgeViewCache instance has not been created!")

fun createInstance(arraySize: Int = 64) {
if (views == null) views = Array(arraySize) { null }
}
/**
* This object stores KorGE specific internal objects which we do not want to store in the components.
* They are accessed by entity id the same way as components.
*/
object KorgeViewCache {

fun addOrUpdate(entity: Entity, view: View) {
if (entity.id >= VIEWS.size) {
views = VIEWS.copyOf(max(VIEWS.size * 2, entity.id + 1))
}
VIEWS[entity.id] = view
}
@Volatile
private lateinit var views: Array<View?>

fun remove(entity: Entity) {
if (VIEWS.size > entity.id) {
VIEWS[entity.id] = null
} else error("KorgeViewCache: Entity '${entity.id}' is out of range on remove!")
}
fun createInstance(arraySize: Int = 64) {
views = Array(arraySize) { null }
}

operator fun get(entity: Entity) : View {
return if (VIEWS.size > entity.id) {
VIEWS[entity.id] ?: error("KorgeViewCache: View of entity '${entity.id}' is null!")
} else error("KorgeViewCache: Entity '${entity.id}' is out of range on get!")
fun addOrUpdate(entity: Entity, view: View) {
if (entity.id >= views.size) {
views = views.copyOf(max(views.size * 2, entity.id + 1))
}
views[entity.id] = view
}

fun getOrNull(entity: Entity) : View? {
return if (VIEWS.size > entity.id) VIEWS[entity.id] // Cache potentially has this view. However, return value can still be null!
else null
}
fun remove(entity: Entity) {
if (views.size > entity.id) {
views[entity.id] = null
} else error("KorgeViewCache: Entity '${entity.id}' is out of range on remove!")
}

operator fun get(entity: Entity) : View {
return if (views.size > entity.id) {
views[entity.id] ?: error("KorgeViewCache: View of entity '${entity.id}' is null!")
} else error("KorgeViewCache: Entity '${entity.id}' is out of range on get!")
}

fun getOrNull(entity: Entity) : View? {
return if (views.size > entity.id) views[entity.id] // Cache potentially has this view. However, return value can still be null!
else null
}

fun getLayer(entity: Entity, name: String) : View {
return when (val view = get(entity)) {
is ImageDataViewEx -> view.getLayer(name)
?: error("KorgeViewCache: Could not find layer '$name' from ImageAnimView entity '${entity.id}'!")
is ParallaxDataView -> view.getLayer(name)
?: error("KorgeViewCache: Could not find layer '$name' from ParallaxDataView entity '${entity.id}'!")
else -> error("KorgeViewCache: View does not have getLayer function!")
}
fun getLayer(entity: Entity, name: String) : View {
return when (val view = get(entity)) {
is ImageDataViewEx -> view.getLayer(name)
?: error("KorgeViewCache: Could not find layer '$name' from ImageAnimView entity '${entity.id}'!")
is ParallaxDataView -> view.getLayer(name)
?: error("KorgeViewCache: Could not find layer '$name' from ParallaxDataView entity '${entity.id}'!")
else -> error("KorgeViewCache: View does not have getLayer function!")
}
}
}

0 comments on commit df86e85

Please sign in to comment.