From 5957f70ff8b8ae972d97f99d1b23c277a8a3c2b3 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Wed, 20 Sep 2023 16:01:40 +0900 Subject: [PATCH] working crafting workbench --- assets/mods/basegame/crafting/items.json | 8 ++--- assets/mods/basegame/items/itemid.csv | 1 + src/net/torvald/terrarum/Terrarum.kt | 20 +++++++++++ .../gamecontroller/IngameController.kt | 9 ++++- .../torvald/terrarum/gameitems/GameItem.kt | 16 ++------- .../gameactors/CraftingStation.kt | 10 ++++++ .../gameactors/FixtureWallCalendar.kt | 2 +- .../gameactors/FixtureWorkbench.kt | 35 +++++++++++++++++++ .../gameactors/PlayerBuilderSigrid.kt | 24 +++++++++---- .../modulebasegame/gameitems/ItemWorkbench.kt | 29 +++++++++++++++ .../terrarum/modulebasegame/ui/UICrafting.kt | 19 ++++++++-- .../ui/UIItemCraftingCandidateGrid.kt | 3 +- 12 files changed, 145 insertions(+), 31 deletions(-) create mode 100644 src/net/torvald/terrarum/modulebasegame/gameactors/CraftingStation.kt create mode 100644 src/net/torvald/terrarum/modulebasegame/gameactors/FixtureWorkbench.kt create mode 100644 src/net/torvald/terrarum/modulebasegame/gameitems/ItemWorkbench.kt diff --git a/assets/mods/basegame/crafting/items.json b/assets/mods/basegame/crafting/items.json index 6e4643820..46d0e3be7 100644 --- a/assets/mods/basegame/crafting/items.json +++ b/assets/mods/basegame/crafting/items.json @@ -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"] ] diff --git a/assets/mods/basegame/items/itemid.csv b/assets/mods/basegame/items/itemid.csv index 32f715ec0..f64f4dc66 100644 --- a/assets/mods/basegame/items/itemid.csv +++ b/assets/mods/basegame/items/itemid.csv @@ -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 diff --git a/src/net/torvald/terrarum/Terrarum.kt b/src/net/torvald/terrarum/Terrarum.kt index d99e8f202..f17e62804 100644 --- a/src/net/torvald/terrarum/Terrarum.kt +++ b/src/net/torvald/terrarum/Terrarum.kt @@ -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() } \ No newline at end of file diff --git a/src/net/torvald/terrarum/gamecontroller/IngameController.kt b/src/net/torvald/terrarum/gamecontroller/IngameController.kt index 19ea883e1..6806b06b1 100644 --- a/src/net/torvald/terrarum/gamecontroller/IngameController.kt +++ b/src/net/torvald/terrarum/gamecontroller/IngameController.kt @@ -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 } diff --git a/src/net/torvald/terrarum/gameitems/GameItem.kt b/src/net/torvald/terrarum/gameitems/GameItem.kt index b15221504..a6158d852 100644 --- a/src/net/torvald/terrarum/gameitems/GameItem.kt +++ b/src/net/torvald/terrarum/gameitems/GameItem.kt @@ -340,7 +340,7 @@ abstract class GameItem(val originalID: ItemID) : Comparable, 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, 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)] diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/CraftingStation.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/CraftingStation.kt new file mode 100644 index 000000000..19f286470 --- /dev/null +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/CraftingStation.kt @@ -0,0 +1,10 @@ +package net.torvald.terrarum.modulebasegame.gameactors + +/** + * Created by minjaesong on 2023-09-20. + */ +open interface CraftingStation { + + val tags: List + +} diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureWallCalendar.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureWallCalendar.kt index 347fe1946..9597e0435 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureWallCalendar.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureWallCalendar.kt @@ -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) diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureWorkbench.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureWorkbench.kt new file mode 100644 index 000000000..6d0de8e90 --- /dev/null +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureWorkbench.kt @@ -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 + } + +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/PlayerBuilderSigrid.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/PlayerBuilderSigrid.kt index 5cc830b8f..a9a1a5e09 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/PlayerBuilderSigrid.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/PlayerBuilderSigrid.kt @@ -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 diff --git a/src/net/torvald/terrarum/modulebasegame/gameitems/ItemWorkbench.kt b/src/net/torvald/terrarum/modulebasegame/gameitems/ItemWorkbench.kt new file mode 100644 index 000000000..261128c67 --- /dev/null +++ b/src/net/torvald/terrarum/modulebasegame/gameitems/ItemWorkbench.kt @@ -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 + } + + +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UICrafting.kt b/src/net/torvald/terrarum/modulebasegame/ui/UICrafting.kt index 48e8c0c1d..3b62b5bac 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UICrafting.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UICrafting.kt @@ -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(); protected set + + fun getCraftingStationsWithinReach(): List { + 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() diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIItemCraftingCandidateGrid.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIItemCraftingCandidateGrid.kt index 99102a573..e3d90ff7d 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIItemCraftingCandidateGrid.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIItemCraftingCandidateGrid.kt @@ -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) } }