wiresim: signal sinking actors are only getting updated when the sim calls for

This commit is contained in:
minjaesong
2023-06-19 16:34:39 +09:00
parent 9e9064dd55
commit 528b975350
7 changed files with 95 additions and 17 deletions

View File

@@ -14,11 +14,20 @@ import org.dyn4j.geometry.Vector2
import java.util.*
typealias BlockBoxIndex = Int
typealias WireEmissionType = String
interface Electric {
val wireEmitterTypes: HashMap<String, BlockBoxIndex>
val wireEmitterTypes: HashMap<BlockBoxIndex, WireEmissionType>
val wireSinkTypes: HashMap<BlockBoxIndex, WireEmissionType>
val wireEmission: HashMap<BlockBoxIndex, Vector2>
val wireConsumption: HashMap<BlockBoxIndex, Vector2>
fun onRisingEdge(readFrom: BlockBoxIndex) {}
fun onFallingEdge(readFrom: BlockBoxIndex) {}
fun onSignalHigh(readFrom: BlockBoxIndex, highThreshold: Double = 0.9) {}
fun onSignalLow(readFrom: BlockBoxIndex, lowThreshold: Double = 0.1) {}
fun updateOnWireGraphTraversal(offsetX: Int, offsetY: Int, sinkType: WireEmissionType) {}
}
/**
@@ -35,7 +44,21 @@ open class FixtureBase : ActorWithBody, CuedByTerrainChange {
protected set
lateinit var blockBox: BlockBox // something like TapestryObject will want to redefine this
fun blockBoxIndexToPoint2i(it: BlockBoxIndex): Point2i = this.blockBox.width.let { w -> Point2i(it % w, it / w) }
fun pointToBlockBoxIndex(point: Point2i) = point.y * this.blockBox.width + point.x
fun pointToBlockBoxIndex(x: Int, y: Int) = y * this.blockBox.width + x
fun getWireEmitterAt(point: Point2i) = if (this is Electric) this.wireEmitterTypes[pointToBlockBoxIndex(point)] else throw IllegalStateException("Fixture is not instance of Electric")
fun getWireEmitterAt(x: Int, y: Int) = if (this is Electric) this.wireEmitterTypes[pointToBlockBoxIndex(x, y)] else throw IllegalStateException("Fixture is not instance of Electric")
fun getWireSinkAt(point: Point2i) = if (this is Electric) this.wireSinkTypes[pointToBlockBoxIndex(point)] else throw IllegalStateException("Fixture is not instance of Electric")
fun getWireSinkAt(x: Int, y: Int) = if (this is Electric) this.wireSinkTypes[pointToBlockBoxIndex(x, y)] else throw IllegalStateException("Fixture is not instance of Electric")
fun setWireEmitterAt(x: Int, y: Int, type: WireEmissionType) { if (this is Electric) wireEmitterTypes[pointToBlockBoxIndex(x, y)] = type else throw IllegalStateException("Fixture is not instance of Electric") }
fun setWireSinkAt(x: Int, y: Int, type: WireEmissionType) { if (this is Electric) wireSinkTypes[pointToBlockBoxIndex(x, y)] = type else throw IllegalStateException("Fixture is not instance of Electric") }
fun setWireEmissionAt(x: Int, y: Int, emission: Vector2) { if (this is Electric) wireEmission[pointToBlockBoxIndex(x, y)] = emission else throw IllegalStateException("Fixture is not instance of Electric") }
fun setWireConsumptionAt(x: Int, y: Int, consumption: Vector2) { if (this is Electric) wireConsumption[pointToBlockBoxIndex(x, y)] = consumption else throw IllegalStateException("Fixture is not instance of Electric") }
var blockBoxProps: BlockBoxProps = BlockBoxProps(0)
@Transient var nameFun: () -> String = { "" }
@Transient var mainUI: UICanvas? = null

View File

@@ -1,9 +1,5 @@
package net.torvald.terrarum.modulebasegame.gameactors
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.TextureRegion
import net.torvald.terrarum.CommonResourcePool
import net.torvald.terrarum.ModMgr
import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZE
import net.torvald.terrarum.gameactors.AVKey
import net.torvald.terrarum.langpack.Lang
@@ -13,9 +9,10 @@ import org.dyn4j.geometry.Vector2
class FixtureLogicSignalEmitter : FixtureBase, Electric {
override val wireEmitterTypes: HashMap<String, BlockBoxIndex> = HashMap()
override val wireEmission: HashMap<BlockBoxIndex, Vector2> = HashMap()
override val wireConsumption: HashMap<BlockBoxIndex, Vector2> = HashMap()
@Transient override val wireEmitterTypes: java.util.HashMap<BlockBoxIndex, WireEmissionType> = java.util.HashMap()
@Transient override val wireSinkTypes: java.util.HashMap<BlockBoxIndex, WireEmissionType> = java.util.HashMap()
@Transient override val wireEmission: HashMap<BlockBoxIndex, Vector2> = HashMap()
@Transient override val wireConsumption: HashMap<BlockBoxIndex, Vector2> = HashMap()
constructor() : super(
@@ -34,12 +31,13 @@ class FixtureLogicSignalEmitter : FixtureBase, Electric {
}
actorValue[AVKey.BASEMASS] = MASS
setWireEmitterAt(0, 0, "digital_bit")
setWireEmissionAt(0, 0, Vector2(1.0, 0.0))
}
override fun update(delta: Float) {
// the values does not get preserved on save reload??
wireEmitterTypes["digital_bit"] = 0
wireEmission[0] = Vector2(1.0, 0.0)
super.update(delta)
}

View File

@@ -6,11 +6,19 @@ import net.torvald.terrarum.modulebasegame.gameactors.FixtureInventory.Companion
import net.torvald.terrarum.modulebasegame.gameitems.FixtureItemBase
import net.torvald.terrarum.modulebasegame.ui.UIWorldPortal
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
import org.dyn4j.geometry.Vector2
import java.util.HashMap
/**
* Created by minjaesong on 2023-05-28.
*/
class FixtureWorldPortal : FixtureBase {
class FixtureWorldPortal : FixtureBase, Electric {
@Transient override val wireEmitterTypes: HashMap<BlockBoxIndex, WireEmissionType> = HashMap()
@Transient override val wireSinkTypes: HashMap<BlockBoxIndex, WireEmissionType> = HashMap()
@Transient override val wireEmission: HashMap<BlockBoxIndex, Vector2> = HashMap()
@Transient override val wireConsumption: HashMap<BlockBoxIndex, Vector2> = HashMap()
constructor() : super(
BlockBox(BlockBox.NO_COLLISION, 5, 2),
@@ -36,6 +44,20 @@ class FixtureWorldPortal : FixtureBase {
}
actorValue[AVKey.BASEMASS] = FixtureLogicSignalEmitter.MASS
setWireSinkAt(2, 1, "digital_bit")
}
override fun update(delta: Float) {
super.update(delta)
}
override fun updateOnWireGraphTraversal(offsetX: Int, offsetY: Int, sinkType: WireEmissionType) {
println("[FixtureWorldPortal] updateOnWireGraphTraversal! ($offsetX, $offsetY, $sinkType)")
}
override fun onRisingEdge(readFrom: BlockBoxIndex) {
println("[FixtureWorldPortal] teleport! ($readFrom)")
}
override fun reload() {
@@ -43,4 +65,5 @@ class FixtureWorldPortal : FixtureBase {
// TODO do something with (mainUI as UIWorldPortal).***
}
}