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/Camera.kt | 6 ++--- src/main/kotlin/CameraController.kt | 46 ++++++++++++++++++++++++++++++++++++ src/main/kotlin/Extensions.kt | 7 ++++++ src/main/kotlin/RaycastContext.kt | 6 +++++ src/main/kotlin/Raycaster.kt | 6 ++--- src/main/kotlin/Renderer.kt | 1 + src/main/kotlin/main.kt | 47 ++++--------------------------------- 7 files changed, 70 insertions(+), 49 deletions(-) create mode 100644 src/main/kotlin/CameraController.kt create mode 100644 src/main/kotlin/Extensions.kt create mode 100644 src/main/kotlin/RaycastContext.kt diff --git a/src/main/kotlin/Camera.kt b/src/main/kotlin/Camera.kt index 7d4bcfc..4eb8d43 100644 --- a/src/main/kotlin/Camera.kt +++ b/src/main/kotlin/Camera.kt @@ -1,8 +1,6 @@ -class Camera( +data class Camera( val fov: Int, var xPos: Double, var yPos: Double, var rotation: Double -) { - val halfFov = fov / 2 -} \ No newline at end of file +) \ No newline at end of file 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 diff --git a/src/main/kotlin/Extensions.kt b/src/main/kotlin/Extensions.kt new file mode 100644 index 0000000..989118f --- /dev/null +++ b/src/main/kotlin/Extensions.kt @@ -0,0 +1,7 @@ +fun Double.sine(): Double { + return kotlin.math.sin(toRadians(this)) +} + +fun Double.cosine(): Double { + return kotlin.math.cos(toRadians(this)) +} \ No newline at end of file diff --git a/src/main/kotlin/RaycastContext.kt b/src/main/kotlin/RaycastContext.kt new file mode 100644 index 0000000..9b4156c --- /dev/null +++ b/src/main/kotlin/RaycastContext.kt @@ -0,0 +1,6 @@ +data class RaycastContext( + val renderer: Renderer, + val camera: Camera, + val map: Map, + val minimap: Minimap +) \ No newline at end of file diff --git a/src/main/kotlin/Raycaster.kt b/src/main/kotlin/Raycaster.kt index d3fa694..640ffef 100644 --- a/src/main/kotlin/Raycaster.kt +++ b/src/main/kotlin/Raycaster.kt @@ -9,7 +9,7 @@ class Raycaster(private val stepPrecision: Int = 32) { val viewportHeight = renderer.viewportHeight val viewportHeightHalf = viewportHeight / 2 - var raySweepAngle = camera.rotation - camera.halfFov + var raySweepAngle = camera.rotation - (camera.fov / 2) for (rayIndex in 0 until viewportWidth) { @@ -17,8 +17,8 @@ class Raycaster(private val stepPrecision: Int = 32) { var rayY = camera.yPos do { - rayX += kotlin.math.cos(toRadians(raySweepAngle)) / stepPrecision - rayY += kotlin.math.sin(toRadians(raySweepAngle)) / stepPrecision + rayX += raySweepAngle.cosine() / stepPrecision + rayY += raySweepAngle.sine() / stepPrecision // TODO bounds checking val wallHit = map.data[kotlin.math.floor(rayY).toInt()][kotlin.math.floor(rayX).toInt()] > 0 } while (!wallHit) diff --git a/src/main/kotlin/Renderer.kt b/src/main/kotlin/Renderer.kt index 0bc8988..8f71352 100644 --- a/src/main/kotlin/Renderer.kt +++ b/src/main/kotlin/Renderer.kt @@ -9,6 +9,7 @@ class Renderer(val viewportWidth: Int, val viewportHeight: Int) { width = viewportWidth height = viewportHeight } + private val context = canvas.getContext("2d") as CanvasRenderingContext2D init { diff --git a/src/main/kotlin/main.kt b/src/main/kotlin/main.kt index c07a661..65995e1 100644 --- a/src/main/kotlin/main.kt +++ b/src/main/kotlin/main.kt @@ -1,19 +1,8 @@ -import kotlinx.browser.document - -private const val MOVE_SPEED = 0.5 -private const val ROTATE_SPEED = 5 - -data class RaycastContext( - val renderer: Renderer, - val camera: Camera, - val map: Map, - val minimap: Minimap -) - fun main() { - val renderer = Renderer(640, 480) + val raycaster = Raycaster() + val renderer = Renderer(viewportWidth = 640, viewportHeight = 480) val camera = Camera( - fov = 60, + fov = 90, xPos = 2.0, yPos = 2.0, rotation = 90.0 @@ -23,37 +12,11 @@ fun main() { val context = RaycastContext(renderer, camera, map, minimap) - val raycaster = Raycaster() - - document.onkeydown = { - when (it.code) { - "KeyW" -> { - console.log("key w") - val cameraCos = kotlin.math.cos(toRadians(camera.rotation)) * MOVE_SPEED - val cameraSin = kotlin.math.sin(toRadians(camera.rotation)) * MOVE_SPEED - camera.xPos += cameraCos - camera.yPos += cameraSin - } - "KeyS" -> { - console.log("key s") - val cameraCos = kotlin.math.cos(toRadians(camera.rotation)) * MOVE_SPEED - val cameraSin = kotlin.math.sin(toRadians(camera.rotation)) * MOVE_SPEED - camera.xPos -= cameraCos - camera.yPos -= cameraSin - } - "KeyA" -> { - console.log("key a") - camera.rotation -= ROTATE_SPEED - } - "KeyD" -> { - console.log("key d") - camera.rotation += ROTATE_SPEED - } - } + CameraController(camera, moveSpeed = 0.5, rotateSpeed = 5) { paint(raycaster, context) - console.log("camera x:${camera.xPos} y:${camera.yPos} r: ${camera.rotation}") } + // Do an initial paint and wait for input paint(raycaster, context) } -- cgit v1.2.3