colour-changing durability and encumbrance bar

This commit is contained in:
minjaesong
2019-03-12 02:29:58 +09:00
parent 73b18e1252
commit e1340aac29
5 changed files with 56 additions and 18 deletions

View File

@@ -5,12 +5,12 @@ import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion
import net.torvald.colourutil.CIELabUtil.darkerLab
import net.torvald.terrarum.itemproperties.GameItem
import net.torvald.terrarum.itemproperties.ItemCodex
import net.torvald.terrarum.modulebasegame.Ingame
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellBase
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes
import net.torvald.terrarum.ui.UIItemTextButton
/***
@@ -45,8 +45,6 @@ class UIItemInventoryElem(
val height = 48
val UNIQUE_ITEM_HAS_NO_AMOUNT = -1
internal val durabilityCol = Color(0x22ff11_ff)
internal val durabilityBack: Color; get() = durabilityCol.darkerLab(0.4f)
internal val durabilityBarThickness = 3f
}
@@ -126,11 +124,14 @@ class UIItemInventoryElem(
// durability metre
val barFullLen = (width - 8f) - textOffsetX
val barOffset = posX + textOffsetX
val percentage = if (item!!.maxDurability < 0.00001f) 0f else item!!.durability / item!!.maxDurability
val durabilityCol = UIItemInventoryCellCommonRes.getHealthMeterColour(percentage, 0f, 1f)
val durabilityBack = durabilityCol mul UIItemInventoryCellCommonRes.meterBackDarkening
if (item!!.maxDurability > 0.0) {
batch.color = durabilityBack
batch.drawStraightLine(barOffset, posY + durabilityBarOffY, barOffset + barFullLen, durabilityBarThickness, false)
batch.color = durabilityCol
batch.drawStraightLine(barOffset, posY + durabilityBarOffY, barOffset + barFullLen * (item!!.durability / item!!.maxDurability), durabilityBarThickness, false)
batch.drawStraightLine(barOffset, posY + durabilityBarOffY, barOffset + barFullLen * percentage, durabilityBarThickness, false)
}

View File

@@ -10,6 +10,7 @@ import net.torvald.terrarum.itemproperties.ItemCodex
import net.torvald.terrarum.modulebasegame.Ingame
import net.torvald.terrarum.modulebasegame.ui.UIInventoryFull
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellBase
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes
import net.torvald.terrarum.ui.UIItemTextButton
/**
@@ -94,11 +95,14 @@ class UIItemInventoryElemSimple(
val barFullLen = width
val barOffset = posX.toFloat()
val thickness = UIItemInventoryElem.durabilityBarThickness
val percentage = item!!.durability / item!!.maxDurability
val durabilityCol = UIItemInventoryCellCommonRes.getHealthMeterColour(percentage, 0f, 1f)
val durabilityBack = durabilityCol mul UIItemInventoryCellCommonRes.meterBackDarkening
if (item!!.maxDurability > 0.0) {
batch.color = UIItemInventoryElem.durabilityBack
batch.color = durabilityBack
batch.drawStraightLine(barOffset, posY + height - thickness, barOffset + barFullLen, thickness, false)
batch.color = UIItemInventoryElem.durabilityCol
batch.drawStraightLine(barOffset, posY + height - thickness, barOffset + barFullLen * (item!!.durability / item!!.maxDurability), thickness, false)
batch.color = durabilityCol
batch.drawStraightLine(barOffset, posY + height - thickness, barOffset + barFullLen * percentage, thickness, false)
}
}
// draw item count when applicable

View File

@@ -7,6 +7,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.glutils.FrameBuffer
import com.badlogic.gdx.graphics.glutils.ShapeRenderer
import net.torvald.terrarum.*
import net.torvald.terrarum.AppLoader.IS_DEVELOPMENT_BUILD
import net.torvald.terrarum.AppLoader.printdbg
import net.torvald.terrarum.Terrarum.gamepadLabelEast
import net.torvald.terrarum.Terrarum.gamepadLabelLStick
@@ -229,7 +230,7 @@ class UIInventoryFull(
private val shapeRenderer = ShapeRenderer()
private val gradHeight = 48f
private val weightBarWidth = 60f
private val weightBarWidth = 64f
private var xEnd = (Terrarum.WIDTH + internalWidth).div(2).toFloat()
private var yEnd = (Terrarum.HEIGHT + internalHeight).div(2).toFloat()
@@ -465,8 +466,6 @@ class UIInventoryFull(
gameMenuButtons.render(batch, camera)
}
private val encumbranceBarBackCol = Color(0xa0a0a0_ff.toInt())
private fun renderScreenInventory(batch: SpriteBatch, camera: Camera) {
itemList.render(batch, camera)
equipped.render(batch, camera)
@@ -483,31 +482,41 @@ class UIInventoryFull(
Terrarum.fontGame.draw(batch,
encumbranceText,
xEnd - 9 - Terrarum.fontGame.getWidth(encumbranceText) - weightBarWidth + inventoryScrOffX,
xEnd - 6 - Terrarum.fontGame.getWidth(encumbranceText) - weightBarWidth + inventoryScrOffX,
yEnd-20
)
// encumbrance bar background
blendMul(batch)
batch.color = encumbranceBarBackCol
blendNormal(batch)
val encumbCol = UIItemInventoryCellCommonRes.getHealthMeterColour(1f - encumbrancePerc, 0f, 1f)
val encumbBack = encumbCol mul UIItemInventoryCellCommonRes.meterBackDarkening
batch.color = encumbBack
batch.fillRect(
xEnd - 3 - weightBarWidth + inventoryScrOffX,
xEnd - weightBarWidth + inventoryScrOffX,
yEnd-20 + 3f,
weightBarWidth,
controlHelpHeight - 6f
)
// encumbrance bar
blendNormal(batch)
batch.color = if (isEncumbered) Color(0xff0000_cc.toInt()) else Color(0x00ff00_cc.toInt())
batch.color = encumbCol
batch.fillRect(
xEnd - 3 - weightBarWidth + inventoryScrOffX,
xEnd - weightBarWidth + inventoryScrOffX,
yEnd-20 + 3f,
if (actor.inventory.capacityMode == CAPACITY_MODE_NO_ENCUMBER)
1f
else // make sure 1px is always be seen
minOf(weightBarWidth, maxOf(1f, weightBarWidth * encumbrancePerc)),
controlHelpHeight - 5f
controlHelpHeight - 6f
)
// debug text
batch.color = Color.LIGHT_GRAY
if (IS_DEVELOPMENT_BUILD) {
AppLoader.fontSmallNumbers.draw(batch,
"${actor.inventory.capacity}/${actor.inventory.maxCapacity}",
xEnd - 6 - Terrarum.fontGame.getWidth(encumbranceText) - weightBarWidth + inventoryScrOffX,
yEnd-20 + 3f + controlHelpHeight - 4f
)
}
}

View File

@@ -1,10 +1,14 @@
package net.torvald.terrarum.modulebasegame.ui
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.g2d.TextureRegion
import net.torvald.terrarum.GdxColorMap
import net.torvald.terrarum.itemproperties.GameItem
import net.torvald.terrarum.ui.UIItem
import kotlin.math.roundToInt
/**
* Cross section of two inventory cell types
@@ -23,4 +27,24 @@ abstract class UIItemInventoryCellBase(
) : UIItem(parentUI) {
abstract override fun update(delta: Float)
abstract override fun render(batch: SpriteBatch, camera: Camera)
}
object UIItemInventoryCellCommonRes {
val meterColourMap = GdxColorMap(Gdx.files.internal("./assets/clut/health_bar_colouring_4096.tga"))
val meterBackDarkening = Color(0x828282ff.toInt())
fun getHealthMeterColour(value: Float, start: Float, end: Float): Color {
if (start > end) throw IllegalArgumentException("Start value is greater than end value: $start..$end")
return if (value <= start)
meterColourMap[0]
else if (value >= end)
meterColourMap[meterColourMap.width - 1]
else {
val scale = (value - start) / (end - start)
meterColourMap[scale.times(meterColourMap.width - 1).roundToInt()]
}
}
fun getHealthMeterColour(value: Int, start: Int, end: Int) = getHealthMeterColour(value.toFloat(), start.toFloat(), end.toFloat())
}