summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorJakob L. Kreuze <jakob@memeware.net>2018-12-22 08:05:52 -0500
committerJakob L. Kreuze <jakob@memeware.net>2018-12-22 08:05:52 -0500
commitd321bb13a9a2ea7af10400b3ce3848733039cbd4 (patch)
treee1c195502543fbec2b3ab86630e2529f28dfdcd4 /app
parent2560d1da9a6f9122371c34c412b21a79ebaf76cb (diff)
Sometimes, the best solution is the most straighforward one.
Diffstat (limited to 'app')
-rw-r--r--app/src/main/java/space/jakob/mines/GameView.kt40
1 files changed, 15 insertions, 25 deletions
diff --git a/app/src/main/java/space/jakob/mines/GameView.kt b/app/src/main/java/space/jakob/mines/GameView.kt
index a53a2a1..118cb45 100644
--- a/app/src/main/java/space/jakob/mines/GameView.kt
+++ b/app/src/main/java/space/jakob/mines/GameView.kt
@@ -63,33 +63,25 @@ class GameView(context: Context, attrs: AttributeSet) : View(context, attrs) {
/**
* Returns the grid coordinates for the tile at the given screen coordinates.
*/
- 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
-
+ fun toGridCoordinates(x: Int, y: Int) = Pair(
// These were derived algebraically from the formula in `drawGrid`.
- return Pair(
- (x + offsetX.toInt()) / scaledTileWidth + minX + 1,
- (y + offsetY.toInt()) / scaledTileWidth + minY + 1
- )
- }
+ ((x.toDouble() / scaledTileWidth.toDouble()) - cameraBreadthX / 2 + cameraX).toInt(),
+ ((y.toDouble() / scaledTileWidth.toDouble()) - cameraBreadthY / 2 + cameraY).toInt()
+ )
/**
* 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())
- cameraY = (cameraY + dy / 100.0).coerceIn(0.0, (it.height - 1).toDouble())
+ cameraX = (cameraX + dx / 100.0).coerceIn(0.0, (it.width).toDouble())
+ cameraY = (cameraY + dy / 100.0).coerceIn(0.0, (it.height).toDouble())
}
}
fun scaleCamera(factor: Double) {
val maxBreadth = grid?.width?.toDouble() ?: 0.0
- cameraBreadth = (cameraBreadth / factor).coerceIn(1.0, maxBreadth)
+ cameraBreadth = (cameraBreadth / factor).coerceIn(5.0, maxBreadth)
}
/**
@@ -97,8 +89,12 @@ class GameView(context: Context, attrs: AttributeSet) : View(context, attrs) {
*/
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]
+ if (it.valid(x, y)) {
+ it.reveal(x, y)
+ return it[x, y]
+ } else {
+ return null
+ }
}
private fun atlasRect(entry: AtlasEntry): Rect {
@@ -166,17 +162,11 @@ class GameView(context: Context, attrs: AttributeSet) : View(context, attrs) {
for (x in 0 until grid.width) {
for (y in 0 until grid.height) {
- 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
-
drawTile(
canvas,
grid[x, y],
- (x - minX - 1) * scaledTileWidth - offsetX.toInt(),
- (y - minY - 1) * scaledTileWidth - offsetY.toInt()
+ ((x + cameraBreadthX / 2 - cameraX) * scaledTileWidth).toInt(),
+ ((y + cameraBreadthY / 2 - cameraY) * scaledTileWidth).toInt()
)
}
}