From 1505a64738badb263d93ca9bf524661012cfb2a1 Mon Sep 17 00:00:00 2001 From: LeHaine Date: Fri, 13 Dec 2024 07:21:53 -0500 Subject: [PATCH] update rect.intersects() & reorder params to a more natural way (x,y,x2,y2) --- .../graphics/g2d/tilemap/ldtk/LDtkLevel.kt | 38 +++++-------------- .../g2d/tilemap/tiled/TiledImageLayer.kt | 8 ++-- .../g2d/tilemap/tiled/TiledTilesLayer.kt | 27 +++++++------ .../kotlin/com/littlekt/math/Rect.kt | 23 ++++------- 4 files changed, 34 insertions(+), 62 deletions(-) diff --git a/core/src/commonMain/kotlin/com/littlekt/graphics/g2d/tilemap/ldtk/LDtkLevel.kt b/core/src/commonMain/kotlin/com/littlekt/graphics/g2d/tilemap/ldtk/LDtkLevel.kt index 7160c8568..dfa8f7108 100644 --- a/core/src/commonMain/kotlin/com/littlekt/graphics/g2d/tilemap/ldtk/LDtkLevel.kt +++ b/core/src/commonMain/kotlin/com/littlekt/graphics/g2d/tilemap/ldtk/LDtkLevel.kt @@ -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 @@ -94,24 +94,12 @@ 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) { @@ -119,17 +107,11 @@ class LDtkLevel( } } - 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 = entitiesByIdentifier[name] ?: error("Entities: '$name' do not exist in this level!") @@ -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) } @@ -238,7 +220,7 @@ class LDtkLevel( scaleY = scaleY * scale, rotation = Angle.ZERO, flipX = false, - flipY = false + flipY = false, ) } } diff --git a/core/src/commonMain/kotlin/com/littlekt/graphics/g2d/tilemap/tiled/TiledImageLayer.kt b/core/src/commonMain/kotlin/com/littlekt/graphics/g2d/tilemap/tiled/TiledImageLayer.kt index 549368686..840f7d6c2 100644 --- a/core/src/commonMain/kotlin/com/littlekt/graphics/g2d/tilemap/tiled/TiledImageLayer.kt +++ b/core/src/commonMain/kotlin/com/littlekt/graphics/g2d/tilemap/tiled/TiledImageLayer.kt @@ -27,7 +27,7 @@ class TiledImageLayer( tintColor: Color?, opacity: Float, properties: Map, - private val texture: TextureSlice? + private val texture: TextureSlice?, ) : TiledLayer( type, @@ -42,7 +42,7 @@ class TiledImageLayer( tileHeight, tintColor, opacity, - properties + properties, ) { override fun render( @@ -52,7 +52,7 @@ class TiledImageLayer( y: Float, scale: Float, displayObjects: Boolean, - shapeRenderer: ShapeRenderer? + shapeRenderer: ShapeRenderer?, ) { if (!visible) return @@ -68,7 +68,7 @@ class TiledImageLayer( ty, scaleX = scale, scaleY = scale, - color = tintColor ?: Color.WHITE + color = tintColor ?: Color.WHITE, ) } } diff --git a/core/src/commonMain/kotlin/com/littlekt/graphics/g2d/tilemap/tiled/TiledTilesLayer.kt b/core/src/commonMain/kotlin/com/littlekt/graphics/g2d/tilemap/tiled/TiledTilesLayer.kt index 667be17b1..603f46b05 100644 --- a/core/src/commonMain/kotlin/com/littlekt/graphics/g2d/tilemap/tiled/TiledTilesLayer.kt +++ b/core/src/commonMain/kotlin/com/littlekt/graphics/g2d/tilemap/tiled/TiledTilesLayer.kt @@ -55,7 +55,7 @@ class TiledTilesLayer( tileHeight, tintColor, opacity, - properties + properties, ) { private val lastFrameTimes by lazy { mutableMapOf() } private val lastFrameIndex by lazy { mutableMapOf() } @@ -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, @@ -79,7 +78,7 @@ class TiledTilesLayer( y: Float, scale: Float, displayObjects: Boolean, - shapeRenderer: ShapeRenderer? + shapeRenderer: ShapeRenderer?, ) { if (!visible) return @@ -380,7 +379,7 @@ class TiledTilesLayer( viewBounds: Rect, x: Float, y: Float, - scale: Float + scale: Float, ) { val tileWidth = tileWidth * scale val tileHeight = tileHeight * scale @@ -411,7 +410,7 @@ class TiledTilesLayer( rotation = tileData.rotation, flipX = tileData.flipX, flipY = tileData.flipY, - color = tintColor ?: Color.WHITE + color = tintColor ?: Color.WHITE, ) } } @@ -424,7 +423,7 @@ class TiledTilesLayer( viewBounds: Rect, x: Float, y: Float, - scale: Float + scale: Float, ) { val tileWidth = tileWidth * scale val tileHeight = tileHeight * scale @@ -464,7 +463,7 @@ class TiledTilesLayer( tx, ty, tx + it.width.toFloat(), - ty + it.height.toFloat() + ty + it.height.toFloat(), ) ) { batch.draw( @@ -478,7 +477,7 @@ class TiledTilesLayer( rotation = tileData.rotation, flipX = tileData.flipX, flipY = tileData.flipY, - color = tintColor ?: Color.WHITE + color = tintColor ?: Color.WHITE, ) } } @@ -492,7 +491,7 @@ class TiledTilesLayer( viewBounds: Rect, x: Float, y: Float, - scale: Float + scale: Float, ) { val tileWidth = tileWidth * scale val tileHeight = tileHeight * scale @@ -529,7 +528,7 @@ class TiledTilesLayer( rotation = tileData.rotation, flipX = tileData.flipX, flipY = tileData.flipY, - color = tintColor ?: Color.WHITE + color = tintColor ?: Color.WHITE, ) } } @@ -551,7 +550,7 @@ class TiledTilesLayer( rotation = tileData.rotation, flipX = tileData.flipX, flipY = tileData.flipY, - color = tintColor ?: Color.WHITE + color = tintColor ?: Color.WHITE, ) } } @@ -564,7 +563,7 @@ class TiledTilesLayer( viewBounds: Rect, x: Float, y: Float, - scale: Float + scale: Float, ) { val tileWidth = tileWidth * scale val tileHeight = tileHeight * scale @@ -599,7 +598,7 @@ class TiledTilesLayer( rotation = tileData.rotation, flipX = tileData.flipX, flipY = tileData.flipY, - color = tintColor ?: Color.WHITE + color = tintColor ?: Color.WHITE, ) } } diff --git a/core/src/commonMain/kotlin/com/littlekt/math/Rect.kt b/core/src/commonMain/kotlin/com/littlekt/math/Rect.kt index 2487152da..c277e7bb5 100644 --- a/core/src/commonMain/kotlin/com/littlekt/math/Rect.kt +++ b/core/src/commonMain/kotlin/com/littlekt/math/Rect.kt @@ -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`. */ @@ -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)" @@ -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