From 659976b8807dbeae4ac37e57c2824f99421a18dd Mon Sep 17 00:00:00 2001 From: minjaesong Date: Mon, 4 Mar 2024 02:39:03 +0900 Subject: [PATCH] signal blocker wip --- assets/mods/basegame/items/itemid.csv | 1 + assets/mods/basegame/items/items.tga | 2 +- assets/mods/basegame/locales/en/items.json | 1 + assets/mods/basegame/locales/koKR/items.json | 1 + .../sprites/fixtures/signal_blocker.tga | 3 + .../modulebasegame/gameactors/FixtureBase.kt | 9 ++ .../gameactors/FixtureSignalBlocker.kt | 138 ++++++++++++++++++ .../gameitems/ItemSignalBlocker.kt | 36 +++++ work_files/graphics/items/basegame_items.kra | 4 +- .../graphics/wires/wire_single_items.kra | 4 +- 10 files changed, 194 insertions(+), 5 deletions(-) create mode 100644 assets/mods/basegame/sprites/fixtures/signal_blocker.tga create mode 100644 src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSignalBlocker.kt create mode 100644 src/net/torvald/terrarum/modulebasegame/gameitems/ItemSignalBlocker.kt diff --git a/assets/mods/basegame/items/itemid.csv b/assets/mods/basegame/items/itemid.csv index b656d4dd8..a647606c7 100644 --- a/assets/mods/basegame/items/itemid.csv +++ b/assets/mods/basegame/items/itemid.csv @@ -42,6 +42,7 @@ id;classname;tags 41;net.torvald.terrarum.modulebasegame.gameitems.ItemTableEbony;FIXTURE,SURFACE 42;net.torvald.terrarum.modulebasegame.gameitems.ItemTableBirch;FIXTURE,SURFACE 43;net.torvald.terrarum.modulebasegame.gameitems.ItemTableRosewood;FIXTURE,SURFACE +44;net.torvald.terrarum.modulebasegame.gameitems.ItemSignalBlocker;FIXTURE,SIGNAL # ingots 26;net.torvald.terrarum.modulebasegame.gameitems.IngotSteel;INGOT diff --git a/assets/mods/basegame/items/items.tga b/assets/mods/basegame/items/items.tga index 142066340..96da69951 100644 --- a/assets/mods/basegame/items/items.tga +++ b/assets/mods/basegame/items/items.tga @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:21f715ab738a949b8b6ea5afe1054fa3da0a48e2cdf54af0c0ab797b86676ade +oid sha256:3c0ec3f25da79b846fbd653d417217456f455b8ee6bdc3ec028f5c4b92551021 size 2408466 diff --git a/assets/mods/basegame/locales/en/items.json b/assets/mods/basegame/locales/en/items.json index 38bc44037..e6591702b 100644 --- a/assets/mods/basegame/locales/en/items.json +++ b/assets/mods/basegame/locales/en/items.json @@ -37,6 +37,7 @@ "ITEM_INGOT_TIN": "Tin Ingot", "ITEM_INGOT_ZINC": "Zinc Ingot", "ITEM_JUKEBOX": "Jukebox", + "ITEM_LOGIC_SIGNAL_BLOCKER": "Logic Signal Blocker", "ITEM_LOGIC_SIGNAL_EMITTER": "Logic Signal Emitter", "ITEM_LOGIC_SIGNAL_SWITCH": "Logic Signal Switch", "ITEM_LOGS_BIRCH": "Birch Logs", diff --git a/assets/mods/basegame/locales/koKR/items.json b/assets/mods/basegame/locales/koKR/items.json index bc4dc8135..55e808463 100644 --- a/assets/mods/basegame/locales/koKR/items.json +++ b/assets/mods/basegame/locales/koKR/items.json @@ -37,6 +37,7 @@ "ITEM_INGOT_TIN": "주석괴", "ITEM_INGOT_ZINC": "아연괴", "ITEM_JUKEBOX": "주크박스", + "ITEM_LOGIC_SIGNAL_BLOCKER": "신호 차단기", "ITEM_LOGIC_SIGNAL_EMITTER": "신호발생기", "ITEM_LOGIC_SIGNAL_SWITCH": "신호 스위치", "ITEM_LOGS_BIRCH": "백단 통나무", diff --git a/assets/mods/basegame/sprites/fixtures/signal_blocker.tga b/assets/mods/basegame/sprites/fixtures/signal_blocker.tga new file mode 100644 index 000000000..e30ad94f4 --- /dev/null +++ b/assets/mods/basegame/sprites/fixtures/signal_blocker.tga @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:cb8884869a534612bf6689ece5f82caa59117a745b2aa48523de0998974c1554 +size 32786 diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt index c7ce8ee39..c49f7599c 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt @@ -89,6 +89,15 @@ 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)) + } + } + } private val oldSinkStatus: Array diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSignalBlocker.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSignalBlocker.kt new file mode 100644 index 000000000..6998b8231 --- /dev/null +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSignalBlocker.kt @@ -0,0 +1,138 @@ +package net.torvald.terrarum.modulebasegame.gameactors + +import com.badlogic.gdx.graphics.g2d.SpriteBatch +import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZE +import net.torvald.terrarum.langpack.Lang +import net.torvald.terrarum.modulebasegame.gameitems.FixtureItemBase +import net.torvald.terrarum.toInt +import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack +import org.dyn4j.geometry.Vector2 + +/** + * Created by minjaesong on 2024-03-04. + */ +interface Reorientable { + /** + * Usually includes code snippet of `orientation = (orientation + 1) % 4` + */ + fun orientClockwise() + /** + * Usually includes code snippet of `orientation = (orientation - 1) % 4` + */ + fun orientAnticlockwise() + /** + * strictly 0 1 2 3. If your fixture can only be oriented in two ways, use value 0 2. + */ + var orientation: Int +} + +/** + * Created by minjaesong on 2024-03-04. + */ +class FixtureSignalBlocker : Electric, Reorientable { + + constructor() : super( + BlockBox(BlockBox.NO_COLLISION, 2, 2), + nameFun = { Lang["ITEM_LOGIC_SIGNAL_BLOCKER"] } + ) + + override var orientation = 0 + + private fun setEmitterAndSink() { + when (orientation) { + 0 -> { + setWireSinkAt(0, 0, "digital_bit") + setWireSinkAt(0, 1, "digital_bit") + setWireEmitterAt(1, 0, "digital_bit") + } + 1 -> { + setWireSinkAt(1, 0, "digital_bit") + setWireSinkAt(0, 0, "digital_bit") + setWireEmitterAt(1, 1, "digital_bit") + } + 2 -> { + setWireSinkAt(1, 1, "digital_bit") + setWireSinkAt(1, 0, "digital_bit") + setWireEmitterAt(0, 1, "digital_bit") + } + 3 -> { + setWireSinkAt(0, 1, "digital_bit") + setWireSinkAt(1, 1, "digital_bit") + setWireEmitterAt(0, 0, "digital_bit") + } + else -> throw IllegalStateException("Orientation not in range ($orientation)") + } + } + + override fun orientClockwise() { + orientation = (orientation + 1) % 4 + setEmitterAndSink(); updateK() + } + override fun orientAnticlockwise() { + orientation = (orientation - 1) % 4 + setEmitterAndSink(); updateK() + } + + init { + val itemImage = FixtureItemBase.getItemImageFromSingleImage("basegame", "sprites/fixtures/signal_blocker.tga") + + density = 1400.0 + setHitboxDimension(2*TILE_SIZE, 2*TILE_SIZE, 0, 1) + + makeNewSprite(TextureRegionPack(itemImage.texture, 2*TILE_SIZE, 2*TILE_SIZE)).let { + it.setRowsAndFrames(8,4) + it.delays = floatArrayOf(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY) + } + + setEmitterAndSink() + } + + override fun reload() { + super.reload() + setEmitterAndSink() + updateK() + } + + 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 + 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 + else -> throw IllegalStateException("Orientation not in range ($orientation)") + } + + private fun updateK() { + val (x, y) = when (orientation) { + 0 -> 1 to 0 + 1 -> 1 to 1 + 2 -> 0 to 1 + 3 -> 0 to 0 + else -> throw IllegalStateException("Orientation not in range ($orientation)") + } + setWireConsumptionAt(x, y, Vector2(I nimply J, 0.0)) + } + + override fun onRisingEdge(readFrom: BlockBoxIndex) { + updateK() + } + + override fun onFallingEdge(readFrom: BlockBoxIndex) { + updateK() + } + + private infix fun Boolean.nimply(other: Boolean) = (this && !other).toInt().toDouble() + + override fun drawBody(frameDelta: Float, batch: SpriteBatch) { + TODO() + } +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/gameitems/ItemSignalBlocker.kt b/src/net/torvald/terrarum/modulebasegame/gameitems/ItemSignalBlocker.kt new file mode 100644 index 000000000..924f42fc6 --- /dev/null +++ b/src/net/torvald/terrarum/modulebasegame/gameitems/ItemSignalBlocker.kt @@ -0,0 +1,36 @@ +package net.torvald.terrarum.modulebasegame.gameitems + +import com.badlogic.gdx.graphics.g2d.TextureRegion +import net.torvald.terrarum.CommonResourcePool +import net.torvald.terrarum.Terrarum +import net.torvald.terrarum.gameactors.ActorWithBody +import net.torvald.terrarum.gameitems.ItemID +import net.torvald.terrarum.modulebasegame.TerrarumIngame +import net.torvald.terrarum.modulebasegame.gameactors.FixtureLogicSignalEmitter + +/** + * Created by minjaesong on 2024-03-04. + */ +class ItemSignalBlocker(originalID: ItemID) : FixtureItemBase(originalID, "net.torvald.terrarum.modulebasegame.gameactors.FixtureSignalBlocker") { + + override var dynamicID: ItemID = originalID + override var baseMass = FixtureLogicSignalEmitter.MASS + override val canBeDynamic = false + override val materialId = "" + override val itemImage: TextureRegion + get() = CommonResourcePool.getAsItemSheet("basegame.items").get(10, 3) + + override var baseToolSize: Double? = baseMass + override var originalName = "ITEM_LOGIC_SIGNAL_BLOCKER" + + override fun effectWhileEquipped(actor: ActorWithBody, delta: Float) { + super.effectWhileEquipped(actor, delta) + (Terrarum.ingame!! as TerrarumIngame).selectedWireRenderClass = "signal" + } + + override fun effectOnUnequip(actor: ActorWithBody) { + super.effectOnUnequip(actor) + (Terrarum.ingame!! as TerrarumIngame).selectedWireRenderClass = "" + } + +} \ No newline at end of file diff --git a/work_files/graphics/items/basegame_items.kra b/work_files/graphics/items/basegame_items.kra index 2b4cf7462..985bbea23 100644 --- a/work_files/graphics/items/basegame_items.kra +++ b/work_files/graphics/items/basegame_items.kra @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:251ab82d1cf73829bdd0d63d1decc7d512966dda09edbb96bdfd1515b995cf57 -size 1426592 +oid sha256:f5de7c1440cf95b983da15c1e758e22614e561985df0ca85e41f7ddffcde5b80 +size 1448581 diff --git a/work_files/graphics/wires/wire_single_items.kra b/work_files/graphics/wires/wire_single_items.kra index 7f43ec726..17bddb590 100644 --- a/work_files/graphics/wires/wire_single_items.kra +++ b/work_files/graphics/wires/wire_single_items.kra @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:3781d29b09be1e80210424e96526f153e5219fc226bf1f2d5fcc0776b97518d8 -size 367721 +oid sha256:d8430213e3a9f6e1d7620cce8404948e30e626cbf916c800f452114bfbeebe06 +size 383829