aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin
diff options
context:
space:
mode:
authorJames Barnett <noreply@jamesbarnett.xyz>2020-12-28 16:25:19 +0000
committerJames Barnett <noreply@jamesbarnett.xyz>2020-12-28 16:25:19 +0000
commit9e886b392fbc6f8dfac1ce8f6943a7a1679bce74 (patch)
tree81ff992d818b5aa92090b6c02f5685e08bb6fdab /src/main/kotlin
parentf28500d963d6546feda522d7748a0462a568ba28 (diff)
downloadkotlin-raycaster-9e886b392fbc6f8dfac1ce8f6943a7a1679bce74.tar.xz
kotlin-raycaster-9e886b392fbc6f8dfac1ce8f6943a7a1679bce74.zip
Refactor input handling. General cleanup
Diffstat (limited to 'src/main/kotlin')
-rw-r--r--src/main/kotlin/Camera.kt6
-rw-r--r--src/main/kotlin/CameraController.kt46
-rw-r--r--src/main/kotlin/Extensions.kt7
-rw-r--r--src/main/kotlin/RaycastContext.kt6
-rw-r--r--src/main/kotlin/Raycaster.kt6
-rw-r--r--src/main/kotlin/Renderer.kt1
-rw-r--r--src/main/kotlin/main.kt47
7 files changed, 70 insertions, 49 deletions
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)
}