aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/Raycaster.kt
blob: 8cc3781c33085472fab6e6780cf470dc774f4e2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import kotlin.js.Date
import kotlin.math.pow

class Raycaster(private val stepPrecision: Int) {

  fun raycast(raycastContext: RaycastContext) {
    val raycastStartMs = Date().getTime()
    val (renderer, textureManager, camera, map, _) = raycastContext

    val viewportWidth = renderer.viewportWidth
    val viewportHeight = renderer.viewportHeight
    val viewportHeightHalf = viewportHeight / 2

    var raySweepAngle = camera.rotation - (camera.fov / 2)

    for (rayIndex in 0 until viewportWidth) {

      var rayX = camera.xPos
      var rayY = camera.yPos
      var objectTypeHit: Int

      do {
        rayX += raySweepAngle.cosine() / stepPrecision
        rayY += raySweepAngle.sine() / stepPrecision

        // TODO bounds checking
        objectTypeHit = map.data[rayY.toFlooredInt()][rayX.toFlooredInt()]
      } while (objectTypeHit == 0)

      val texture = textureManager.getTexture(objectTypeHit)
      val textureXIndex = ((texture.width * (rayX + rayY)) % texture.width).toFlooredInt()

      val distanceToWall = kotlin.math.sqrt((camera.xPos - rayX).pow(2) + (camera.yPos - rayY).pow(2))
      val wallHeight = viewportHeightHalf / distanceToWall

      // Ceiling
      renderer.drawLine(rayIndex, 0.0, rayIndex, viewportHeightHalf - wallHeight, "#505050")
      // Wall
      drawWallTexture(rayIndex, wallHeight, textureXIndex, texture, renderer)
      // Floor
      renderer.drawLine(rayIndex, viewportHeightHalf + wallHeight, rayIndex, viewportHeight, "#A9A9A9")

      raySweepAngle += (camera.fov / viewportWidth.toDouble())
    }

    console.log("Viewport raycast in ${Date().getTime() - raycastStartMs}ms")
  }

  private fun drawWallTexture(rayIndex: Int, wallHeight: Double, textureXIndex: Int, texture: Texture, renderer: Renderer) {
    val yIncrement = (wallHeight * 2) / texture.height
    var y = (renderer.viewportHeight/2) - wallHeight

    for (i in 0 until texture.height) {
      // Extend y length by 0.1 to overlap strips slightly to avoid screen-door like effect
      renderer.drawLine(rayIndex, y, rayIndex, y + yIncrement + 0.1, texture.cssColourPixelList[textureXIndex + i * texture.width])
      y += yIncrement
    }
  }

}