Skip to content

Commit

Permalink
update rect.intersects() & reorder params to a more natural way (x,y,…
Browse files Browse the repository at this point in the history
…x2,y2)
  • Loading branch information
LeHaine committed Dec 13, 2024
1 parent 131d453 commit 1505a64
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ class LDtkLevel(
x = levelBackgroundPos.cropRect[0],
y = levelBackgroundPos.cropRect[1],
w = levelBackgroundPos.cropRect[2],
h = levelBackgroundPos.cropRect[3]
h = levelBackgroundPos.cropRect[3],
),
slice =
backgroundImageSlice
?: error(
"Unable to retrieve background TextureSlice when it should be available."
)
),
)
} else {
null
Expand All @@ -94,42 +94,24 @@ class LDtkLevel(
layers.fastForEach { it.removeFromCache(cache) }
}

fun render(
batch: Batch,
camera: Camera,
x: Float,
y: Float,
scale: Float = 1f,
) {
fun render(batch: Batch, camera: Camera, x: Float, y: Float, scale: Float = 1f) {
viewBounds.calculateViewBounds(camera)
render(batch, viewBounds, x, y, scale)
}

fun render(
batch: Batch,
viewBounds: Rect,
x: Float,
y: Float,
scale: Float = 1f,
) {
fun render(batch: Batch, viewBounds: Rect, x: Float, y: Float, scale: Float = 1f) {
levelBackgroundImage?.render(batch, viewBounds, x, y, scale)
// need to render back to front - layers last in the list need to render first
for (i in layers.size - 1 downTo 0) {
layers[i].render(batch, viewBounds, x, y, scale)
}
}

fun render(
batch: Batch,
camera: Camera,
scale: Float = 1f,
) = render(batch, camera, worldX * scale, worldY * scale, scale)
fun render(batch: Batch, camera: Camera, scale: Float = 1f) =
render(batch, camera, worldX * scale, worldY * scale, scale)

fun render(
batch: Batch,
viewBounds: Rect,
scale: Float = 1f,
) = render(batch, viewBounds, worldX * scale, worldY * scale, scale)
fun render(batch: Batch, viewBounds: Rect, scale: Float = 1f) =
render(batch, viewBounds, worldX * scale, worldY * scale, scale)

fun entities(name: String): List<LDtkEntity> =
entitiesByIdentifier[name] ?: error("Entities: '$name' do not exist in this level!")
Expand Down Expand Up @@ -204,7 +186,7 @@ class LDtkLevel(
cache.add(slice) {
position.set(
topLeftX * scale + x + slice.width * 0.5f * scale * scaleX,
topLeftY * scale + y + slice.height * 0.5f * scale * scaleY
topLeftY * scale + y + slice.height * 0.5f * scale * scaleY,
)
this.scale.set(scaleX * scale, scaleY * scale)
}
Expand Down Expand Up @@ -238,7 +220,7 @@ class LDtkLevel(
scaleY = scaleY * scale,
rotation = Angle.ZERO,
flipX = false,
flipY = false
flipY = false,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TiledImageLayer(
tintColor: Color?,
opacity: Float,
properties: Map<String, TiledMap.Property>,
private val texture: TextureSlice?
private val texture: TextureSlice?,
) :
TiledLayer(
type,
Expand All @@ -42,7 +42,7 @@ class TiledImageLayer(
tileHeight,
tintColor,
opacity,
properties
properties,
) {

override fun render(
Expand All @@ -52,7 +52,7 @@ class TiledImageLayer(
y: Float,
scale: Float,
displayObjects: Boolean,
shapeRenderer: ShapeRenderer?
shapeRenderer: ShapeRenderer?,
) {
if (!visible) return

Expand All @@ -68,7 +68,7 @@ class TiledImageLayer(
ty,
scaleX = scale,
scaleY = scale,
color = tintColor ?: Color.WHITE
color = tintColor ?: Color.WHITE,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class TiledTilesLayer(
tileHeight,
tintColor,
opacity,
properties
properties,
) {
private val lastFrameTimes by lazy { mutableMapOf<Int, Duration>() }
private val lastFrameIndex by lazy { mutableMapOf<Int, Int>() }
Expand All @@ -69,8 +69,7 @@ class TiledTilesLayer(
private val bottomLeft = MutableVec2f()

/** Returns [TileData.id] of a tile with coordinates of [x], [y]. */
fun getTileId(x: Int, y: Int): Int =
tileData[getCoordId(x, y)].bitsToTileId()
fun getTileId(x: Int, y: Int): Int = tileData[getCoordId(x, y)].bitsToTileId()

override fun render(
batch: Batch,
Expand All @@ -79,7 +78,7 @@ class TiledTilesLayer(
y: Float,
scale: Float,
displayObjects: Boolean,
shapeRenderer: ShapeRenderer?
shapeRenderer: ShapeRenderer?,
) {
if (!visible) return

Expand Down Expand Up @@ -380,7 +379,7 @@ class TiledTilesLayer(
viewBounds: Rect,
x: Float,
y: Float,
scale: Float
scale: Float,
) {
val tileWidth = tileWidth * scale
val tileHeight = tileHeight * scale
Expand Down Expand Up @@ -411,7 +410,7 @@ class TiledTilesLayer(
rotation = tileData.rotation,
flipX = tileData.flipX,
flipY = tileData.flipY,
color = tintColor ?: Color.WHITE
color = tintColor ?: Color.WHITE,
)
}
}
Expand All @@ -424,7 +423,7 @@ class TiledTilesLayer(
viewBounds: Rect,
x: Float,
y: Float,
scale: Float
scale: Float,
) {
val tileWidth = tileWidth * scale
val tileHeight = tileHeight * scale
Expand Down Expand Up @@ -464,7 +463,7 @@ class TiledTilesLayer(
tx,
ty,
tx + it.width.toFloat(),
ty + it.height.toFloat()
ty + it.height.toFloat(),
)
) {
batch.draw(
Expand All @@ -478,7 +477,7 @@ class TiledTilesLayer(
rotation = tileData.rotation,
flipX = tileData.flipX,
flipY = tileData.flipY,
color = tintColor ?: Color.WHITE
color = tintColor ?: Color.WHITE,
)
}
}
Expand All @@ -492,7 +491,7 @@ class TiledTilesLayer(
viewBounds: Rect,
x: Float,
y: Float,
scale: Float
scale: Float,
) {
val tileWidth = tileWidth * scale
val tileHeight = tileHeight * scale
Expand Down Expand Up @@ -529,7 +528,7 @@ class TiledTilesLayer(
rotation = tileData.rotation,
flipX = tileData.flipX,
flipY = tileData.flipY,
color = tintColor ?: Color.WHITE
color = tintColor ?: Color.WHITE,
)
}
}
Expand All @@ -551,7 +550,7 @@ class TiledTilesLayer(
rotation = tileData.rotation,
flipX = tileData.flipX,
flipY = tileData.flipY,
color = tintColor ?: Color.WHITE
color = tintColor ?: Color.WHITE,
)
}
}
Expand All @@ -564,7 +563,7 @@ class TiledTilesLayer(
viewBounds: Rect,
x: Float,
y: Float,
scale: Float
scale: Float,
) {
val tileWidth = tileWidth * scale
val tileHeight = tileHeight * scale
Expand Down Expand Up @@ -599,7 +598,7 @@ class TiledTilesLayer(
rotation = tileData.rotation,
flipX = tileData.flipX,
flipY = tileData.flipY,
color = tintColor ?: Color.WHITE
color = tintColor ?: Color.WHITE,
)
}
}
Expand Down
23 changes: 7 additions & 16 deletions core/src/commonMain/kotlin/com/littlekt/math/Rect.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ open class Rect(
/** The width of the rect. */
var width: Float = 0f,
/** The height of the rect. */
var height: Float = 0f
var height: Float = 0f,
) {

/** The right-most x-coordinate. This is the same as doing `x + width`. */
Expand All @@ -44,27 +44,18 @@ open class Rect(
}

/** @return true if this [Rect] intersects with the target [rect]. */
fun intersects(rect: Rect) = intersects(left = rect.x, rect.y2, rect.x2, rect.y)
fun intersects(rect: Rect) = intersects(left = rect.x, rect.y, rect.x2, rect.y2)

/**
* @param left the left-most coordinates
* @param top the top-most coordinates
* @param right the right=most coordinates
* @param bottom the bottom-most coordinates
* @param right the right=most coordinates
* @param top the top-most coordinates
* @return true if this [Rect] intersects the given rectangle at the specified corner
* coordinates.
*/
fun intersects(left: Float, top: Float, right: Float, bottom: Float): Boolean {
if (x >= right || left >= x2) {
return false
}

if (y >= bottom || top >= y2) {
return false
}

return true
}
fun intersects(left: Float, bottom: Float, right: Float, top: Float): Boolean =
!(x >= right || x2 <= left || y >= top || y2 <= bottom)

override fun toString(): String {
return "Rect(x=$x, y=$y, width=$width, height=$height, x2=$x2, y2=$y2)"
Expand Down Expand Up @@ -158,7 +149,7 @@ class RectBuilder {
x2: Float,
y2: Float,
x: Float,
y: Float
y: Float,
) {
val p0 = x0 to y0
val p1 = x1 to y1
Expand Down

0 comments on commit 1505a64

Please sign in to comment.