aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/CameraController.kt
diff options
context:
space:
mode:
authorJames Barnett <noreply@jamesbarnett.xyz>2020-12-31 15:48:37 +0000
committerJames Barnett <noreply@jamesbarnett.xyz>2020-12-31 15:48:37 +0000
commit58ff14f5c31b933a1d2f91d6cae7280366d3ffb9 (patch)
tree343ed88fc4b6ecab173e4d24e4ca810b70588c1f /src/main/kotlin/CameraController.kt
parent29e52544d389f88a4af836ae1d82b838ed7a10c7 (diff)
downloadkotlin-raycaster-58ff14f5c31b933a1d2f91d6cae7280366d3ffb9.tar.xz
kotlin-raycaster-58ff14f5c31b933a1d2f91d6cae7280366d3ffb9.zip
Keep camera in map boundary
Diffstat (limited to 'src/main/kotlin/CameraController.kt')
-rw-r--r--src/main/kotlin/CameraController.kt17
1 files changed, 17 insertions, 0 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