diff options
| author | James Barnett <noreply@jamesbarnett.xyz> | 2020-12-28 16:25:19 +0000 |
|---|---|---|
| committer | James Barnett <noreply@jamesbarnett.xyz> | 2020-12-28 16:25:19 +0000 |
| commit | 9e886b392fbc6f8dfac1ce8f6943a7a1679bce74 (patch) | |
| tree | 81ff992d818b5aa92090b6c02f5685e08bb6fdab /src/main/kotlin/CameraController.kt | |
| parent | f28500d963d6546feda522d7748a0462a568ba28 (diff) | |
| download | kotlin-raycaster-9e886b392fbc6f8dfac1ce8f6943a7a1679bce74.tar.xz kotlin-raycaster-9e886b392fbc6f8dfac1ce8f6943a7a1679bce74.zip | |
Refactor input handling. General cleanup
Diffstat (limited to 'src/main/kotlin/CameraController.kt')
| -rw-r--r-- | src/main/kotlin/CameraController.kt | 46 |
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 |