From 759f8a4c4ef61e8b8d2136759e4bdd900c4cc40f Mon Sep 17 00:00:00 2001 From: minjaesong Date: Tue, 25 Mar 2025 16:58:55 +0900 Subject: [PATCH] bottom parts on conveyor now works --- .../gameactors/ActorConveyors.kt | 180 +++++++++--------- 1 file changed, 88 insertions(+), 92 deletions(-) diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/ActorConveyors.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/ActorConveyors.kt index 95f6f8781..b4ee6e7ac 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/ActorConveyors.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/ActorConveyors.kt @@ -122,7 +122,7 @@ class ActorConveyors : ActorWithBody { cy2 = (this.y2 + 0.5) * TILE_SIZED r = 0.5 * TILE_SIZED.minus(2) - c = (s / 4).roundToInt() * 2 // 4px segments rounded towards nearest even number + c = (s / 32).roundToInt() * 2 // 16px segments rounded towards nearest even number btx1 = cx1 + r * sin(di) bty1 = cy1 + r * cos(di) @@ -150,15 +150,94 @@ class ActorConveyors : ActorWithBody { // turn = 0.0 } - @Transient var smu = 0 - override var tooltipText: String? = "" + private fun drawBeltMvmtTop(segmentLen: Double) { + // Pre-compute ranges once outside the loop + val xRange = btx1..btx2 + val yRange = if (bty1 <= bty2) bty1..bty2 else bty2..bty1 + + for (i in -1 until c / 2) { + //= originPoint + segmentLen * (translation terms) * (movement on the belt terms) + val m = segmentLen * (i + 0.00 + turn) + val n = segmentLen * (i + 0.25 + turn) + val x1 = btx1 + m * cos(dd) + val y1 = bty1 - m * sin(dd) + val x2 = btx1 + n * cos(dd) + val y2 = bty1 - n * sin(dd) + + drawLineOnWorld( + x1.coerceIn(xRange), + y1.coerceIn(yRange), + x2.coerceIn(xRange), + y2.coerceIn(yRange), + 2f + ) + } + } + + private fun drawBeltMvmtBottom(segmentLen: Double) { + // Pre-compute ranges once outside the loop + val xRange = bbx1..bbx2 + val yRange = if (bby1 <= bby2) bby1..bby2 else bby2..bby1 + val eps = 1.0 / 1024.0 + val isVert = (dd.absoluteValue in HALF_PI - eps..HALF_PI + eps) + val isHorz = (dd.absoluteValue in -eps..eps) + + for (i in -1 until c / 2) { + //= originPoint + segmentLen * (translation terms) * (movement on the belt terms) + val (m, n) = if (isVert || isHorz) // a "hack" to fix the GL's pixel rounding error? + segmentLen * (-i + 0.125 + turn) to + segmentLen * (-i - 0.125 + turn) + else + segmentLen * (-i - 0.00 + turn) to + segmentLen * (-i - 0.25 + turn) + + val x1 = bbx1 + m * cos(di) + val y1 = bby1 - m * sin(di) + val x2 = bbx1 + n * cos(di) + val y2 = bby1 - n * sin(di) + + drawLineOnWorld( + x1.coerceIn(xRange), + y1.coerceIn(yRange), + x2.coerceIn(xRange), + y2.coerceIn(yRange), + 2f + ) + } + } + + private fun drawBeltMvmtRightSpindle(segmentLen: Double) { + // stripes at the right spindle + // eq: k units/s on straight part == (k / r) rad/s on curve + val lSegCnt = l / segmentLen + val cSegCnt = (c / 2.0) - lSegCnt + val cSegOffset = (cSegCnt fmod 1.0) * segmentLen // [pixels] + val turnOffset = cSegOffset / r + for (i in 0 until 3) { + + val arcStart = di - turnOffset - segmentLen * (-i + turn) / r // use `di` as the baseline + val arcSize = -(segmentLen * 0.25) / r + val arcEnd = arcStart + arcSize + val arcRange = di-Math.PI..di + + // if the arc overlaps the larger arc... + if (arcStart in arcRange || arcEnd in arcRange) + drawArcOnWorld2(cx2, cy2, r, + arcStart.coerceIn(arcRange), + arcEnd.coerceIn(arcRange), + 2f + ) + } + } + override fun drawBody(frameDelta: Float, batch: SpriteBatch) { batch.end() shapeRender.projectionMatrix = batch.projectionMatrix - Gdx.gl.glEnable(GL11.GL_LINE_SMOOTH) + Gdx.gl.glEnable(GL11.GL_LINE_SMOOTH) + val segmentLen = s / c shapeRender.inUse { it.color = COL_BELT @@ -172,95 +251,12 @@ class ActorConveyors : ActorWithBody { // right arc drawArcOnWorld(cx2, cy2, r, di, -Math.PI, 2f) - // draw belt stripes under + // draw belt stripes it.color = COL_BELT_ALT - val segmentLen = s / c - var segmentsUsed = 0 - for (i in 0 until c / 2) { // not exact code but whatever - //= originPoint + segmentLen * (translation terms) * (movement on the belt terms) - val m = segmentLen * (i + 0.00 + turn) - val n = segmentLen * (i + 0.25 + turn) - val x1 = btx1 + m * cos(dd) - val y1 = bty1 - m * sin(dd) - val x2 = btx1 + n * cos(dd) - val y2 = bty1 - n * sin(dd) - - if (x1 in btx1..btx2) { - if (bty2 > bty1) { - drawLineOnWorld( - x1.coerceIn(btx1..btx2), - y1.coerceIn(bty1..bty2), - x2.coerceIn(btx1..btx2), - y2.coerceIn(bty1..bty2), - 2f - ) - } - else { - drawLineOnWorld( - x1.coerceIn(btx1..btx2), - y1.coerceIn(bty2..bty1), - x2.coerceIn(btx1..btx2), - y2.coerceIn(bty2..bty1), - 2f - ) - } - segmentsUsed++ - } - } - smu = segmentsUsed - // stripes at the right spindle - // eq: k units/s on straight part == (k / r) rad/s on curve - - val lSegCnt = l / segmentLen - val cSegCnt = (c / 2.0) - lSegCnt - val cSegOffset = (cSegCnt fmod 1.0) * segmentLen // [pixels] - val turnOffset = cSegOffset / r - tooltipText = "di=$di, dd=$dd\nsegLen=$segmentLen\ntotalSegCnt=$c\nlSegCnt=$lSegCnt\ncSegCnt=$cSegCnt\ncSegOffset=$cSegOffset\nturnOffset=$turnOffset\nr=$r" - for (i in 0 until 3) { -// it.color = listOf(Color.LIME, Color.CORAL, Color.CYAN)[i] - - val arcStart = di - turnOffset - segmentLen * (-i + turn) / r // use `di` as the baseline - val arcSize = -(segmentLen * 0.25) / r - val arcEnd = arcStart + arcSize - val arcRange = di-Math.PI..di - - // if the arc overlaps the larger arc... - if (arcStart in arcRange || arcEnd in arcRange) - drawArcOnWorld2(cx2, cy2, r, - arcStart.coerceIn(arcRange), // this doesn't work due to sign flipping - arcEnd.coerceIn(arcRange), - 2f - ) - } - - // bottom straight part - it.color = COL_BELT_ALT - /*val bottomSectionOffset = (l + Math.PI * r) % segmentLen - for (i in 0 until c / 2) { // not exact code but whatever - val x1 = bbx1 + bottomSectionOffset + r * sin(di - HALF_PI) * (i + turn - 0.00) * segmentLen - val y1 = bby1 + bottomSectionOffset + r * cos(di - HALF_PI) * (i + turn - 0.00) * segmentLen - 1 - val x2 = bbx1 + bottomSectionOffset + r * sin(di - HALF_PI) * (i + turn - 0.25) * segmentLen - val y2 = bby1 + bottomSectionOffset + r * cos(di - HALF_PI) * (i + turn - 0.25) * segmentLen - 1 - - if (x1 in bbx1 - 4..bbx2 + 4) { - if (bby2 > bby1) - drawLineOnWorld( - x1.coerceIn(bbx1..bbx2), - y1.coerceIn(bby1..bby2), - x2.coerceIn(bbx1..bbx2), - y2.coerceIn(bby1..bby2), - 2f - ) - else - drawLineOnWorld( - x1.coerceIn(bbx1..bbx2), - y1.coerceIn(bby2..bby1), - x2.coerceIn(bbx1..bbx2), - y2.coerceIn(bby2..bby1), - 2f - ) - } - }*/ + drawBeltMvmtTop(segmentLen) + drawBeltMvmtRightSpindle(segmentLen) + drawBeltMvmtBottom(segmentLen) +// drawBeltMvmtLeftSpindle(segmentLen)