aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/CameraController.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/CameraController.kt')
-rw-r--r--src/main/kotlin/CameraController.kt46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/main/kotlin/CameraController.kt b/src/main/kotlin/CameraController.kt
new file mode 100644
index 0000000..a3508c1
--- /dev/null
+++ b/src/main/kotlin/CameraController.kt
@@ -0,0 +1,46 @@
+import kotlinx.browser.document
+
+class CameraController(
+ private val camera: Camera,
+ private val moveSpeed: Double = 0.5,
+ private val rotateSpeed: Int = 5,
+ private val afterInput: () -> Unit
+) {
+
+ init {
+ document.onkeydown = { inputHandler(it.code) }
+ }
+
+ private fun inputHandler(code: String) {
+ when (code) {
+ "KeyW" -> moveForward()
+ "KeyS" -> moveBack()
+ "KeyA" -> rotateAntiClockwise()
+ "KeyD" -> rotateClockwise()
+ }
+ afterInput()
+ }
+
+ private fun moveForward() {
+ val cameraCos = camera.rotation.cosine() * moveSpeed
+ val cameraSin = camera.rotation.sine() * moveSpeed
+ camera.xPos += cameraCos
+ camera.yPos += cameraSin
+ }
+
+ private fun moveBack() {
+ val cameraCos = camera.rotation.cosine() * moveSpeed
+ val cameraSin = camera.rotation.sine() * moveSpeed
+ camera.xPos -= cameraCos
+ camera.yPos -= cameraSin
+ }
+
+ private fun rotateClockwise() {
+ camera.rotation += rotateSpeed
+ }
+
+ private fun rotateAntiClockwise() {
+ camera.rotation -= rotateSpeed
+ }
+
+} \ No newline at end of file