Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Computer Graphics with WebGL / Project / spaceship.js
diff options
context:
space:
mode:
1 files changed, 24 insertions, 9 deletions
-rw-r--r--Year 2/Computer Graphics with WebGL/Project/spaceship.js33
diff --git a/Year 2/Computer Graphics with WebGL/Project/spaceship.js b/Year 2/Computer Graphics with WebGL/Project/spaceship.js
index ca7de7f..075ae4d 100644
--- a/Year 2/Computer Graphics with WebGL/Project/spaceship.js
+++ b/Year 2/Computer Graphics with WebGL/Project/spaceship.js
@@ -1,3 +1,5 @@
+// This file contains navigation and "pause" menu configuration
+
/*
* Helpers
*/
@@ -67,6 +69,8 @@ var up = new SphericalCoord(0, 0);
// this vector can be calculated from forward and up, but this way we improve speed
var left = new SphericalCoord(Math.PI / 2, Math.PI / 2);
+var isPaused = true;
+
/*
* Navigation keys
*/
@@ -80,7 +84,9 @@ var keyAction = [
() => { pos = vecSum(pos, left.toCartesian()); }, // 'a'
() => { pos = vecSub(pos, left.toCartesian()); }, // 'd'
];
-addEventListener("keydown", (event) => {
+document.addEventListener("keydown", (event) => {
+ if (isPaused) return;
+
switch (event.key) {
case ' ' : pressedKeys[0] = true; break;
case 'Shift': pressedKeys[1] = true; break;
@@ -90,7 +96,9 @@ addEventListener("keydown", (event) => {
case 'd' : pressedKeys[5] = true; break;
}
});
-addEventListener("keyup", (event) => {
+document.addEventListener("keyup", (event) => {
+ if (isPaused) return;
+
switch (event.key) {
case ' ' : pressedKeys[0] = false; break;
case 'Shift': pressedKeys[1] = false; break;
@@ -105,6 +113,8 @@ addEventListener("keyup", (event) => {
* Mouse movement
*/
function updateLookingDirection(e) {
+ if (isPaused) return;
+
var vertical = (e.movementY / canvas.offsetHeight) * ROTATION_SPEED;
var horizontal = (-e.movementX / canvas.offsetWidth) * ROTATION_SPEED;
@@ -112,17 +122,22 @@ function updateLookingDirection(e) {
up.addAngles(vertical, horizontal);
left.addAngles(vertical, horizontal);
}
+document.addEventListener('mousemove', updateLookingDirection);
+document.addEventListener('pointerlockchange', () => {
+ isPaused = document.pointerLockElement !== canvas;
+ menu.classList.toggle('hide');
+});
function mouseInit() {
+ paused = document.getElementById('paused');
+
canvas.addEventListener('click', async () => {
- await canvas.requestPointerLock();
+ if (isPaused)
+ await canvas.requestPointerLock();
});
- document.addEventListener('pointerlockchange', () => {
- if (document.pointerLockElement === canvas) {
- document.addEventListener("mousemove", updateLookingDirection);
- } else {
- document.removeEventListener("mousemove", updateLookingDirection);
- }
+ paused.addEventListener('click', async () => {
+ if (isPaused)
+ await canvas.requestPointerLock();
});
}