mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-09 13:21:51 +09:00
pressure plates
This commit is contained in:
@@ -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))
|
||||
}
|
||||
}
|
||||
@@ -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 = ""
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user