mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-10 18:44:05 +09:00
centering of labels for crafting ui
This commit is contained in:
@@ -720,6 +720,8 @@ fun <T> Array<T>.init() = this.sliceArray(0 until this.lastIndex)
|
|||||||
fun <T> List<T>.tail() = this.subList(1, this.size)
|
fun <T> List<T>.tail() = this.subList(1, this.size)
|
||||||
fun <T> List<T>.init() = this.subList(0, this.lastIndex)
|
fun <T> List<T>.init() = this.subList(0, this.lastIndex)
|
||||||
|
|
||||||
|
fun <T> Collection<T>.notEmptyOrNull() = this.ifEmpty { null }
|
||||||
|
|
||||||
val BlockCodex: BlockCodex
|
val BlockCodex: BlockCodex
|
||||||
get() = Terrarum.blockCodex
|
get() = Terrarum.blockCodex
|
||||||
val ItemCodex: ItemCodex
|
val ItemCodex: ItemCodex
|
||||||
|
|||||||
@@ -362,6 +362,10 @@ abstract class GameItem(val originalID: ItemID) : Comparable<GameItem>, 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 actor actor to check the reach
|
||||||
* @param action returns non-negative integer if the action was successfully performed
|
* @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
|
* @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
|
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
|
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 {
|
fun mouseInInteractableRangeTools(actor: ActorWithBody, item: GameItem?, reachMultiplierInTiles: (Int) -> Double = { it.toDouble() }, action: () -> Boolean): Boolean {
|
||||||
val mousePos1 = Vector2(Terrarum.mouseX, Terrarum.mouseY)
|
val mousePos1 = Vector2(Terrarum.mouseX, Terrarum.mouseY)
|
||||||
val mousePos2 = Vector2(Terrarum.mouseX + INGAME.world.width * TILE_SIZED, Terrarum.mouseY)
|
val mousePos2 = Vector2(Terrarum.mouseX + INGAME.world.width * TILE_SIZED, Terrarum.mouseY)
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import net.torvald.terrarum.*
|
|||||||
import net.torvald.terrarum.App.gamepadLabelLEFTRIGHT
|
import net.torvald.terrarum.App.gamepadLabelLEFTRIGHT
|
||||||
import net.torvald.terrarum.App.gamepadLabelStart
|
import net.torvald.terrarum.App.gamepadLabelStart
|
||||||
import net.torvald.terrarum.UIItemInventoryCatBar.Companion.CAT_ALL
|
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.GameItem
|
||||||
import net.torvald.terrarum.gameitems.ItemID
|
import net.torvald.terrarum.gameitems.ItemID
|
||||||
import net.torvald.terrarum.itemproperties.CraftingCodex
|
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 thisOffsetX2 = thisOffsetX + (listGap + UIItemInventoryElemWide.height) * 7
|
||||||
private val thisXend = thisOffsetX + (listGap + UIItemInventoryElemWide.height) * 13 - listGap
|
private val thisXend = thisOffsetX + (listGap + UIItemInventoryElemWide.height) * 13 - listGap
|
||||||
private val thisOffsetY = UIInventoryFull.INVENTORY_CELLS_OFFSET_Y()
|
private val thisOffsetY = UIInventoryFull.INVENTORY_CELLS_OFFSET_Y()
|
||||||
|
private val cellsWidth = (listGap + UIItemInventoryElemWide.height) * 6 - listGap
|
||||||
|
|
||||||
private val TEXT_GAP = 26
|
private val TEXT_GAP = 26
|
||||||
private val LAST_LINE_IN_GRID = ((UIItemInventoryElemWide.height + listGap) * (UIInventoryFull.CELLS_VRT - 2)) + 22//359 // TEMPORARY VALUE!
|
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
|
batch.color = Color.WHITE
|
||||||
|
|
||||||
// text label for two inventory grids
|
// text label for two inventory grids
|
||||||
App.fontGame.draw(batch, Lang["GAME_CRAFTING"], thisOffsetX + 2, thisOffsetY - TEXT_GAP)
|
val craftingLabel = Lang["GAME_CRAFTING"]
|
||||||
App.fontGame.draw(batch, Lang["GAME_INVENTORY_INGREDIENTS"], thisOffsetX + 2, thisOffsetY + LAST_LINE_IN_GRID - TEXT_GAP)
|
val ingredientsLabel = Lang["GAME_INVENTORY_INGREDIENTS"]
|
||||||
App.fontGame.draw(batch, Lang["GAME_INVENTORY"], thisOffsetX2 + 2, thisOffsetY - TEXT_GAP)
|
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
|
// control hints
|
||||||
|
|||||||
@@ -76,11 +76,11 @@ object BlockBase {
|
|||||||
(Terrarum.ingame!! as TerrarumIngame).selectedWireRenderClass = ""
|
(Terrarum.ingame!! as TerrarumIngame).selectedWireRenderClass = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun Int.shiftByTwo() = this.shl(4).or(this).ushr(2).and(15)
|
fun Int.wireNodeMirror() = this.shl(4).or(this).ushr(2).and(15)
|
||||||
private fun connectedEachOther(oldToNewVector: Int, new: Int?, old: Int?): Boolean {
|
fun wireNodesConnectedEachOther(oldToNewVector: Int, new: Int?, old: Int?): Boolean {
|
||||||
return if (new == null || old == null || oldToNewVector == 0) false
|
return if (new == null || old == null || oldToNewVector == 0) false
|
||||||
else {
|
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')}")
|
//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 p = oldToNewVector and old
|
||||||
val q = newToOldVector and new
|
val q = newToOldVector and new
|
||||||
@@ -153,7 +153,7 @@ object BlockBase {
|
|||||||
else if (mouseTileY - oldTileY == -1) 8
|
else if (mouseTileY - oldTileY == -1) 8
|
||||||
else 0 // if xy == oxy, the vector will be 0
|
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
|
val thisTileWasDraggedOn = initialMouseDownTileX != mouseTileX || initialMouseDownTileY != mouseTileY
|
||||||
|
|
||||||
var ret = -1L
|
var ret = -1L
|
||||||
|
|||||||
@@ -2,17 +2,75 @@ package net.torvald.terrarum.modulebasegame.gameitems
|
|||||||
|
|
||||||
import com.badlogic.gdx.graphics.g2d.TextureRegion
|
import com.badlogic.gdx.graphics.g2d.TextureRegion
|
||||||
import net.torvald.terrarum.CommonResourcePool
|
import net.torvald.terrarum.CommonResourcePool
|
||||||
import net.torvald.terrarum.Point2i
|
|
||||||
import net.torvald.terrarum.Terrarum
|
import net.torvald.terrarum.Terrarum
|
||||||
import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZED
|
import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZED
|
||||||
import net.torvald.terrarum.gameactors.ActorWithBody
|
import net.torvald.terrarum.gameactors.ActorWithBody
|
||||||
import net.torvald.terrarum.gameitems.GameItem
|
import net.torvald.terrarum.gameitems.GameItem
|
||||||
import net.torvald.terrarum.gameitems.ItemID
|
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.itemproperties.Material
|
||||||
import net.torvald.terrarum.modulebasegame.TerrarumIngame
|
import net.torvald.terrarum.modulebasegame.TerrarumIngame
|
||||||
import net.torvald.terrarum.modulebasegame.gameactors.DroppedItem
|
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
|
* 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
|
super.equipPosition = GameItem.EquipPosition.HAND_GRIP
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun startPrimaryUse(actor: ActorWithBody, delta: Float) = mouseInInteractableRange(actor) {
|
override fun startPrimaryUse(actor: ActorWithBody, delta: Float) =
|
||||||
val ingame = Terrarum.ingame!! as TerrarumIngame
|
WireCutterBase.startPrimaryUse(this, actor, delta) { true }
|
||||||
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 effectWhileEquipped(actor: ActorWithBody, delta: Float) {
|
override fun effectWhileEquipped(actor: ActorWithBody, delta: Float) {
|
||||||
(Terrarum.ingame!! as TerrarumIngame).selectedWireRenderClass = "wire_render_all"
|
(Terrarum.ingame!! as TerrarumIngame).selectedWireRenderClass = "wire_render_all"
|
||||||
|
|||||||
Reference in New Issue
Block a user