|
|
@@ -9,6 +9,7 @@ class MapEditor {
|
|
|
this.lastMousePos = { x: 0, y: 0 };
|
|
|
this.selectedTile = null;
|
|
|
this.tileSize = 50; // Rayon de l'hexagone (du centre au sommet)
|
|
|
+ this.tileDefinitions = [];
|
|
|
|
|
|
this.init();
|
|
|
}
|
|
|
@@ -25,6 +26,7 @@ class MapEditor {
|
|
|
}
|
|
|
|
|
|
this.setupCanvas();
|
|
|
+ this.loadTileDefinitions();
|
|
|
this.renderMap();
|
|
|
this.setupEventListeners();
|
|
|
this.hideLoading();
|
|
|
@@ -36,6 +38,35 @@ class MapEditor {
|
|
|
this.canvas.style.transformOrigin = '0 0';
|
|
|
}
|
|
|
|
|
|
+ loadTileDefinitions() {
|
|
|
+ // Charger les définitions de tuiles depuis le template
|
|
|
+ if (typeof templateData !== 'undefined' && templateData.tileDefinitions) {
|
|
|
+ this.tileDefinitions = templateData.tileDefinitions;
|
|
|
+ } else {
|
|
|
+ // Défauts si pas de template
|
|
|
+ this.tileDefinitions = [
|
|
|
+ { id: 'empty', name: 'Vide', color: '#ffffff' },
|
|
|
+ { id: 'grass', name: 'Herbe', color: '#90EE90' }
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ getTileType(q, r) {
|
|
|
+ // Chercher le type de tuile dans mapData
|
|
|
+ if (typeof mapData !== 'undefined' && mapData.hexes) {
|
|
|
+ for (const hex of mapData.hexes) {
|
|
|
+ if (hex.q === q && hex.r === r) {
|
|
|
+ return hex.tile ? hex.tile.type : 'empty';
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 'empty';
|
|
|
+ }
|
|
|
+
|
|
|
+ getTileDefinition(tileType) {
|
|
|
+ return this.tileDefinitions.find(def => def.id === tileType) || this.tileDefinitions[0];
|
|
|
+ }
|
|
|
+
|
|
|
// Conversion axial -> pixels pour hexagones pointus (pointy-top)
|
|
|
axialToPixel(q, r) {
|
|
|
const size = this.tileSize; // rayon (centre -> sommet)
|
|
|
@@ -94,50 +125,68 @@ class MapEditor {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-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';
|
|
|
+ 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.clipPath = 'polygon(50% 0%, 100% 25%, 100% 75%, 50% 100%, 0% 75%, 0% 25%)';
|
|
|
+
|
|
|
+ // Appliquer la tuile
|
|
|
+ const tileType = this.getTileType(q, r);
|
|
|
+ const tileDef = this.getTileDefinition(tileType);
|
|
|
+
|
|
|
+ if (tileDef && tileDef.asset) {
|
|
|
+ // Utiliser l'asset SVG comme background
|
|
|
+ hex.style.backgroundColor = 'transparent';
|
|
|
+ hex.style.backgroundImage = `url('${tileDef.asset}')`;
|
|
|
+ hex.style.backgroundSize = 'contain';
|
|
|
+ hex.style.backgroundPosition = 'center';
|
|
|
+ hex.style.backgroundRepeat = 'no-repeat';
|
|
|
+ hex.style.filter = 'none';
|
|
|
+ } else {
|
|
|
+ // Utiliser la couleur
|
|
|
+ hex.style.backgroundColor = tileDef ? (tileDef.color || '#ffffff') : '#ffffff';
|
|
|
+ hex.style.backgroundImage = 'none';
|
|
|
+ hex.style.filter = 'none';
|
|
|
}
|
|
|
- });
|
|
|
|
|
|
- hex.addEventListener('mouseleave', () => {
|
|
|
- if (!this.selectedTile || this.selectedTile.q !== q || this.selectedTile.r !== r) {
|
|
|
- hex.style.backgroundColor = '#ffffff';
|
|
|
- }
|
|
|
- });
|
|
|
+ hex.addEventListener('click', (e) => {
|
|
|
+ e.stopPropagation();
|
|
|
+ this.selectTile(q, r);
|
|
|
+ });
|
|
|
|
|
|
- this.canvas.appendChild(hex);
|
|
|
-}
|
|
|
+ hex.addEventListener('mouseenter', () => {
|
|
|
+ if (!this.selectedTile || this.selectedTile.q !== q || this.selectedTile.r !== r) {
|
|
|
+ hex.style.filter = 'brightness(1.1)';
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ hex.addEventListener('mouseleave', () => {
|
|
|
+ if (!this.selectedTile || this.selectedTile.q !== q || this.selectedTile.r !== r) {
|
|
|
+ hex.style.filter = 'none';
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ this.canvas.appendChild(hex);
|
|
|
+ }
|
|
|
|
|
|
|
|
|
selectTile(q, r) {
|
|
|
@@ -145,8 +194,8 @@ renderHexagon(q, r, x, y, hexWidth, hexHeight) {
|
|
|
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';
|
|
|
+ prevHex.style.boxShadow = 'none';
|
|
|
+ prevHex.style.border = 'none';
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -154,8 +203,8 @@ renderHexagon(q, r, x, y, hexWidth, hexHeight) {
|
|
|
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';
|
|
|
+ hex.style.boxShadow = '0 0 0 3px #2196f3';
|
|
|
+ hex.style.border = '2px solid #1976d2';
|
|
|
}
|
|
|
|
|
|
// Afficher les informations de la tuile
|