DEADSOFTWARE

Prettier world + ores
[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
8 import java.awt.Color as JavaColor
10 private fun Rectangle.shifted(shift: Float) = Rectangle(x + shift, y, width, height)
12 private fun Rectangle.getLazyShifts(worldWidthPx: Float)
13 = Triple(
14 first = lazy { shifted(0f) },
15 second = lazy { shifted(-worldWidthPx) },
16 third = lazy { shifted(worldWidthPx) }
17 )
19 fun Rectangle.cycledInsideWorld(
20 viewport: Rectangle,
21 worldWidthPx: Float,
22 ): Rectangle? {
23 val (notShifted, shiftedLeft, shiftedRight) = getLazyShifts(worldWidthPx)
25 return when {
26 viewport.overlaps(notShifted.value) -> notShifted.value
27 viewport.overlaps(shiftedLeft.value) -> shiftedLeft.value
28 viewport.overlaps(shiftedRight.value) -> shiftedRight.value
29 else -> null
30 }
31 }
33 fun forEachBlockInArea(
34 area: Rectangle,
35 func: (x: Int, y: Int) -> Unit,
36 ) {
37 val startMapX = area.x.bl
38 val endMapX = (area.x + area.width - 1f).bl
39 val startMapY = area.y.bl
40 val endMapY = (area.y + area.height - 1f).bl
42 for (x in startMapX..endMapX) {
43 for (y in startMapY..endMapY) {
44 func(x, y)
45 }
46 }
47 }
49 @JvmOverloads
50 fun SpriteBatch.drawString(str: String, x: Float, y: Float, color: Color = Color.WHITE): GlyphLayout {
51 Assets.minecraftFont.color = color
52 return Assets.minecraftFont.draw(this, str, x, y)
53 }
55 fun colorFromHexString(hex: String): Color {
56 val rgba = (JavaColor.decode(hex).rgb shl 8) or (0xFF)
57 return Color(rgba)
58 }