wire colour fade by signal strength

This commit is contained in:
minjaesong
2024-03-14 00:18:25 +09:00
parent 6abb6f84ef
commit 5b462a2559
4 changed files with 47 additions and 3 deletions

View File

@@ -566,6 +566,7 @@ object ModMgr {
}
Terrarum.wireCodex.portsFromModule(module, "wires/")
Terrarum.wireCodex.wireDecaysFromModule(module, "wires/")
}
private fun makeNewItemObj(tile: BlockProp, isWall: Boolean) = object : GameItem(

View File

@@ -23,6 +23,8 @@ class WireCodex {
@Transient val wirePorts = HashMap<WireEmissionType, Triple<TextureRegionPack, Int, Int>>()
@Transient val wireDecays = HashMap<ItemID, Double>()
fun clear() {
wireProps.clear()
}
@@ -123,6 +125,23 @@ class WireCodex {
}
}
fun wireDecaysFromModule(module: String, path: String) {
printdbg(this, "Building wire ports table for module $module")
try {
registerDecays(module, path, CSVFetcher.readFromModule(module, path + "decayconsts.csv"))
}
catch (e: IOException) { e.printStackTrace() }
}
private fun registerDecays(module: String, path: String, records: List<CSVRecord>) {
records.forEach {
val item = it.get("wireItemID")
val d = it.get("const").toDouble()
wireDecays[item] = d
}
}
fun getAll() = wireProps.values
/*fun get(index: Int): WireProp {

View File

@@ -10,6 +10,7 @@ import net.torvald.terrarum.gameitems.ItemID
import net.torvald.terrarum.modulebasegame.TerrarumIngame
import net.torvald.terrarum.modulebasegame.gameactors.WireEmissionType
import net.torvald.terrarum.ui.Toolkit
import kotlin.math.cos
/**
* Created by minjaesong on 2024-03-05.
@@ -63,11 +64,33 @@ class WireActor : ActorWithBody, NoSerialise, InternalActor {
override fun updateImpl(delta: Float) {
}
private fun essfun0(x: Double) = -cos(Math.PI * x) / 2.0 + 0.5
private fun essfun(x: Double) = essfun0(x)
override fun drawBody(frameDelta: Float, batch: SpriteBatch) {
if (isVisible && sprite != null) {
(sprite as SheetSpriteAnimation).currentRow = (world?.getWireEmitStateOf(worldX, worldY, wireID)?.isNotZero == true).toInt()
BlendMode.resolve(drawMode, batch)
drawSpriteInGoodPosition(frameDelta, sprite!!, batch)
// signal wires?
if (WireCodex.wireProps[wireID]?.accepts == "digital_bit") {
val strength = world?.getWireEmitStateOf(worldX, worldY, wireID)?.x ?: 0.0
// draw base (unlit) sprite
batch.color = Color.WHITE
(sprite as SheetSpriteAnimation).currentRow = 0
drawSpriteInGoodPosition(frameDelta, sprite!!, batch, 0, Color.WHITE)
// draw lit sprite
val alpha = Color(1f, 1f, 1f, essfun(strength.coerceIn(0.0, 1.0)).toFloat())
(sprite as SheetSpriteAnimation).currentRow = 1
drawSpriteInGoodPosition(frameDelta, sprite!!, batch, 0, alpha)
}
else {
(sprite as SheetSpriteAnimation).currentRow = 0
drawSpriteInGoodPosition(frameDelta, sprite!!, batch, 0, Color.WHITE)
}
}
}
}

View File

@@ -520,7 +520,8 @@ object WorldSimulator {
}
private fun calculateDecay(signal: Vector2, dist: Int, wire: ItemID, signalType: WireEmissionType): Vector2 {
return signal * 0.995.pow(dist.toDouble())
val d = WireCodex.wireDecays[wire]!!
return signal * d.pow(dist.toDouble())
}
private fun traverseWireGraph(world: GameWorld, wire: ItemID, startingPoint: WireGraphCursor, signal: Vector2, signalType: WireEmissionType) {