// Configuration de l'éditeur de carte class MapEditor { constructor() { this.canvas = document.getElementById('mapCanvas'); this.container = document.querySelector('.map-canvas-container'); this.zoomLevel = 1; this.panOffset = { x: 0, y: 0 }; this.isDragging = false; this.lastMousePos = { x: 0, y: 0 }; this.selectedTile = null; this.tileSize = 50; // Rayon de l'hexagone (du centre au sommet) this.init(); } init() { if (!this.canvas) { console.error('MapEditor: Élément canvas non trouvé!'); return; } if (!this.container) { console.error('MapEditor: Conteneur non trouvé!'); return; } this.setupCanvas(); this.renderMap(); this.setupEventListeners(); this.hideLoading(); this.fitToScreen(); } setupCanvas() { // La taille du canvas est définie dynamiquement dans renderMap() selon l'enveloppe. this.canvas.style.transformOrigin = '0 0'; } // Conversion axial -> pixels pour hexagones pointus (pointy-top) axialToPixel(q, r) { const size = this.tileSize; // rayon (centre -> sommet) const x = size * Math.sqrt(3) * (q + r / 2); const y = size * 1.5 * r; // 3/2 * size return { x, y }; } renderMap() { this.canvas.innerHTML = ''; // Dimensions d'un hexagone (boîte englobante) const hexWidth = this.tileSize * Math.sqrt(3); const hexHeight = this.tileSize * 2; // Rayon de la carte hexagonale (en tuiles) // On utilise le plus grand hexagone qui tient dans (mapWidth, mapHeight) const radius = Math.floor((Math.min(mapWidth, mapHeight) - 1) / 2); if (radius < 0) return; // Première passe: calculer toutes les positions et les bornes pour dimensionner le canvas const positions = []; let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity; for (let q = -radius; q <= radius; q++) { const rMin = Math.max(-radius, -q - radius); const rMax = Math.min(radius, -q + radius); for (let r = rMin; r <= rMax; r++) { const { x, y } = this.axialToPixel(q, r); positions.push({ q, r, x, y }); // consider hex bounding box (center ± half size) when computing extents const halfW = hexWidth / 2; const halfH = hexHeight / 2; if (x - halfW < minX) minX = x - halfW; if (y - halfH < minY) minY = y - halfH; if (x + halfW > maxX) maxX = x + halfW; if (y + halfH > maxY) maxY = y + halfH; } } // Padding pour éviter des coupures visuelles const padding = 20; // Dimensionner le canvas d'après l'enveloppe + la taille d'un hex const canvasWidth = (maxX - minX) + hexWidth + padding; const canvasHeight = (maxY - minY) + hexHeight + padding; this.canvas.style.width = canvasWidth + 'px'; this.canvas.style.height = canvasHeight + 'px'; // Seconde passe: rendre chaque hexagone avec un offset pour commencer à (padding/2, padding/2) const offsetX = -minX + padding / 2; const offsetY = -minY + padding / 2; for (const pos of positions) { // pass center coordinates adjusted by offset; renderHexagon will place box centered on these this.renderHexagon(pos.q, pos.r, pos.x + offsetX, pos.y + offsetY, hexWidth, hexHeight); } } renderHexagon(q, r, x, y, hexWidth, hexHeight) { const hex = document.createElement('div'); hex.className = 'hexagon'; hex.dataset.q = q; hex.dataset.r = r; // Expansion fine juste pour fondre l'anti-aliasing (évite interstice ET chevauchement) const expansion = Math.round(hexWidth * 0.004 * 1000) / 1000; const adjWidth = hexWidth + expansion; const adjHeight = hexHeight + expansion; const halfW = adjWidth / 2; const halfH = adjHeight / 2; hex.style.width = adjWidth + 'px'; hex.style.height = adjHeight + 'px'; hex.style.left = (Math.round((x - halfW) * 1000) / 1000) + 'px'; hex.style.top = (Math.round((y - halfH) * 1000) / 1000) + 'px'; hex.style.position = 'absolute'; hex.style.cursor = 'pointer'; hex.style.transition = 'background-color 0.2s'; hex.style.backgroundColor = '#ffffff'; hex.style.clipPath = 'polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%)'; hex.addEventListener('click', (e) => { e.stopPropagation(); this.selectTile(q, r); }); hex.addEventListener('mouseenter', () => { if (!this.selectedTile || this.selectedTile.q !== q || this.selectedTile.r !== r) { hex.style.backgroundColor = '#e3f2fd'; } }); hex.addEventListener('mouseleave', () => { if (!this.selectedTile || this.selectedTile.q !== q || this.selectedTile.r !== r) { hex.style.backgroundColor = '#ffffff'; } }); this.canvas.appendChild(hex); } selectTile(q, r) { // Désélectionner la tuile précédente if (this.selectedTile) { const prevHex = this.canvas.querySelector(`[data-q="${this.selectedTile.q}"][data-r="${this.selectedTile.r}"]`); if (prevHex) { prevHex.style.backgroundColor = '#ffffff'; prevHex.style.borderColor = '#666'; } } // Sélectionner la nouvelle tuile this.selectedTile = { q, r }; const hex = this.canvas.querySelector(`[data-q="${q}"][data-r="${r}"]`); if (hex) { hex.style.backgroundColor = '#2196f3'; hex.style.borderColor = '#1976d2'; } // Afficher les informations de la tuile this.showTileInfo(q, r); } showTileInfo(q, r) { const tileInfo = document.getElementById('tileInfo'); const tilePosition = document.getElementById('tilePosition'); const tileType = document.getElementById('tileType'); const tileProperties = document.getElementById('tileProperties'); tilePosition.textContent = `Q: ${q}, R: ${r}`; tileType.textContent = 'Vide'; // Par défaut // TODO: Récupérer les vraies propriétés de la tuile depuis les données tileProperties.innerHTML = `