diff --git a/assets/mods/basegame/materials/materials.csv b/assets/mods/basegame/materials/materials.csv index 803b63d5a..02021f213 100644 --- a/assets/mods/basegame/materials/materials.csv +++ b/assets/mods/basegame/materials/materials.csv @@ -8,7 +8,7 @@ "EAUR"; "768"; "0";"8192"; "21"; "1.36"; "1.0";"elven aurichalcum" "TIAL"; "900"; "0";"4420"; "33"; "2.16"; "1.0";"titanium alloy (Ti6Al4V)" "ADMT";"2000";"4090";"2700"; "71"; "3.42"; "1.0";"adamant" -"OOZE"; "20"; "0";"1000"; "1"; "N/A"; "0.5";"genetic ooze; data is about human skin" +"OOZE"; "20"; "0";"1000"; "1"; "0.08"; "0.5";"genetic ooze; data is about human skin" "BONE"; "130"; "0";"2000"; "1"; "0.23"; "0.55";"data is that of bovine bone" # idst: ID_STRING, ALL CAPS diff --git a/src/net/torvald/terrarum/UIItemInventoryCatBar.kt b/src/net/torvald/terrarum/UIItemInventoryCatBar.kt index 0b3fc37f7..4859119ee 100644 --- a/src/net/torvald/terrarum/UIItemInventoryCatBar.kt +++ b/src/net/torvald/terrarum/UIItemInventoryCatBar.kt @@ -20,10 +20,6 @@ class UIItemInventoryCatBar( override val width: Int ) : UIItem(parentUI, initialX, initialY) { - // deal with the moving position - override var oldPosX = posX - override var oldPosY = posY - private val parentInventory = parentUI private val catIcons = parentUI.catIcons diff --git a/src/net/torvald/terrarum/UIItemInventoryElem.kt b/src/net/torvald/terrarum/UIItemInventoryElem.kt index 05a10a6c5..1123272db 100644 --- a/src/net/torvald/terrarum/UIItemInventoryElem.kt +++ b/src/net/torvald/terrarum/UIItemInventoryElem.kt @@ -41,10 +41,6 @@ class UIItemInventoryElem( val drawBackOnNull: Boolean = true ) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot) { - // deal with the moving position - override var oldPosX = posX - override var oldPosY = posY - companion object { val height = 48 val UNIQUE_ITEM_HAS_NO_AMOUNT = -1 diff --git a/src/net/torvald/terrarum/UIItemInventoryElemSimple.kt b/src/net/torvald/terrarum/UIItemInventoryElemSimple.kt index a0d1cac07..867b9049f 100644 --- a/src/net/torvald/terrarum/UIItemInventoryElemSimple.kt +++ b/src/net/torvald/terrarum/UIItemInventoryElemSimple.kt @@ -38,11 +38,7 @@ class UIItemInventoryElemSimple( override var equippedSlot: Int? = null, val drawBackOnNull: Boolean = true ) : UIItemInventoryCellBase(parentUI, initialX, initialY, item, amount, itemImage, quickslot, equippedSlot) { - - // deal with the moving position - override var oldPosX = posX - override var oldPosY = posY - + companion object { val height = UIItemInventoryElem.height } diff --git a/src/net/torvald/terrarum/blockproperties/BlockCodex.kt b/src/net/torvald/terrarum/blockproperties/BlockCodex.kt index c3244c980..07d5e54c4 100644 --- a/src/net/torvald/terrarum/blockproperties/BlockCodex.kt +++ b/src/net/torvald/terrarum/blockproperties/BlockCodex.kt @@ -178,6 +178,8 @@ fun str16ToInt(rec: CSVRecord, s: String): Int { } catch (e1: IllegalStateException) { } + catch (e2: NullPointerException) { + } return ret } @@ -191,6 +193,8 @@ fun intVal(rec: CSVRecord, s: String): Int { } catch (e1: IllegalStateException) { } + catch (e2: NullPointerException) { + } return ret } @@ -204,6 +208,8 @@ fun floatVal(rec: CSVRecord, s: String): Float { } catch (e1: IllegalStateException) { } + catch (e2: NullPointerException) { + } return ret } diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt new file mode 100644 index 000000000..c37c4c2ae --- /dev/null +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryCells.kt @@ -0,0 +1,145 @@ +package net.torvald.terrarum.modulebasegame.ui + +import com.badlogic.gdx.graphics.Camera +import com.badlogic.gdx.graphics.Color +import com.badlogic.gdx.graphics.g2d.SpriteBatch +import net.torvald.terrarum.* +import net.torvald.terrarum.gameactors.ActorWithBody +import net.torvald.terrarum.langpack.Lang +import net.torvald.terrarum.modulebasegame.gameactors.ActorInventory +import net.torvald.terrarum.ui.UICanvas +import kotlin.math.roundToInt + +internal class UIInventoryCells( + val full: UIInventoryFull +) : UICanvas() { + + override var width: Int = AppLoader.screenW + override var height: Int = AppLoader.screenH + override var openCloseTime: Second = 0.0f + + + private val weightBarWidth = UIItemInventoryElemSimple.height * 2f + UIItemInventoryDynamicList.listGap + + internal var encumbrancePerc = 0f + private set + internal var isEncumbered = false + private set + + + internal val itemList: UIItemInventoryDynamicList = + UIItemInventoryDynamicList( + full, + full.actor.inventory, + 0 + (AppLoader.screenW - full.internalWidth) / 2, + 107 + (AppLoader.screenH - full.internalHeight) / 2, + full.CELLS_HOR, full.CELLS_VRT + ) + + + private val equipped: UIItemInventoryEquippedView = + UIItemInventoryEquippedView( + full, + full.actor.inventory, + full.actor as ActorWithBody, + full.internalWidth - UIItemInventoryEquippedView.WIDTH + (AppLoader.screenW - full.internalWidth) / 2, + 107 + (AppLoader.screenH - full.internalHeight) / 2 + ) + + fun rebuildList() { + AppLoader.printdbg(this, "rebuilding list") + + itemList.rebuild(full.catIconsMeaning[full.categoryBar.selectedIcon]) + equipped.rebuild() + + encumbrancePerc = full.actor.inventory.capacity.toFloat() / full.actor.inventory.maxCapacity + isEncumbered = full.actor.inventory.isEncumbered + } + + fun resetStatusAsCatChanges(oldcat: Int?, newcat: Int) { + itemList.itemPage = 0 // set scroll to zero + itemList.rebuild(full.catIconsMeaning[full.catArrangement[newcat]]) // have to manually rebuild, too! + } + + override fun updateUI(delta: Float) { + itemList.update(delta) + equipped.update(delta) + } + + override fun renderUI(batch: SpriteBatch, camera: Camera) { + //itemList.posX = itemList.initialX + inventoryScrOffX.roundToInt() + itemList.render(batch, camera) + //equipped.posX = equipped.initialX + inventoryScrOffX.roundToInt() + equipped.render(batch, camera) + + + // control hints + val controlHintXPos = full.offsetX + blendNormal(batch) + batch.color = Color.WHITE + AppLoader.fontGame.draw(batch, full.listControlHelp, controlHintXPos, full.yEnd - 20) + + + // encumbrance meter + val encumbranceText = Lang["GAME_INVENTORY_ENCUMBRANCE"] + // encumbrance bar will go one row down if control help message is too long + val encumbBarXPos = full.xEnd - weightBarWidth + val encumbBarTextXPos = encumbBarXPos - 6 - AppLoader.fontGame.getWidth(encumbranceText) + val encumbBarYPos = full.yEnd-20 + 3f + + if (AppLoader.fontGame.getWidth(full.listControlHelp) + 2 + controlHintXPos >= encumbBarTextXPos) + AppLoader.fontGame.lineHeight + else 0f + + AppLoader.fontGame.draw(batch, + encumbranceText, + encumbBarTextXPos, + encumbBarYPos - 3f + ) + + // encumbrance bar background + blendNormal(batch) + val encumbCol = UIItemInventoryCellCommonRes.getHealthMeterColour(1f - encumbrancePerc, 0f, 1f) + val encumbBack = encumbCol mul UIItemInventoryCellCommonRes.meterBackDarkening + batch.color = encumbBack + batch.fillRect( + encumbBarXPos, encumbBarYPos, + weightBarWidth, full.controlHelpHeight - 6f + ) + // encumbrance bar + batch.color = encumbCol + batch.fillRect( + encumbBarXPos, encumbBarYPos, + if (full.actor.inventory.capacityMode == ActorInventory.CAPACITY_MODE_NO_ENCUMBER) + 1f + else // make sure 1px is always be seen + minOf(weightBarWidth, maxOf(1f, weightBarWidth * encumbrancePerc)), + full.controlHelpHeight - 6f + ) + // debug text + batch.color = Color.LIGHT_GRAY + if (AppLoader.IS_DEVELOPMENT_BUILD) { + AppLoader.fontSmallNumbers.draw(batch, + "${full.actor.inventory.capacity}/${full.actor.inventory.maxCapacity}", + encumbBarTextXPos, + encumbBarYPos + full.controlHelpHeight - 4f + ) + } + } + + override fun doOpening(delta: Float) { + } + + override fun doClosing(delta: Float) { + } + + override fun endOpening(delta: Float) { + } + + override fun endClosing(delta: Float) { + } + + override fun dispose() { + itemList.dispose() + equipped.dispose() + } +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryFull.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryFull.kt index bdd0a86d6..e98a90f2e 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryFull.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIInventoryFull.kt @@ -1,29 +1,21 @@ package net.torvald.terrarum.modulebasegame.ui import com.badlogic.gdx.Gdx -import com.badlogic.gdx.Input import com.badlogic.gdx.graphics.* import com.badlogic.gdx.graphics.g2d.SpriteBatch -import com.badlogic.gdx.graphics.glutils.FrameBuffer import com.badlogic.gdx.graphics.glutils.ShapeRenderer import net.torvald.ENDASH import net.torvald.terrarum.* import net.torvald.terrarum.AppLoader.* import net.torvald.terrarum.blockstats.MinimapComposer -import net.torvald.terrarum.gameactors.ActorWithBody import net.torvald.terrarum.gameitem.GameItem import net.torvald.terrarum.langpack.Lang import net.torvald.terrarum.modulebasegame.TerrarumIngame -import net.torvald.terrarum.modulebasegame.gameactors.ActorInventory.Companion.CAPACITY_MODE_NO_ENCUMBER import net.torvald.terrarum.modulebasegame.gameactors.Pocketed import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryDynamicList.Companion.CAT_ALL -import net.torvald.terrarum.ui.UICanvas -import net.torvald.terrarum.ui.UIItem -import net.torvald.terrarum.ui.UIItemTextButtonList +import net.torvald.terrarum.ui.* import net.torvald.terrarum.ui.UIItemTextButtonList.Companion.DEFAULT_LINE_HEIGHT -import net.torvald.terrarum.ui.UIUtils import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack -import kotlin.math.roundToInt /** * Created by minjaesong on 2017-10-21. @@ -41,11 +33,12 @@ class UIInventoryFull( override var width: Int = AppLoader.screenW override var height: Int = AppLoader.screenH + override var openCloseTime: Second = 0.0f - private val REQUIRED_MARGIN = 166 // hard-coded value. Don't know the details + val REQUIRED_MARGIN = 166 // hard-coded value. Don't know the details - private val CELLS_HOR = 10 - private val CELLS_VRT = (AppLoader.screenH - REQUIRED_MARGIN - 134 + UIItemInventoryDynamicList.listGap) / // 134 is another magic number + val CELLS_HOR = 10 + val CELLS_VRT = (AppLoader.screenH - REQUIRED_MARGIN - 134 + UIItemInventoryDynamicList.listGap) / // 134 is another magic number (UIItemInventoryElemSimple.height + UIItemInventoryDynamicList.listGap) private val itemListToEquipViewGap = UIItemInventoryDynamicList.listGap // used to be 24; figured out that the extra gap does nothig @@ -53,6 +46,8 @@ class UIInventoryFull( val internalWidth: Int = UIItemInventoryDynamicList.getEstimatedW(CELLS_HOR) + UIItemInventoryEquippedView.WIDTH + itemListToEquipViewGap val internalHeight: Int = REQUIRED_MARGIN + UIItemInventoryDynamicList.getEstimatedH(CELLS_VRT) // grad_begin..grad_end..contents..grad_begin..grad_end + val itemListHeight: Int = CELLS_VRT * UIItemInventoryElemSimple.height + (CELLS_VRT - 1) * net.torvald.terrarum.modulebasegame.ui.UIItemInventoryDynamicList.Companion.listGap + init { handler.allowESCtoClose = true CommonResourcePool.addToLoadingList("inventory_caticons") { @@ -107,10 +102,6 @@ class UIInventoryFull( "$gamepadLabelLT ${Lang["GAME_INVENTORY"]}" val controlHelpHeight = AppLoader.fontGame.lineHeight - private var encumbrancePerc = 0f - private var isEncumbered = false - - val catBarWidth = 330 val categoryBar = UIItemInventoryCatBar( this, @@ -120,34 +111,27 @@ class UIInventoryFull( ) - override var openCloseTime: Second = 0.0f + private val transitionalMinimap = UIInventoryCells(this) // PLACEHOLDER + private val transitionalItemCells = UIInventoryCells(this) + private val transitionalEscMenu = UIInventoryCells(this) // PLACEHOLDER + private val transitionPanel = UIItemHorizontalFadeSlide( + this, + (AppLoader.screenW - internalWidth) / 2, + 107 + (AppLoader.screenH - internalHeight) / 2, + AppLoader.screenW, + AppLoader.screenH, + 1f, + transitionalMinimap, transitionalItemCells, transitionalEscMenu + ) - internal val itemList: UIItemInventoryDynamicList = - UIItemInventoryDynamicList( - this, - actor.inventory, - 0 + (AppLoader.screenW - internalWidth) / 2, - 107 + (AppLoader.screenH - internalHeight) / 2, - CELLS_HOR, CELLS_VRT - ) - - - private val equipped: UIItemInventoryEquippedView = - UIItemInventoryEquippedView( - this, - actor.inventory, - actor as ActorWithBody, - internalWidth - UIItemInventoryEquippedView.WIDTH + (AppLoader.screenW - internalWidth) / 2, - 107 + (AppLoader.screenH - internalHeight) / 2 - ) private val gameMenu = arrayOf("MENU_LABEL_MAINMENU", "MENU_LABEL_DESKTOP", "MENU_OPTIONS_CONTROLS", "MENU_OPTIONS_SOUND", "MENU_LABEL_GRAPHICS") private val gameMenuListHeight = DEFAULT_LINE_HEIGHT * gameMenu.size private val gameMenuListWidth = 400 private val gameMenuButtons = UIItemTextButtonList( this, gameMenu, (AppLoader.screenW - gameMenuListWidth) / 2, - (itemList.height - gameMenuListHeight) / 2 + itemList.posY, + (transitionalItemCells.itemList.height - gameMenuListHeight) / 2 + transitionalItemCells.itemList.posY, gameMenuListWidth, gameMenuListHeight, readFromLang = true, textAreaWidth = gameMenuListWidth, @@ -162,31 +146,19 @@ class UIInventoryFull( private val SCREEN_INVENTORY = 1f private val SCREEN_MENU = 2f - /** 0..2 where 0 is minimap, 1 is inventory, 2 is menu. Non-integer value means transition is on-going */ - private var currentScreenTransition = SCREEN_INVENTORY - private var transitionRequested = false - private var transitionOngoing = false - private var transitionReqSource = SCREEN_INVENTORY - private var transitionReqTarget = SCREEN_INVENTORY - private var transitionTimer = 0f - private val transitionLength = 0.212f - - - private val transitionalUpdateUIs = ArrayList() - private val transitionalUpdateUIoriginalPosX = ArrayList() init { - addItem(categoryBar) - itemList.let { addItem(it) } - equipped.let { addItem(it) } + addUIitem(categoryBar) + addUIitem(transitionPanel) + //itemList.let { addUIitem(it) } + //equipped.let { addUIitem(it) } categoryBar.selectionChangeListener = { old, new -> rebuildList() - itemList.itemPage = 0 // set scroll to zero - itemList.rebuild(catIconsMeaning[catArrangement[new]]) // have to manually rebuild, too! + transitionalItemCells.resetStatusAsCatChanges(old, new) } @@ -204,10 +176,12 @@ class UIInventoryFull( } - private var offsetX = ((AppLoader.screenW - internalWidth) / 2).toFloat() - private var offsetY = ((AppLoader.screenH - internalHeight) / 2).toFloat() - + internal var offsetX = ((AppLoader.screenW - internalWidth) / 2).toFloat() + private set + internal var offsetY = ((AppLoader.screenH - internalHeight) / 2).toFloat() + private set + fun requestTransition(target: Int) = transitionPanel.requestTransition(target) override fun updateUI(delta: Float) { if (handler.openFired) { @@ -216,25 +190,25 @@ class UIInventoryFull( categoryBar.update(delta) + transitionPanel.update(delta) - transitionalUpdateUIs.forEach { it.update(delta) } // update map while visible - if (currentScreenTransition > 1f + epsilon) { + /*if (currentScreenTransition > 1f + epsilon) { MinimapComposer.setWorld(Terrarum.ingame!!.world) MinimapComposer.update() - } + }*/ // update inventory while visible - if (currentScreenTransition in epsilon..2f - epsilon) { + /*if (currentScreenTransition in epsilon..2f - epsilon) { itemList.update(delta) equipped.update(delta) - } + }*/ // update menu while visible - if (currentScreenTransition < 1f - epsilon) { + /*if (currentScreenTransition < 1f - epsilon) { gameMenuButtons.update(delta) - } + }*/ minimapRerenderTimer += Gdx.graphics.rawDeltaTime } @@ -244,50 +218,25 @@ class UIInventoryFull( private val shapeRenderer = ShapeRenderer() private val gradHeight = 48f - private val weightBarWidth = UIItemInventoryElemSimple.height * 2f + UIItemInventoryDynamicList.listGap - - private var xEnd = (AppLoader.screenW + internalWidth).div(2).toFloat() - private var yEnd = (AppLoader.screenH + internalHeight).div(2).toFloat() + internal var xEnd = (AppLoader.screenW + internalWidth).div(2).toFloat() + private set + internal var yEnd = (AppLoader.screenH + internalHeight).div(2).toFloat() + private set private var minimapRerenderTimer = 0f private val minimapRerenderInterval = .5f - fun requestTransition(target: Int) { + // TODO implemented on UIItemTransitionContainer + /*fun requestTransition(target: Int) { if (!transitionOngoing) { transitionRequested = true transitionReqSource = currentScreenTransition.round() transitionReqTarget = target.toFloat() } - } + }*/ override fun renderUI(batch: SpriteBatch, camera: Camera) { - if (transitionRequested && !transitionOngoing) { - transitionRequested = false - transitionOngoing = true - transitionTimer = 0f - } - - if (transitionOngoing) { - transitionTimer += Gdx.graphics.rawDeltaTime - - currentScreenTransition = UIUtils.moveLinear(transitionReqSource, transitionReqTarget, transitionTimer, transitionLength) - - if (transitionTimer > transitionLength) { - transitionOngoing = false - currentScreenTransition = transitionReqTarget - } - } - - - - // update at render time - if (debugvals) { - batch.color = Color.WHITE - AppLoader.fontSmallNumbers.draw(batch, "screen:$currentScreenTransition", 500f, 30f) - } - - // background fill batch.end() @@ -314,41 +263,41 @@ class UIInventoryFull( categoryBar.render(batch, camera) // render map while visible - if (currentScreenTransition > 1f + epsilon) { + /*if (currentScreenTransition > 1f + epsilon) { renderScreenMinimap(batch, camera) if (debugvals) { batch.color = Color.CORAL AppLoader.fontSmallNumbers.draw(batch, "Map", 300f, 10f) } - } + }*/ // render inventory while visible - if (currentScreenTransition in epsilon..2f - epsilon) { + /*if (currentScreenTransition in epsilon..2f - epsilon) { renderScreenInventory(batch, camera) if (debugvals) { batch.color = Color.CHARTREUSE AppLoader.fontSmallNumbers.draw(batch, "Inv", 350f, 10f) } - } + }*/ // render menu while visible - if (currentScreenTransition < 1f - epsilon) { + /*if (currentScreenTransition < 1f - epsilon) { renderScreenGamemenu(batch, camera) if (debugvals) { batch.color = Color.SKY AppLoader.fontSmallNumbers.draw(batch, "Men", 400f, 10f) } - } + }*/ - if (debugvals) { + /*if (debugvals) { batch.color = Color.WHITE AppLoader.fontSmallNumbers.draw(batch, "minimap:$minimapScrOffX", 500f, 0f) AppLoader.fontSmallNumbers.draw(batch, "inven:$inventoryScrOffX", 500f, 10f) AppLoader.fontSmallNumbers.draw(batch, "menu:$menuScrOffX", 500f, 20f) - } + }*/ } @@ -360,7 +309,7 @@ class UIInventoryFull( * - +WIDTH on minimap screen * - -WIDTH on gamemenu screen */ - private val minimapScrOffX: Float + /*private val minimapScrOffX: Float get() = (currentScreenTransition - 2f) * AppLoader.screenW / 2f private val inventoryScrOffX: Float get() = (currentScreenTransition - 1f) * AppLoader.screenW / 2f @@ -372,24 +321,24 @@ class UIInventoryFull( private val inventoryScrOpacity: Float get() = (currentScreenTransition - 1f).coerceIn(0f, 1f) private val menuScrOpacity: Float - get() = (currentScreenTransition - 0f).coerceIn(0f, 1f) + get() = (currentScreenTransition - 0f).coerceIn(0f, 1f)*/ - private val MINIMAP_WIDTH = 800f - private val MINIMAP_HEIGHT = itemList.height.toFloat() - private val MINIMAP_SKYCOL = Color(0x88bbddff.toInt()) - private var minimapZoom = 1f - private var minimapPanX = -MinimapComposer.totalWidth / 2f - private var minimapPanY = -MinimapComposer.totalHeight / 2f - private val MINIMAP_ZOOM_MIN = 0.5f - private val MINIMAP_ZOOM_MAX = 8f - private val minimapFBO = FrameBuffer(Pixmap.Format.RGBA8888, MINIMAP_WIDTH.toInt(), MINIMAP_HEIGHT.toInt(), false) - private val minimapCamera = OrthographicCamera(MINIMAP_WIDTH, MINIMAP_HEIGHT) + //private val MINIMAP_WIDTH = 800f + //private val MINIMAP_HEIGHT = itemList.height.toFloat() + //private val MINIMAP_SKYCOL = Color(0x88bbddff.toInt()) + //private var minimapZoom = 1f + //private var minimapPanX = -MinimapComposer.totalWidth / 2f + //private var minimapPanY = -MinimapComposer.totalHeight / 2f + //private val MINIMAP_ZOOM_MIN = 0.5f + //private val MINIMAP_ZOOM_MAX = 8f + //private val minimapFBO = FrameBuffer(Pixmap.Format.RGBA8888, MINIMAP_WIDTH.toInt(), MINIMAP_HEIGHT.toInt(), false) + //private val minimapCamera = OrthographicCamera(MINIMAP_WIDTH, MINIMAP_HEIGHT) // TODO put 3 bare sub-UIs into proper UIcanvas to handle the motherfucking opacity - - private fun renderScreenMinimap(batch: SpriteBatch, camera: Camera) { + // TODO not yet refactored + /*private fun renderScreenMinimap(batch: SpriteBatch, camera: Camera) { blendNormal(batch) // update map panning @@ -482,9 +431,10 @@ class UIInventoryFull( // the minimap batch.draw(minimapFBO.colorBufferTexture, minimapScrOffX + (AppLoader.screenW - MINIMAP_WIDTH) / 2, itemList.posY.toFloat()) - } + }*/ - private fun renderScreenGamemenu(batch: SpriteBatch, camera: Camera) { + // TODO not yet refactored + /*private fun renderScreenGamemenu(batch: SpriteBatch, camera: Camera) { // control hints blendNormal(batch) batch.color = Color.WHITE @@ -493,9 +443,10 @@ class UIInventoryFull( // text buttons gameMenuButtons.posX = gameMenuButtons.initialX + menuScrOffX.roundToInt() gameMenuButtons.render(batch, camera) - } + }*/ - private fun renderScreenInventory(batch: SpriteBatch, camera: Camera) { + // TODO refactoring wip + /*private fun renderScreenInventory(batch: SpriteBatch, camera: Camera) { itemList.posX = itemList.initialX + inventoryScrOffX.roundToInt() itemList.render(batch, camera) equipped.posX = equipped.initialX + inventoryScrOffX.roundToInt() @@ -553,17 +504,11 @@ class UIInventoryFull( encumbBarYPos + controlHelpHeight - 4f ) } - } + }*/ fun rebuildList() { - printdbg(this, "rebuilding list") - - itemList.rebuild(catIconsMeaning[categoryBar.selectedIcon]) - equipped.rebuild() - - encumbrancePerc = actor.inventory.capacity.toFloat() / actor.inventory.maxCapacity - isEncumbered = actor.inventory.isEncumbered + transitionalItemCells.rebuildList() } private fun Int.fastLen(): Int { @@ -582,8 +527,8 @@ class UIInventoryFull( override fun dispose() { categoryBar.dispose() - itemList.dispose() - equipped.dispose() + transitionPanel.dispose() + } diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryDynamicList.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryDynamicList.kt index efbd6dfab..db94ca239 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryDynamicList.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryDynamicList.kt @@ -40,8 +40,8 @@ class UIItemInventoryDynamicList( ) : UIItem(parentUI, initialX, initialY) { // deal with the moving position - override var oldPosX = posX - override var oldPosY = posY + //override var oldPosX = posX + //override var oldPosY = posY override val width = horizontalCells * UIItemInventoryElemSimple.height + (horizontalCells - 1) * listGap override val height = verticalCells * UIItemInventoryElemSimple.height + (verticalCells - 1) * listGap diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryEquippedView.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryEquippedView.kt index ad25e66cb..e0578453c 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryEquippedView.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemInventoryEquippedView.kt @@ -28,7 +28,7 @@ class UIItemInventoryEquippedView( override val width = WIDTH - override val height = parentUI.itemList.height + override val height = parentUI.itemListHeight companion object { val WIDTH = 2 * UIItemInventoryElemSimple.height + UIItemInventoryDynamicList.listGap @@ -71,10 +71,6 @@ class UIItemInventoryEquippedView( itemGrid.forEach { it.update(delta) } } - // deal with the moving position - override var oldPosX = posX - override var oldPosY = posY - override fun render(batch: SpriteBatch, camera: Camera) { val posXDelta = posX - oldPosX itemGrid.forEach { it.posX += posXDelta } diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemPlayerInfoCell.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemPlayerInfoCell.kt index ed0e6940b..0daf29dc7 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemPlayerInfoCell.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemPlayerInfoCell.kt @@ -25,10 +25,6 @@ class UIItemPlayerInfoCell( var ingamePlayer: IngamePlayer? = null ) : UIItem(parent, initialX, initialY) { - // deal with the moving position - override var oldPosX = posX - override var oldPosY = posY - override val height = HEIGHT companion object { diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemSavegameInfoCell.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemSavegameInfoCell.kt index 4ef01eceb..5e92350a8 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemSavegameInfoCell.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemSavegameInfoCell.kt @@ -21,10 +21,6 @@ class UIItemSavegameInfoCell( initialY: Int ) : UIItem(parent, initialX, initialY) { - // deal with the moving position - override var oldPosX = posX - override var oldPosY = posY - override val height: Int = AppLoader.fontGame.lineHeight.toInt() * 2 override fun render(batch: SpriteBatch, camera: Camera) { diff --git a/src/net/torvald/terrarum/modulebasegame/weather/WeatherMixer.kt b/src/net/torvald/terrarum/modulebasegame/weather/WeatherMixer.kt index 5b6362b44..b6650b9c8 100644 --- a/src/net/torvald/terrarum/modulebasegame/weather/WeatherMixer.kt +++ b/src/net/torvald/terrarum/modulebasegame/weather/WeatherMixer.kt @@ -300,13 +300,13 @@ internal object WeatherMixer : RNGConsumer { var mixFrom: String? try { mixFrom = JSON.get("mixFrom").asJsonPrimitive.asString } - catch (e: IllegalStateException) { mixFrom = null } + catch (e: NullPointerException) { mixFrom = null } var mixPercentage: Double? try { mixPercentage = JSON.get("mixPercentage").asJsonPrimitive.asDouble } - catch (e: IllegalStateException) { mixPercentage = null } + catch (e: NullPointerException) { mixPercentage = null } diff --git a/src/net/torvald/terrarum/tests/WorldgenNoiseSandbox.kt b/src/net/torvald/terrarum/tests/WorldgenNoiseSandbox.kt index e17fe39a6..d1edeab66 100644 --- a/src/net/torvald/terrarum/tests/WorldgenNoiseSandbox.kt +++ b/src/net/torvald/terrarum/tests/WorldgenNoiseSandbox.kt @@ -23,10 +23,11 @@ import net.torvald.terrarum.modulebasegame.worldgenerator.BiomegenParams import net.torvald.terrarum.modulebasegame.worldgenerator.TerragenParams import net.torvald.terrarum.modulebasegame.worldgenerator.shake import net.torvald.terrarum.worlddrawer.toRGBA +import java.util.concurrent.Future import kotlin.math.cos import kotlin.math.sin import kotlin.random.Random -import kotlinx.coroutines.* +import kotlin.coroutines.* const val WIDTH = 768 const val HEIGHT = 512 @@ -52,6 +53,8 @@ class WorldgenNoiseSandbox : ApplicationAdapter() { private var generationTimeInMeasure = false private var generationStartTime = 0L + private var genSlices: Int = 0 + private var genFutures: Array?> = arrayOfNulls(genSlices) override fun create() { font = BitmapFont() // use default because fuck it @@ -67,6 +70,8 @@ class WorldgenNoiseSandbox : ApplicationAdapter() { testTex.blending = Pixmap.Blending.None tempTex = Texture(1, 1, Pixmap.Format.RGBA8888) + genSlices = maxOf(ThreadExecutor.threadCount, testTex.width / 8) + println("Init done") } @@ -91,7 +96,7 @@ class WorldgenNoiseSandbox : ApplicationAdapter() { renderNoise() } - val coroutineExecFinished = coroutineJobs.fold(true) { acc, it -> acc and it.isCompleted } + val coroutineExecFinished = genFutures.fold(true) { acc, it -> acc and (it?.isDone ?: true) } // check if generation is done if (coroutineExecFinished) { generateKeyLatched = false @@ -158,12 +163,6 @@ class WorldgenNoiseSandbox : ApplicationAdapter() { } } - //private val xSlices = (0 until WIDTH).sliceEvenly(ThreadExecutor.threadCount) - private val xSlices = (0 until WIDTH).sliceEvenly(maxOf(WIDTH, ThreadExecutor.threadCount, WIDTH / 8)) - - private val runs = (0 until WIDTH).map { x -> (x until WIDTH * HEIGHT step WIDTH) }.flatten() - - private lateinit var coroutineJobs: List private fun renderNoise() { generationStartTime = System.nanoTime() @@ -220,7 +219,7 @@ class WorldgenNoiseSandbox : ApplicationAdapter() { } }*/ // 2. each runner gets their own copy of Joise - val runnables: List = xSlices.map { range -> { + val runnables: List = (0 until testTex.width).sliceEvenly(genSlices).map { range -> { val localJoise = getNoiseGenerator(seed) for (x in range) { for (y in 0 until HEIGHT) { @@ -235,7 +234,12 @@ class WorldgenNoiseSandbox : ApplicationAdapter() { } } - coroutineJobs = runnables.map { r -> GlobalScope.launch { r() } } + ThreadExecutor.renew() + runnables.forEach { + ThreadExecutor.submit(it) + } + + ThreadExecutor.join() initialGenDone = true } diff --git a/src/net/torvald/terrarum/ui/UICanvas.kt b/src/net/torvald/terrarum/ui/UICanvas.kt index 38e9ec7c6..29d2ba6d2 100644 --- a/src/net/torvald/terrarum/ui/UICanvas.kt +++ b/src/net/torvald/terrarum/ui/UICanvas.kt @@ -53,6 +53,13 @@ abstract class UICanvas( get() = handler.posY set(value) { handler.posY = value } + inline var initialX: Int + get() = handler.initialX + set(value) { handler.initialX = value } + inline var initialY: Int + get() = handler.initialY + set(value) { handler.initialY = value } + /** * Usage: (in StateInGame:) uiHandlerField.ui.handler = uiHandlerField */ @@ -103,9 +110,12 @@ abstract class UICanvas( } - /** Override this for the actual update. Note that you must update uiItems by yourself. */ + /** **DO NOT CALL THIS FUNCTION FOR THE ACTUAL UPDATING OF THE UI — USE update() INSTEAD** + * + * Override this for the actual update. Note that you must update uiItems by yourself. */ abstract fun updateUI(delta: Float) - /** + /** **DO NOT CALL THIS FUNCTION FOR THE ACTUAL RENDERING OF THE UI — USE render() INSTEAD** + * * Override this for the actual render. Note that you must render uiItems by yourself. * * Under normal circumstances, draws are automatically translated as per the handler's X/Y position. @@ -138,7 +148,7 @@ abstract class UICanvas( abstract override fun dispose() - fun addItem(uiItem: UIItem) { + fun addUIitem(uiItem: UIItem) { uiItems.add(uiItem) } diff --git a/src/net/torvald/terrarum/ui/UIHandler.kt b/src/net/torvald/terrarum/ui/UIHandler.kt index 5df4898eb..d6899880a 100644 --- a/src/net/torvald/terrarum/ui/UIHandler.kt +++ b/src/net/torvald/terrarum/ui/UIHandler.kt @@ -32,6 +32,9 @@ class UIHandler(//var UI: UICanvas, var posX: Int = 0 var posY: Int = 0 + var initialX = posX + var initialY = posY + private var alwaysVisible = false var isOpening = false diff --git a/src/net/torvald/terrarum/ui/UIItem.kt b/src/net/torvald/terrarum/ui/UIItem.kt index e13841b5f..a8b15e417 100644 --- a/src/net/torvald/terrarum/ui/UIItem.kt +++ b/src/net/torvald/terrarum/ui/UIItem.kt @@ -66,7 +66,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I * oldPosX = posX * ``` */ - protected abstract var oldPosX: Int + protected var oldPosX: Int = initialX /** This variable is NOT updated on its own. * ``` * val posYDelta = posY - oldPosY @@ -75,7 +75,7 @@ abstract class UIItem(var parentUI: UICanvas, val initialX: Int, val initialY: I * oldPosY = posY * ``` */ - protected abstract var oldPosY: Int + protected var oldPosY: Int = initialY /** Position of mouse relative to this item */ protected val relativeMouseX: Int diff --git a/src/net/torvald/terrarum/ui/UIItemConfigKeycap.kt b/src/net/torvald/terrarum/ui/UIItemConfigKeycap.kt index b12fe0f7e..5e8229ae6 100644 --- a/src/net/torvald/terrarum/ui/UIItemConfigKeycap.kt +++ b/src/net/torvald/terrarum/ui/UIItemConfigKeycap.kt @@ -36,9 +36,6 @@ class UIItemConfigKeycap( override val width = capTex.tileW * keySize override val height = capTex.tileH - override var oldPosX = posX - override var oldPosY = posY - override fun update(delta: Float) { super.update(delta) } diff --git a/src/net/torvald/terrarum/ui/UIItemHorizontalFadeSlide.kt b/src/net/torvald/terrarum/ui/UIItemHorizontalFadeSlide.kt new file mode 100644 index 000000000..4340de32a --- /dev/null +++ b/src/net/torvald/terrarum/ui/UIItemHorizontalFadeSlide.kt @@ -0,0 +1,41 @@ +package net.torvald.terrarum.ui + +import kotlin.math.roundToInt + +/** + * @param width size of the canvas where transition occurs + * @param height size of the canvas where transition occurs + */ +class UIItemHorizontalFadeSlide( + parent: UICanvas, + initialX: Int, + initialY: Int, + width: Int, + height: Int, + //transitionLength: Float, + currentPosition: Float, + vararg uis: UICanvas +) : UIItemTransitionContainer(parent, initialX, initialY, width, height, 0.212f, currentPosition, uis) { + + fun getOffX(index: Int) = ((currentPosition - index) * width).roundToInt() + fun getOpacity(index: Int) = (currentPosition - index).coerceIn(0f, 1f) + + init { + // re-position the uis according to the initial choice of currentPosition + uis.forEachIndexed { index, it -> + it.posX = posX + getOffX(index) + it.initialX = posX + getOffX(index) + it.posY = posY + it.initialY = posY + it.opacity = getOpacity(index) + } + } + + override fun onTransition(currentPosition: Float, uis: Array) { + uis.forEachIndexed { index, it -> + it.posX = it.initialX + getOffX(index) + it.posY = posY + it.opacity = getOpacity(index) + } + } +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/ui/UIItemImageButton.kt b/src/net/torvald/terrarum/ui/UIItemImageButton.kt index 8f488057e..f51553b52 100644 --- a/src/net/torvald/terrarum/ui/UIItemImageButton.kt +++ b/src/net/torvald/terrarum/ui/UIItemImageButton.kt @@ -41,10 +41,6 @@ open class UIItemImageButton( var highlightable: Boolean ) : UIItem(parent, initialX, initialY) { - // deal with the moving position - override var oldPosX = posX - override var oldPosY = posY - var highlighted = false override fun render(batch: SpriteBatch, camera: Camera) { diff --git a/src/net/torvald/terrarum/ui/UIItemImageGallery.kt b/src/net/torvald/terrarum/ui/UIItemImageGallery.kt index 55ec41dd8..cc932e71b 100644 --- a/src/net/torvald/terrarum/ui/UIItemImageGallery.kt +++ b/src/net/torvald/terrarum/ui/UIItemImageGallery.kt @@ -20,10 +20,6 @@ class UIItemImageGallery( val column: Int = 1 ) : UIItem(parentUI, initialX, initialY) { - // deal with the moving position - override var oldPosX = posX - override var oldPosY = posY - override fun update(delta: Float) { } diff --git a/src/net/torvald/terrarum/ui/UIItemIntSlider.kt b/src/net/torvald/terrarum/ui/UIItemIntSlider.kt index 8a5b8ffd5..6135e019f 100644 --- a/src/net/torvald/terrarum/ui/UIItemIntSlider.kt +++ b/src/net/torvald/terrarum/ui/UIItemIntSlider.kt @@ -75,10 +75,6 @@ class UIItemIntSlider( ) - // deal with the moving position - override var oldPosX = posX - override var oldPosY = posY - var value = initValue diff --git a/src/net/torvald/terrarum/ui/UIItemList.kt b/src/net/torvald/terrarum/ui/UIItemList.kt index 79c633d30..d106b9a70 100644 --- a/src/net/torvald/terrarum/ui/UIItemList.kt +++ b/src/net/torvald/terrarum/ui/UIItemList.kt @@ -36,10 +36,6 @@ class UIItemList( val border: Int = 0 ) : UIItem(parentUI, initialX, initialY) { - // deal with the moving position - override var oldPosX = posX - override var oldPosY = posY - init { itemList.forEachIndexed { index, item -> item.posX = this.posX + border diff --git a/src/net/torvald/terrarum/ui/UIItemModuleInfoCell.kt b/src/net/torvald/terrarum/ui/UIItemModuleInfoCell.kt index e6cd649f2..cfdd77516 100644 --- a/src/net/torvald/terrarum/ui/UIItemModuleInfoCell.kt +++ b/src/net/torvald/terrarum/ui/UIItemModuleInfoCell.kt @@ -16,10 +16,6 @@ class UIItemModuleInfoCell( initialY: Int ) : UIItem(parent, initialX, initialY) { - // deal with the moving position - override var oldPosX = posX - override var oldPosY = posY - override val height: Int = AppLoader.fontGame.lineHeight.toInt() * 2 private val numberAreaWidth = AppLoader.fontSmallNumbers.W * 3 + 4 diff --git a/src/net/torvald/terrarum/ui/UIItemTextArea.kt b/src/net/torvald/terrarum/ui/UIItemTextArea.kt index ce032dcd3..9abd6bded 100644 --- a/src/net/torvald/terrarum/ui/UIItemTextArea.kt +++ b/src/net/torvald/terrarum/ui/UIItemTextArea.kt @@ -17,10 +17,6 @@ class UIItemTextArea( val align: Alignment = Alignment.LEFT ) : UIItem(parentUI, initialX, initialY) { - // deal with the moving position - override var oldPosX = posX - override var oldPosY = posY - private var entireText: List = listOf("") // placeholder var scrollPos = 0 diff --git a/src/net/torvald/terrarum/ui/UIItemTextButton.kt b/src/net/torvald/terrarum/ui/UIItemTextButton.kt index 6c3820f51..fd2dcea06 100644 --- a/src/net/torvald/terrarum/ui/UIItemTextButton.kt +++ b/src/net/torvald/terrarum/ui/UIItemTextButton.kt @@ -48,10 +48,6 @@ open class UIItemTextButton( val hitboxSize: Int = UIItemTextButton.height ) : UIItem(parentUI, initialX, initialY) { - // deal with the moving position - override var oldPosX = posX - override var oldPosY = posY - companion object { val font = AppLoader.fontGame val height = font.lineHeight.toInt() diff --git a/src/net/torvald/terrarum/ui/UIItemTextButtonList.kt b/src/net/torvald/terrarum/ui/UIItemTextButtonList.kt index e91a1a130..f75fe51a8 100644 --- a/src/net/torvald/terrarum/ui/UIItemTextButtonList.kt +++ b/src/net/torvald/terrarum/ui/UIItemTextButtonList.kt @@ -57,10 +57,6 @@ class UIItemTextButtonList( val DEFAULT_LINE_HEIGHT = 36 } - // deal with the moving position - override var oldPosX = posX - override var oldPosY = posY - val iconToTextGap = 20 val iconCellWidth = (iconSpriteSheet?.tileW ?: -iconToTextGap) / (iconSpriteSheet?.horizontalCount ?: 1) val iconCellHeight = (iconSpriteSheet?.tileH ?: 0) / (iconSpriteSheet?.verticalCount ?: 1) diff --git a/src/net/torvald/terrarum/ui/UIItemToggleButton.kt b/src/net/torvald/terrarum/ui/UIItemToggleButton.kt index 3a78260a2..a54cc9b88 100644 --- a/src/net/torvald/terrarum/ui/UIItemToggleButton.kt +++ b/src/net/torvald/terrarum/ui/UIItemToggleButton.kt @@ -34,8 +34,6 @@ class UIItemToggleButton( get() = togglerBase.width override val height: Int get() = togglerBase.height - override var oldPosX = posX - override var oldPosY = posY private var togglerBase = CommonResourcePool.getAsTexture("ui_item_toggler_base") private var togglerHandle = CommonResourcePool.getAsTexture("ui_item_toggler_handle") diff --git a/src/net/torvald/terrarum/ui/UIItemTransitionContainer.kt b/src/net/torvald/terrarum/ui/UIItemTransitionContainer.kt new file mode 100644 index 000000000..b907841ce --- /dev/null +++ b/src/net/torvald/terrarum/ui/UIItemTransitionContainer.kt @@ -0,0 +1,95 @@ +package net.torvald.terrarum.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 net.torvald.terrarum.AppLoader + +open class UIItemTransitionContainer( + parent: UICanvas, + initialX: Int, + initialY: Int, + override val width: Int, + override val height: Int, + val transitionLength: Float = 0.212f, + var currentPosition: Float = 0f, + val uis: Array +) : UIItem(parent, initialX, initialY) { + + val debugvals = true + + private var transitionRequested = false + private var transitionOngoing = false + private var transitionReqSource = currentPosition + private var transitionReqTarget = currentPosition + private var transitionTimer = 0f + + private val epsilon = 0.001f + + fun requestTransition(target: Int) { + if (!transitionOngoing) { + transitionRequested = true + transitionReqSource = Math.round(currentPosition).toFloat() + transitionReqTarget = target.toFloat() + } + } + + override fun update(delta: Float) { + super.update(delta) + + uis.forEachIndexed { index, ui -> + if (currentPosition > index - 1 + epsilon && currentPosition < index + 1 - epsilon) { + ui.update(delta) + } + } + } + + open fun onTransition(currentPosition: Float, uis: Array) {} + + override fun render(batch: SpriteBatch, camera: Camera) { + super.render(batch, camera) + + if (transitionRequested && !transitionOngoing) { + transitionRequested = false + transitionOngoing = true + transitionTimer = 0f + } + + if (transitionOngoing) { + transitionTimer += Gdx.graphics.rawDeltaTime + + currentPosition = UIUtils.moveLinear(transitionReqSource, transitionReqTarget, transitionTimer, transitionLength) + + if (transitionTimer > transitionLength) { + transitionOngoing = false + currentPosition = transitionReqTarget + } + + onTransition(currentPosition, uis) + } + + uis.forEachIndexed { index, ui -> + if (currentPosition > index - 1 + epsilon && currentPosition < index + 1 - epsilon) { + ui.setAsOpen() + ui.render(batch, camera) + + if (debugvals) { + AppLoader.fontSmallNumbers.draw(batch, "$index", 300f + (20 * index), 10f) + } + } + else { + ui.setAsClose() + } + } + + if (debugvals) { + batch.color = Color.WHITE + AppLoader.fontSmallNumbers.draw(batch, "position:$currentPosition", 500f, 10f) + } + } + + override fun dispose() { + uis.forEach { it.dispose() } + } +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/ui/UIUtils.kt b/src/net/torvald/terrarum/ui/UIUtils.kt index 3d01251d8..576226d2e 100644 --- a/src/net/torvald/terrarum/ui/UIUtils.kt +++ b/src/net/torvald/terrarum/ui/UIUtils.kt @@ -8,6 +8,17 @@ import net.torvald.terrarum.sqr object UIUtils { fun moveQuick(start: Float, end: Float, timer: Float, duration: Float) = (start - end) * ((timer / duration) - 1).sqr() + end - fun moveLinear(start: Float, end: Float, timer: Float, duration: Float) = - (start - end) * (1 - (timer / duration)) + end + fun moveLinear(start: Float, end: Float, timer: Float, duration: Float): Float { + val scale = timer / duration + if (start == end) { + return start + } + if (scale <= 0f) { + return start + } + if (scale >= 1f) { + return end + } + return ((1f - scale) * start) + (scale * end) + } } \ No newline at end of file diff --git a/terrarum.iml b/terrarum.iml index 2664b041f..5ead1470d 100644 --- a/terrarum.iml +++ b/terrarum.iml @@ -8,7 +8,6 @@ -