From dbd95cd2a204accec340cf9a591b1e8e72860c7e Mon Sep 17 00:00:00 2001 From: minjaesong Date: Fri, 27 Sep 2024 20:35:08 +0900 Subject: [PATCH] pressure plates --- assets/mods/basegame/crafting/items.json | 4 + assets/mods/basegame/items/itemid.csv | 1 + assets/mods/basegame/items/items.tga | 2 +- assets/mods/basegame/locales/en/items.json | 2 + .../fixtures/signal_pressure_plate.tga | 3 + .../fixtures/signal_pressure_plate_emsv.tga | 3 + .../FixtureLogicSignalSwitchManual.kt | 79 +++++++++++++++++++ .../gameitems/ItemLogicSignalSwitchManual.kt | 25 ++++++ work_files/graphics/items/basegame_items.kra | 4 +- .../fixtures/signal_pressure_plate.kra | 3 + 10 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 assets/mods/basegame/sprites/fixtures/signal_pressure_plate.tga create mode 100644 assets/mods/basegame/sprites/fixtures/signal_pressure_plate_emsv.tga create mode 100644 work_files/graphics/sprites/fixtures/signal_pressure_plate.kra diff --git a/assets/mods/basegame/crafting/items.json b/assets/mods/basegame/crafting/items.json index 0be3da265..0507118f1 100644 --- a/assets/mods/basegame/crafting/items.json +++ b/assets/mods/basegame/crafting/items.json @@ -15,6 +15,10 @@ "workbench": "", "ingredients": [[2, 1, "item@basegame:112", 1, "$ROCK", 1, "item@basegame:113"]] /* 1 copper ingot, 1 rock, 1 iron */ }, + "item@basegame:58": { /* signal pressure plate */ + "workbench": "", + "ingredients": [[1, 1, "item@basegame:112", 1, "$ROCK", 1, "item@basegame:113"]] /* 1 copper ingot, 1 rock, 1 iron */ + }, "item@basegame:1048576": { /* wooden bucket */ "workbench": "basiccrafting", "ingredients": [[1, 3, "$WOOD"]] /* 3 planks */ diff --git a/assets/mods/basegame/items/itemid.csv b/assets/mods/basegame/items/itemid.csv index f0f84b0a0..b106f5cd3 100644 --- a/assets/mods/basegame/items/itemid.csv +++ b/assets/mods/basegame/items/itemid.csv @@ -56,6 +56,7 @@ id;classname;tags 55;net.torvald.terrarum.modulebasegame.gameitems.ItemGlowOrb;LIGHT,THROWABLE 56;net.torvald.terrarum.modulebasegame.gameitems.ItemClayBrick; 57;net.torvald.terrarum.modulebasegame.gameitems.ItemLogicSignalPushbutton;FIXTURE,SIGNAL +58;net.torvald.terrarum.modulebasegame.gameitems.ItemLogicSignalPressurePlate;FIXTURE,SIGNAL diff --git a/assets/mods/basegame/items/items.tga b/assets/mods/basegame/items/items.tga index 99aa4d3d0..0f988088e 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:8feec26254c144344667b35800a2c6db4dc70703bc2cb8a5cffe7eed5a7912f8 +oid sha256:c87d1561269a2de48040d883fe8903afd72f751b89377dd77c59023914e315a5 size 2408466 diff --git a/assets/mods/basegame/locales/en/items.json b/assets/mods/basegame/locales/en/items.json index 7d2ce5110..54b6a6420 100644 --- a/assets/mods/basegame/locales/en/items.json +++ b/assets/mods/basegame/locales/en/items.json @@ -50,6 +50,8 @@ "ITEM_LOGIC_SIGNAL_EMITTER": "Logic Signal Emitter", "ITEM_LOGIC_SIGNAL_LATCH": "Logic Signal Latch", "ITEM_LOGIC_SIGNAL_NUMERIC_DISPLAY": "Logic Signal Numeric Display", + "ITEM_LOGIC_SIGNAL_PRESSURE_PLATE": "Logic Signal Pressure Plate", + "ITEM_LOGIC_SIGNAL_PUSHBUTTON": "Logic Signal Push-Button", "ITEM_LOGIC_SIGNAL_REPEATER": "Logic Signal Repeater", "ITEM_LOGIC_SIGNAL_SWITCH": "Logic Signal Switch", "ITEM_LOGS_BIRCH": "Birch Logs", diff --git a/assets/mods/basegame/sprites/fixtures/signal_pressure_plate.tga b/assets/mods/basegame/sprites/fixtures/signal_pressure_plate.tga new file mode 100644 index 000000000..9280c307f --- /dev/null +++ b/assets/mods/basegame/sprites/fixtures/signal_pressure_plate.tga @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:868b67964a1f65885ff0f156b469e2688db4e9e3b3eb3edfdb66e6323757e8f4 +size 4114 diff --git a/assets/mods/basegame/sprites/fixtures/signal_pressure_plate_emsv.tga b/assets/mods/basegame/sprites/fixtures/signal_pressure_plate_emsv.tga new file mode 100644 index 000000000..f8fdd9982 --- /dev/null +++ b/assets/mods/basegame/sprites/fixtures/signal_pressure_plate_emsv.tga @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:50bb75e752cc637bef73a94a0d49a7c708c3c107bee01a93822b7da84a0c91f0 +size 4114 diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureLogicSignalSwitchManual.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureLogicSignalSwitchManual.kt index 9e563db43..4767c600d 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureLogicSignalSwitchManual.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureLogicSignalSwitchManual.kt @@ -2,6 +2,7 @@ package net.torvald.terrarum.modulebasegame.gameactors import net.torvald.spriteanimation.SheetSpriteAnimation import net.torvald.terrarum.INGAME +import net.torvald.terrarum.Point2d import net.torvald.terrarum.TerrarumAppConfiguration import net.torvald.terrarum.gameactors.AVKey import net.torvald.terrarum.langpack.Lang @@ -130,4 +131,82 @@ class FixtureLogicSignalPushbutton : Electric { override fun onInteract(mx: Double, my: Double) { triggeredTime = INGAME.world.worldTime.TIME_T } +} + +class FixtureLogicSignalPressurePlate : Electric { + + @Transient override val spawnNeedsFloor = true + @Transient override val spawnNeedsWall = false + + constructor() : super( + BlockBox(BlockBox.NO_COLLISION, 2, 1), + nameFun = { Lang["ITEM_LOGIC_SIGNAL_PRESSURE_PLATE"] } + ) + + @Transient open val minMass = 2.0 // different types of switches can have different minimal mass? + @Transient open val holdTime = 30 // ticks + + + private var triggeredTime: Long? = null // null = off; number: TIME_T that the button was held down + + private val state: Int + get() = (triggeredTime != null).toInt() + + init { + val itemImage = FixtureItemBase.getItemImageFromSingleImage("basegame", "sprites/fixtures/signal_pressure_plate.tga") + val itemImage2 = FixtureItemBase.getItemImageFromSingleImage("basegame", "sprites/fixtures/signal_pressure_plate_emsv.tga") + + density = 1400.0 + setHitboxDimension(2*TerrarumAppConfiguration.TILE_SIZE, TerrarumAppConfiguration.TILE_SIZE, 0, 1) + + makeNewSprite(TextureRegionPack(itemImage.texture, 2*TerrarumAppConfiguration.TILE_SIZE, TerrarumAppConfiguration.TILE_SIZE)).let { + it.setRowsAndFrames(2,1) + it.delays = floatArrayOf(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY) + } + makeNewSpriteEmissive(TextureRegionPack(itemImage2.texture, 2*TerrarumAppConfiguration.TILE_SIZE, TerrarumAppConfiguration.TILE_SIZE)).let { + it.setRowsAndFrames(2,1) + it.delays = floatArrayOf(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY) + } + + actorValue[AVKey.BASEMASS] = FixtureLogicSignalEmitter.MASS + + + setWireEmitterAt(0, 0, "digital_bit") + // right side of the plate can be used to route signal wire + } + + private fun updateState() { + val pointStart = Point2d(hitbox.startX + 5, hitbox.endY - 6) + val pointEnd = Point2d(hitbox.startX + 5 + 22, hitbox.endY) + + val massSum = INGAME.getActorsAt(pointStart, pointEnd).filter { + it.physProp.usePhysics + }.sumOf { it.mass } + + if (massSum >= minMass) + triggeredTime = INGAME.world.worldTime.TIME_T + + } + + override fun updateSignal() { + // detect and measure weight of actors + updateState() + + // decide when to un-trigger + if (INGAME.world.worldTime.TIME_T - (triggeredTime ?: 0L) >= holdTime) { + triggeredTime = null + } + + (sprite as SheetSpriteAnimation).currentRow = state + (spriteEmissive as SheetSpriteAnimation).currentRow = state + setWireEmissionAt(0, 0, Vector2(state.toDouble(), 0.0)) + } + + override fun reload() { + super.reload() + + (sprite as SheetSpriteAnimation).currentRow = state + (spriteEmissive as SheetSpriteAnimation).currentRow = state + setWireEmissionAt(0, 0, Vector2(state.toDouble(), 0.0)) + } } \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/gameitems/ItemLogicSignalSwitchManual.kt b/src/net/torvald/terrarum/modulebasegame/gameitems/ItemLogicSignalSwitchManual.kt index 7f60fbb6a..c69988a68 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameitems/ItemLogicSignalSwitchManual.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameitems/ItemLogicSignalSwitchManual.kt @@ -61,4 +61,29 @@ class ItemLogicSignalPushbutton(originalID: ItemID) : FixtureItemBase(originalID (Terrarum.ingame!! as TerrarumIngame).selectedWireRenderClass = "" } +} + +class ItemLogicSignalPressurePlate(originalID: ItemID) : FixtureItemBase(originalID, "net.torvald.terrarum.modulebasegame.gameactors.FixtureLogicSignalPressurePlate") { + + override var dynamicID: ItemID = originalID + override var baseMass = FixtureLogicSignalEmitter.MASS + override val canBeDynamic = false + override val materialId = "" + init { + itemImage = CommonResourcePool.getAsItemSheet("basegame.items").get(9, 2) + } + + override var baseToolSize: Double? = baseMass + override var originalName = "ITEM_LOGIC_SIGNAL_PRESSURE_PLATE" + + 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 11a25f5cf..99c8d4c31 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:a6e238743c393404c4dea112605ae7ecc89f331d969b71173a1adfaa13b8325e -size 1780248 +oid sha256:76339f18f3f1ee355d8639181b785f22415ee68601c2a507284d4e064e4bbede +size 1790388 diff --git a/work_files/graphics/sprites/fixtures/signal_pressure_plate.kra b/work_files/graphics/sprites/fixtures/signal_pressure_plate.kra new file mode 100644 index 000000000..398f2d714 --- /dev/null +++ b/work_files/graphics/sprites/fixtures/signal_pressure_plate.kra @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b4fd42de9d2f4b6535572f24774a31596d8e1c6715b30a613d67182bd2a15f87 +size 119774