1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
// 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 <https://www.gnu.org/licenses/>.
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
const val atlasWidth = 3
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
var grid: Grid? = null
var cameraX = 0.0f
var cameraY = 0.0f
var sightX = 5.0f
var sightY = 5.0f
val scaledWidth: Int
get() = width / (sightX - cameraX).toInt()
val scaledHeight: Int
get() = height / (sightY - cameraY).toInt()
private fun atlasRect(entry: AtlasEntry, tileWidth: Int): Rect {
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, entry: AtlasEntry, x: Int, y: Int) {
val src = atlasRect(entry, tileWidth)
val dst = Rect(x, y, x + scaledWidth, y + scaledHeight)
canvas.drawBitmap(atlas, src, dst, null)
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
for (i in 0..5) {
drawTile(canvas, AtlasEntry.MINE, i * scaledWidth - tileWidth / 2, 0)
}
}
}
|