diff options
Diffstat (limited to 'app/src/main/java/space/jakob/mines/GameView.kt')
| -rw-r--r-- | app/src/main/java/space/jakob/mines/GameView.kt | 61 |
1 files changed, 44 insertions, 17 deletions
diff --git a/app/src/main/java/space/jakob/mines/GameView.kt b/app/src/main/java/space/jakob/mines/GameView.kt index 5acc94d..a53a2a1 100644 --- a/app/src/main/java/space/jakob/mines/GameView.kt +++ b/app/src/main/java/space/jakob/mines/GameView.kt @@ -46,16 +46,40 @@ class GameView(context: Context, attrs: AttributeSet) : View(context, attrs) { var cameraY = 0.0 var cameraBreadth = 5.0 - // TODO: Break up into multiple expressions. This isn't readable. + // Properties for scaling cameraBreadth to the screen resolution. + val cameraBreadthX: Double + get() = if (width > height) { + cameraBreadth * width.toDouble() / height.toDouble() + } else { + cameraBreadth + } + val cameraBreadthY: Double + get() = if (height > width) { + cameraBreadth * height.toDouble() / width.toDouble() + } else { + cameraBreadth + } + /** * Returns the grid coordinates for the tile at the given screen coordinates. */ - fun toGridCoordinates(x: Int, y: Int) = Pair( - (x + (scaledTileWidth * (cameraX % 1)).toInt()) / scaledTileWidth + cameraX.toInt(), - (y + (scaledTileWidth * (cameraY % 1)).toInt()) / scaledTileWidth + cameraY.toInt() - ) + fun toGridCoordinates(x: Int, y: Int): Pair<Int, Int> { + val minX = floor(cameraX - cameraBreadthX / 2).toInt() + val minY = floor(cameraY - cameraBreadthY / 2).toInt() + + val offsetX = (cameraX - cameraBreadthX / 2) % 1 * scaledTileWidth + val offsetY = (cameraY - cameraBreadthY / 2) % 1 * scaledTileWidth - // TODO: Document this + // These were derived algebraically from the formula in `drawGrid`. + return Pair( + (x + offsetX.toInt()) / scaledTileWidth + minX + 1, + (y + offsetY.toInt()) / scaledTileWidth + minY + 1 + ) + } + + /** + * Moves the camera by (@param dx, @param dy), clamping to grid bounds. + */ fun moveCamera(dx: Double, dy: Double) { grid?.let { cameraX = (cameraX + dx / 100.0).coerceIn(0.0, (it.width - 1).toDouble()) @@ -63,6 +87,20 @@ class GameView(context: Context, attrs: AttributeSet) : View(context, attrs) { } } + fun scaleCamera(factor: Double) { + val maxBreadth = grid?.width?.toDouble() ?: 0.0 + cameraBreadth = (cameraBreadth / factor).coerceIn(1.0, maxBreadth) + } + + /** + * Reveal and @return the tile at (@param x, @param y). + */ + fun revealAt(x: Int, y: Int): Tile? = grid?.let { + val (x, y) = toGridCoordinates(x.toInt(), y.toInt()) + it.reveal(x, y) + return it[x, y] + } + private fun atlasRect(entry: AtlasEntry): Rect { val atlasWidth = 3 @@ -126,17 +164,6 @@ class GameView(context: Context, attrs: AttributeSet) : View(context, attrs) { // v // cameraBreadthX - val cameraBreadthX = if (width > height) { - cameraBreadth * width.toDouble() / height.toDouble() - } else { - cameraBreadth - } - val cameraBreadthY = if (height > width) { - cameraBreadth * height.toDouble() / width.toDouble() - } else { - cameraBreadth - } - for (x in 0 until grid.width) { for (y in 0 until grid.height) { val minX = floor(cameraX - cameraBreadthX / 2).toInt() |