fix: proper update with locking

This commit is contained in:
stitchy 2024-12-26 04:12:38 +00:00
parent b63b7d2b3f
commit 77c7b9cade
Signed by: stitchy
SSH key fingerprint: SHA256:yz2SoxdnY67tfY5Jzb0f2v8f5W3o/IF359kbcquWip8

View file

@ -10,23 +10,25 @@ let mouse_pos = [0, 0];
// Update the Camera position // Update the Camera position
function updateCamera(gl, program) { function updateCamera(gl, program) {
if (keymap.get("w")) { if (document.pointerLockElement) {
camera_pos[2] += 0.1; if (keymap.get("w")) {
} camera_pos[2] += 0.1;
if (keymap.get("a")) { }
camera_pos[0] += -0.1; if (keymap.get("a")) {
} camera_pos[0] += -0.1;
if (keymap.get("s")) { }
camera_pos[2] += -0.1; if (keymap.get("s")) {
} camera_pos[2] += -0.1;
if (keymap.get("d")) { }
camera_pos[0] += 0.1; if (keymap.get("d")) {
} camera_pos[0] += 0.1;
if (keymap.get("e")) { }
camera_pos[1] += 0.1; if (keymap.get("e")) {
} camera_pos[1] += 0.1;
if (keymap.get("q")) { }
camera_pos[1] += -0.1; if (keymap.get("q")) {
camera_pos[1] += -0.1;
}
} }
// Position // Position
@ -51,15 +53,15 @@ function draw(gl, program) {
TIME += 1 / 60; TIME += 1 / 60;
gl.uniform1f(loc, TIME); gl.uniform1f(loc, TIME);
if (document.pointerLockElement) { updateCamera(gl, program);
updateCamera(gl, program);
}
// Draw the frame recursively on next frame // Draw the frame recursively on next frame
gl.drawArrays(gl.TRIANGLES, 0, 6); gl.drawArrays(gl.TRIANGLES, 0, 6);
requestAnimationFrame(() => draw(gl, program)); requestAnimationFrame(() => draw(gl, program));
} }
//** MAIN PROGRAM **//
// Initialize the shader // Initialize the shader
const [gl, program, canvas] = await initShader(CANVAS_WIDTH, CANVAS_HEIGHT); const [gl, program, canvas] = await initShader(CANVAS_WIDTH, CANVAS_HEIGHT);
@ -85,5 +87,4 @@ canvas.addEventListener("click", async () => {
}); });
// Update loop // Update loop
updateCamera(gl, program);
draw(gl, program); draw(gl, program);