Private
Public Access
1
0
Files
it232Abschied/static/js/input.js
Sebastian Unterschütz 8950b70378
Some checks failed
Dynamic Branch Deploy / build-and-deploy (push) Has been cancelled
fix README, SYNC, DATENSCHUTZ
2025-11-30 19:33:20 +01:00

103 lines
2.9 KiB
JavaScript

// ==========================================
// INPUT HANDLING (WEBSOCKET VERSION)
// ==========================================
function handleInput(action, active) {
if (isGameOver) {
if(active) location.reload();
return;
}
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 sendInput === "function") {
sendInput("input", "JUMP");
}
}
}
if (action === "DUCK") {
const wasCrouching = isCrouching;
isCrouching = active;
if (wasCrouching !== isCrouching) {
if (typeof sendInput === "function") {
sendInput("input", active ? "DUCK_START" : "DUCK_END");
}
}
}
}
// ==========================================
// EVENT LISTENERS
// ==========================================
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);
});
// 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);
}
}
});