blob: 510f1a08a70761368c3dcd9a06e006027cb44885 (
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
|
import kotlinx.browser.document
import org.w3c.dom.HTMLButtonElement
class CameraController(
private val camera: Camera,
private val moveSpeed: Double,
private val rotateSpeed: Int,
private val afterInput: () -> Unit
) {
init {
document.onkeydown = { keyHandler(it.code) }
(document.getElementById("up") as HTMLButtonElement).onclick = { moveForward() }
(document.getElementById("down") as HTMLButtonElement).onclick = { moveBack() }
(document.getElementById("left") as HTMLButtonElement).onclick = { rotateAntiClockwise() }
(document.getElementById("right") as HTMLButtonElement).onclick = { rotateClockwise() }
}
private fun keyHandler(code: String) {
when (code) {
"KeyW" -> moveForward()
"KeyS" -> moveBack()
"KeyA" -> rotateAntiClockwise()
"KeyD" -> rotateClockwise()
}
}
private fun moveForward() {
val cameraCos = camera.rotation.cosine() * moveSpeed
val cameraSin = camera.rotation.sine() * moveSpeed
camera.xPos += cameraCos
camera.yPos += cameraSin
afterInput()
}
private fun moveBack() {
val cameraCos = camera.rotation.cosine() * moveSpeed
val cameraSin = camera.rotation.sine() * moveSpeed
camera.xPos -= cameraCos
camera.yPos -= cameraSin
afterInput()
}
private fun rotateClockwise() {
camera.rotation += rotateSpeed
afterInput()
}
private fun rotateAntiClockwise() {
camera.rotation -= rotateSpeed
afterInput()
}
}
|