Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/app/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const Config = {
MaxCameraDistance: 4000,
SlippyMapTransitionDuration: 400,
MinFreeCameraHeight: 10,
FreeCameraBikeHeight: 2,
CameraZoomSmoothing: 0.4,
CameraZoomSpeed: 0.0005,
CameraZoomTrackpadFactor: 4,
Expand All @@ -21,6 +22,10 @@ const Config = {
GroundCameraSpeedFast: 1200,
FreeCameraSpeed: 400,
FreeCameraSpeedFast: 1200,
BikeCameraSpeed: 10,
BikeCameraSpeedFast: 50,
BikeCameraNear: 1,
CameraNear: 10,
FreeCameraRotationSensitivity: 0.00002,
FreeCameraYawSpeed: 0.8,
FreeCameraPitchSpeed: 0.8,
Expand Down
27 changes: 23 additions & 4 deletions src/app/controls/FreeControlsNavigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import ControlsNavigator from "./ControlsNavigator";
import Vec3 from "~/lib/math/Vec3";
import Config from "../Config";
import MathUtils from "~/lib/math/MathUtils";
import {ControlsState} from "../systems/ControlsSystem";
import ControlsSystem, {ControlsState} from "../systems/ControlsSystem";
import PerspectiveCamera from "~/lib/core/PerspectiveCamera";
import TerrainHeightProvider from "~/app/terrain/TerrainHeightProvider";

export default class FreeControlsNavigator extends ControlsNavigator {
private readonly terrainHeightProvider: TerrainHeightProvider;
private readonly camera: PerspectiveCamera;
private readonly parent: ControlsSystem
private pitch: number = MathUtils.toRad(45);
private yaw: number = MathUtils.toRad(0);
private forwardKeyPressed: boolean = false;
Expand All @@ -21,16 +22,19 @@ export default class FreeControlsNavigator extends ControlsNavigator {
private yawMinusKeyPressed: boolean = false;
private yawPlusKeyPressed: boolean = false;
private pointerLocked: boolean = false;
private bikeMode: boolean = false;

public constructor(
element: HTMLElement,
camera: PerspectiveCamera,
terrainHeightProvider: TerrainHeightProvider
terrainHeightProvider: TerrainHeightProvider,
parent: ControlsSystem
) {
super(element);

this.camera = camera;
this.terrainHeightProvider = terrainHeightProvider;
this.parent = parent;

this.addEventListeners();
}
Expand Down Expand Up @@ -107,6 +111,9 @@ export default class FreeControlsNavigator extends ControlsNavigator {
case 'KeyF':
this.pitchPlusKeyPressed = true;
break;
case 'KeyB':
this.parent.switchBikeMode();
break;
}
}

Expand Down Expand Up @@ -216,11 +223,18 @@ export default class FreeControlsNavigator extends ControlsNavigator {
};
}

public setBikeMode(bikeMode: boolean): void {
this.bikeMode = bikeMode;
}

public update(deltaTime: number): void {
const mat = this.camera.matrixWorld.values;
const forwardDir = Vec3.normalize(new Vec3(mat[8], mat[9], mat[10]));
const rightDir = Vec3.normalize(new Vec3(mat[0], mat[1], mat[2]));
const speed = this.fastMovementKeyPressed ? Config.FreeCameraSpeedFast : Config.FreeCameraSpeed;
const fastSpeed = this.bikeMode ? Config.BikeCameraSpeedFast : Config.FreeCameraSpeedFast;
const slowSpeed = this.bikeMode ? Config.BikeCameraSpeed : Config.FreeCameraSpeed;

const speed = this.fastMovementKeyPressed ? fastSpeed : slowSpeed;

let movementDelta = new Vec3();
if (this.forwardKeyPressed) {
Expand All @@ -242,7 +256,12 @@ export default class FreeControlsNavigator extends ControlsNavigator {
this.camera.position.z += movementDelta.z;

const heightmapValue = this.getHeightmapValueAtPosition(this.camera.position.x, this.camera.position.z);
this.camera.position.y = Math.max(this.camera.position.y, heightmapValue + Config.MinFreeCameraHeight);
if (this.bikeMode) {
this.camera.position.y = heightmapValue + Config.FreeCameraBikeHeight;
} else {
this.camera.position.y = Math.max(this.camera.position.y, heightmapValue + Config.MinFreeCameraHeight);
}


this.camera.updateMatrix();

Expand Down
18 changes: 15 additions & 3 deletions src/app/controls/GroundControlsNavigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Vec3 from "~/lib/math/Vec3";
import Config from "../Config";
import MathUtils from "~/lib/math/MathUtils";
import CursorStyleSystem from "../systems/CursorStyleSystem";
import {ControlsState} from "../systems/ControlsSystem";
import ControlsSystem, {ControlsState} from "../systems/ControlsSystem";
import PerspectiveCamera from "~/lib/core/PerspectiveCamera";
import TerrainHeightProvider from "~/app/terrain/TerrainHeightProvider";
import FreeControlsNavigator from "~/app/controls/FreeControlsNavigator";
Expand All @@ -21,6 +21,7 @@ export default class GroundControlsNavigator extends ControlsNavigator {
private readonly camera: PerspectiveCamera;
private readonly cursorStyleSystem: CursorStyleSystem;
private readonly terrainHeightProvider: TerrainHeightProvider;
private readonly parent: ControlsSystem;
public target: Vec3 = new Vec3();
private direction: Vec3 = new Vec3();
public distance: number = 0;
Expand Down Expand Up @@ -48,18 +49,21 @@ export default class GroundControlsNavigator extends ControlsNavigator {
public switchToSlippy: boolean = false;
private transitionYawFrom: number = 0;
private transitionPitchFrom: number = 0;
private bikeMode: boolean = false;

public constructor(
element: HTMLElement,
camera: PerspectiveCamera,
cursorStyleSystem: CursorStyleSystem,
terrainHeightProvider: TerrainHeightProvider
terrainHeightProvider: TerrainHeightProvider,
parent: ControlsSystem
) {
super(element);

this.camera = camera;
this.cursorStyleSystem = cursorStyleSystem;
this.terrainHeightProvider = terrainHeightProvider;
this.parent = parent;

this.addEventListeners();
}
Expand Down Expand Up @@ -207,6 +211,9 @@ export default class GroundControlsNavigator extends ControlsNavigator {
case 'KeyF':
this.pitchMinusKeyPressed = true;
break;
case 'KeyB':
this.parent.switchBikeMode();
break;
}
}

Expand Down Expand Up @@ -335,8 +342,13 @@ export default class GroundControlsNavigator extends ControlsNavigator {
}
}

public setBikeMode(bikeMode: boolean): void {
this.bikeMode = bikeMode;
this.updateCameraProjectionMatrix();
}

private updateCameraProjectionMatrix(): void {
this.camera.near = 10;
this.camera.near = this.bikeMode ? Config.BikeCameraNear : Config.CameraNear;
this.camera.far = 100000;
this.camera.updateProjectionMatrix();
}
Expand Down
25 changes: 23 additions & 2 deletions src/app/systems/ControlsSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export enum NavigationMode {
export default class ControlsSystem extends System {
private readonly element: HTMLElement;
public mode: NavigationMode = NavigationMode.Ground;
public bikeMode: boolean = false;
private switchBikeModeOnUpdate: boolean = false;
private camera: PerspectiveCamera;
private tick: number = 0;
public target: Vec3 = new Vec3();
Expand Down Expand Up @@ -72,8 +74,8 @@ export default class ControlsSystem extends System {
const cursorStyleSystem = this.systemManager.getSystem(CursorStyleSystem);
const terrainHeightProvider = this.systemManager.getSystem(TerrainSystem).terrainHeightProvider;

this.groundNavigator = new GroundControlsNavigator(this.element, this.camera, cursorStyleSystem, terrainHeightProvider);
this.freeNavigator = new FreeControlsNavigator(this.element, this.camera, terrainHeightProvider);
this.groundNavigator = new GroundControlsNavigator(this.element, this.camera, cursorStyleSystem, terrainHeightProvider, this);
this.freeNavigator = new FreeControlsNavigator(this.element, this.camera, terrainHeightProvider, this);
this.slippyNavigator = new SlippyControlsNavigator(this.element, this.camera, cursorStyleSystem, terrainHeightProvider);

this.activeNavigator = this.slippyNavigator;
Expand Down Expand Up @@ -182,6 +184,10 @@ export default class ControlsSystem extends System {
}
}

public switchBikeMode(): void {
this.switchBikeModeOnUpdate = true;
}

private mouseDownEvent(e: MouseEvent): void {
e.preventDefault();

Expand Down Expand Up @@ -250,6 +256,21 @@ export default class ControlsSystem extends System {

this.activeNavigator.update(deltaTime);

if (this.switchBikeModeOnUpdate) {
this.switchBikeModeOnUpdate = false;

this.bikeMode = !this.bikeMode;

this.freeNavigator.setBikeMode(this.bikeMode);
this.groundNavigator.setBikeMode(this.bikeMode);

this.mode = NavigationMode.Free;
this.activeNavigator = this.freeNavigator;
this.groundNavigator.disable();
this.freeNavigator.enable();
this.freeNavigator.syncWithCamera(this.groundNavigator);
}

if (this.groundNavigator.switchToSlippy) {
this.groundNavigator.switchToSlippy = false;

Expand Down
3 changes: 3 additions & 0 deletions src/app/ui/components/InfoModalPanel/InfoModalPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const KeysConfig: {keys: JSX.Element; desc: string}[] = [
}, {
keys: <Key text='Tab'/>,
desc: 'Toggle between ground camera mode (default) and free camera mode'
}, {
keys: <Key text='B'/>,
desc: 'Toggle between bike and free camera mode'
}, {
keys: <><Key text='Ctrl'/> + <Key text='P'/></>,
desc: 'Purge all loaded tiles'
Expand Down