mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-19 06:54:05 +09:00
crafting: clicking on the recipe will show all possible ingredients player has
This commit is contained in:
@@ -948,3 +948,5 @@ fun distBetween(a: ActorWithBody, bpos: Vector2): Double {
|
||||
val dist = min(min(bpos.distanceSquared(apos1), bpos.distanceSquared(apos2)), bpos.distanceSquared(apos3))
|
||||
return dist.sqrt()
|
||||
}
|
||||
|
||||
fun getHashStr(length: Int = 5) = (0 until length).map { "YBNDRFG8EJKMCPQXOTLVWIS2A345H769"[Math.random().times(32).toInt()] }.joinToString("")
|
||||
|
||||
@@ -155,6 +155,7 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
|
||||
|
||||
_getItemListPlayer().let {
|
||||
it.removeFromForceHighlightList(oldSelectedItems)
|
||||
filterPlayerListUsing(recipeClicked)
|
||||
it.addToForceHighlightList(selectedItems)
|
||||
it.rebuild(catAll)
|
||||
}
|
||||
@@ -236,7 +237,7 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
|
||||
val wantsWall = tags.contains("WALL")
|
||||
(mode == CraftingCodex.CraftingItemKeyMode.TAG && ItemCodex[itm]!!.hasAllTags(tags) && (wantsWall == itm.isWall())) // true if (wants wall and is wall) or (wants no wall and is not wall)
|
||||
}
|
||||
changeIngredient(oldItem, itemID)
|
||||
changeIngredient(recipe, oldItem, itemID)
|
||||
refreshCraftButtonStatus()
|
||||
}
|
||||
} } }
|
||||
@@ -271,7 +272,7 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
|
||||
|
||||
_getItemListPlayer().removeFromForceHighlightList(oldSelectedItems)
|
||||
_getItemListPlayer().addToForceHighlightList(selectedItems)
|
||||
_getItemListPlayer().rebuild(catAll)
|
||||
filterPlayerListUsing(recipeClicked)
|
||||
_getItemListIngredients().rebuild(catAll)
|
||||
|
||||
highlightCraftingCandidateButton(recipe)
|
||||
@@ -330,6 +331,18 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
|
||||
addUIitem(buttonCraft)
|
||||
}
|
||||
|
||||
private fun filterPlayerListUsing(recipe: CraftingCodex.CraftingRecipe?) {
|
||||
if (recipe == null)
|
||||
itemListPlayer.rebuild(catAll)
|
||||
else {
|
||||
val items = recipe.ingredients.flatMap { getItemCandidatesForIngredient(getPlayerInventory(), it).map { it.itm } }.sorted()
|
||||
val filterFun = { pair: InventoryPair ->
|
||||
items.binarySearch(pair.itm) >= 0
|
||||
}
|
||||
itemListPlayer.rebuild(filterFun)
|
||||
}
|
||||
}
|
||||
|
||||
var nearbyCraftingStations = emptyList<String>(); protected set
|
||||
|
||||
fun getCraftingStationsWithinReach(): List<String> {
|
||||
@@ -340,14 +353,14 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
|
||||
return nearbyCraftingStations.flatMap { (it.get() as CraftingStation).tags }
|
||||
}
|
||||
|
||||
private fun changeIngredient(old: InventoryPair, new: ItemID) {
|
||||
private fun changeIngredient(recipe: CraftingCodex.CraftingRecipe?, old: InventoryPair, new: ItemID) {
|
||||
itemListPlayer.removeFromForceHighlightList(oldSelectedItems)
|
||||
|
||||
oldSelectedItems.remove(old.itm)
|
||||
oldSelectedItems.add(new)
|
||||
|
||||
itemListPlayer.addToForceHighlightList(oldSelectedItems)
|
||||
itemListPlayer.rebuild(catAll)
|
||||
filterPlayerListUsing(recipe)
|
||||
|
||||
// change highlight status of itemListIngredients
|
||||
itemListIngredients.getInventory().let {
|
||||
@@ -384,6 +397,7 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
|
||||
resetSpinner()
|
||||
// reset selected recipe status
|
||||
recipeClicked = null
|
||||
filterPlayerListUsing(recipeClicked)
|
||||
highlightCraftingCandidateButton(null)
|
||||
ingredients.clear()
|
||||
itemListPlayer.removeFromForceHighlightList(oldSelectedItems)
|
||||
@@ -411,6 +425,8 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
|
||||
|
||||
UIItemInventoryItemGrid.tooltipShowing.clear()
|
||||
INGAME.setTooltipMessage(null)
|
||||
|
||||
resetUI()
|
||||
}
|
||||
|
||||
private var encumbrancePerc = 0f
|
||||
@@ -507,7 +523,6 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
|
||||
|
||||
override fun doOpening(delta: Float) {
|
||||
super.doOpening(delta)
|
||||
resetUI()
|
||||
INGAME.setTooltipMessage(null)
|
||||
}
|
||||
|
||||
@@ -541,14 +556,28 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
|
||||
val craftingStationAvailable: Boolean,
|
||||
)
|
||||
|
||||
fun getItemForIngredient(inventory: FixtureInventory, ingredient: CraftingCodex.CraftingIngredients): ItemID {
|
||||
fun getItemCandidatesForIngredient(inventory: FixtureInventory, ingredient: CraftingCodex.CraftingIngredients): List<InventoryPair> {
|
||||
return if (ingredient.keyMode == CraftingCodex.CraftingItemKeyMode.TAG) {
|
||||
val tags = ingredient.key.split(',')
|
||||
val wantsWall = tags.contains("WALL")
|
||||
// If the player has the required item, use it; otherwise, will take an item from the ItemCodex
|
||||
inventory.filter { (itm, qty) ->
|
||||
ItemCodex[itm]?.hasAllTags(tags) == true && qty >= ingredient.qty && (wantsWall == itm.isWall()) // true if (wants wall and is wall) or (wants no wall and is not wall)
|
||||
}.maxByOrNull { it.qty }?.itm ?: ((ItemCodex.itemCodex.firstNotNullOfOrNull { if (it.value.hasTag(ingredient.key)) it.key else null }) ?: throw NullPointerException("Item with tag '${ingredient.key}' not found. Possible cause: game or a module not updated or installed"))
|
||||
}
|
||||
}
|
||||
else {
|
||||
listOf(InventoryPair(ingredient.key, -1))
|
||||
}
|
||||
}
|
||||
|
||||
fun getItemForIngredient(inventory: FixtureInventory, ingredient: CraftingCodex.CraftingIngredients): ItemID {
|
||||
val candidate = getItemCandidatesForIngredient(inventory, ingredient)
|
||||
|
||||
return if (ingredient.keyMode == CraftingCodex.CraftingItemKeyMode.TAG) {
|
||||
candidate.maxByOrNull { it.qty }?.itm ?: (
|
||||
(ItemCodex.itemCodex.firstNotNullOfOrNull { if (it.value.hasTag(ingredient.key)) it.key else null }) ?:
|
||||
throw NullPointerException("Item with tag '${ingredient.key}' not found. Possible cause: game or a module not updated or installed")
|
||||
)
|
||||
}
|
||||
else {
|
||||
ingredient.key
|
||||
|
||||
@@ -8,6 +8,7 @@ import net.torvald.terrarum.ceilToInt
|
||||
import net.torvald.terrarum.gameitems.GameItem
|
||||
import net.torvald.terrarum.itemproperties.CraftingCodex
|
||||
import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory
|
||||
import net.torvald.terrarum.modulebasegame.gameactors.InventoryPair
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2022-06-28.
|
||||
@@ -69,7 +70,11 @@ class UIItemCraftingCandidateGrid(
|
||||
highlightRecipe(highlightedRecipe)
|
||||
}
|
||||
|
||||
private var currentFilter1 = arrayOf("")
|
||||
|
||||
override fun rebuild(filter: Array<String>) {
|
||||
currentFilter1 = filter
|
||||
|
||||
// filtering policy: if the player have all the ingredient item (regardless of the amount!), make the recipe visible
|
||||
craftingRecipes.clear()
|
||||
CraftingRecipeCodex.props.forEach { (_, recipes) ->
|
||||
@@ -111,14 +116,12 @@ class UIItemCraftingCandidateGrid(
|
||||
|
||||
|
||||
itemPageCount = (recipesSortList.size.toFloat() / items.size.toFloat()).ceilToInt()
|
||||
|
||||
|
||||
|
||||
rebuildList = false
|
||||
}
|
||||
|
||||
|
||||
|
||||
override fun rebuild(filterFun: (InventoryPair) -> Boolean) {
|
||||
rebuild(currentFilter1)
|
||||
}
|
||||
|
||||
override fun scrolled(amountX: Float, amountY: Float): Boolean {
|
||||
super.scrolled(amountX, amountY)
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.badlogic.gdx.graphics.Color
|
||||
import com.badlogic.gdx.graphics.OrthographicCamera
|
||||
import com.badlogic.gdx.graphics.g2d.SpriteBatch
|
||||
import net.torvald.terrarum.*
|
||||
import net.torvald.terrarum.App.printdbg
|
||||
import net.torvald.terrarum.UIItemInventoryCatBar.Companion.CAT_ALL
|
||||
import net.torvald.terrarum.gameactors.AVKey
|
||||
import net.torvald.terrarum.gameitems.GameItem
|
||||
@@ -54,6 +55,8 @@ open class UIItemInventoryItemGrid(
|
||||
//override var oldPosX = posX
|
||||
//override var oldPosY = posY
|
||||
|
||||
internal val instanceHash = getHashStr(5)
|
||||
|
||||
var numberMultiplier = 1L
|
||||
|
||||
private val hash = System.nanoTime()
|
||||
@@ -81,7 +84,7 @@ open class UIItemInventoryItemGrid(
|
||||
arrayOf(GameItem.Category.MISC),
|
||||
arrayOf(CAT_ALL)
|
||||
)*/
|
||||
protected var currentFilter = arrayOf(CAT_ALL)
|
||||
protected var currentFilter: (InventoryPair) -> Boolean = { _: InventoryPair -> true }
|
||||
|
||||
private val inventoryUI = parentUI
|
||||
|
||||
@@ -373,21 +376,21 @@ open class UIItemInventoryItemGrid(
|
||||
forceHighlightList.removeAll(items)
|
||||
}
|
||||
|
||||
open fun rebuild(filter: Array<String>) {
|
||||
open fun rebuild(filterFun: (InventoryPair) -> Boolean) {
|
||||
currentFilter = filterFun
|
||||
|
||||
//println("Rebuilt inventory")
|
||||
//println("rebuild: actual itempage: $itemPage")
|
||||
|
||||
|
||||
//val filter = catIconsMeaning[selectedIcon]
|
||||
currentFilter = filter
|
||||
|
||||
inventorySortList.clear()
|
||||
|
||||
// filter items
|
||||
getInventory().forEach {
|
||||
if ((filter.contains((ItemCodex[it.itm]?.inventoryCategory ?: throw IllegalArgumentException("Unknown item: ${it.itm}"))) || filter[0] == CAT_ALL))
|
||||
inventorySortList.add(it)
|
||||
}
|
||||
val filteredItems = getInventory().filter(filterFun)
|
||||
inventorySortList.addAll(filteredItems)
|
||||
|
||||
|
||||
// sort if needed
|
||||
// test sort by name
|
||||
@@ -455,6 +458,12 @@ open class UIItemInventoryItemGrid(
|
||||
rebuildList = false
|
||||
}
|
||||
|
||||
open fun rebuild(filter: Array<String>) {
|
||||
val filterFun = if (filter[0] == CAT_ALL) { item: InventoryPair -> true }
|
||||
else { item: InventoryPair -> filter.contains(ItemCodex[item.itm]?.inventoryCategory ?: throw IllegalArgumentException("Unknown item: ${item.itm}")) }
|
||||
rebuild(filterFun)
|
||||
}
|
||||
|
||||
override fun dispose() {
|
||||
tooltipShowing.remove(hash)
|
||||
}
|
||||
@@ -485,7 +494,7 @@ open class UIItemInventoryItemGrid(
|
||||
super.keyDown(keycode)
|
||||
|
||||
items.forEach { if (it.mouseUp) it.keyDown(keycode) }
|
||||
rebuild(currentFilter)
|
||||
// rebuild(currentFilter)
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -494,7 +503,7 @@ open class UIItemInventoryItemGrid(
|
||||
super.keyUp(keycode)
|
||||
|
||||
items.forEach { if (it.mouseUp) it.keyUp(keycode) }
|
||||
rebuild(currentFilter)
|
||||
// rebuild(currentFilter)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user