From 9a33375286fa5737fd80cdd826943495a97619bc Mon Sep 17 00:00:00 2001 From: minjaesong Date: Sun, 6 Oct 2024 01:27:45 +0900 Subject: [PATCH] gearbox wip --- assets/mods/basegame/items/itemid.csv | 4 +- assets/mods/basegame/wires/ports.tga | 2 +- .../modulebasegame/gameactors/Electric.kt | 3 +- .../gameactors/FixtureInductionMotor.kt | 136 +++++++++++++++++- .../gameactors/FixtureLogicSignalAdder.kt | 2 +- .../gameitems/ItemInductionMotor.kt | 29 ++++ .../modulebasegame/ui/UIQuickslotBar.kt | 16 ++- .../modulebasegame/ui/UIQuickslotPie.kt | 2 +- .../graphics/sprites/fixtures/gearbox.kra | 3 + work_files/graphics/wires/ports.kra | 4 +- 10 files changed, 181 insertions(+), 20 deletions(-) create mode 100644 work_files/graphics/sprites/fixtures/gearbox.kra diff --git a/assets/mods/basegame/items/itemid.csv b/assets/mods/basegame/items/itemid.csv index 6736164dc..4d285359c 100644 --- a/assets/mods/basegame/items/itemid.csv +++ b/assets/mods/basegame/items/itemid.csv @@ -57,7 +57,6 @@ id;classname;tags 56;net.torvald.terrarum.modulebasegame.gameitems.ItemClayBrick; 57;net.torvald.terrarum.modulebasegame.gameitems.ItemLogicSignalPushbutton;FIXTURE,SIGNAL 58;net.torvald.terrarum.modulebasegame.gameitems.ItemLogicSignalPressurePlate;FIXTURE,SIGNAL -59;net.torvald.terrarum.modulebasegame.gameitems.ItemInductionMotor;FIXTURE,POWER,KINETIC # ingots @@ -132,6 +131,9 @@ id;classname;tags 320;net.torvald.terrarum.modulebasegame.gameitems.ItemWorldPortal;FIXTURE,STATION +# industrial +2048;net.torvald.terrarum.modulebasegame.gameitems.ItemInductionMotor;FIXTURE,POWER,KINETIC +2049;net.torvald.terrarum.modulebasegame.gameitems.ItemGearbox;FIXTURE,POWER,KINETIC # data storage (discs; 256) # 32768 is a reserved number for a blank disc diff --git a/assets/mods/basegame/wires/ports.tga b/assets/mods/basegame/wires/ports.tga index 33b37c6b2..30c112563 100644 --- a/assets/mods/basegame/wires/ports.tga +++ b/assets/mods/basegame/wires/ports.tga @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:fabe4a500202846e042b6679bad30e271ea5c3ee1c33e25313e5f95ec6f6ef2c +oid sha256:c5fb7d049fb3f0ac6c8bd9031a6c602b61d84c0e80debdb0ce06a419e9bd5404 size 4114 diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/Electric.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/Electric.kt index adf5990e0..2f96a348b 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/Electric.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/Electric.kt @@ -52,6 +52,8 @@ open class Electric : FixtureBase { const val ELECTRIC_THRESHOLD_HIGH = 0.6666666666666666 const val ELECTRIC_THRESHOLD_LOW = 0.3333333333333333 const val ELECTRIC_THRESHOLD_EDGE_DELTA = 0.33333333333333337 + + const val ELECTRIC_EPSILON_GENERIC = 1.0 / 1024.0 } fun getWireEmitterAt(blockBoxIndex: BlockBoxIndex) = this.wireEmitterTypes[blockBoxIndex] @@ -98,7 +100,6 @@ open class Electric : FixtureBase { open fun updateSignal() {} fun getWireStateAt(offsetX: Int, offsetY: Int, sinkType: WireEmissionType): Vector2 { - val index = pointToBlockBoxIndex(offsetX, offsetY) val wx = offsetX + intTilewiseHitbox.startX.toInt() val wy = offsetY + intTilewiseHitbox.startY.toInt() diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureInductionMotor.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureInductionMotor.kt index 457267acf..ff272d9ca 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureInductionMotor.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureInductionMotor.kt @@ -1,12 +1,20 @@ package net.torvald.terrarum.modulebasegame.gameactors +import net.torvald.spriteanimation.SheetSpriteAnimation +import net.torvald.terrarum.App +import net.torvald.terrarum.INGAME +import net.torvald.terrarum.Point2i import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZE import net.torvald.terrarum.gameactors.AVKey import net.torvald.terrarum.langpack.Lang +import net.torvald.terrarum.modulebasegame.gameactors.FixtureInductionMotor.Companion.MASS import net.torvald.terrarum.modulebasegame.gameitems.FixtureItemBase import net.torvald.terrarum.modulebasegame.ui.UIItemInventoryCellCommonRes.tooltipShowing +import net.torvald.terrarum.toInt import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack import org.dyn4j.geometry.Vector2 +import kotlin.math.absoluteValue +import kotlin.math.roundToLong /** * Created by minjaesong on 2024-10-03. @@ -21,12 +29,6 @@ class FixtureInductionMotor : Electric { nameFun = { Lang["ITEM_INDUCTION_MOTOR"] } ) - override fun placeActorBlocks() { - worldBlockPos?.let { (x, y) -> - world!!.setTileTerrain(x+1, y, blockBox.collisionType, true) - } - } - override fun getBlockBoxPositions(posX: Int, posY: Int): List> { return listOf(posX+1 to posY) } @@ -61,6 +63,128 @@ class FixtureInductionMotor : Electric { companion object { const val MASS = 20.0 } +} + +/** + * Created by minjaesong on 2024-10-05. + */ +class FixtureGearbox : Electric { + + @Transient override val spawnNeedsFloor = true + @Transient override val spawnNeedsWall = false + + constructor() : super( + BlockBox(BlockBox.NO_COLLISION, 3, 3), + nameFun = { Lang["ITEM_GEARBOX"] } + ) + + override fun getBlockBoxPositions(posX: Int, posY: Int): List> { + return listOf(posX+1 to posY+1) + } + + init { + val itemImage = FixtureItemBase.getItemImageFromSingleImage("basegame", "sprites/fixtures/gearbox.tga") + + density = 7874.0 + setHitboxDimension(TILE_SIZE, TILE_SIZE, 0, TILE_SIZE) + + makeNewSprite(TextureRegionPack(itemImage.texture, TILE_SIZE, TILE_SIZE+1)).let { + it.setRowsAndFrames(1,1) + } + + actorValue[AVKey.BASEMASS] = MASS + + posVecs.forEach { (x, y) -> + setWireEmitterAt(x, y, "axle") + setWireSinkAt(x, y, "axle") + } + } + + override fun updateImpl(delta: Float) { + super.updateImpl(delta) + + // animate the sprite + val worldX = intTilewiseHitbox.startX.toInt() + val worldY = intTilewiseHitbox.startY.toInt() + + val targetTick = (App.TICK_SPEED / speedMax).let { + it.coerceIn(0.0, Long.MAX_VALUE.toDouble()) + }.roundToLong() + + val sprite = (sprite as SheetSpriteAnimation) + + val phaseShift = if (worldY % 2 == 0) + (worldX % 2 == 1) + else + (worldX % 2 == 0) + if (targetTick > 0) { + (INGAME.WORLD_UPDATE_TIMER % (targetTick * 2)).let { + sprite.currentFrame = + if (phaseShift) + (it < targetTick).toInt() + else + (it >= targetTick).toInt() + } + } + } + + @Transient private var speedMax = 0.0 + + private var maxSpeedLoc = ArrayList() + private var minTorqueLoc = ArrayList() + + @Transient private val newMaxSpeedLoc = ArrayList() + @Transient private val newMinTorqueLoc = ArrayList() + + override fun updateSignal() { + var torqueMin = Double.POSITIVE_INFINITY + newMaxSpeedLoc.clear() + newMinTorqueLoc.clear() + + speedMax = 0.0 + posVecs.forEach { + val vec = getWireStateAt(it.x, it.y, "axle") + if (!maxSpeedLoc.contains(it) && vec.x >= speedMax) { + newMaxSpeedLoc.add(Point2i(it.x, it.y)) + speedMax = vec.x + } + if (!minTorqueLoc.contains(it) && vec.y.absoluteValue >= ELECTRIC_EPSILON_GENERIC && vec.y <= torqueMin) { + newMinTorqueLoc.add(Point2i(it.x, it.y)) + torqueMin = if (vec.y == Double.POSITIVE_INFINITY) 0.0 else vec.y + } + + // FIXME: intTilewiseHitbox discrepancy with spawn position. + val wx = it.x + intTilewiseHitbox.startX.toInt() + val wy = it.y + intTilewiseHitbox.startY.toInt() + print("$wx,$wy\t") + + if (maxSpeedLoc.contains(it)) + println("$it*\t$vec") + else + println("$it\t$vec") + } + println("--------") + + maxSpeedLoc.clear(); maxSpeedLoc.addAll(newMaxSpeedLoc) + minTorqueLoc.clear(); minTorqueLoc.addAll(newMinTorqueLoc) + + posVecs.forEach { (x, y) -> + setWireEmissionAt(x, y, Vector2(speedMax, torqueMin)) + } + } + + override fun dispose() { + tooltipShowing.remove(tooltipHash) + } + + companion object { + @Transient val posVecs = listOf( + Point2i(1, 0), + Point2i(0, 1), + Point2i(2, 1), + Point2i(1, 2), + ) + } } \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureLogicSignalAdder.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureLogicSignalAdder.kt index 17fe08950..59762baf6 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureLogicSignalAdder.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureLogicSignalAdder.kt @@ -97,7 +97,7 @@ class FixtureLogicSignalAdder : Electric, Reorientable { private val I: Boolean get() = when (orientation) { - 0 -> getWireStateAt(0, 0, "digital_bit").x >= ELECTRIC_THRESHOLD_HIGH +// 0 -> getWireStateAt(0, 0, "digital_bit").x >= ELECTRIC_THRESHOLD_HIGH 1 -> getWireStateAt(1, 0, "digital_bit").x >= ELECTRIC_THRESHOLD_HIGH 2 -> getWireStateAt(1, 1, "digital_bit").x >= ELECTRIC_THRESHOLD_HIGH 3 -> getWireStateAt(0, 1, "digital_bit").x >= ELECTRIC_THRESHOLD_HIGH diff --git a/src/net/torvald/terrarum/modulebasegame/gameitems/ItemInductionMotor.kt b/src/net/torvald/terrarum/modulebasegame/gameitems/ItemInductionMotor.kt index 0b58fe9c3..791ba0ee0 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameitems/ItemInductionMotor.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameitems/ItemInductionMotor.kt @@ -34,4 +34,33 @@ class ItemInductionMotor(originalID: ItemID) : FixtureItemBase(originalID, "net. (Terrarum.ingame!! as TerrarumIngame).selectedWireRenderClass = "" } +} + +/** + * Created by minjaesong on 2024-10-05. + */ +class ItemGearbox(originalID: ItemID) : FixtureItemBase(originalID, "net.torvald.terrarum.modulebasegame.gameactors.FixtureGearbox") { + + override var dynamicID: ItemID = originalID + override var baseMass = FixtureInductionMotor.MASS + override val canBeDynamic = false + override val materialId = "" + init { + itemImage = getItemImageFromSheet("basegame", "sprites/fixtures/gearbox.tga", TILE_SIZE, TILE_SIZE+1) + } + + override var baseToolSize: Double? = baseMass + override var originalName = "ITEM_GEARBOX" + override var inventoryCategory = Category.FIXTURE + + override fun effectWhileEquipped(actor: ActorWithBody, delta: Float) { + super.effectWhileEquipped(actor, delta) + (Terrarum.ingame!! as TerrarumIngame).selectedWireRenderClass = "axle" + } + + override fun effectOnUnequip(actor: ActorWithBody) { + super.effectOnUnequip(actor) + (Terrarum.ingame!! as TerrarumIngame).selectedWireRenderClass = "" + } + } \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIQuickslotBar.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIQuickslotBar.kt index 8f42f98d9..d369216c0 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIQuickslotBar.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIQuickslotBar.kt @@ -49,22 +49,24 @@ class UIQuickslotBar : UICanvas() { const val DISPLAY_OPACITY = 0.92f const val COMMON_OPEN_CLOSE = 0.12f // val QUICKSLOT_ITEMCOUNT_TEXTCOL = Color(0xdb6f00ff.toInt()) - val QUICKSLOT_ITEMCOUNT_TEXTCOL = Color(0x0099bbff.toInt()) +// val QUICKSLOT_ITEMCOUNT_TEXTCOL = Color(0x0099bbff.toInt()) + val QUICKSLOT_ITEMCOUNT_TEXTCOL = Color(0xce773bff.toInt()) } override fun updateImpl(delta: Float) { - var newSelection = (Terrarum.ingame!! as TerrarumIngame).actorNowPlaying?.actorValue?.getAsInt(AVKey.__PLAYER_QUICKSLOTSEL) ?: 0 + val ingame = Terrarum.ingame!! as TerrarumIngame + var newSelection = ingame.actorNowPlaying?.actorValue?.getAsInt(AVKey.__PLAYER_QUICKSLOTSEL) ?: 0 - // make clicking work - if (mouseUp && mousePushed) { + // make clicking work only when no fullscreen UI is open + if (mouseUp && mousePushed && ingame.uiContainer.hasNoUIsUnderMouse) { for (i in 0 until SLOT_COUNT) { val slotX = cellSize / 2 + (cellSize + gutter) * i - ItemSlotImageFactory.TILE_WIDTH/2 val slotY = cellSize / 2 - ItemSlotImageFactory.TILE_WIDTH/2 if (relativeMouseX in slotX until slotX + cellSize && relativeMouseY in slotY until slotY + cellSize) { newSelection = i - (Terrarum.ingame!! as TerrarumIngame).actorNowPlaying?.actorValue?.set(AVKey.__PLAYER_QUICKSLOTSEL, i) + ingame.actorNowPlaying?.actorValue?.set(AVKey.__PLAYER_QUICKSLOTSEL, i) } } } @@ -142,7 +144,7 @@ class UIQuickslotBar : UICanvas() { val amountString = qs!!.qty.toItemCountText() batch.color = QUICKSLOT_ITEMCOUNT_TEXTCOL val textLen = amountString.length * App.fontSmallNumbers.W - val y = slotY + 25 - App.fontSmallNumbers.H + val y = slotY + 25 - App.fontSmallNumbers.H - 1 val x = slotX - 19 + (38 - textLen) / 2 App.fontSmallNumbers.draw(batch, amountString, x.toFloat(), y.toFloat()) } @@ -160,7 +162,7 @@ class UIQuickslotBar : UICanvas() { drawColor.set(item?.nameColour ?: Color.WHITE) drawColor.a = nameShowupAlpha batch.color = drawColor - App.fontGame.draw(batch, text, (width - textWidth) / 2, height - 20) + App.fontGame.draw(batch, text, (width - textWidth) / 2, height - 24) } } } diff --git a/src/net/torvald/terrarum/modulebasegame/ui/UIQuickslotPie.kt b/src/net/torvald/terrarum/modulebasegame/ui/UIQuickslotPie.kt index 11dc897e4..99a6bf6f0 100644 --- a/src/net/torvald/terrarum/modulebasegame/ui/UIQuickslotPie.kt +++ b/src/net/torvald/terrarum/modulebasegame/ui/UIQuickslotPie.kt @@ -113,7 +113,7 @@ class UIQuickslotPie : UICanvas() { val amountString = qs!!.qty.toItemCountText() batch.color = QUICKSLOT_ITEMCOUNT_TEXTCOL val textLen = amountString.length * App.fontSmallNumbers.W - val y = slotY + 25 - App.fontSmallNumbers.H + val y = slotY + 25 - App.fontSmallNumbers.H - 1 val x = slotX - 19 + (38 - textLen) / 2 App.fontSmallNumbers.draw(batch, amountString, x.toFloat(), y.toFloat()) } diff --git a/work_files/graphics/sprites/fixtures/gearbox.kra b/work_files/graphics/sprites/fixtures/gearbox.kra new file mode 100644 index 000000000..30d6ad3e3 --- /dev/null +++ b/work_files/graphics/sprites/fixtures/gearbox.kra @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fcd7f43303a3fe8f116714ab7e2643398e790e67bd459fe0c4b1f3d4c37f90f4 +size 121639 diff --git a/work_files/graphics/wires/ports.kra b/work_files/graphics/wires/ports.kra index 36ad58dea..5c5a4c52f 100644 --- a/work_files/graphics/wires/ports.kra +++ b/work_files/graphics/wires/ports.kra @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:d4ca9b6979ecaba0d3c9a2cf1c302ca64298a18605e8d5e149a080d9bbf99e6e -size 82186 +oid sha256:f46792bcbaeacb52b88134759b9c1ef8f3bdbd7ee79a047445b4c2430a58b5e0 +size 101733