fixture clicking action is now integrated into the FixtureBase

This commit is contained in:
minjaesong
2024-03-05 23:59:32 +09:00
parent 1dcdd8867a
commit 3182843a48
6 changed files with 87 additions and 76 deletions

View File

@@ -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

View File

@@ -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:
*

View File

@@ -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) {

View File

@@ -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))
}
}

View File

@@ -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<ActorWithBody>()
// auto opening and closing

View File

@@ -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)