/** * TileService - Handles tile loading, caching, and manifest management */ class TileService { constructor() { this.tileCache = {}; this.loadingTiles = new Set(); this.manifests = {}; // slug -> Set of tile keys } /** * Load tile manifest for a map */ async loadManifest(slug) { if (this.manifests[slug]) return; try { const response = await fetch(`${CONFIG.TILES_DIR}/${slug}/tile_manifest.json`); if (!response.ok) throw new Error('Manifest not found'); const data = await response.json(); this.manifests[slug] = new Set(data.tiles || []); } catch (e) { console.warn(`Failed to load manifest for ${slug}:`, e); this.manifests[slug] = null; // No manifest, try all tiles } } /** * Hoechste Zoomstufe dieser Karte = hoechste Stufe mit Kacheln im Manifest. Eltnen, Morheim * und die beiden Chaotic-Karten haben nur Kacheln bis Stufe 5 - darueber waere alles leer. * Ohne Manifest (oder ohne Kacheln darin) gilt CONFIG.MAX_ZOOM. */ maxZoom(slug) { const manifest = this.manifests[slug]; if (!manifest || manifest.size === 0) return CONFIG.MAX_ZOOM; let max = CONFIG.MIN_ZOOM; for (const key of manifest) { // Schluessel "zoom_x_y", z.B. "5_12_7" const zoom = parseInt(key, 10); if (zoom > max) max = zoom; } return Math.min(max, CONFIG.MAX_ZOOM); } /** * Check if a tile exists in the manifest */ hasTile(slug, zoom, x, y) { const key = `${zoom}_${x}_${y}`; const manifest = this.manifests[slug]; // If no manifest or null, assume tile might exist return !manifest || manifest.has(key); } /** * Load a single tile image */ async loadTile(slug, zoom, x, y) { const key = `${slug}_${zoom}_${x}_${y}`; if (this.tileCache[key]) { return this.tileCache[key]; } // Check manifest before attempting to load if (!this.hasTile(slug, zoom, x, y)) { return null; } // If already loading, wait briefly and check again if (this.loadingTiles.has(key)) { await new Promise(r => setTimeout(r, 10)); return this.tileCache[key] || null; } this.loadingTiles.add(key); try { const img = new Image(); img.src = `${CONFIG.TILES_DIR}/${slug}/${zoom}_${x}_${y}.jpg`; await new Promise((resolve, reject) => { img.onload = resolve; img.onerror = () => reject(new Error('Load failed')); setTimeout(() => reject(new Error('Timeout')), 5000); }); this.tileCache[key] = img; } catch (e) { // Silently fail for missing tiles } finally { this.loadingTiles.delete(key); } return this.tileCache[key]; } /** * Preload tiles at adjacent zoom levels in background */ async preloadAdjacentZoomLevels(slug, currentZoom, viewBounds) { const worldWidth = CONFIG.WORLD_MAX_X - CONFIG.WORLD_MIN_X; for (let targetZoom of [currentZoom - 1, currentZoom + 1]) { if (targetZoom < CONFIG.MIN_ZOOM || targetZoom > CONFIG.MAX_ZOOM) continue; const tilesPerSide = Math.pow(2, targetZoom); const tileWorldSize = worldWidth / tilesPerSide; // Calculate which tiles overlap with viewport const startTX = Math.floor((viewBounds.minX - CONFIG.WORLD_MIN_X) / tileWorldSize); const endTX = Math.ceil((viewBounds.maxX - CONFIG.WORLD_MIN_X) / tileWorldSize); const startTY = Math.floor((viewBounds.minY - CONFIG.WORLD_MIN_Y) / tileWorldSize); const endTY = Math.ceil((viewBounds.maxY - CONFIG.WORLD_MIN_Y) / tileWorldSize); // Load tiles in background (fire and forget) for (let tx = startTX; tx <= endTX; tx++) { for (let ty = startTY; ty <= endTY; ty++) { if (tx >= 0 && tx < tilesPerSide && ty >= 0 && ty < tilesPerSide) { this.loadTile(slug, targetZoom, tx, ty); } } } } } /** * Clear cache for a specific map (when switching maps) */ clearCache() { this.tileCache = {}; this.loadingTiles.clear(); } }