summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/src/main/java/space/jakob/mines/GameActivity.kt58
-rw-r--r--app/src/main/java/space/jakob/mines/GameView.kt61
2 files changed, 59 insertions, 60 deletions
diff --git a/app/src/main/java/space/jakob/mines/GameActivity.kt b/app/src/main/java/space/jakob/mines/GameActivity.kt
index 9e231c6..b6135b3 100644
--- a/app/src/main/java/space/jakob/mines/GameActivity.kt
+++ b/app/src/main/java/space/jakob/mines/GameActivity.kt
@@ -27,17 +27,13 @@ class GameActivity : AppCompatActivity() {
// Implements the callbacks for gestureDetector.
val gestureListener = object : GestureDetector.SimpleOnGestureListener() {
- // TODO: Replace with onUp
- // override fun onDown(e: MotionEvent): Boolean {
- // with (gameView) {
- // grid?.let {
- // val (x, y) = toGridCoordinates(e.x.toInt(), e.y.toInt())
- // it.reveal(x, y)
- // invalidate()
- // }
- // }
- // return false
- // }
+ override fun onSingleTapUp(e: MotionEvent): Boolean {
+ with (gameView) {
+ revealAt(e.x.toInt(), e.y.toInt())
+ invalidate()
+ }
+ return false
+ }
override fun onScroll(e1: MotionEvent, e2: MotionEvent, dx: Float, dy: Float): Boolean {
with (gameView) {
@@ -50,38 +46,14 @@ class GameActivity : AppCompatActivity() {
// Implements the callbacks for scaleGestureDetector.
val scaleListener = object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
- // override fun onScale(scaleGestureDetector: ScaleGestureDetector) : Boolean {
- // with (gameView) {
- // grid?.let {
- // val factor = scaleGestureDetector.getScaleFactor()
-
- // var currentDistance = sightX - cameraX
- // var desiredDistance = currentDistance / factor
- // var deltaDistance = Math.abs(desiredDistance - currentDistance) / 2
-
- // if (factor < 1.0) {
- // cameraX = (cameraX - deltaDistance).coerceAtLeast(0.0)
- // sightX = (sightX + deltaDistance).coerceAtMost(it.width.toDouble() - cameraX)
- // } else {
- // cameraX = cameraX + deltaDistance
- // sightX = sightX - deltaDistance
- // }
-
- // currentDistance = sightY - cameraY
- // desiredDistance = currentDistance / factor
- // deltaDistance = Math.abs(desiredDistance - currentDistance) / 2
-
- // if (factor < 1.0) {
- // cameraY = (cameraY - deltaDistance).coerceAtLeast(0.0)
- // sightY = (sightY + deltaDistance).coerceAtMost(it.height.toDouble() - cameraY)
- // } else {
- // cameraY = cameraY + deltaDistance
- // sightY = sightY - deltaDistance
- // }
- // }
- // }
- // return false
- // }
+ override fun onScale(scaleGestureDetector: ScaleGestureDetector) : Boolean {
+ with (gameView) {
+ val factor = scaleGestureDetector.getScaleFactor()
+ scaleCamera(factor.toDouble())
+ invalidate()
+ }
+ return false
+ }
}
override fun onTouchEvent(event: MotionEvent): Boolean {
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()