Pointer-Lock: Maus per Klick im Spielfeld gefangen, ESC gibt sie frei
This commit is contained in:
1 parent
5d95465880
commit
5b83617dc7
2 files changed
+33
-3
No files matched your search
@@ -10,8 +10,10 @@ Komplettes Breakout in einer einzigen HTML-Datei — ohne Build, ohne Abhängigk
|
||||
|
||||
| Eingabe | Wirkung |
|
||||
|---|---|
|
||||
| Klick ins Spielfeld | Maus fangen (Pointer-Lock) und Ball abschießen |
|
||||
| `ESC` | Maus freigeben |
|
||||
| Maus / Touch | Paddle folgt |
|
||||
| Klick / Leertaste | Ball abschießen |
|
||||
| Leertaste | Ball abschießen |
|
||||
| ← → | Paddle per Tastatur |
|
||||
| `P` | Pause |
|
||||
| `R` | Neustart |
|
||||
|
||||
+30
-2
@@ -142,7 +142,7 @@
|
||||
<div id="head">
|
||||
<div>
|
||||
<div id="title">Breakout · Neon Clone</div>
|
||||
<div id="sub">Paddle folgt der Maus · Klick / Leertaste schießt · <kbd>P</kbd> pausiert · <kbd>R</kbd> Neustart</div>
|
||||
<div id="sub">Maus klickt ins Spielfeld und wird dort gefangen · <kbd>ESC</kbd> gibt sie frei · Klick / Leertaste schießt · <kbd>P</kbd> pausiert · <kbd>R</kbd> Neustart</div>
|
||||
</div>
|
||||
<div id="strip">
|
||||
<span class="pill" id="score">Punkte <b>0</b></span>
|
||||
@@ -897,17 +897,45 @@
|
||||
for (var i = 0; i < balls.length; i++) if (balls[i].attached) { launchBall(balls[i]); any = true; }
|
||||
return any;
|
||||
}
|
||||
/* Pointer-Lock: Klick fängt die Maus im Spielfeld ein, ESC gibt sie frei */
|
||||
var pointerLocked = false;
|
||||
function canvasRequestLock() {
|
||||
if (!canvas.requestPointerLock || pointerLocked) return;
|
||||
try { var r = canvas.requestPointerLock(); if (r && r.catch) r.catch(function () {}); } catch (e) {}
|
||||
}
|
||||
function canvasExitLock() {
|
||||
if (document.exitPointerLock) { try { document.exitPointerLock(); } catch (e) {} }
|
||||
}
|
||||
document.addEventListener('pointerlockchange', function () {
|
||||
pointerLocked = document.pointerLockElement === canvas;
|
||||
rectCache = null;
|
||||
wrap.style.cursor = pointerLocked ? 'none' : 'default';
|
||||
if (!pointerLocked && state === 'playing') {
|
||||
state = 'paused';
|
||||
showMsg('Maus freigegeben', 'Klick ins Spielfeld fängt die Maus wieder ein');
|
||||
}
|
||||
});
|
||||
document.addEventListener('pointerlockerror', function () { pointerLocked = false; });
|
||||
|
||||
wrap.addEventListener('pointermove', function (e) {
|
||||
if (state !== 'playing' && state !== 'ready' && state !== 'paused') return;
|
||||
paddle.x = clamp(localX(e.clientX) - paddle.w / 2, PAD, W - PAD - paddle.w);
|
||||
if (pointerLocked) {
|
||||
if (!rectCache) rectCache = canvas.getBoundingClientRect();
|
||||
var scale = rectCache.width ? W / rectCache.width : 1;
|
||||
paddle.x = clamp(paddle.x + (e.movementX || 0) * scale, PAD, W - PAD - paddle.w);
|
||||
} else {
|
||||
paddle.x = clamp(localX(e.clientX) - paddle.w / 2, PAD, W - PAD - paddle.w);
|
||||
}
|
||||
});
|
||||
wrap.addEventListener('pointerdown', function (e) {
|
||||
if (e.pointerType !== 'mouse' && wrap.setPointerCapture) { try { wrap.setPointerCapture(e.pointerId); } catch (err) {} }
|
||||
if (e.pointerType === 'mouse' && !pointerLocked) canvasRequestLock();
|
||||
if (state === 'playing') { launchAttached(); return; }
|
||||
if (state === 'paused') { state = 'playing'; hideMsg(); last = performance.now(); return; }
|
||||
startOrRestart();
|
||||
});
|
||||
window.addEventListener('keydown', function (e) {
|
||||
if (e.key === 'Escape') { canvasExitLock(); return; }
|
||||
if (e.key === 'ArrowLeft') keys.left = true;
|
||||
if (e.key === 'ArrowRight') keys.right = true;
|
||||
if (e.code === 'Space' || e.key === 'Enter') {
|
||||
|
||||
Reference in new issue
Block a user