diff options
| author | James Barnett <noreply@jamesbarnett.xyz> | 2020-12-31 15:48:37 +0000 |
|---|---|---|
| committer | James Barnett <noreply@jamesbarnett.xyz> | 2020-12-31 15:48:37 +0000 |
| commit | 58ff14f5c31b933a1d2f91d6cae7280366d3ffb9 (patch) | |
| tree | 343ed88fc4b6ecab173e4d24e4ca810b70588c1f /src/main/kotlin | |
| parent | 29e52544d389f88a4af836ae1d82b838ed7a10c7 (diff) | |
| download | kotlin-raycaster-58ff14f5c31b933a1d2f91d6cae7280366d3ffb9.tar.xz kotlin-raycaster-58ff14f5c31b933a1d2f91d6cae7280366d3ffb9.zip | |
Keep camera in map boundary
Diffstat (limited to 'src/main/kotlin')
| -rw-r--r-- | src/main/kotlin/CameraController.kt | 17 | ||||
| -rw-r--r-- | src/main/kotlin/Raycaster.kt | 2 | ||||
| -rw-r--r-- | src/main/kotlin/main.kt | 8 |
3 files changed, 24 insertions, 3 deletions
diff --git a/src/main/kotlin/CameraController.kt b/src/main/kotlin/CameraController.kt index 510f1a0..c2885de 100644 --- a/src/main/kotlin/CameraController.kt +++ b/src/main/kotlin/CameraController.kt @@ -5,6 +5,8 @@ class CameraController( private val camera: Camera, private val moveSpeed: Double, private val rotateSpeed: Int, + private val xMax: Int, + private val yMax: Int, private val afterInput: () -> Unit ) { @@ -30,6 +32,7 @@ class CameraController( val cameraSin = camera.rotation.sine() * moveSpeed camera.xPos += cameraCos camera.yPos += cameraSin + boundsCheck(camera) afterInput() } @@ -38,6 +41,7 @@ class CameraController( val cameraSin = camera.rotation.sine() * moveSpeed camera.xPos -= cameraCos camera.yPos -= cameraSin + boundsCheck(camera) afterInput() } @@ -51,4 +55,17 @@ class CameraController( afterInput() } + private fun boundsCheck(camera: Camera) { + camera.xPos = clamp(camera.xPos, xMax) + camera.yPos = clamp(camera.yPos, yMax) + } + + private fun clamp(position: Double, max: Int): Double { + return when { + position < 1 -> 1.0 + position > max -> max.toDouble() + else -> position + } + } + }
\ No newline at end of file diff --git a/src/main/kotlin/Raycaster.kt b/src/main/kotlin/Raycaster.kt index f012ba8..53c6dc6 100644 --- a/src/main/kotlin/Raycaster.kt +++ b/src/main/kotlin/Raycaster.kt @@ -27,8 +27,6 @@ class Raycaster { do { rayX += raySweepAngle.cosine() / options.stepPrecision rayY += raySweepAngle.sine() / options.stepPrecision - - // TODO bounds checking objectTypeHit = map.data[rayY.toFlooredInt()][rayX.toFlooredInt()] } while (objectTypeHit == 0) diff --git a/src/main/kotlin/main.kt b/src/main/kotlin/main.kt index c29374d..0d0d600 100644 --- a/src/main/kotlin/main.kt +++ b/src/main/kotlin/main.kt @@ -18,7 +18,13 @@ fun main() { val raycaster = Raycaster() - CameraController(camera, moveSpeed = 1.0, rotateSpeed = 15) { + CameraController( + camera, + moveSpeed = 1.0, + rotateSpeed = 15, + xMax = map.width - 1, + yMax = map.height - 1 + ) { paint(raycaster, context) } |