Entfernung von Debugging-Ausgaben und Optimierung der Spiel-Logik im Taxi-Spiel

Änderungen:
- Entfernen von Konsolenausgaben zur Fahrzeug-Generierung, -Bewegung und -Kollision, um die Lesbarkeit des Codes zu verbessern.
- Anpassung der Spiel-Update-Logik zur Verwendung von requestAnimationFrame anstelle von setTimeout für eine flüssigere Animation.
- Vereinheitlichung der Timer-Logik zur Aktualisierung von Passagier-Timern und Minimap-Darstellung.

Diese Anpassungen verbessern die Performance und Benutzererfahrung des Spiels durch reduzierte Debugging-Ausgaben und optimierte Animationslogik.
This commit is contained in:
Torsten Schulz (local)
2025-10-20 15:38:57 +02:00
parent bc3c765948
commit a542d99eb9

View File

@@ -1439,7 +1439,6 @@ export default {
// 20% Wahrscheinlichkeit pro Sekunde // 20% Wahrscheinlichkeit pro Sekunde
if (!this.isPaused && Math.random() < this.carSpawnProbability) { if (!this.isPaused && Math.random() < this.carSpawnProbability) {
console.log('[🚗 GENERATION] Attempting to spawn car, current cars:', this.cars.length);
this.spawnCar(now); this.spawnCar(now);
} }
}, },
@@ -1448,14 +1447,12 @@ export default {
spawnCar(now = Date.now()) { spawnCar(now = Date.now()) {
// Begrenze die Anzahl der Autos (TEST: nur 1 Auto) // Begrenze die Anzahl der Autos (TEST: nur 1 Auto)
if (this.cars.length >= 1) { if (this.cars.length >= 1) {
console.log('[🚗 SPAWN] Max cars reached:', this.cars.length);
return; return;
} }
// Spawne Auto auf einer befahrbaren Position // Spawne Auto auf einer befahrbaren Position
const spawnPosition = this.getRandomCarSpawnPosition(); const spawnPosition = this.getRandomCarSpawnPosition();
if (!spawnPosition) { if (!spawnPosition) {
console.log('[🚗 SPAWN] No valid spawn position found');
return; // Keine gültige Spawn-Position gefunden return; // Keine gültige Spawn-Position gefunden
} }
@@ -1478,11 +1475,7 @@ export default {
targetLane: null // Zielspur basierend auf Zielrichtung targetLane: null // Zielspur basierend auf Zielrichtung
}; };
console.log(`[🚗 CREATE] New car created - ID: ${car.id}, Position: x=${car.x.toFixed(2)}, y=${car.y.toFixed(2)}, direction=${car.direction}, angle=${car.angle.toFixed(2)}, speed=${car.speed.toFixed(2)}`);
this.cars.push(car); this.cars.push(car);
console.log(`[🚗 CREATE] Added car with ID: ${car.id}, Total cars now: ${this.cars.length}`);
console.log(`[🚗 CREATE] Car details: x=${car.x.toFixed(2)}, y=${car.y.toFixed(2)}, direction=${car.direction}, speed=${car.speed.toFixed(2)}`);
}, },
// Ermittelt erlaubte Spawn-Richtungen basierend auf dem aktuellen Tile-Typ // Ermittelt erlaubte Spawn-Richtungen basierend auf dem aktuellen Tile-Typ
@@ -1524,7 +1517,6 @@ export default {
// Finde eine zufällige befahrbare Spawn-Position für ein Auto // Finde eine zufällige befahrbare Spawn-Position für ein Auto
getRandomCarSpawnPosition() { getRandomCarSpawnPosition() {
if (!this.currentMap || !this.currentMap.tiles) { if (!this.currentMap || !this.currentMap.tiles) {
console.log('[🚗 SPAWN] No current map or tiles');
return null; return null;
} }
@@ -1534,11 +1526,9 @@ export default {
// Erlaubte Spawn-Richtungen für den aktuellen Tile-Typ ermitteln // Erlaubte Spawn-Richtungen für den aktuellen Tile-Typ ermitteln
const allowedDirections = this.getAllowedSpawnDirections(); const allowedDirections = this.getAllowedSpawnDirections();
console.log('[🚗 SPAWN] Tile type:', tileType, 'Allowed directions:', allowedDirections);
console.log('[🚗 SPAWN] Tile size:', tileSize);
if (allowedDirections.length === 0) { if (allowedDirections.length === 0) {
console.log('[🚗 SPAWN] No allowed spawn directions');
return null; // Keine erlaubten Spawn-Richtungen return null; // Keine erlaubten Spawn-Richtungen
} }
@@ -1570,14 +1560,13 @@ export default {
return null; return null;
} }
console.log('[🚗 SPAWN] Selected direction:', randomDirection);
console.log('[🚗 SPAWN] Spawn position data:', spawnPos);
// Konvertiere relative Koordinaten zu absoluten Pixeln // Konvertiere relative Koordinaten zu absoluten Pixeln
let x = (spawnPos.relativeX != null ? spawnPos.relativeX : (spawnPos.pickRelX ? spawnPos.pickRelX() : 0.5)) * tileSize; let x = (spawnPos.relativeX != null ? spawnPos.relativeX : (spawnPos.pickRelX ? spawnPos.pickRelX() : 0.5)) * tileSize;
let y = (spawnPos.relativeY != null ? spawnPos.relativeY : (spawnPos.pickRelY ? spawnPos.pickRelY() : 0.5)) * tileSize; let y = (spawnPos.relativeY != null ? spawnPos.relativeY : (spawnPos.pickRelY ? spawnPos.pickRelY() : 0.5)) * tileSize;
console.log('[🚗 SPAWN] Before adjustment - x:', x.toFixed(2), 'y:', y.toFixed(2));
// Passe Koordinaten an: Die Spawn-Position ist die Straßenlinie, aber wir brauchen die linke obere Ecke des Autos // Passe Koordinaten an: Die Spawn-Position ist die Straßenlinie, aber wir brauchen die linke obere Ecke des Autos
// Das Auto muss auf der Linie zentriert sein // Das Auto muss auf der Linie zentriert sein
@@ -1598,8 +1587,7 @@ export default {
break; break;
} }
console.log('[🚗 SPAWN] After adjustment - x:', x.toFixed(2), 'y:', y.toFixed(2));
console.log('[🚗 SPAWN] Direction:', randomDirection, 'Absolute:', { x: x.toFixed(2), y: y.toFixed(2) });
// laneCoord speichert die Zielspur (Mitte) auf der Querachse // laneCoord speichert die Zielspur (Mitte) auf der Querachse
// Verwende die bereits berechneten absoluten x/y, um exakt dieselbe Spur zu halten // Verwende die bereits berechneten absoluten x/y, um exakt dieselbe Spur zu halten
@@ -1658,10 +1646,8 @@ export default {
} }
if (onRoadPoints.length > 0) { if (onRoadPoints.length > 0) {
console.log(`[🚗 ON_ROAD] Car at x=${car.x.toFixed(2)}, y=${car.y.toFixed(2)} - ON ROAD (points: ${onRoadPoints.join(', ')})`);
return true; return true;
} else { } else {
console.log(`[🚗 OFF_ROAD] Car at x=${car.x.toFixed(2)}, y=${car.y.toFixed(2)}, centerX=${centerX.toFixed(2)}, centerY=${centerY.toFixed(2)} - OFF ROAD (no points on road)`);
return false; return false;
} }
}, },
@@ -1730,10 +1716,10 @@ export default {
const maxAge = 45000; // 45 Sekunden const maxAge = 45000; // 45 Sekunden
const initialCarCount = this.cars.length; const initialCarCount = this.cars.length;
console.log(`[🚗 UPDATE] Starting with ${initialCarCount} cars`);
this.cars = this.cars.filter((car, index) => { this.cars = this.cars.filter((car, index) => {
console.log(`[🚗 UPDATE] Processing car ${index}: x=${car.x.toFixed(2)}, y=${car.y.toFixed(2)}, age=${(now - car.createdAt)}ms`);
// Entferne alte Autos // Entferne alte Autos
// Bewege das Auto // Bewege das Auto
@@ -1741,7 +1727,7 @@ export default {
// Entferne Autos mit speed=0 (off-road) // Entferne Autos mit speed=0 (off-road)
if (car.speed === 0) { if (car.speed === 0) {
console.log(`[🚗 UPDATE] Removing car ${index} - speed=0 (off-road)`);
return false; return false;
} }
@@ -1758,20 +1744,19 @@ export default {
// - Obere Kante unter dem Canvas: y > tileSize // - Obere Kante unter dem Canvas: y > tileSize
if (car.x + carWidth < 0 || car.x > tileSize || if (car.x + carWidth < 0 || car.x > tileSize ||
car.y + carHeight < 0 || car.y > tileSize) { car.y + carHeight < 0 || car.y > tileSize) {
console.log(`[🚗 UPDATE] Removing car ${index} - completely out of bounds (x=${car.x.toFixed(2)}, y=${car.y.toFixed(2)})`);
return false; return false;
} }
console.log(`[🚗 UPDATE] Keeping car ${index}`);
return true; return true;
}); });
console.log(`[🚗 UPDATE] Finished with ${this.cars.length} cars (removed ${initialCarCount - this.cars.length})`);
}, },
// Aktualisiere die Bewegung eines einzelnen Autos (vereinfachte Taxi-Logik) // Aktualisiere die Bewegung eines einzelnen Autos (vereinfachte Taxi-Logik)
updateCarMovement(car) { updateCarMovement(car) {
console.log(`[🚗 MOVE] Car ID: ${car.id} - Current: x=${car.x.toFixed(2)}, y=${car.y.toFixed(2)}, direction=${car.direction}`);
// 1) An Abbiegepunkt? Dann (ggf.) vorgegebene Zielrichtung übernehmen // 1) An Abbiegepunkt? Dann (ggf.) vorgegebene Zielrichtung übernehmen
const forcedDir = this.shouldCarTurnAtIntersection(car); const forcedDir = this.shouldCarTurnAtIntersection(car);
@@ -1781,7 +1766,7 @@ export default {
let preferred = forcedDir; let preferred = forcedDir;
if (preferred && preferred !== car.direction) { if (preferred && preferred !== car.direction) {
console.log(`[🚗 MOVE] Car ID: ${car.id} - Turning on ${tileType} from ${car.direction} to ${preferred}`);
// Beim Abbiegen: neue Spur bestimmen // Beim Abbiegen: neue Spur bestimmen
car.direction = preferred; car.direction = preferred;
if (preferred === 'left' || preferred === 'right') { if (preferred === 'left' || preferred === 'right') {
@@ -1828,16 +1813,18 @@ export default {
newX = centerX - car.width / 2; newX = centerX - car.width / 2;
} }
// 3) Position übernehmen // 3) Position übernehmen (vorher letzte Position merken)
car.lastPosition = { x: car.x, y: car.y };
car.x = newX; car.x = newX;
car.y = newY; car.y = newY;
console.log(`[🚗 MOVE] Car ID: ${car.id} - Moved to: x=${car.x.toFixed(2)}, y=${car.y.toFixed(2)}`);
// 4) Straße prüfen bleibt es auf befahrbarer Fläche? // 4) Straße prüfen bleibt es auf befahrbarer Fläche?
if (!this.isCarOnRoad(car)) { if (!this.isCarOnRoad(car)) {
console.log(`[🚗 MOVE] Car ID: ${car.id} - OFF ROAD! Stopping car.`); // Keine harte Korrektur und kein Stoppen: Autos dürfen weiterfahren,
car.speed = 0; // Entfernen erfolgt erst, wenn das Fahrzeug komplett außerhalb ist.
// Optional könnten wir hier eine leichte Korrektur zur Spurmitte einbauen.
} }
}, },
getPassengerTimeLeft(passenger) { getPassengerTimeLeft(passenger) {
@@ -1964,8 +1951,13 @@ export default {
// Stoppe bestehende Game-Loop falls vorhanden // Stoppe bestehende Game-Loop falls vorhanden
if (this.gameLoop) { if (this.gameLoop) {
clearTimeout(this.gameLoop); clearTimeout(this.gameLoop);
this.gameLoop = null;
} }
this.gameLoop = setTimeout(this.update, 62); if (this.rafId) {
cancelAnimationFrame(this.rafId);
this.rafId = null;
}
this.rafId = requestAnimationFrame(this.update);
}, },
update() { update() {
@@ -1977,9 +1969,7 @@ export default {
// Debug: Log every 60 frames (about once per second) // Debug: Log every 60 frames (about once per second)
if (!this._frameCount) this._frameCount = 0; if (!this._frameCount) this._frameCount = 0;
this._frameCount++; this._frameCount++;
if (this._frameCount % 60 === 0) { // (Logs entfernt)
console.log(`[🔄 UPDATE] Frame ${this._frameCount}, gameRunning: ${this.gameRunning}, isPaused: ${this.isPaused}, cars: ${this.cars.length}`);
}
// Ampelschaltung tick // Ampelschaltung tick
this.updateTrafficLights(); this.updateTrafficLights();
@@ -1999,12 +1989,10 @@ export default {
this.updateTaxi(); this.updateTaxi();
this.handlePassengerActions(); this.handlePassengerActions();
// Timer für geladene Passagiere aktualisieren (nach Passagier-Aktionen) - gedrosselt // Timer für geladene Passagiere aktualisieren (ohne Drosselung)
const nowTs = Date.now(); const nowTs = Date.now();
if (nowTs - this.lastPassengerTimerUpdate >= 200) { // Alle 200ms statt jeden Frame this.updatePassengerTimers();
this.updatePassengerTimers(); this.lastPassengerTimerUpdate = nowTs;
this.lastPassengerTimerUpdate = nowTs;
}
this.checkCollisions(); this.checkCollisions();
this.render(); this.render();
@@ -2012,14 +2000,12 @@ export default {
// Radar-Messung prüfen (falls aktiv) // Radar-Messung prüfen (falls aktiv)
this.checkRadarMeasurement(); this.checkRadarMeasurement();
// Minimap zeichnen (gedrosselt) // Minimap jedes Frame zeichnen
if (nowTs - this.lastMinimapDraw >= this.minimapDrawInterval) { this.drawMinimap();
this.drawMinimap(); this.lastMinimapDraw = nowTs;
this.lastMinimapDraw = nowTs;
}
// Update-Rate schneller: ~16x pro Sekunde (62ms) // Nächsten Frame planen
this.gameLoop = setTimeout(this.update, 62); this.rafId = requestAnimationFrame(this.update);
}, },
updateTaxi() { updateTaxi() {
@@ -2708,7 +2694,7 @@ export default {
}, },
handleCrashDialogClose() { handleCrashDialogClose() {
console.log('Crash-Dialog geschlossen, Spiel wird fortgesetzt'); // (Log entfernt)
this.isPaused = false; this.isPaused = false;
this.showPauseOverlay = false; this.showPauseOverlay = false;
@@ -2722,7 +2708,9 @@ export default {
console.log('[🚗 GAME] Cleared all cars after dialog close'); console.log('[🚗 GAME] Cleared all cars after dialog close');
try { clearTimeout(this.gameLoop); } catch (_) {} try { clearTimeout(this.gameLoop); } catch (_) {}
this.gameLoop = setTimeout(this.update, 62); this.gameLoop = null;
if (this.rafId) { cancelAnimationFrame(this.rafId); this.rafId = null; }
this.rafId = requestAnimationFrame(this.update);
// Taxi bleibt auf dem aktuellen Tile, mittig platzieren // Taxi bleibt auf dem aktuellen Tile, mittig platzieren
this.taxi.speed = 0; this.taxi.speed = 0;
@@ -3374,7 +3362,6 @@ export default {
// Zeichne alle Autos // Zeichne alle Autos
drawCars() { drawCars() {
console.log(`[🚗 DRAW] Drawing ${this.cars.length} cars`);
this.cars.forEach((car, index) => { this.cars.forEach((car, index) => {
const centerX = car.x + car.width/2; const centerX = car.x + car.width/2;
const centerY = car.y + car.height/2; const centerY = car.y + car.height/2;
@@ -3382,7 +3369,7 @@ export default {
// Auto ist sichtbar wenn IRGENDEIN Teil im Canvas ist (nicht nur das Zentrum) // Auto ist sichtbar wenn IRGENDEIN Teil im Canvas ist (nicht nur das Zentrum)
const isVisible = !(car.x + car.width < 0 || car.x > 500 || car.y + car.height < 0 || car.y > 500); const isVisible = !(car.x + car.width < 0 || car.x > 500 || car.y + car.height < 0 || car.y > 500);
console.log(`[🚗 DRAW] Car ${index}: x=${car.x.toFixed(2)}, y=${car.y.toFixed(2)}, centerX=${centerX.toFixed(2)}, centerY=${centerY.toFixed(2)}, direction=${car.direction}, speed=${car.speed.toFixed(2)}, VISIBLE=${isVisible}`);
this.ctx.save(); this.ctx.save();
@@ -3412,7 +3399,6 @@ export default {
this.ctx.rotate(finalAngle); this.ctx.rotate(finalAngle);
if (this.carImage) { if (this.carImage) {
console.log(`[🚗 DRAW] Drawing car ${index} with image at x=${car.x.toFixed(2)}, y=${car.y.toFixed(2)}`);
// Zeichne Auto-Bild zentriert um den Transformationspunkt // Zeichne Auto-Bild zentriert um den Transformationspunkt
this.ctx.drawImage( this.ctx.drawImage(
this.carImage, this.carImage,
@@ -3422,7 +3408,6 @@ export default {
car.height car.height
); );
} else { } else {
console.log(`[🚗 DRAW] Drawing car ${index} with fallback rectangle at x=${car.x.toFixed(2)}, y=${car.y.toFixed(2)}`);
// Fallback: Zeichne farbiges Rechteck wenn Bild nicht geladen // Fallback: Zeichne farbiges Rechteck wenn Bild nicht geladen
this.ctx.fillStyle = car.color; this.ctx.fillStyle = car.color;
this.ctx.fillRect(-car.width/2, -car.height/2, car.width, car.height); this.ctx.fillRect(-car.width/2, -car.height/2, car.width, car.height);
@@ -3800,11 +3785,15 @@ export default {
async handleKeyDown(event) { async handleKeyDown(event) {
// Browser-Shortcuts (F-Tasten, Strg/Meta+R) passieren lassen // Browser-Shortcuts (F-Tasten, Strg/Meta+R) passieren lassen
const key = event.key; const key = event.key;
event.preventDefault();
const isFunctionKey = /^F\d{1,2}$/.test(key); const isFunctionKey = /^F\d{1,2}$/.test(key);
const isReloadShortcut = (event.ctrlKey || event.metaKey) && (key === 'r' || key === 'R'); const isReloadShortcut = (event.ctrlKey || event.metaKey) && (key === 'r' || key === 'R');
if (isFunctionKey || isReloadShortcut) { if (isFunctionKey || isReloadShortcut) {
return; // nicht abfangen, Browser soll handeln return; // nicht abfangen, Browser soll handeln
} }
// Keydown-Repeat und Mehrfach-Setzen unterdrücken
if (event.repeat) return;
if (this.keys[key]) return;
this.keys[key] = true; this.keys[key] = true;
@@ -4433,8 +4422,8 @@ export default {
this.showPauseOverlay = false; this.showPauseOverlay = false;
// Game Loop neu starten // Game Loop neu starten
if (this.gameRunning && !this.gameLoop) { if (this.gameRunning && !this.rafId) {
this.gameLoop = setTimeout(this.update, 62); this.rafId = requestAnimationFrame(this.update);
} }
// Motor startet automatisch bei der nächsten Beschleunigung // Motor startet automatisch bei der nächsten Beschleunigung