From b577df0155e89db9162f51e7215dcaad90ddbe99 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Sat, 15 Mar 2025 21:40:14 +0900 Subject: [PATCH] partially working conveyor --- assets/mods/basegame/items/itemid.csv | 1 + .../gameactors/ActorConveyors.kt | 71 +++++++++++--- .../gameitems/FixtureItemBase.kt | 10 +- .../gameitems/ItemConveyorBelt.kt | 95 +++++++++++++++++++ 4 files changed, 161 insertions(+), 16 deletions(-) rename src/net/torvald/terrarum/{ => modulebasegame}/gameactors/ActorConveyors.kt (64%) create mode 100644 src/net/torvald/terrarum/modulebasegame/gameitems/ItemConveyorBelt.kt diff --git a/assets/mods/basegame/items/itemid.csv b/assets/mods/basegame/items/itemid.csv index e4faac0d9..d3adf0b68 100644 --- a/assets/mods/basegame/items/itemid.csv +++ b/assets/mods/basegame/items/itemid.csv @@ -134,6 +134,7 @@ id;classname;tags # industrial 2048;net.torvald.terrarum.modulebasegame.gameitems.ItemInductionMotor;FIXTURE,POWER,KINETIC 2049;net.torvald.terrarum.modulebasegame.gameitems.ItemGearbox;FIXTURE,POWER,KINETIC +2050;net.torvald.terrarum.modulebasegame.gameitems.ItemConveyorBelt;FIXTURE,KINETIC # data storage (pre-composed discs; 256) # 32768 is a reserved number for a BASEOBJECT disc (players can't produce own recordings... yet) diff --git a/src/net/torvald/terrarum/gameactors/ActorConveyors.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/ActorConveyors.kt similarity index 64% rename from src/net/torvald/terrarum/gameactors/ActorConveyors.kt rename to src/net/torvald/terrarum/modulebasegame/gameactors/ActorConveyors.kt index 8a7920ff4..cc8088844 100644 --- a/src/net/torvald/terrarum/gameactors/ActorConveyors.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/ActorConveyors.kt @@ -1,9 +1,14 @@ -package net.torvald.terrarum.gameactors +package net.torvald.terrarum.modulebasegame.gameactors +import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.graphics.g2d.SpriteBatch import net.torvald.terrarum.App +import net.torvald.terrarum.INGAME import net.torvald.terrarum.TerrarumAppConfiguration.TILE_SIZED +import net.torvald.terrarum.gameactors.ActorWithBody +import net.torvald.terrarum.inUse import net.torvald.terrarum.worlddrawer.WorldCamera +import java.util.* import kotlin.math.* /** @@ -29,6 +34,7 @@ class ActorConveyors : ActorWithBody { cy2 = 0.0 r = 0.5 * TILE_SIZED.minus(2) + c = 0 btx1 = 0.0 bty1 = 0.0 @@ -47,6 +53,7 @@ class ActorConveyors : ActorWithBody { val y2: Int private val s: Double // belt length + private val c: Int // segment counts private val di: Double // inclination deg private val dd: Double // declination deg @@ -89,6 +96,7 @@ class ActorConveyors : ActorWithBody { cy2 = (this.y2 + 0.5) * TILE_SIZED r = 0.5 * TILE_SIZED.minus(2) + c = (s / 8).roundToInt() * 2 // 4px segments rounded towards nearest even number btx1 = cx1 + r * sin(di) bty1 = cy1 + r * cos(di) @@ -107,15 +115,18 @@ class ActorConveyors : ActorWithBody { override fun drawBody(frameDelta: Float, batch: SpriteBatch) { + App.shapeRender.inUse { + it.color = Color.RED - // belt top - drawLineOnWorld(btx1, bty1, btx2, bty2) - // belt bottom - drawLineOnWorld(bbx1, bby1, bbx2, bby2) - // left arc - drawArcOnWorld(cx1, cy1, r, dd, Math.PI) - // right arc - drawArcOnWorld(cx2, cy2, r, di, Math.PI) + // belt top + drawLineOnWorld(btx1, bty1, btx2, bty2) + // belt bottom + drawLineOnWorld(bbx1, bby1, bbx2, bby2) + // left arc + drawArcOnWorld(cx1, cy1, r, dd, Math.PI) + // right arc + drawArcOnWorld(cx2, cy2, r, di, Math.PI) + } } private fun drawLineOnWorld(x1: Double, y1: Double, x2: Double, y2: Double) { @@ -128,11 +139,11 @@ class ActorConveyors : ActorWithBody { } private fun drawArcOnWorld(xc: Double, yc: Double, r: Double, arcStart: Double, arcDeg: Double) { - val w = 2.0f // dissect the circle - val pathLen = arcDeg * r +// val pathLen = arcDeg * r //// estimated number of segments. pathLen divided by sqrt(2) - val segments = Math.round(pathLen / Double.fromBits(0x3FF6A09E667F3BCDL)).coerceAtLeast(1L).toInt() +// val segments = Math.round(pathLen / Double.fromBits(0x3FF6A09E667F3BCDL)).coerceAtLeast(1L).toInt() + val segments = 12 * 8 for (i in 0 until segments) { val degStart = (i.toDouble() / segments) * arcDeg + arcStart @@ -146,4 +157,40 @@ class ActorConveyors : ActorWithBody { drawLineOnWorld(x1, y1, x2, y2) } } + + /** Real time, in nanoseconds */ + @Transient var spawnRequestedTime: Long = 0L + protected set + + internal var actorThatInstalledThisFixture: UUID? = null + + open fun spawn(installerUUID: UUID?): Boolean { + this.isVisible = true + + val posXtl = minOf(x1, x2).toDouble() + val posYtl = minOf(y1, y2).toDouble() + val posXbr = maxOf(x1, x2).toDouble() + val posYbr = maxOf(y1, y2).toDouble() + + this.hitbox.setFromTwoPoints(posXtl * TILE_SIZED, posYtl * TILE_SIZED, (posXbr+1) * TILE_SIZED, (posYbr+1) * TILE_SIZED) + this.setHitboxDimension(this.hitbox.width.toInt(), this.hitbox.height.toInt(), 0, 1) + this.intTilewiseHitbox.setFromTwoPoints(posXtl, posYtl, posXbr, posYbr) + + // actually add this actor into the world + INGAME.queueActorAddition(this) + spawnRequestedTime = System.nanoTime() + + actorThatInstalledThisFixture = installerUUID + + //makeNoiseAndDust(posXtl, posYtl) + + onSpawn() + + return true + } + + /** + * Callend whenever the fixture was spawned successfully. + */ + open fun onSpawn() {} } \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/gameitems/FixtureItemBase.kt b/src/net/torvald/terrarum/modulebasegame/gameitems/FixtureItemBase.kt index 8581ab930..d16b83733 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameitems/FixtureItemBase.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameitems/FixtureItemBase.kt @@ -104,10 +104,7 @@ open class FixtureItemBase(originalID: ItemID, val fixtureClassName: String) : G // return true when placed, false when cannot be placed } - override fun startSecondaryUse(actor: ActorWithBody, delta: Float) = mouseInInteractableRange(actor) { mwx, mwy, mtx, mty -> - (INGAME as TerrarumIngame).pickupFixtureOrDroppedItem(actor, delta, mwx, mwy, mtx, mty, false) - -1 - } + override fun startSecondaryUse(actor: ActorWithBody, delta: Float) = fixturePickupFun(actor, delta) /** * Also see: [net.torvald.terrarum.modulebasegame.gameactors.FixtureBase.Companion] @@ -128,5 +125,10 @@ open class FixtureItemBase(originalID: ItemID, val fixtureClassName: String) : G TextureRegion(Texture(ModMgr.getGdxFile(module, path))) } as TextureRegion } + + fun fixturePickupFun(actor: ActorWithBody, delta: Float) = mouseInInteractableRange(actor) { mwx, mwy, mtx, mty -> + (INGAME as TerrarumIngame).pickupFixtureOrDroppedItem(actor, delta, mwx, mwy, mtx, mty, false) + -1 + } } } \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/gameitems/ItemConveyorBelt.kt b/src/net/torvald/terrarum/modulebasegame/gameitems/ItemConveyorBelt.kt new file mode 100644 index 000000000..f7c76e546 --- /dev/null +++ b/src/net/torvald/terrarum/modulebasegame/gameitems/ItemConveyorBelt.kt @@ -0,0 +1,95 @@ +package net.torvald.terrarum.modulebasegame.gameitems + +import net.torvald.terrarum.INGAME +import net.torvald.terrarum.Point2i +import net.torvald.terrarum.gameactors.ActorWithBody +import net.torvald.terrarum.gameitems.GameItem +import net.torvald.terrarum.gameitems.ItemID +import net.torvald.terrarum.gameitems.mouseInInteractableRange +import net.torvald.terrarum.modulebasegame.gameactors.ActorConveyors +import net.torvald.terrarum.modulebasegame.gameactors.IngamePlayer + +/** + * Created by minjaesong on 2025-03-15. + */ +open class ItemConveyorBelt(originalID: ItemID) : GameItem(originalID) { + override var baseMass = 10.0 + override var baseToolSize: Double? = null + override var inventoryCategory = Category.FIXTURE + override val canBeDynamic = false + override val materialId = "" + override var equipPosition = EquipPosition.HAND_GRIP + + override var originalName = "CONVEYOR_BELT" + + + protected val AXLE_COUNT = 2 + + + private var currentStatus = 0 // 0: initial, 1+: place n-th axle + + private val occupiedPoints = HashSet() + + override fun startPrimaryUse(actor: ActorWithBody, delta: Float) = mouseInInteractableRange(actor) { _, _, mtx, mty -> + val ret = when (currentStatus) { + 0 -> placeFirstAxle(mtx, mty) + 1 -> placeSecondAxleAndFinalise(mtx, mty) + else -> { + currentStatus = 0 + -1L + } + } + + currentStatus += 1 + + ret + } + + override fun startSecondaryUse(actor: ActorWithBody, delta: Float): Long { + return when (currentStatus) { + 0 -> FixtureItemBase.fixturePickupFun(actor, delta) + else -> { + currentStatus = -1 + -1L + } + } + } + + + private fun placeFirstAxle(mx: Int, my: Int): Long { + occupiedPoints.add(Point2i(mx, my)) + return 0L + } + + private fun placeSecondAxleAndFinalise(mx: Int, my: Int): Long { + Point2i(mx, my).let { p2 -> + if (!occupiedPoints.contains(p2)) { + occupiedPoints.add(p2) + } + else { + currentStatus = -1 + return 0L + } + + // sort occupiedPoints by its x value + val points = occupiedPoints.toMutableList().also { it.sortBy { it.x } }.also { list -> + // check for ROUNDWORLD + val xMin = list[0].x + val xMax = list[1].x + // normalise it by making the x value for left spindle to negative + if (xMin >= 0 && xMin < INGAME.world.width / 2 && xMax >= INGAME.world.width / 2) { + list[0].x -= INGAME.world.width + } + } + + val conveyors = ActorConveyors(points[0].x, points[0].y, points[1].x, points[1].y) + conveyors.spawn((INGAME.actorNowPlaying as? IngamePlayer)?.uuid) + } + + occupiedPoints.clear() + currentStatus = -1 + + return 1L + } +} +