From 2a7e2b7ce498e5975283bd82435df4ba8d3557f5 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Wed, 13 Jul 2022 17:40:07 +0900 Subject: [PATCH] centering of labels for crafting ui --- src/net/torvald/terrarum/Terrarum.kt | 2 + .../torvald/terrarum/gameitems/GameItem.kt | 16 ++++ .../modulebasegame/gameactors/UICrafting.kt | 12 ++- .../modulebasegame/gameitems/BlockBase.kt | 8 +- .../modulebasegame/gameitems/WireCutterAll.kt | 78 +++++++++++++++---- 5 files changed, 93 insertions(+), 23 deletions(-) diff --git a/src/net/torvald/terrarum/Terrarum.kt b/src/net/torvald/terrarum/Terrarum.kt index eef309295..51fdf8c97 100644 --- a/src/net/torvald/terrarum/Terrarum.kt +++ b/src/net/torvald/terrarum/Terrarum.kt @@ -720,6 +720,8 @@ fun Array.init() = this.sliceArray(0 until this.lastIndex) fun List.tail() = this.subList(1, this.size) fun List.init() = this.subList(0, this.lastIndex) +fun Collection.notEmptyOrNull() = this.ifEmpty { null } + val BlockCodex: BlockCodex get() = Terrarum.blockCodex val ItemCodex: ItemCodex diff --git a/src/net/torvald/terrarum/gameitems/GameItem.kt b/src/net/torvald/terrarum/gameitems/GameItem.kt index 36f0641c7..7193fdccd 100644 --- a/src/net/torvald/terrarum/gameitems/GameItem.kt +++ b/src/net/torvald/terrarum/gameitems/GameItem.kt @@ -362,6 +362,10 @@ abstract class GameItem(val originalID: ItemID) : Comparable, Cloneabl } /** + * When the mouse cursor is within the reach of the actor, make the given action happen. + * + * The reach is calculated using the actor's reach, reach buff actorvalue and the actor's scale. + * * @param actor actor to check the reach * @param action returns non-negative integer if the action was successfully performed * @return an amount to remove from the inventory (>= 0); -1 if the action failed or not in interactable range @@ -375,6 +379,18 @@ fun mouseInInteractableRange(actor: ActorWithBody, action: () -> Long): Long { val distMax = actor.actorValue.getAsDouble(AVKey.REACH)!! * (actor.actorValue.getAsDouble(AVKey.REACHBUFF) ?: 1.0) * actor.scale // perform some error checking here if (dist <= distMax.sqr()) return action() else return -1 } + +/** + * When the mouse cursor is within the reach of the actor, make the given action happen. + * + * The reach is calculated using the actor's reach, reach buff actorvalue and the actor's scale as well as the tool-material's reach bonus. + * + * @param actor actor to check the reach + * @param item the item that represents the tool + * @param reachMultiplierInTiles optional: a function that modifies the calculated reach + * @param action returns boolean if the action was successfully performed + * @return true if the action was successful, false if the action failed or the mouse is not in interactable range + */ fun mouseInInteractableRangeTools(actor: ActorWithBody, item: GameItem?, reachMultiplierInTiles: (Int) -> Double = { it.toDouble() }, action: () -> Boolean): Boolean { val mousePos1 = Vector2(Terrarum.mouseX, Terrarum.mouseY) val mousePos2 = Vector2(Terrarum.mouseX + INGAME.world.width * TILE_SIZED, Terrarum.mouseY) diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/UICrafting.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/UICrafting.kt index 24ea7280b..d64667aca 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/UICrafting.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/UICrafting.kt @@ -7,6 +7,7 @@ import net.torvald.terrarum.* import net.torvald.terrarum.App.gamepadLabelLEFTRIGHT import net.torvald.terrarum.App.gamepadLabelStart import net.torvald.terrarum.UIItemInventoryCatBar.Companion.CAT_ALL +import net.torvald.terrarum.gameactors.AVKey import net.torvald.terrarum.gameitems.GameItem import net.torvald.terrarum.gameitems.ItemID import net.torvald.terrarum.itemproperties.CraftingCodex @@ -64,6 +65,7 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory { private val thisOffsetX2 = thisOffsetX + (listGap + UIItemInventoryElemWide.height) * 7 private val thisXend = thisOffsetX + (listGap + UIItemInventoryElemWide.height) * 13 - listGap private val thisOffsetY = UIInventoryFull.INVENTORY_CELLS_OFFSET_Y() + private val cellsWidth = (listGap + UIItemInventoryElemWide.height) * 6 - listGap private val TEXT_GAP = 26 private val LAST_LINE_IN_GRID = ((UIItemInventoryElemWide.height + listGap) * (UIInventoryFull.CELLS_VRT - 2)) + 22//359 // TEMPORARY VALUE! @@ -452,9 +454,13 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory { batch.color = Color.WHITE // text label for two inventory grids - App.fontGame.draw(batch, Lang["GAME_CRAFTING"], thisOffsetX + 2, thisOffsetY - TEXT_GAP) - App.fontGame.draw(batch, Lang["GAME_INVENTORY_INGREDIENTS"], thisOffsetX + 2, thisOffsetY + LAST_LINE_IN_GRID - TEXT_GAP) - App.fontGame.draw(batch, Lang["GAME_INVENTORY"], thisOffsetX2 + 2, thisOffsetY - TEXT_GAP) + val craftingLabel = Lang["GAME_CRAFTING"] + val ingredientsLabel = Lang["GAME_INVENTORY_INGREDIENTS"] + val playerName = INGAME.actorNowPlaying!!.actorValue.getAsString(AVKey.NAME).orEmpty().let { it.ifBlank { Lang["GAME_INVENTORY"] } } + + App.fontGame.draw(batch, craftingLabel, thisOffsetX + (cellsWidth - App.fontGame.getWidth(craftingLabel)) / 2, thisOffsetY - TEXT_GAP) + App.fontGame.draw(batch, ingredientsLabel, thisOffsetX + (cellsWidth - App.fontGame.getWidth(ingredientsLabel)) / 2, thisOffsetY + LAST_LINE_IN_GRID - TEXT_GAP) + App.fontGame.draw(batch, playerName, thisOffsetX2 + (cellsWidth - App.fontGame.getWidth(playerName)) / 2, thisOffsetY - TEXT_GAP) // control hints diff --git a/src/net/torvald/terrarum/modulebasegame/gameitems/BlockBase.kt b/src/net/torvald/terrarum/modulebasegame/gameitems/BlockBase.kt index fce025dac..858b3a10e 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameitems/BlockBase.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameitems/BlockBase.kt @@ -76,11 +76,11 @@ object BlockBase { (Terrarum.ingame!! as TerrarumIngame).selectedWireRenderClass = "" } - private fun Int.shiftByTwo() = this.shl(4).or(this).ushr(2).and(15) - private fun connectedEachOther(oldToNewVector: Int, new: Int?, old: Int?): Boolean { + fun Int.wireNodeMirror() = this.shl(4).or(this).ushr(2).and(15) + fun wireNodesConnectedEachOther(oldToNewVector: Int, new: Int?, old: Int?): Boolean { return if (new == null || old == null || oldToNewVector == 0) false else { - val newToOldVector = oldToNewVector.shiftByTwo() + val newToOldVector = oldToNewVector.wireNodeMirror() //printdbg(this, "connected? ${one.and(15).toString(2).padStart(4, '0')} vs ${other.and(15).toString(2).padStart(4, '0')}") val p = oldToNewVector and old val q = newToOldVector and new @@ -153,7 +153,7 @@ object BlockBase { else if (mouseTileY - oldTileY == -1) 8 else 0 // if xy == oxy, the vector will be 0 - val connectedEachOther = connectedEachOther(oldToNewVector, thisTileWireCnx, oldTileWireCnx) + val connectedEachOther = wireNodesConnectedEachOther(oldToNewVector, thisTileWireCnx, oldTileWireCnx) val thisTileWasDraggedOn = initialMouseDownTileX != mouseTileX || initialMouseDownTileY != mouseTileY var ret = -1L diff --git a/src/net/torvald/terrarum/modulebasegame/gameitems/WireCutterAll.kt b/src/net/torvald/terrarum/modulebasegame/gameitems/WireCutterAll.kt index b509c1f95..a8008993e 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameitems/WireCutterAll.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameitems/WireCutterAll.kt @@ -2,17 +2,75 @@ package net.torvald.terrarum.modulebasegame.gameitems import com.badlogic.gdx.graphics.g2d.TextureRegion import net.torvald.terrarum.CommonResourcePool -import net.torvald.terrarum.Point2i import net.torvald.terrarum.Terrarum import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZED import net.torvald.terrarum.gameactors.ActorWithBody import net.torvald.terrarum.gameitems.GameItem import net.torvald.terrarum.gameitems.ItemID -import net.torvald.terrarum.gameitems.mouseInInteractableRange +import net.torvald.terrarum.gameitems.mouseInInteractableRangeTools +import net.torvald.terrarum.gameworld.GameWorld import net.torvald.terrarum.itemproperties.Material import net.torvald.terrarum.modulebasegame.TerrarumIngame import net.torvald.terrarum.modulebasegame.gameactors.DroppedItem +import net.torvald.terrarum.notEmptyOrNull +import net.torvald.terrarum.toInt +/** + * Modular approach to the wire cutter function + * + * Created by minjaesong on 2022-07-13. + */ +object WireCutterBase { + + private fun disconnect(world: GameWorld, item: ItemID, x1: Int, y1: Int, x2: Int, y2: Int) { + + } + + fun startPrimaryUse(item: GameItem, actor: ActorWithBody, delta: Float, wireFilter: (ItemID) -> Boolean) = mouseInInteractableRangeTools(actor, item) { + val ingame = Terrarum.ingame!! as TerrarumIngame + val mouseTile = Terrarum.getMouseSubtile4() + + if (mouseTile.vector == Terrarum.SubtileVector.INVALID) return@mouseInInteractableRangeTools false + + val (mtx, mty) = mouseTile.currentTileCoord + val mvec = mouseTile.vector + + if (mvec == Terrarum.SubtileVector.CENTRE) { + val wireNet = ingame.world.getAllWiresFrom(mtx, mty) + val wireItems = wireNet.first?.cloneToList() + + wireItems?.filter(wireFilter)?.notEmptyOrNull()?.forEach { + ingame.world.removeTileWire(mtx, mty, it, false) + ingame.queueActorAddition(DroppedItem(it, mtx * TILE_SIZED, mty * TILE_SIZED)) + } ?: return@mouseInInteractableRangeTools false + + true + } + else { + val (ntx, nty) = mouseTile.nextTileCoord + + val wireNetP = ingame.world.getAllWiresFrom(mtx, mty) + val wireNetN = ingame.world.getAllWiresFrom(mtx, mty) + val wireItemsP = wireNetP.first?.cloneToList() + val wireItemsN = wireNetN.first?.cloneToList() + + // get intersection of wireItemsP and wireItemsN + if (wireItemsP != null && wireItemsN != null) { + val wireItems = wireItemsP intersect wireItemsN + + wireItems.filter(wireFilter).notEmptyOrNull()?.forEach { + disconnect(ingame.world, it, mtx, mty, ntx, nty) + } ?: return@mouseInInteractableRangeTools false + + true + } + else + false + } + }.toInt().minus(1L) // 0L if successful, -1L if not + + +} /** * TEST ITEM; this item cuts every wire on a cell, and has no durability drop * @@ -36,20 +94,8 @@ class WireCutterAll(originalID: ItemID) : GameItem(originalID) { super.equipPosition = GameItem.EquipPosition.HAND_GRIP } - override fun startPrimaryUse(actor: ActorWithBody, delta: Float) = mouseInInteractableRange(actor) { - val ingame = Terrarum.ingame!! as TerrarumIngame - val mouseTile = Point2i(Terrarum.mouseTileX, Terrarum.mouseTileY) - - val wireNet = ingame.world.getAllWiresFrom(mouseTile.x, mouseTile.y) - val wireItems = wireNet.first?.cloneToList() - - wireItems?.forEach { - ingame.world.removeTileWire(mouseTile.x, mouseTile.y, it, false) - ingame.queueActorAddition(DroppedItem(it, mouseTile.x * TILE_SIZED, mouseTile.y * TILE_SIZED)) - } ?: return@mouseInInteractableRange -1L - - 0L - } + override fun startPrimaryUse(actor: ActorWithBody, delta: Float) = + WireCutterBase.startPrimaryUse(this, actor, delta) { true } override fun effectWhileEquipped(actor: ActorWithBody, delta: Float) { (Terrarum.ingame!! as TerrarumIngame).selectedWireRenderClass = "wire_render_all"