diff --git a/src/net/torvald/terrarum/modulebasegame/TerrarumIngame.kt b/src/net/torvald/terrarum/modulebasegame/TerrarumIngame.kt index 6e9b4b381..ffb2ec278 100644 --- a/src/net/torvald/terrarum/modulebasegame/TerrarumIngame.kt +++ b/src/net/torvald/terrarum/modulebasegame/TerrarumIngame.kt @@ -692,6 +692,13 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) { private var worldPrimaryClickLatch = false + + private fun fireFixtureInteractEvent(fixture: FixtureBase, mwx: Double, mwy: Double) { + if (fixture.mouseUp) { + fixture.onInteract(mwx, mwy) + } + } + // left click: use held item, attack, pick up fixture if i'm holding a pickaxe or hammer (aka tool), do 'bare hand action' if holding nothing override fun worldPrimaryClickStart(actor: ActorWithBody, delta: Float) { //println("[Ingame] worldPrimaryClickStart $delta") @@ -709,13 +716,25 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) { //////////////////////////////// - // #1. If ~~there is no UI under and~~ I'm holding an item, use it + // #1. If holding an item, use it // don't want to open the UI and use the item at the same time, would ya? if (itemOnGrip != null) { val consumptionSuccessful = itemOnGrip.startPrimaryUse(actor, delta) if (consumptionSuccessful > -1) (actor as Pocketed).inventory.consumeItem(itemOnGrip, consumptionSuccessful) + // TODO filter blocks/walls/wires/wire cutter + if (fixtureUnderMouse != null) { + if (!worldPrimaryClickLatch) { + mouseInInteractableRange(actor) { mwx, mwy, mtx, mty -> + fixtureUnderMouse.let { fixture -> + fireFixtureInteractEvent(fixture, mwx, mwy) + } + 0L + } + } + } + worldPrimaryClickLatch = true } else { @@ -740,6 +759,10 @@ open class TerrarumIngame(batch: FlippingSpriteBatch) : IngameInstance(batch) { ) ui.setAsOpen() } + + if (!uiOpened) { + fireFixtureInteractEvent(fixture, mwx, mwy) + } } } 0L diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt index 5aee3cbcf..27b46a2ed 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt @@ -241,6 +241,19 @@ open class FixtureBase : ActorWithBody, CuedByTerrainChange { */ open fun onSpawn(tx: Int, ty: Int) {} + /** + * If the fixture has mainUI, do NOT override this function! Opening of the UI is handled by TerrarumIngame.worldPrimaryClickStart + * + * For someone might be concerened, you don't need to have a MouseLatch or worry about the `if (mouseUp)`; it's considered by the Ingame + * + * Fired when "just clicked" by mousePrimary + * + * @param mx Terrarum.mouseX (mouse position in the world) + * @param my Terrarum.omuseY (mouse position in the world) + */ + open fun onInteract(mx: Double, my: Double) { + } + /** * Making the sprite: do not address the CommonResourcePool directly; just do it like this snippet: * diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureMusicalTurntable.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureMusicalTurntable.kt index 941b961ee..dcbb872c0 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureMusicalTurntable.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureMusicalTurntable.kt @@ -28,9 +28,7 @@ class FixtureMusicalTurntable : Electric, PlaysMusic { constructor() : super( BlockBox(BlockBox.NO_COLLISION, 1, 1), nameFun = { Lang["ITEM_TURNTABLE"] } - ) { - clickLatch.forceLatch() - } + ) @Transient var musicNowPlaying: MusicContainer? = null; private set @@ -67,42 +65,36 @@ class FixtureMusicalTurntable : Electric, PlaysMusic { internal var disc: ItemID? = null - @Transient private val clickLatch = MouseLatch() - override val canBeDespawned: Boolean get() = disc == null - override fun updateImpl(delta: Float) { - super.updateImpl(delta) + override fun onInteract(mx: Double, my: Double) { + if (disc == null) { + if (INGAME.actorNowPlaying != null) { + val itemOnGrip = + INGAME.actorNowPlaying!!.inventory.itemEquipped.get(GameItem.EquipPosition.HAND_GRIP) + val itemProp = ItemCodex[itemOnGrip] - // right click - if (mouseUp) { - clickLatch.latch { - if (disc == null) { - if (INGAME.actorNowPlaying != null) { - val itemOnGrip = - INGAME.actorNowPlaying!!.inventory.itemEquipped.get(GameItem.EquipPosition.HAND_GRIP) - val itemProp = ItemCodex[itemOnGrip] - - if (itemProp?.hasAllTagOf("MUSIC", "PHONO") == true) { - disc = itemOnGrip - INGAME.actorNowPlaying!!.removeItem(itemOnGrip!!) - playDisc() - } - } - } - else { - stopGracefully() - PickaxeCore.dropItem( - disc!!, - intTilewiseHitbox.canonicalX.toInt(), - intTilewiseHitbox.canonicalY.toInt() - ) - disc = null + if (itemProp?.hasAllTagOf("MUSIC", "PHONO") == true) { + disc = itemOnGrip + INGAME.actorNowPlaying!!.removeItem(itemOnGrip!!) + playDisc() } } } + else { + stopGracefully() + PickaxeCore.dropItem( + disc!!, + intTilewiseHitbox.canonicalX.toInt(), + intTilewiseHitbox.canonicalY.toInt() + ) + disc = null + } + } + override fun updateImpl(delta: Float) { + super.updateImpl(delta) // supress the normal background music playback if (musicIsPlaying && !flagDespawn) { diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSignalSwitchManual.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSignalSwitchManual.kt index b4a45b548..e6785ee4f 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSignalSwitchManual.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSignalSwitchManual.kt @@ -21,9 +21,7 @@ class FixtureSignalSwitchManual : Electric { constructor() : super( BlockBox(BlockBox.NO_COLLISION, 1, 1), nameFun = { Lang["ITEM_LOGIC_SIGNAL_SWITCH"] } - ) { - clickLatch.forceLatch() - } + ) private val variant = (Math.random() * 8).toInt() private var state = false // false = off @@ -61,19 +59,9 @@ class FixtureSignalSwitchManual : Electric { setWireEmissionAt(0, 0, Vector2(state.toInt().toDouble(), 0.0)) } - @Transient private val clickLatch = MouseLatch() - - override fun updateImpl(delta: Float) { - super.updateImpl(delta) - - // right click - if (mouseUp) { - clickLatch.latch { - state = !state - (sprite as SheetSpriteAnimation).currentRow = state.toInt() - setWireEmissionAt(0, 0, Vector2(state.toInt().toDouble(), 0.0)) - } - } - + override fun onInteract(mx: Double, my: Double) { + state = !state + (sprite as SheetSpriteAnimation).currentRow = state.toInt() + setWireEmissionAt(0, 0, Vector2(state.toInt().toDouble(), 0.0)) } } \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSwingingDoorBase.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSwingingDoorBase.kt index 2f928f298..47e90316f 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSwingingDoorBase.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSwingingDoorBase.kt @@ -366,6 +366,25 @@ open class FixtureSwingingDoorBase : FixtureBase { private var lastDoorHandler = 0 // 0: automatic, 1: manual private var doorCloseQueueHandler = 0 + override fun onInteract(mx: Double, my: Double) { + // keep opened/closed as long as the mouse is down + if (doorStateTimer != 0f) { + oldStateBeforeMouseDown = doorState + } + + if (oldStateBeforeMouseDown == 0) { + if (mouseOnLeftSide(mx, my)) + openToLeft(1) + else if (mouseOnRightSide(mx, my)) + openToRight(1) + } + else { + closeDoor(1) + } + + doorStateTimer = 0f + } + override fun updateImpl(delta: Float) { super.updateImpl(delta) @@ -389,34 +408,10 @@ open class FixtureSwingingDoorBase : FixtureBase { } // manual opening/closing - if (mouseUp && Gdx.input.isButtonPressed(App.getConfigInt("config_mousesecondary"))) { + // is handled on the onInteract() - INGAME.actorNowPlaying?.let { player -> - mouseInInteractableRange(player) { mx, my, _, _ -> - // keep opened/closed as long as the mouse is down - if (doorStateTimer != 0f) { - oldStateBeforeMouseDown = doorState - } - - if (oldStateBeforeMouseDown == 0) { - if (mouseOnLeftSide(mx, my)) - openToLeft(1) - else if (mouseOnRightSide(mx, my)) - openToRight(1) - } - else { - closeDoor(1) - } - - doorStateTimer = 0f - - 0L - } - } - - } // automatic opening/closing - else if (doorStateTimer > doorHoldLength[doorState]!!) { + if (doorStateTimer > doorHoldLength[doorState]!!) { // val actors = INGAME.actorContainerActive.filterIsInstance() // auto opening and closing diff --git a/src/net/torvald/terrarum/modulebasegame/gameitems/WireCutterAll.kt b/src/net/torvald/terrarum/modulebasegame/gameitems/WireCutterAll.kt index a5ac96009..40608b2db 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameitems/WireCutterAll.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameitems/WireCutterAll.kt @@ -89,7 +89,7 @@ class WireCutterAll(originalID: ItemID) : GameItem(originalID) { override var baseToolSize: Double? = baseMass override var inventoryCategory = Category.TOOL override val canBeDynamic = false - override val materialId = "" + override val materialId = "STAL" // this is to just increase the reach override val itemImage: TextureRegion get() = CommonResourcePool.getAsItemSheet("basegame.items").get(1, 3)