partially working conveyor

This commit is contained in:
minjaesong
2025-03-15 21:40:14 +09:00
parent 18feeb1826
commit b577df0155
4 changed files with 161 additions and 16 deletions

View File

@@ -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)
1 id classname tags
134 32776 32775 net.torvald.terrarum.modulebasegame.gameitems.MusicDisc08 net.torvald.terrarum.modulebasegame.gameitems.MusicDisc07 MUSIC,PHONO
135 32777 32776 net.torvald.terrarum.modulebasegame.gameitems.MusicDisc09 net.torvald.terrarum.modulebasegame.gameitems.MusicDisc08 MUSIC,PHONO
136 # data storage (pre-composed tapestries 32777 256) net.torvald.terrarum.modulebasegame.gameitems.MusicDisc09 MUSIC,PHONO
137 # data storage (pre-composed tapestries 256)
138 33024 net.torvald.terrarum.modulebasegame.gameitems.ItemTapestry FIXTURE,BASEOBJECT
139 # data storage (pre-composed text signs 256)
140 33280 net.torvald.terrarum.modulebasegame.gameitems.ItemTextSignCopper FIXTURE,BASEOBJECT

View File

@@ -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,6 +115,8 @@ class ActorConveyors : ActorWithBody {
override fun drawBody(frameDelta: Float, batch: SpriteBatch) {
App.shapeRender.inUse {
it.color = Color.RED
// belt top
drawLineOnWorld(btx1, bty1, btx2, bty2)
@@ -117,6 +127,7 @@ class ActorConveyors : ActorWithBody {
// right arc
drawArcOnWorld(cx2, cy2, r, di, Math.PI)
}
}
private fun drawLineOnWorld(x1: Double, y1: Double, x2: Double, y2: Double) {
val w = 2.0f
@@ -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() {}
}

View File

@@ -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
}
}
}

View File

@@ -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<Point2i>()
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
}
}