code for new itemsheet format

This commit is contained in:
minjaesong
2023-06-06 14:37:54 +09:00
parent ac53f821e2
commit eb2c716691
8 changed files with 65 additions and 21 deletions

View File

@@ -118,6 +118,7 @@ object CommonResourcePool {
fun getAsTextureRegionPack(identifier: String) = getAs<TextureRegionPack>(identifier)
fun getAsTextureRegion(identifier: String) = getAs<TextureRegion>(identifier)
fun getAsTexture(identifier: String) = getAs<Texture>(identifier)
fun getAsItemSheet(identifier: String) = getAs<ItemSheet>(identifier)
fun dispose() {
pool.forEach { (name, u) ->

View File

@@ -0,0 +1,49 @@
package net.torvald.terrarum
import com.badlogic.gdx.files.FileHandle
import com.badlogic.gdx.graphics.Pixmap
import com.badlogic.gdx.graphics.g2d.TextureRegion
import com.badlogic.gdx.utils.Disposable
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
/**
* Created by minjaesong on 2023-06-06.
*/
class ItemSheet(ref: FileHandle, tileW: Int = 48, tileH: Int = 48) : Disposable {
private val textureRegionPack = TextureRegionPack(ref, tileW, tileH + 1)
init {
val pixmap = Pixmap(ref)
for (y in 0 until textureRegionPack.verticalCount) {
for (x in 0 until textureRegionPack.horizontalCount) {
var w = 0
var h = 0
for (i in 0..7) {
// width
w = w or (pixmap.getPixel(x * tileW + i, y * (tileH + 1)).and(255) > 127).toInt(7 - i)
// height
h = h or (pixmap.getPixel(x * tileW + i + 8, y * (tileH + 1)).and(255) > 127).toInt(7 - i)
}
textureRegionPack.get(x, y).apply {
this.setRegion(x * tileW, y * (tileH + 1) + 1, w, h)
}
// println("[ItemSheet] ${ref.path()} ($x,$y) dim ($w,$h)")
}
}
pixmap.dispose()
}
val horizontalCount = textureRegionPack.horizontalCount
val verticalCount = textureRegionPack.verticalCount
fun get(x: Int, y: Int) = textureRegionPack.get(x, y)
fun forEach(action: (TextureRegion) -> Unit) = textureRegionPack.regions.forEach(action)
override fun dispose() {
textureRegionPack.dispose()
}
}

View File

@@ -627,7 +627,7 @@ fun Double.sqrt() = Math.sqrt(this)
fun Float.sqrt() = FastMath.sqrt(this)
fun Int.abs() = this.absoluteValue
fun Double.bipolarClamp(limit: Double) = this.coerceIn(-limit, limit)
fun Boolean.toInt() = if (this) 1 else 0
fun Boolean.toInt(shift: Int = 0) = if (this) 1.shl(shift) else 0
fun Int.bitCount() = java.lang.Integer.bitCount(this)
fun Long.bitCount() = java.lang.Long.bitCount(this)

View File

@@ -28,14 +28,8 @@ class EntryPoint : ModuleEntryPoint() {
printdbg(this, "Hello, world!")
// load common resources to the AssetsManager
CommonResourcePool.addToLoadingList("$moduleName.items16") {
TextureRegionPack(ModMgr.getGdxFile(moduleName, "items/items.tga"), 16, 16)
}
CommonResourcePool.addToLoadingList("$moduleName.items24") {
TextureRegionPack(ModMgr.getGdxFile(moduleName, "items/items24.tga"), 24, 24)
}
CommonResourcePool.addToLoadingList("$moduleName.items48") {
TextureRegionPack(ModMgr.getGdxFile(moduleName, "items/items48.tga"), 48, 48)
CommonResourcePool.addToLoadingList("$moduleName.items") {
ItemSheet(ModMgr.getGdxFile(moduleName, "items/items.tga"))
}
CommonResourcePool.loadAll()

View File

@@ -121,7 +121,7 @@ class PickaxeCopper(originalID: ItemID) : GameItem(originalID) {
override val materialId = "CUPR"
override var baseMass = material.density.toDouble() / MaterialCodex["IRON"].density * BASE_MASS_AND_SIZE
override val itemImage: TextureRegion
get() = CommonResourcePool.getAsTextureRegionPack("basegame.items24").get(0,0)
get() = CommonResourcePool.getAsItemSheet("basegame.items").get(0,0)
init {
super.equipPosition = GameItem.EquipPosition.HAND_GRIP
@@ -151,7 +151,7 @@ class PickaxeIron(originalID: ItemID) : GameItem(originalID) {
override val materialId = "IRON"
override var baseMass = material.density.toDouble() / MaterialCodex["IRON"].density * BASE_MASS_AND_SIZE
override val itemImage: TextureRegion
get() = CommonResourcePool.getAsTextureRegionPack("basegame.items24").get(1,0)
get() = CommonResourcePool.getAsItemSheet("basegame.items").get(1,0)
init {
super.equipPosition = GameItem.EquipPosition.HAND_GRIP
@@ -181,7 +181,7 @@ class PickaxeSteel(originalID: ItemID) : GameItem(originalID) {
override val materialId = "STAL"
override var baseMass = material.density.toDouble() / MaterialCodex["IRON"].density * BASE_MASS_AND_SIZE
override val itemImage: TextureRegion
get() = CommonResourcePool.getAsTextureRegionPack("basegame.items24").get(2,0)
get() = CommonResourcePool.getAsItemSheet("basegame.items").get(2,0)
init {
super.equipPosition = GameItem.EquipPosition.HAND_GRIP

View File

@@ -94,7 +94,7 @@ class WireCutterAll(originalID: ItemID) : GameItem(originalID) {
override val isDynamic = false
override val materialId = ""
override val itemImage: TextureRegion
get() = CommonResourcePool.getAsTextureRegionPack("basegame.items16").get(0, 9)
get() = CommonResourcePool.getAsItemSheet("basegame.items").get(1, 3)
init {
super.equipPosition = GameItem.EquipPosition.HAND_GRIP

View File

@@ -24,7 +24,7 @@ class WirePieceSignalWire(originalID: ItemID, private val atlasID: String, priva
override val isDynamic = false
override val materialId = ""
override val itemImage: TextureRegion
get() = CommonResourcePool.getAsTextureRegionPack(atlasID).get(sheetX, sheetY)
get() = CommonResourcePool.getAsItemSheet(atlasID).get(sheetX, sheetY)
init {
super.equipPosition = GameItem.EquipPosition.HAND_GRIP