diff --git a/static/js/input.js b/static/js/input.js index ee9f318..7e94aef 100644 --- a/static/js/input.js +++ b/static/js/input.js @@ -1,5 +1,5 @@ // ========================================== -// INPUT HANDLING (WEBSOCKET VERSION) +// INPUT HANDLING // ========================================== function handleInput(action, active) { @@ -8,24 +8,22 @@ function handleInput(action, active) { return; } + // JUMP if (action === "JUMP" && active) { if (player.grounded && !isCrouching) { - player.vy = JUMP_POWER; player.grounded = false; - playSound('jump'); - spawnParticles(player.x + 15, player.y + 50, 'dust', 5); // Staubwolke an den Füßen + if (typeof playSound === 'function') playSound('jump'); + if (typeof spawnParticles === 'function') spawnParticles(player.x + 15, player.y + 50, 'dust', 5); - if (typeof sendInput === "function") { - sendInput("input", "JUMP"); - } + if (typeof sendInput === "function") sendInput("input", "JUMP"); } } + // DUCK if (action === "DUCK") { const wasCrouching = isCrouching; - isCrouching = active; if (wasCrouching !== isCrouching) { @@ -37,67 +35,75 @@ function handleInput(action, active) { } // ========================================== -// EVENT LISTENERS +// EVENT LISTENERS (KEYBOARD) // ========================================== - - window.addEventListener('keydown', (e) => { - if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') return; - if (e.code === 'Space' || e.code === 'ArrowUp') handleInput("JUMP", true); if (e.code === 'ArrowDown' || e.code === 'KeyS') handleInput("DUCK", true); - if (e.code === 'F9') { - e.preventDefault(); - console.log("🐞 Fordere Debug-Daten vom Server an..."); - if (typeof sendInput === "function") { - if (socket && socket.readyState === WebSocket.OPEN) { - socket.send(JSON.stringify({ type: "debug" })); - } - } - } }); window.addEventListener('keyup', (e) => { if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') return; - if (e.code === 'ArrowDown' || e.code === 'KeyS') handleInput("DUCK", false); }); -// Maus / Touch (Einfach) window.addEventListener('mousedown', (e) => { - // Nur Linksklick und nur auf dem Canvas - if (e.target === canvas && e.button === 0) handleInput("JUMP", true); + if (e.target.tagName === 'BUTTON' || e.target.tagName === 'INPUT') return; + if (e.button === 0) handleInput("JUMP", true); }); -// Touch (Swipe Gesten) -let touchStartY = 0; +// ========================================== +// TOUCH HANDLING (INSTANT RESPONSE) +// ========================================== +let touchStartY = 0; +let isSwiping = false; + +// 1. TOUCH START -> SOFORT SPRINGEN window.addEventListener('touchstart', (e) => { - if(e.target === canvas) { - e.preventDefault(); - touchStartY = e.touches[0].clientY; + // UI Buttons ignorieren + if (e.target.tagName === 'BUTTON' || e.target.tagName === 'INPUT') return; + e.preventDefault(); + + touchStartY = e.touches[0].clientY; + isSwiping = false; + + // FIX: Wir springen SOFORT, ohne zu warten! + // Das gibt das "Snappy" Gefühl. + handleInput("JUMP", true); + +}, { passive: false }); + +// 2. TOUCH MOVE -> DUCKEN ERKENNEN +window.addEventListener('touchmove', (e) => { + if (e.target.tagName === 'BUTTON') return; + e.preventDefault(); + + if (isSwiping) return; // Nur einmal pro Swipe auslösen + + const currentY = e.touches[0].clientY; + const diff = currentY - touchStartY; + + // Wenn wir mehr als 30px nach UNTEN wischen... + if (diff > 30) { + // ... korrigieren wir den Sprung in ein Ducken! + // Da wir im Spiel eine Physik haben, wo Ducken in der Luft + // zu "Fast Fall" (schnellem Fallen) führt, fühlt sich das gut an. + + handleInput("DUCK", true); + isSwiping = true; // Sperren bis zum nächsten Touch + + // Automatisch aufstehen nach kurzer Zeit + setTimeout(() => handleInput("DUCK", false), 800); } }, { passive: false }); +// 3. TOUCH END -> RESET +// (Hier müssen wir nichts mehr tun, da der Jump schon beim Start passiert ist) window.addEventListener('touchend', (e) => { - if(e.target === canvas) { - e.preventDefault(); - const touchEndY = e.changedTouches[0].clientY; - const diff = touchEndY - touchStartY; - - // Nach oben wischen oder Tippen = Sprung - if (diff < -30) { - handleInput("JUMP", true); - } - // Nach unten wischen = Ducken (kurz) - else if (diff > 30) { - handleInput("DUCK", true); - setTimeout(() => handleInput("DUCK", false), 800); - } - // Einfaches Tippen (wenig Bewegung) = Sprung - else if (Math.abs(diff) < 10) { - handleInput("JUMP", true); - } - } -}); \ No newline at end of file + if (e.target.tagName === 'BUTTON') return; + e.preventDefault(); + // Ggf. Ducken beenden falls man gedrückt hält? + // Hier lassen wir den Timeout regeln. +}, { passive: false }); \ No newline at end of file