distinguishable item placeholder image

This commit is contained in:
minjaesong
2019-03-11 00:20:08 +09:00
parent d7846bf332
commit 03df390e38
6 changed files with 41 additions and 19 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -1,22 +1,38 @@
package net.torvald.terrarum
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.utils.Disposable
import com.badlogic.gdx.utils.Queue
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
/**
* Created by minjaesong on 2019-03-10.
*/
object CommonResourcePool {
private val loadingList = Queue<Pair<Pair<String, Class<*>>, () -> Disposable>>()
private val pool = HashMap<String, Disposable>()
private val loadingList = Queue<Pair<String, () -> Any>>()
private val pool = HashMap<String, Any>()
//private val typesMap = HashMap<String, Class<*>>()
private var loadCounter = -1 // using counters so that the loading can be done on separate thread (gg if the asset requires GL context to be loaded)
val loaded: Boolean
get() = loadCounter == 0
fun <T> addToLoadingList(identifier: String, type: Class<T>, loadFunction: () -> Disposable) {
loadingList.addFirst(identifier to type to loadFunction)
init {
addToLoadingList("itemplaceholder_24") {
val t = TextureRegion(Texture("assets/item_kari_24.tga"))
t.flip(false, true)
/*return*/t
}
addToLoadingList("itemplaceholder_48") {
val t = TextureRegion(Texture("assets/item_kari_48.tga"))
t.flip(false, true)
/*return*/t
}
}
fun addToLoadingList(identifier: String, loadFunction: () -> Any) {
loadingList.addFirst(identifier to loadFunction)
if (loadCounter == -1)
loadCounter = 1
@@ -31,8 +47,7 @@ object CommonResourcePool {
if (loaded) throw IllegalStateException("Assets are already loaded and shipped out :p")
while (!loadingList.isEmpty) {
val (k, loadfun) = loadingList.removeFirst()
val (name, type) = k
val (name, loadfun) = loadingList.removeFirst()
if (pool.containsKey(name)) {
throw IllegalArgumentException("Assets with identifier '$name' already exists.")
@@ -45,16 +60,24 @@ object CommonResourcePool {
}
}
operator fun get(identifier: String): Disposable {
val obj = pool[identifier]!!
return obj
operator fun get(identifier: String): Any {
return pool[identifier]!!
}
fun getAsTextureRegionPack(identifier: String) = get(identifier) as TextureRegionPack
fun getAsTextureRegion(identifier: String) = get(identifier) as TextureRegion
fun getAsTexture(identifier: String) = get(identifier) as Texture
fun dispose() {
pool.forEach { _, u ->
try {
if (u is Disposable)
u.dispose()
if (u is Texture)
u.dispose()
if (u is TextureRegion)
u.texture.dispose()
// TODO
}
catch (e: Throwable) {
e.printStackTrace()

View File

@@ -31,7 +31,7 @@ object LoadScreen : ScreenAdapter() {
private var arrowObjPos = 0f // 0 means at starting position, regardless of screen position
private var arrowObjGlideOffsetX = 0f
private var arrowObjGlideSize = 0f
private val arrowGlideSpeed: Float; get() = Terrarum.WIDTH / 2f // pixels per sec
private val arrowGlideSpeed: Float; get() = Terrarum.WIDTH * 2f // pixels per sec
private lateinit var arrowObjTex: Texture
private var glideTimer = 0f
private var glideDispY = 0f

View File

@@ -9,7 +9,6 @@ import net.torvald.terrarum.gameworld.GameWorld
import net.torvald.terrarum.modulebasegame.Ingame
import net.torvald.terrarum.modulebasegame.gameactors.CanBeAnItem
import net.torvald.terrarum.worlddrawer.BlocksDrawer
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
import java.util.*
/**
@@ -32,7 +31,7 @@ object ItemCodex {
val ACTORID_MIN = ITEM_DYNAMIC.endInclusive + 1
private val itemImagePlaceholder: TextureRegion
get() = (AppLoader.resourcePool["basegame.items24"] as TextureRegionPack).get(0,0) // copper pickaxe
get() = AppLoader.resourcePool.getAsTextureRegion("itemplaceholder_24") // copper pickaxe
//private val ingame = Terrarum.ingame!! as Ingame // WARNING you can't put this here, ExceptionInInitializerError

View File

@@ -31,13 +31,13 @@ class EntryPoint : ModuleEntryPoint() {
// load common resources to the AssetsManager
AppLoader.resourcePool.addToLoadingList("$moduleName.items16", TextureRegionPack.javaClass) {
AppLoader.resourcePool.addToLoadingList("$moduleName.items16") {
TextureRegionPack(ModMgr.getGdxFile(moduleName, "items/items.tga"), 16, 16)
}
AppLoader.resourcePool.addToLoadingList("$moduleName.items24", TextureRegionPack.javaClass) {
AppLoader.resourcePool.addToLoadingList("$moduleName.items24") {
TextureRegionPack(ModMgr.getGdxFile(moduleName, "items/items24.tga"), 24, 24)
}
AppLoader.resourcePool.addToLoadingList("$moduleName.items48", TextureRegionPack.javaClass) {
AppLoader.resourcePool.addToLoadingList("$moduleName.items48") {
TextureRegionPack(ModMgr.getGdxFile(moduleName, "items/items48.tga"), 48, 48)
}