diff --git a/src/ts/controller/inputs/mouse.ts b/src/ts/controller/inputs/mouse.ts index e279221d3..f3ee7e712 100644 --- a/src/ts/controller/inputs/mouse.ts +++ b/src/ts/controller/inputs/mouse.ts @@ -15,11 +15,14 @@ export class Mouse implements Input { deadAreaRadius = 100; private canvas: HTMLCanvasElement; + private onMouseEnterListeners: (() => void)[] = []; + private onMouseLeaveListeners: (() => void)[] = []; + constructor(canvas: HTMLCanvasElement, deadAreaRadius = 50) { this.deadAreaRadius = deadAreaRadius; this.canvas = canvas; - window.addEventListener("mousemove", (e) => { + document.addEventListener("mousemove", (e) => { this.dx = (e.x - this.x) / this.canvas.width; this.dy = (e.y - this.y) / this.canvas.height; @@ -29,6 +32,20 @@ export class Mouse implements Input { this.dxToCenter = e.x - this.canvas.width / 2; this.dyToCenter = e.y - this.canvas.height / 2; }); + + document.addEventListener("mouseenter", () => { + this.onMouseEnterListeners.forEach((listener) => listener()); + }); + document.addEventListener("mouseleave", () => { + this.onMouseLeaveListeners.forEach((listener) => listener()); + }); + } + + addOnMouseEnterListener(listener: () => void) { + this.onMouseEnterListeners.push(listener); + } + addOnMouseLeaveListener(listener: () => void) { + this.onMouseLeaveListeners.push(listener); } getRoll() { diff --git a/src/ts/controller/planetEngine.ts b/src/ts/controller/planetEngine.ts index 2185318e7..a42437573 100644 --- a/src/ts/controller/planetEngine.ts +++ b/src/ts/controller/planetEngine.ts @@ -29,6 +29,11 @@ import { SystemUI } from "../ui/systemUI"; import { BlackHole } from "../view/bodies/stellarObjects/blackHole"; import { ShipController } from "../spaceship/shipController"; +enum EngineState { + RUNNING, + PAUSED +} + export class PlanetEngine { // UI private readonly helmetOverlay: HelmetOverlay; @@ -50,6 +55,8 @@ export class PlanetEngine { private readonly collisionWorker = new CollisionWorker(); + private state = EngineState.RUNNING; + constructor() { this.helmetOverlay = new HelmetOverlay(); this.bodyEditor = new BodyEditor(); @@ -91,6 +98,14 @@ export class PlanetEngine { }); } + pause(): void { + this.state = EngineState.PAUSED; + } + + resume(): void { + this.state = EngineState.RUNNING; + } + /** * Toggles the star map * @throws Error if the star map is null @@ -151,6 +166,8 @@ export class PlanetEngine { }); this.starSystemScene.registerBeforeRender(() => { + if (this.state === EngineState.PAUSED) return; + const starSystemScene = this.getStarSystemScene(); const starSystem = this.getStarSystem(); const activeController = starSystemScene.getActiveController(); diff --git a/src/ts/index.ts b/src/ts/index.ts index d81c2b700..63aecb2ed 100644 --- a/src/ts/index.ts +++ b/src/ts/index.ts @@ -43,6 +43,13 @@ spaceshipController.addInput(keyboard); spaceshipController.addInput(gamepad); spaceshipController.addInput(mouse); +mouse.addOnMouseEnterListener(() => { + if (scene.getActiveController() === spaceshipController) engine.resume(); +}); +mouse.addOnMouseLeaveListener(() => { + if (scene.getActiveController() === spaceshipController) engine.pause(); +}); + scene.setActiveController(spaceshipController); engine.registerStarSystemUpdateCallback(() => {