From 9e886b392fbc6f8dfac1ce8f6943a7a1679bce74 Mon Sep 17 00:00:00 2001 From: James Barnett Date: Mon, 28 Dec 2020 16:25:19 +0000 Subject: Refactor input handling. General cleanup --- src/main/kotlin/CameraController.kt | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/main/kotlin/CameraController.kt (limited to 'src/main/kotlin/CameraController.kt') 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 -- cgit v1.2.3