|
| 1 | +// FIXME: make key bindings configurable, add arrow keys |
| 2 | + |
| 3 | +export default class CesiumWalk { |
| 4 | + /** |
| 5 | + * @param {import('cesium').Viewer} viewer |
| 6 | + * @param {number} [speed=1.6] Walk speed in meters per second. |
| 7 | + * @param {number} [height=2.0] Height of the camera above the terrain. |
| 8 | + */ |
| 9 | + constructor(viewer, speed = 1.6, height = 2.0) { |
| 10 | + /** |
| 11 | + * @type {import('cesium').Viewer} |
| 12 | + */ |
| 13 | + this.viewer = viewer; |
| 14 | + |
| 15 | + /** |
| 16 | + * @type {number} |
| 17 | + */ |
| 18 | + this.speed = speed; |
| 19 | + |
| 20 | + /** |
| 21 | + * @type {number} |
| 22 | + */ |
| 23 | + this.height = height; |
| 24 | + |
| 25 | + /** |
| 26 | + * @type {boolean} |
| 27 | + */ |
| 28 | + this.active_ = false; |
| 29 | + |
| 30 | + /** |
| 31 | + * @type {number} |
| 32 | + */ |
| 33 | + this.lastTick; |
| 34 | + |
| 35 | + /** |
| 36 | + * @type {{forward: boolean, left: boolean, backward: boolean, right: boolean}} |
| 37 | + */ |
| 38 | + this.buttons = { |
| 39 | + forward: false, |
| 40 | + left: false, |
| 41 | + backward: false, |
| 42 | + right: false, |
| 43 | + }; |
| 44 | + |
| 45 | + /** |
| 46 | + * @type {function(KeyboardEvent): void} |
| 47 | + */ |
| 48 | + this.handleKeyUpDownFunction = this.handleKeyUpDown.bind(this); |
| 49 | + |
| 50 | + /** |
| 51 | + * @type {function(): void} |
| 52 | + */ |
| 53 | + this.handleTickFunction = this.handleTick.bind(this); |
| 54 | + } |
| 55 | + |
| 56 | + get active() { |
| 57 | + return this.active_; |
| 58 | + } |
| 59 | + |
| 60 | + set active(active) { |
| 61 | + this.active_ = active; |
| 62 | + if (this.active_) { |
| 63 | + document.addEventListener("keydown", this.handleKeyUpDownFunction); |
| 64 | + document.addEventListener("keyup", this.handleKeyUpDownFunction); |
| 65 | + this.viewer.clock.onTick.addEventListener(this.handleTickFunction); |
| 66 | + this.clampCameraToTerrain(); |
| 67 | + } else { |
| 68 | + document.removeEventListener("keydown", this.handleKeyUpDownFunction); |
| 69 | + document.removeEventListener("keyup", this.handleKeyUpDownFunction); |
| 70 | + this.viewer.clock.onTick.removeEventListener(this.handleTickFunction); |
| 71 | + } |
| 72 | + this.enableNavigation(!this.active_); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param {KeyboardEvent} event |
| 77 | + */ |
| 78 | + handleKeyUpDown(event) { |
| 79 | + const pressed = event.type === "keydown"; |
| 80 | + switch (event.key) { |
| 81 | + case "w": |
| 82 | + this.buttons.forward = pressed; |
| 83 | + break; |
| 84 | + case "a": |
| 85 | + this.buttons.left = pressed; |
| 86 | + break; |
| 87 | + case "s": |
| 88 | + this.buttons.backward = pressed; |
| 89 | + break; |
| 90 | + case "d": |
| 91 | + this.buttons.right = pressed; |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + handleTick() { |
| 97 | + const timestamp = performance.now(); |
| 98 | + if (this.needsTick()) { |
| 99 | + const camera = this.viewer.camera; |
| 100 | + const deltaTime = timestamp - this.lastTick; |
| 101 | + |
| 102 | + const distance = (this.speed * deltaTime) / 1000; |
| 103 | + |
| 104 | + if (this.buttons.forward) { |
| 105 | + camera.moveForward(distance); |
| 106 | + } |
| 107 | + if (this.buttons.left) { |
| 108 | + camera.moveLeft(distance); |
| 109 | + } |
| 110 | + if (this.buttons.backward) { |
| 111 | + camera.moveBackward(distance); |
| 112 | + } |
| 113 | + if (this.buttons.right) { |
| 114 | + camera.moveRight(distance); |
| 115 | + } |
| 116 | + this.clampCameraToTerrain(); |
| 117 | + } |
| 118 | + |
| 119 | + this.lastTick = timestamp; |
| 120 | + } |
| 121 | + |
| 122 | + clampCameraToTerrain() { |
| 123 | + const camera = this.viewer.camera; |
| 124 | + const terrainHeight = this.viewer.scene.globe.getHeight( |
| 125 | + camera.positionCartographic |
| 126 | + ); |
| 127 | + const cameraHeight = camera.positionCartographic.height; |
| 128 | + |
| 129 | + camera.moveDown(cameraHeight - terrainHeight - this.height); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * @return {boolean} |
| 134 | + */ |
| 135 | + needsTick() { |
| 136 | + return ( |
| 137 | + this.buttons.forward || |
| 138 | + this.buttons.left || |
| 139 | + this.buttons.backward || |
| 140 | + this.buttons.right |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Enable or disable cesium navigation interactions. |
| 146 | + * @param {boolean} enable |
| 147 | + */ |
| 148 | + enableNavigation(enable) { |
| 149 | + const controller = this.viewer.scene.screenSpaceCameraController; |
| 150 | + controller.enableTranslate = enable; |
| 151 | + controller.enableZoom = enable; |
| 152 | + controller.enableRotate = enable; |
| 153 | + controller.enableTilt = enable; |
| 154 | + } |
| 155 | +} |
0 commit comments