diff options
| author | James Barnett <noreply@jamesbarnett.xyz> | 2020-12-28 15:06:36 +0000 |
|---|---|---|
| committer | James Barnett <noreply@jamesbarnett.xyz> | 2020-12-28 15:06:36 +0000 |
| commit | 01727649bb1fbe3e0c721a850bd21973d7e760f2 (patch) | |
| tree | f9beb156683534e2619879598cd29f8c9e5f5ed7 /src/main/kotlin/Minimap.kt | |
| parent | a4250a3959dd99b567d41a4e85f02a17de2c00ce (diff) | |
| download | kotlin-raycaster-01727649bb1fbe3e0c721a850bd21973d7e760f2.tar.xz kotlin-raycaster-01727649bb1fbe3e0c721a850bd21973d7e760f2.zip | |
Initial implementation
Diffstat (limited to 'src/main/kotlin/Minimap.kt')
| -rw-r--r-- | src/main/kotlin/Minimap.kt | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/main/kotlin/Minimap.kt b/src/main/kotlin/Minimap.kt new file mode 100644 index 0000000..dc7cc91 --- /dev/null +++ b/src/main/kotlin/Minimap.kt @@ -0,0 +1,57 @@ +import kotlinx.browser.document +import org.w3c.dom.CanvasRenderingContext2D +import org.w3c.dom.HTMLCanvasElement + +class Minimap(private val map: List<List<Int>>) { + + private val scale = 30 + private val mapWidth = map[0].size + private val mapHeight = map.size + + private val canvas = (document.createElement("canvas") as HTMLCanvasElement) + .apply { + width = mapWidth * scale + height = mapHeight * scale + id = "minimap" + style.width = "${width}px" + style.height = "${height}px" + } + private val context = canvas.getContext("2d") as CanvasRenderingContext2D + + init { + document.body!!.appendChild(canvas) + } + + private fun drawMap() { + for (y in 0 until mapHeight) { + for (x in 0 until mapWidth) { + val wall = map[y][x] + if (wall > 0) { + context.fillStyle = "#000000" + context.fillRect((x * scale).toDouble(), (y * scale).toDouble(), scale.toDouble(), scale.toDouble()) + } + } + } + } + + fun update(camera: Camera) { + context.clearRect(0.0, 0.0, (mapWidth * scale).toDouble(), (mapHeight * scale).toDouble()) + drawMap() + context.fillStyle = "#FF0000" + context.fillRect(camera.xPos * scale, camera.yPos * scale, scale.toDouble(), scale.toDouble()) + + context.strokeStyle = "#00FF00" + context.lineWidth = 2.0 + context.beginPath() + context.moveTo(camera.xPos * scale, camera.yPos * scale) + + val cameraCos = kotlin.math.cos(toRadians(camera.rotation)) * 4 + val cameraSin = kotlin.math.sin(toRadians(camera.rotation)) * 4 + + val dirX = camera.xPos + cameraCos + val dirY = camera.yPos + cameraSin + + context.lineTo(dirX * scale, dirY * scale) + context.stroke() + } +}
\ No newline at end of file |