mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-15 08:06:06 +09:00
signal blocker wip
This commit is contained in:
@@ -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<Vector2>
|
||||
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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 = ""
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user