Summary Files Commits Branches @commit @diff @stats
path: root / Year 2 / Computer Graphics with WebGL / Project / spaceship.js
diff options
context:
space:
mode:
Commit10889181959cada7e397ac377cb6d43dd977cf62 (patch)
Author
 Syndamia <kamen@syndamia.com>
2023-08-24 20:28:47 +0300
Committer
 Syndamia <kamen@syndamia.com>
2023-08-24 20:28:47 +0300
Filesa3569682b8e713ba7a5f561bc3e38d3fb1a97ac8 /Year 2/Computer Graphics with WebGL/Project/spaceship.js
uni-projects-10889181959cada7e397ac377cb6d43dd977cf62.tar
uni-projects-10889181959cada7e397ac377cb6d43dd977cf62.tar.gz
uni-projects-10889181959cada7e397ac377cb6d43dd977cf62.zip
Parentb9ee09f239f0b3e7014d36ac134e9c895092b19f (diff)
[WGL/P] Added basic UI when you aren't moving around
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();
});
}