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

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