All checks were successful
Dynamic Branch Deploy / build-and-deploy (push) Successful in 2m18s
114 lines
3.6 KiB
JavaScript
114 lines
3.6 KiB
JavaScript
// ==========================================
|
|
// INPUT HANDLING (WEBSOCKET VERSION)
|
|
// ==========================================
|
|
|
|
function handleInput(action, active) {
|
|
// 1. Game Over Reset
|
|
if (isGameOver) {
|
|
if(active) location.reload();
|
|
return;
|
|
}
|
|
|
|
// 2. JUMP LOGIK
|
|
if (action === "JUMP" && active) {
|
|
// Wir prüfen lokal, ob wir springen dürfen (Client Prediction)
|
|
if (player.grounded && !isCrouching) {
|
|
|
|
// A. Sofort lokal anwenden (damit es sich direkt anfühlt)
|
|
player.vy = JUMP_POWER;
|
|
player.grounded = false;
|
|
|
|
playSound('jump');
|
|
spawnParticles(player.x + 15, player.y + 50, 'dust', 5); // Staubwolke an den Füßen
|
|
|
|
// B. An Server senden ("Ich habe JETZT gedrückt")
|
|
// Die Funktion sendInput ist in network.js definiert
|
|
if (typeof sendInput === "function") {
|
|
sendInput("input", "JUMP");
|
|
}
|
|
}
|
|
}
|
|
|
|
// 3. DUCK LOGIK
|
|
if (action === "DUCK") {
|
|
// Status merken, um unnötiges Senden zu vermeiden
|
|
const wasCrouching = isCrouching;
|
|
|
|
// A. Lokal anwenden
|
|
isCrouching = active;
|
|
|
|
// B. An Server senden (State Change: Start oder Ende)
|
|
if (wasCrouching !== isCrouching) {
|
|
if (typeof sendInput === "function") {
|
|
sendInput("input", active ? "DUCK_START" : "DUCK_END");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// ==========================================
|
|
// EVENT LISTENERS
|
|
// ==========================================
|
|
|
|
// Tastatur
|
|
window.addEventListener('keydown', (e) => {
|
|
// Ignorieren, wenn User gerade Name in Highscore tippt
|
|
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") {
|
|
// Wir senden ein manuelles Paket, da sendInput meist nur für Game-Inputs ist
|
|
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);
|
|
});
|
|
|
|
// Touch (Swipe Gesten)
|
|
let touchStartY = 0;
|
|
|
|
window.addEventListener('touchstart', (e) => {
|
|
if(e.target === canvas) {
|
|
e.preventDefault();
|
|
touchStartY = e.touches[0].clientY;
|
|
}
|
|
}, { passive: false });
|
|
|
|
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);
|
|
}
|
|
}
|
|
}); |