diff options
Diffstat (limited to 'app/src/main')
| -rw-r--r-- | app/src/main/java/space/jakob/mines/GameActivity.kt | 27 | ||||
| -rw-r--r-- | app/src/main/java/space/jakob/mines/GameView.kt | 41 |
2 files changed, 49 insertions, 19 deletions
diff --git a/app/src/main/java/space/jakob/mines/GameActivity.kt b/app/src/main/java/space/jakob/mines/GameActivity.kt index 929c020..927955e 100644 --- a/app/src/main/java/space/jakob/mines/GameActivity.kt +++ b/app/src/main/java/space/jakob/mines/GameActivity.kt @@ -13,26 +13,43 @@ package space.jakob.mines import android.support.v7.app.AppCompatActivity +import android.support.v4.view.GestureDetectorCompat import android.os.Bundle +import android.view.GestureDetector import android.view.MotionEvent import android.util.Log class GameActivity : AppCompatActivity() { lateinit var gameView: GameView + lateinit var gestureDetector: GestureDetectorCompat - override fun onTouchEvent(event: MotionEvent): Boolean { - val (x, y) = gameView.toGridCoordinates(event.x.toInt(), event.y.toInt()) - gameView.grid?.let { - it.reveal(x, y) + // Implements the callbacks for gestureDetector. + val gestureListener = object : GestureDetector.SimpleOnGestureListener() { + override fun onScroll(e1: MotionEvent, e2: MotionEvent, dx: Float, dy: Float): Boolean { + with (gameView) { + cameraX = (cameraX + dx / 100.0).coerceIn(0.0, gameView.maxCameraX) + cameraY = (cameraY + dy / 100.0).coerceIn(0.0, gameView.maxCameraY) + } gameView.invalidate() + return false + } + } + + override fun onTouchEvent(event: MotionEvent): Boolean { + return if (gestureDetector.onTouchEvent(event)) { + true + } else { + super.onTouchEvent(event) } - return true } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_game) + gameView = findViewById(R.id.view) as GameView gameView.grid = Grid(8, 8, 10) + + gestureDetector = GestureDetectorCompat(this, gestureListener) } } diff --git a/app/src/main/java/space/jakob/mines/GameView.kt b/app/src/main/java/space/jakob/mines/GameView.kt index 80218ce..36fe2ec 100644 --- a/app/src/main/java/space/jakob/mines/GameView.kt +++ b/app/src/main/java/space/jakob/mines/GameView.kt @@ -37,15 +37,34 @@ class GameView(context: Context, attrs: AttributeSet) : View(context, attrs) { var grid: Grid? = null - var cameraX = 0.5f - var cameraY = 0.0f - var sightX = 5.0f - var sightY = 5.0f + var cameraX = 0.0 + var cameraY = 0.0 + var sightX = 5.0 + var sightY = 5.0 + val maxCameraX: Double + get() = grid?.let { + it.width - sightX + } ?: 0.0 + val maxCameraY: Double + get() = grid?.let { + it.height - sightY + } ?: 0.0 + + // The size, in pixels, of an individual tile based on the current scale + // factor. val scaledWidth: Int - get() = width / (sightX - cameraX).toInt() + get() = width / sightX.toInt() val scaledHeight: Int - get() = height / (sightY - cameraY).toInt() + get() = height / sightY.toInt() + + /** + * Returns the grid coordinates for the tile at the given screen coordinates. + */ + fun toGridCoordinates(x: Int, y: Int) = Pair( + (x + (scaledWidth * (cameraX % 1)).toInt()) / scaledWidth + cameraX.toInt(), + (y + (scaledHeight * (cameraY % 1)).toInt()) / scaledHeight + cameraY.toInt() + ) private fun atlasRect(entry: AtlasEntry): Rect { val x = (entry.ordinal % atlasWidth) * tileWidth @@ -89,8 +108,8 @@ class GameView(context: Context, attrs: AttributeSet) : View(context, attrs) { private fun drawGrid(canvas: Canvas, grid: Grid) { val startX = cameraX.toInt() // Truncates the fractional component. val startY = cameraY.toInt() - val howManyX = (sightX - cameraX).toInt() + 1 - val howManyY = (sightY - cameraY).toInt() + 1 + val howManyX = (sightX.toInt() + 1).coerceAtMost(grid.width - startX - 1) + val howManyY = (sightY.toInt() + 1).coerceAtMost(grid.height - startY - 1) for (i in 0..howManyX) { for (j in 0..howManyY) { @@ -109,14 +128,8 @@ class GameView(context: Context, attrs: AttributeSet) : View(context, attrs) { } } - fun toGridCoordinates(x: Int, y: Int) = Pair( - (x + (scaledWidth * (cameraX % 1)).toInt()) / scaledWidth + cameraX.toInt(), - (y + (scaledHeight * (cameraY % 1)).toInt()) / scaledHeight + cameraY.toInt() - ) - override fun onDraw(canvas: Canvas) { super.onDraw(canvas) - grid?.let { drawGrid(canvas, it) } |