DEADSOFTWARE

Refactor rendering
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / misc / utils / RenderingUtils.kt
1 package ru.deadsoftware.cavedroid.misc.utils
3 import com.badlogic.gdx.math.Rectangle
5 private fun Rectangle.shifted(shift: Float) = Rectangle(x + shift, y, width, height)
7 private fun Rectangle.getLazyShifts(worldWidthPx: Float)
8 = Triple(
9 first = lazy { shifted(0f) },
10 second = lazy { shifted(-worldWidthPx) },
11 third = lazy { shifted(worldWidthPx) }
12 )
14 fun Rectangle.cycledInsideWorld(
15 viewport: Rectangle,
16 worldWidthPx: Float,
17 ): Rectangle? {
18 val (notShifted, shiftedLeft, shiftedRight) = getLazyShifts(worldWidthPx)
20 return when {
21 viewport.overlaps(notShifted.value) -> notShifted.value
22 viewport.overlaps(shiftedLeft.value) -> shiftedLeft.value
23 viewport.overlaps(shiftedRight.value) -> shiftedRight.value
24 else -> null
25 }
26 }
28 fun forEachBlockInArea(
29 area: Rectangle,
30 func: (x: Int, y: Int) -> Unit
31 ) {
32 val startMapX = area.x.bl
33 val endMapX = startMapX + area.width.bl + 1
34 val startMapY = area.y.bl
35 val endMapY = startMapY + area.height.bl + 1
37 for (x in startMapX..endMapX) {
38 for (y in startMapY..endMapY) {
39 func(x, y)
40 }
41 }
42 }