diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt index c49f7599c..2c3906a4f 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt @@ -89,14 +89,9 @@ open class Electric : FixtureBase { /** Triggered when 'digital_bit' is held low. This function WILL NOT be triggered simultaneously with the falling edge. Level detection only considers the real component (labeled as 'x') of the vector */ open fun onSignalLow(readFrom: BlockBoxIndex) {} - fun getWireStateAt(offsetX: Int, offsetY: Int, type: String): Vector2 { - val wx = offsetX + intTilewiseHitbox.startX.toInt() - val wy = offsetY + intTilewiseHitbox.startY.toInt() - return WireCodex.getAllWiresThatAccepts(type).fold(Vector2()) { acc, (id, _) -> - INGAME.world.getWireEmitStateOf(wx, wy, id).let { - Vector2(acc.x + (it?.x ?: 0.0), acc.y + (it?.y ?: 0.0)) - } - } + fun getWireStateAt(offsetX: Int, offsetY: Int): Vector2 { + val index = pointToBlockBoxIndex(offsetX, offsetY) + return oldSinkStatus[index] } private val oldSinkStatus: Array @@ -112,6 +107,12 @@ open class Electric : FixtureBase { } } + val new2 = WireCodex.getAllWiresThatAccepts(wireSinkTypes[index] ?: "").fold(Vector2()) { acc, (id, _) -> + INGAME.world.getWireEmitStateOf(wx, wy, id).let { + Vector2(acc.x + (it?.x ?: 0.0), acc.y + (it?.y ?: 0.0)) + } + } + if (sinkType == "digital_bit") { if (new.x - old.x >= ELECTRIC_THRESHOLD_EDGE_DELTA && new.x >= ELECTIC_THRESHOLD_HIGH) onRisingEdge(index) @@ -122,11 +123,16 @@ open class Electric : FixtureBase { else if (new.y <= ELECTRIC_THRESHOLD_LOW) onSignalLow(index) } + + oldSinkStatus[index].set(new2) } + /** + * Refrain from updating signal output from this function: there will be 1 extra tick delay if you do so + */ override fun updateImpl(delta: Float) { super.updateImpl(delta) - oldSinkStatus.indices.forEach { index -> + /*oldSinkStatus.indices.forEach { index -> val wx = (index % blockBox.width) + intTilewiseHitbox.startX.toInt() val wy = (index / blockBox.width) + intTilewiseHitbox.startY.toInt() val new = WireCodex.getAllWiresThatAccepts(getWireSinkAt(index % blockBox.width, index / blockBox.width) ?: "").fold(Vector2()) { acc, (id, _) -> @@ -135,7 +141,7 @@ open class Electric : FixtureBase { } } oldSinkStatus[index].set(new) - } + }*/ } } diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSignalBlocker.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSignalBlocker.kt index 6998b8231..ca94c889d 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSignalBlocker.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSignalBlocker.kt @@ -1,6 +1,7 @@ package net.torvald.terrarum.modulebasegame.gameactors import com.badlogic.gdx.graphics.g2d.SpriteBatch +import net.torvald.terrarum.App.printdbg import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZE import net.torvald.terrarum.langpack.Lang import net.torvald.terrarum.modulebasegame.gameitems.FixtureItemBase @@ -31,6 +32,9 @@ interface Reorientable { */ class FixtureSignalBlocker : Electric, Reorientable { + @Transient override val spawnNeedsWall = false + @Transient override val spawnNeedsFloor = false + constructor() : super( BlockBox(BlockBox.NO_COLLISION, 2, 2), nameFun = { Lang["ITEM_LOGIC_SIGNAL_BLOCKER"] } @@ -95,19 +99,19 @@ class FixtureSignalBlocker : Electric, Reorientable { private val I: Boolean get() = when (orientation) { - 0 -> getWireStateAt(0, 0, "digital_bit").x >= ELECTIC_THRESHOLD_HIGH - 1 -> getWireStateAt(1, 0, "digital_bit").x >= ELECTIC_THRESHOLD_HIGH - 2 -> getWireStateAt(1, 1, "digital_bit").x >= ELECTIC_THRESHOLD_HIGH - 3 -> getWireStateAt(0, 1, "digital_bit").x >= ELECTIC_THRESHOLD_HIGH + 0 -> getWireStateAt(0, 0).x >= ELECTIC_THRESHOLD_HIGH + 1 -> getWireStateAt(1, 0).x >= ELECTIC_THRESHOLD_HIGH + 2 -> getWireStateAt(1, 1).x >= ELECTIC_THRESHOLD_HIGH + 3 -> getWireStateAt(0, 1).x >= ELECTIC_THRESHOLD_HIGH else -> throw IllegalStateException("Orientation not in range ($orientation)") } private val J: Boolean get() = when (orientation) { - 0 -> getWireStateAt(0, 1, "digital_bit").x >= ELECTIC_THRESHOLD_HIGH - 1 -> getWireStateAt(0, 0, "digital_bit").x >= ELECTIC_THRESHOLD_HIGH - 2 -> getWireStateAt(1, 0, "digital_bit").x >= ELECTIC_THRESHOLD_HIGH - 3 -> getWireStateAt(1, 1, "digital_bit").x >= ELECTIC_THRESHOLD_HIGH + 0 -> getWireStateAt(0, 1).x >= ELECTIC_THRESHOLD_HIGH + 1 -> getWireStateAt(0, 0).x >= ELECTIC_THRESHOLD_HIGH + 2 -> getWireStateAt(1, 0).x >= ELECTIC_THRESHOLD_HIGH + 3 -> getWireStateAt(1, 1).x >= ELECTIC_THRESHOLD_HIGH else -> throw IllegalStateException("Orientation not in range ($orientation)") } @@ -119,7 +123,7 @@ class FixtureSignalBlocker : Electric, Reorientable { 3 -> 0 to 0 else -> throw IllegalStateException("Orientation not in range ($orientation)") } - setWireConsumptionAt(x, y, Vector2(I nimply J, 0.0)) + setWireEmissionAt(x, y, Vector2(I nimply J, 0.0)) } override fun onRisingEdge(readFrom: BlockBoxIndex) { @@ -130,9 +134,17 @@ class FixtureSignalBlocker : Electric, Reorientable { updateK() } + override fun onSignalHigh(readFrom: BlockBoxIndex) { + updateK() + } + + override fun onSignalLow(readFrom: BlockBoxIndex) { + updateK() + } + private infix fun Boolean.nimply(other: Boolean) = (this && !other).toInt().toDouble() override fun drawBody(frameDelta: Float, batch: SpriteBatch) { - TODO() + super.drawBody(frameDelta, batch) } } \ No newline at end of file