working crafting workbench

This commit is contained in:
minjaesong
2023-09-20 16:01:40 +09:00
parent a168db23de
commit 5957f70ff8
12 changed files with 145 additions and 31 deletions

View File

@@ -13,25 +13,25 @@
},
"item@basegame:256": {
"workbench": "",
"workbench": "basiccrafting",
"ingredients": [
[1, 4, "basegame:48"]
]
},
"item@basegame:257": {
"workbench": "",
"workbench": "basiccrafting",
"ingredients": [
[1, 4, "basegame:49"]
]
},
"item@basegame:258": {
"workbench": "",
"workbench": "basiccrafting",
"ingredients": [
[1, 4, "basegame:50"]
]
},
"item@basegame:259": {
"workbench": "",
"workbench": "basiccrafting",
"ingredients": [
[1, 4, "basegame:51"]
]

View File

@@ -13,6 +13,7 @@ id;classname
12;net.torvald.terrarum.modulebasegame.gameitems.SledgehammerCopper
13;net.torvald.terrarum.modulebasegame.gameitems.SledgehammerSteel
16;net.torvald.terrarum.modulebasegame.gameitems.ItemWorkbench
256;net.torvald.terrarum.modulebasegame.gameitems.ItemSwingingDoorOak
257;net.torvald.terrarum.modulebasegame.gameitems.ItemSwingingDoorEbony
1 id classname
13 12 net.torvald.terrarum.modulebasegame.gameitems.SledgehammerCopper
14 13 net.torvald.terrarum.modulebasegame.gameitems.SledgehammerSteel
15 256 16 net.torvald.terrarum.modulebasegame.gameitems.ItemSwingingDoorOak net.torvald.terrarum.modulebasegame.gameitems.ItemWorkbench
16 256 net.torvald.terrarum.modulebasegame.gameitems.ItemSwingingDoorOak
17 257 net.torvald.terrarum.modulebasegame.gameitems.ItemSwingingDoorEbony
18 258 net.torvald.terrarum.modulebasegame.gameitems.ItemSwingingDoorBirch
19 259 net.torvald.terrarum.modulebasegame.gameitems.ItemSwingingDoorRosewood

View File

@@ -21,6 +21,7 @@ import net.torvald.terrarum.blockproperties.BlockCodex
import net.torvald.terrarum.blockproperties.WireCodex
import net.torvald.terrarum.gameactors.Actor
import net.torvald.terrarum.gameactors.ActorID
import net.torvald.terrarum.gameactors.ActorWithBody
import net.torvald.terrarum.gameactors.faction.FactionCodex
import net.torvald.terrarum.gameworld.fmod
import net.torvald.terrarum.itemproperties.CraftingCodex
@@ -33,6 +34,7 @@ import net.torvald.terrarum.worlddrawer.WorldCamera
import net.torvald.terrarumsansbitmap.gdx.TerrarumSansBitmap
import net.torvald.unsafe.UnsafeHelper
import net.torvald.util.CircularArray
import org.dyn4j.geometry.Vector2
import java.io.File
import java.io.PrintStream
import java.util.*
@@ -904,4 +906,22 @@ fun checkForSavegameDamage(skimmer: DiskSkimmer): Boolean {
inline fun Disposable.tryDispose() {
try { this.dispose() }
catch (_: Throwable) {}
}
fun distBetweenActors(a: ActorWithBody, b: ActorWithBody): Double {
val ww = INGAME.world.width * TILE_SIZED
val apos1 = a.centrePosVector
val apos2 = Vector2(apos1.x + ww, apos1.y)
val apos3 = Vector2(apos1.x - ww, apos1.y)
val bpos = b.centrePosVector
val dist = min(min(bpos.distanceSquared(apos1), bpos.distanceSquared(apos2)), bpos.distanceSquared(apos3))
return dist.sqrt()
}
fun distBetween(a: ActorWithBody, bpos: Vector2): Double {
val ww = INGAME.world.width * TILE_SIZED
val apos1 = a.centrePosVector
val apos2 = Vector2(apos1.x + ww, apos1.y)
val apos3 = Vector2(apos1.x - ww, apos1.y)
val dist = min(min(bpos.distanceSquared(apos1), bpos.distanceSquared(apos2)), bpos.distanceSquared(apos3))
return dist.sqrt()
}

View File

@@ -113,6 +113,13 @@ class IngameController(val terrarumIngame: TerrarumIngame) : InputAdapter() {
inputMouseY = newmy
}
private val inventoryCategoryAllowClickAndDrag = listOf(
GameItem.Category.TOOL,
GameItem.Category.WALL,
GameItem.Category.WIRE,
GameItem.Category.BLOCK
)
fun update() {
///////////////////
@@ -140,7 +147,7 @@ class IngameController(val terrarumIngame: TerrarumIngame) : InputAdapter() {
// - not clicking anymore
// - using any item that is not fixture (blocks, picks)
if (!Terrarum.mouseDown ||
GameItem.Category.MISC != ItemCodex.get(terrarumIngame.actorNowPlaying?.inventory?.itemEquipped?.get(GameItem.EquipPosition.HAND_GRIP))?.inventoryCategory) {
inventoryCategoryAllowClickAndDrag.contains(ItemCodex[terrarumIngame.actorNowPlaying?.inventory?.itemEquipped?.get(GameItem.EquipPosition.HAND_GRIP)]?.inventoryCategory)) {
worldPrimaryClickLatched = false
}

View File

@@ -340,7 +340,7 @@ abstract class GameItem(val originalID: ItemID) : Comparable<GameItem>, Cloneabl
@JvmStatic val BLOCK = "block"
@JvmStatic val WALL = "wall"
@JvmStatic val WIRE = "wire"
// @JvmStatic val FIXTURE = "fixture"
@JvmStatic val FIXTURE = "fixture"
@JvmStatic val MISC = "misc"
}
@@ -389,12 +389,8 @@ abstract class GameItem(val originalID: ItemID) : Comparable<GameItem>, Cloneabl
*/
fun mouseInInteractableRange(actor: ActorWithBody, action: () -> Long): Long {
val mousePos1 = Vector2(Terrarum.mouseX, Terrarum.mouseY)
val mousePos2 = Vector2(Terrarum.mouseX + INGAME.world.width * TILE_SIZED, Terrarum.mouseY)
val mousePos3 = Vector2(Terrarum.mouseX - INGAME.world.width * TILE_SIZED, Terrarum.mouseY)
val actorPos = actor.centrePosVector
val dist = min(min(actorPos.distanceSquared(mousePos1), actorPos.distanceSquared(mousePos2)), actorPos.distanceSquared(mousePos3))
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
return if (distBetween(actor, mousePos1) <= distMax) action() else -1
}
/**
@@ -410,16 +406,10 @@ fun mouseInInteractableRange(actor: ActorWithBody, action: () -> Long): Long {
*/
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)
val mousePos3 = Vector2(Terrarum.mouseX - INGAME.world.width * TILE_SIZED, Terrarum.mouseY)
val actorPos = actor.centrePosVector
val dist = min(min(actorPos.distanceSquared(mousePos1), actorPos.distanceSquared(mousePos2)), actorPos.distanceSquared(mousePos3))
val reachBonus = (actor.actorValue.getAsDouble(AVKey.REACHBUFF) ?: 1.0) * actor.scale
val distMax = actor.actorValue.getAsDouble(AVKey.REACH)!! * reachBonus // perform some error checking here
val toolDistMax = (TILE_SIZED * reachMultiplierInTiles(item?.material?.toolReach ?: Int.MAX_VALUE)) * reachBonus
if (dist <= min(toolDistMax, distMax).sqr()) return action() else return false
return if (distBetween(actor, mousePos1) <= min(toolDistMax, distMax)) action() else false
}
//fun IntRange.pickRandom() = HQRNG().nextInt(this.last - this.first + 1) + this.first // count() on 200 million entries? Se on vitun hyvää idea
//fun IntArray.pickRandom(): Int = this[HQRNG().nextInt(this.size)]

View File

@@ -0,0 +1,10 @@
package net.torvald.terrarum.modulebasegame.gameactors
/**
* Created by minjaesong on 2023-09-20.
*/
open interface CraftingStation {
val tags: List<String>
}

View File

@@ -21,7 +21,7 @@ class FixtureWallCalendar : FixtureBase {
val itemImage = FixtureItemBase.getItemImageFromSingleImage("basegame", "sprites/fixtures/calendar.tga")
density = 600.0
setHitboxDimension(TILE_SIZE, TILE_SIZE, 0, 1)
setHitboxDimension(TILE_SIZE, TILE_SIZE, 0, 0)
makeNewSprite(TextureRegionPack(itemImage.texture, TILE_SIZE, TILE_SIZE)).let {
it.setRowsAndFrames(1,1)

View File

@@ -0,0 +1,35 @@
package net.torvald.terrarum.modulebasegame.gameactors
import net.torvald.terrarum.BlockCodex
import net.torvald.terrarum.blockproperties.Block
import net.torvald.terrarum.gameactors.AVKey
import net.torvald.terrarum.langpack.Lang
import net.torvald.terrarum.modulebasegame.gameitems.FixtureItemBase
import net.torvald.terrarum.modulebasegame.ui.UIWallCalendar
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
/**
* Created by minjaesong on 2023-09-20.
*/
class FixtureWorkbench : FixtureBase, CraftingStation {
@Transient override val tags = listOf("basiccrafting")
constructor() : super(
BlockBox(BlockBox.ALLOW_MOVE_DOWN, 2, 1),
nameFun = { Lang["ITEM_WORKBENCH"] },
mainUI = UIWallCalendar()
) {
val itemImage = FixtureItemBase.getItemImageFromSingleImage("basegame", "sprites/fixtures/workbench.tga")
density = BlockCodex[Block.PLANK_NORMAL].density.toDouble()
setHitboxDimension(itemImage.texture.width, itemImage.texture.height, 0, 0)
makeNewSprite(TextureRegionPack(itemImage.texture, itemImage.texture.width, itemImage.texture.height)).let {
it.setRowsAndFrames(1,1)
}
actorValue[AVKey.BASEMASS] = 20.0
}
}

View File

@@ -1,6 +1,7 @@
package net.torvald.terrarum.modulebasegame.gameactors
import net.torvald.terrarum.App
import net.torvald.terrarum.BlockCodex
import net.torvald.terrarum.WireCodex
/**
@@ -72,13 +73,20 @@ object PlayerBuilderSigrid {
fun fillTestInventory(inventory: ActorInventory) {
App.tileMaker.tags.forEach { t, _ ->
inventory.add(t, 9995)
try {
inventory.add("wall@$t", 9995) // this code will try to add nonexisting wall items, do not get surprised with NPEs
}
catch (e: Throwable) {
System.err.println("[PlayerBuilder] $e")
App.tileMaker.tags.forEach { (t, _) ->
if (!BlockCodex[t].isActorBlock) {
inventory.add(t, 9995)
try {
inventory.add(
"wall@$t",
9995
) // this code will try to add nonexisting wall items, do not get surprised with NPEs
}
catch (e: NullPointerException) { /* tHiS iS fInE */ }
catch (e: Throwable) {
System.err.println("[PlayerBuilder] $e")
}
}
}
@@ -100,6 +108,8 @@ object PlayerBuilderSigrid {
inventory.add("item@basegame:11", 10) // calendar
inventory.add("item@basegame:16", 10) // workbench
// inventory.add("item@basegame:256", 995) // doors
// inventory.add("item@basegame:257", 995) // doors
// inventory.add("item@basegame:258", 995) // doors

View File

@@ -0,0 +1,29 @@
package net.torvald.terrarum.modulebasegame.gameitems
import com.badlogic.gdx.graphics.g2d.TextureRegion
import net.torvald.terrarum.gameitems.ItemID
/**
* Created by minjaesong on 2023-09-20.
*/
class ItemWorkbench(originalID: ItemID) : FixtureItemBase(originalID, "net.torvald.terrarum.modulebasegame.gameactors.FixtureWorkbench") {
override val originalName = "ITEM_WORKBENCH"
override var baseMass = 20.0
override var stackable = true
override var inventoryCategory = Category.MISC
override val isUnique = false
override val isDynamic = false
override val materialId = ""
override val itemImage: TextureRegion
get() = getItemImageFromSingleImage("basegame", "sprites/fixtures/workbench.tga")
override var baseToolSize: Double? = baseMass
init {
equipPosition = EquipPosition.HAND_GRIP
}
}

View File

@@ -4,8 +4,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.gamepadLabelLEFTRIGHT
import net.torvald.terrarum.App.gamepadLabelStart
import net.torvald.terrarum.App.*
import net.torvald.terrarum.UIItemInventoryCatBar.Companion.CAT_ALL
import net.torvald.terrarum.gameactors.AVKey
import net.torvald.terrarum.gameitems.GameItem
@@ -13,6 +12,7 @@ import net.torvald.terrarum.gameitems.ItemID
import net.torvald.terrarum.gameitems.isWall
import net.torvald.terrarum.itemproperties.CraftingCodex
import net.torvald.terrarum.langpack.Lang
import net.torvald.terrarum.modulebasegame.gameactors.CraftingStation
import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory
import net.torvald.terrarum.modulebasegame.gameactors.InventoryPair
import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryItemGrid.Companion.listGap
@@ -121,7 +121,7 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
// list of [Score, Ingredients, Recipe]
recipes.map { recipe ->
// list of (Item, How many player has, How many the recipe requires)
val items = recipeToIngredientRecord(player, recipe, listOf(/*todo: nearby crafting stations*/))
val items = recipeToIngredientRecord(player, recipe, nearbyCraftingStations)
val score = items.fold(1L) { acc, item ->
(item.howManyPlayerHas).times(16L) + 1L
@@ -330,6 +330,16 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
addUIitem(buttonCraft)
}
var nearbyCraftingStations = emptyList<String>(); protected set
fun getCraftingStationsWithinReach(): List<String> {
val reach = INGAME.actorNowPlaying!!.actorValue.getAsDouble(AVKey.REACH)!! * (INGAME.actorNowPlaying!!.actorValue.getAsDouble(AVKey.REACHBUFF) ?: 1.0) * INGAME.actorNowPlaying!!.scale
val nearbyCraftingStations = INGAME.findKNearestActors(INGAME.actorNowPlaying!!, 256) {
it is CraftingStation && (distBetweenActors(it, INGAME.actorNowPlaying!!) < reach)
}
return nearbyCraftingStations.flatMap { (it.get() as CraftingStation).tags }
}
private fun changeIngredient(old: InventoryPair, new: ItemID) {
itemListPlayer.removeFromForceHighlightList(oldSelectedItems)
@@ -391,6 +401,9 @@ class UICrafting(val full: UIInventoryFull) : UICanvas(), HasInventory {
private var openingClickLatched = false
override fun show() {
nearbyCraftingStations = getCraftingStationsWithinReach()
// printdbg(this, "Nearby crafting stations: $nearbyCraftingStations")
itemListPlayer.getInventory = { INGAME.actorNowPlaying!!.inventory }
itemListUpdate()

View File

@@ -74,8 +74,7 @@ class UIItemCraftingCandidateGrid(
craftingRecipes.clear()
CraftingRecipeCodex.props.forEach { (_, recipes) ->
recipes.forEach {
// TODO check for nearby crafting stations
if (isCraftable((parentUI as UICrafting).getPlayerInventory(), it, listOf(/*todo: nearby crafting stations*/))) craftingRecipes.add(it)
if (isCraftable((parentUI as UICrafting).getPlayerInventory(), it, (parentUI as UICrafting).nearbyCraftingStations)) craftingRecipes.add(it)
}
}