DEADSOFTWARE

Add environment damage
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / misc / utils / RenderingUtils.kt
1 package ru.deadsoftware.cavedroid.misc.utils
3 import com.badlogic.gdx.graphics.Color
4 import com.badlogic.gdx.graphics.g2d.GlyphLayout
5 import com.badlogic.gdx.graphics.g2d.SpriteBatch
6 import com.badlogic.gdx.math.Rectangle
7 import ru.deadsoftware.cavedroid.misc.Assets
9 private fun Rectangle.shifted(shift: Float) = Rectangle(x + shift, y, width, height)
11 private fun Rectangle.getLazyShifts(worldWidthPx: Float)
12 = Triple(
13 first = lazy { shifted(0f) },
14 second = lazy { shifted(-worldWidthPx) },
15 third = lazy { shifted(worldWidthPx) }
16 )
18 fun Rectangle.cycledInsideWorld(
19 viewport: Rectangle,
20 worldWidthPx: Float,
21 ): Rectangle? {
22 val (notShifted, shiftedLeft, shiftedRight) = getLazyShifts(worldWidthPx)
24 return when {
25 viewport.overlaps(notShifted.value) -> notShifted.value
26 viewport.overlaps(shiftedLeft.value) -> shiftedLeft.value
27 viewport.overlaps(shiftedRight.value) -> shiftedRight.value
28 else -> null
29 }
30 }
32 fun forEachBlockInArea(
33 area: Rectangle,
34 func: (x: Int, y: Int) -> Unit,
35 ) {
36 val startMapX = area.x.bl
37 val endMapX = (area.x + area.width - 1f).bl
38 val startMapY = area.y.bl
39 val endMapY = (area.y + area.height - 1f).bl
41 for (x in startMapX..endMapX) {
42 for (y in startMapY..endMapY) {
43 func(x, y)
44 }
45 }
46 }
48 @JvmOverloads
49 fun SpriteBatch.drawString(str: String, x: Float, y: Float, color: Color = Color.WHITE): GlyphLayout {
50 Assets.minecraftFont.color = color
51 return Assets.minecraftFont.draw(this, str, x, y)
52 }