// Mines is free software: you can redistribute it and/or modify it under the // terms of the GNU General Public License as published by the Free Software // Foundation, either version 3 of the License, or (at your option) any later // version. // Mines is distributed in the hope that it will be useful, but WITHOUT ANY // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR // A PARTICULAR PURPOSE. See the GNU General Public License for more details. // You should have received a copy of the GNU General Public License along with // Mines. If not, see . package space.jakob.mines import android.content.Context import android.graphics.Bitmap import android.graphics.BitmapFactory import android.graphics.Canvas import android.graphics.Rect import android.graphics.Paint import android.util.AttributeSet import android.view.View import android.util.Log import kotlin.math.ceil import kotlin.math.floor enum class AtlasEntry { BLANK, MASKED, UNMASKED, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, FLAG, UNKNOWN, MINE, TRIPPED, } class GameView(context: Context, attrs: AttributeSet) : View(context, attrs) { val atlas = BitmapFactory.decodeResource(context.resources, R.drawable.atlas); val tileWidth = atlas.width / 3 // FIXME: Is this correct? `breadth` tiles, excluding halfsies and such? val scaledTileWidth: Int get() = (minOf(width, height) / cameraBreadth).toInt() var grid: Grid? = null // All of the state for maintaining the view's current positioning. For an // explanation of these values, see `drawGrid`. var cameraX = 0.0 var cameraY = 0.0 var cameraBreadth = 5.0 // TODO: Break up into multiple expressions. This isn't readable. /** * 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() ) private fun atlasRect(entry: AtlasEntry): Rect { val atlasWidth = 3 val x = (entry.ordinal % atlasWidth) * tileWidth val y = (entry.ordinal / atlasWidth) * tileWidth return Rect(x, y, x + tileWidth, y + tileWidth) } private fun drawTile(canvas: Canvas, tile: Tile, x: Int, y: Int) { val tileSpacing = 2.0 / 27.0 val xSpacing = (scaledTileWidth * tileSpacing).toInt() val ySpacing = (scaledTileWidth * tileSpacing).toInt() // TODO: Opportunity for more ASCII art explanations :) val dst = Rect( x + xSpacing, y + ySpacing, x + scaledTileWidth - xSpacing, y + scaledTileWidth - ySpacing ) if (tile.masked) { canvas.drawBitmap(atlas, atlasRect(AtlasEntry.MASKED), dst, null) } else { canvas.drawBitmap(atlas, atlasRect(AtlasEntry.UNMASKED), dst, null) val overlay = when { tile.mine -> AtlasEntry.MINE tile.adjacentMines == 1 -> AtlasEntry.ONE tile.adjacentMines == 2 -> AtlasEntry.TWO tile.adjacentMines == 3 -> AtlasEntry.THREE tile.adjacentMines == 4 -> AtlasEntry.FOUR tile.adjacentMines == 5 -> AtlasEntry.FIVE tile.adjacentMines == 6 -> AtlasEntry.SIX tile.adjacentMines == 7 -> AtlasEntry.SEVEN tile.adjacentMines == 8 -> AtlasEntry.EIGHT else -> AtlasEntry.BLANK } canvas.drawBitmap(atlas, atlasRect(overlay), dst, null) } } private fun drawGrid(canvas: Canvas, grid: Grid) { // The state maintained for drawing the view is a position, (`cameraX`, // `cameraY`), representing the centroid of a rectangle whose sides are // of length `cameraBreadthX` and `cameraBreadthY`. Only the tiles // intersecting this rectangle are drawn. // // +============+ // / | | // | | | // | | | // cameraBreadthY < | x | // | | \__________ (cameraX, cameraY) // | | | // \ | | // +============+ // \_____ ______/ // 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 } // This is where the fractional component comes into play -- drawing // tiles that are partially obscured by the limited reaches of the // camera view. val offsetX = (scaledTileWidth * ((cameraX + (cameraBreadthX / 2)) % 1)).toInt() val offsetY = (scaledTileWidth * ((cameraY + (cameraBreadthY / 2)) % 1) / 2).toInt() val howManyX = ceil(cameraBreadthX).toInt() val howManyY = ceil(cameraBreadthY).toInt() val startX = cameraX - (cameraBreadthX / 2) val startY = cameraY - (cameraBreadthY / 2) for (i in 0..howManyX) { for (j in 0..howManyY) { // The fractional components here are truncated, giving a // equivalent indices into `grid`. Of course, these indices // aren't necessarily within the bounds of the grid. If a tile // doesn't exist, we'll just draw blank space. val x = (startX + i.toDouble()).toInt() val y = (startY + j.toDouble()).toInt() if (grid.valid(x, y)) { val tile = grid[x, y] val screenX = i * scaledTileWidth val screenY = j * scaledTileWidth drawTile(canvas, tile, screenX + offsetX, screenY + offsetY) } } } } override fun onDraw(canvas: Canvas) { super.onDraw(canvas) grid?.let { drawGrid(canvas, it) } } }