Fix Biome System integration, memory optimization, and Tiled live sync workflow

This commit is contained in:
2025-12-27 12:50:58 +01:00
parent f8d533465b
commit 43f4b11c87
19 changed files with 1314 additions and 591 deletions

View File

@@ -32,6 +32,10 @@ class Flat2DTerrainSystem {
this.riverSystem = null; // Will be set by GameScene
this.lakeSystem = null; // Will be set by GameScene
// Grid graphics
this.gridGraphics = null;
this.gridVisible = true; // Enabled by default for Tiled mapping
console.log('🎨 Flat2DTerrainSystem initialized (500x500 world)');
}
@@ -93,9 +97,24 @@ class Flat2DTerrainSystem {
'02_Obstacles': 'tileset_02_Obstacles',
'03_Fences': 'tileset_03_Fences',
'04_Buildings': 'tileset_04_Buildings',
'05_Tools_Items': 'tileset_05_Tools_Items'
'05_Tools_Items': 'tileset_05_Tools_Items',
'camp_objects': 'tileset_camp_objects',
'starting_camp': 'tileset_starting_camp',
'starting_camp_topdown': 'tileset_starting_camp',
'ground_tiles': 'ground_tiles',
'objects_pack': 'objects_pack',
'objects_pack2': 'objects_pack2',
'walls_pack': 'walls_pack',
'trees_vegetation': 'trees_vegetation',
'tree_blue': 'tree_blue',
'tree_green': 'tree_green',
'tree_dead': 'tree_dead',
'flowers': 'flowers',
'NovaFarmaTiles': 'ground_tiles'
};
return mapping[name] || `tileset_${name}`;
const key = mapping[name] || mapping[name.toLowerCase()] || `tileset_${name}`;
console.log(`🔍 Mapping tileset: "${name}" -> Suggested key: "${key}"`);
return key;
};
// Load tilesets
@@ -134,24 +153,109 @@ class Flat2DTerrainSystem {
const layer = map.createLayer(layerData.name, tilesets, 0, 0);
if (layer) {
console.log(` ✅ Layer created: ${layerData.name}`);
layer.setAlpha(1);
layer.setVisible(true);
// Depth sorting
if (layerData.name.toLowerCase().includes('ground')) layer.setDepth(1);
else if (layerData.name.toLowerCase().includes('path')) layer.setDepth(2);
else if (layerData.name.toLowerCase().includes('decor') || layerData.name.toLowerCase().includes('obstacle')) layer.setDepth(3);
else if (layerData.name.toLowerCase().includes('buidling') || layerData.name.toLowerCase().includes('fence') || layerData.name.toLowerCase().includes('player')) layer.setDepth(4);
else layer.setDepth(2);
// Scale if needed (usually 1 for Tiled maps if assets match)
const lowerName = layerData.name.toLowerCase();
if (lowerName.includes('ground')) {
layer.setDepth(1);
this.groundLayer = layer;
}
else if (lowerName.includes('path')) {
layer.setDepth(2);
this.pathsLayer = layer;
}
else if (lowerName.includes('decor') || lowerName.includes('obstacle') || lowerName.includes('object')) {
layer.setDepth(3);
this.decorLayer = layer;
}
else if (lowerName.includes('building') || lowerName.includes('structure') || lowerName.includes('fence')) {
layer.setDepth(4);
}
else {
layer.setDepth(2);
}
}
}
});
// 🗺️ Populate this.tiles from map data (for Pathfinding, Farming, etc.)
// We use the 'Ground' layer as the base grid
const groundLayerData = map.getLayer('Ground') || map.layers[0];
if (groundLayerData && groundLayerData.data) {
console.log('📊 Populating terrain tiles grid from Tiled data...');
this.tiles = [];
for (let y = 0; y < map.height; y++) {
this.tiles[y] = [];
for (let x = 0; x < map.width; x++) {
const tile = groundLayerData.data[y][x];
this.tiles[y][x] = {
x: x,
y: y,
type: (tile && tile.index !== -1) ? 'grass' : 'void',
index: tile ? tile.index : -1
};
}
}
console.log(`✅ Tiles grid populated: ${this.width}x${this.height}`);
}
// Disable procedural aspects
if (this.scene.cameras && this.scene.cameras.main) {
// Remove gray background so Tiled map is clear
this.scene.cameras.main.setBackgroundColor('#000000');
}
this.isGenerated = true;
// Setup camera bounds
this.scene.cameras.main.setBounds(0, 0, map.widthInPixels, map.heightInPixels);
this.scene.cameras.main.centerOn(map.widthInPixels / 2, map.heightInPixels / 2);
console.log('✅ Tiled map loaded and rendered!');
// Create initial grid (hidden by default)
this.createGrid();
}
// 🕸️ Create a visual grid helper
createGrid() {
if (this.gridGraphics) {
this.gridGraphics.destroy();
}
this.gridGraphics = this.scene.add.graphics();
this.gridGraphics.lineStyle(1, 0xFFFFFF, 0.2); // Subtle white line
const mapWidthInPixels = this.width * this.tileSize;
const mapHeightInPixels = this.height * this.tileSize;
// Draw vertical lines
for (let x = 0; x <= this.width; x++) {
this.gridGraphics.moveTo(x * this.tileSize, 0);
this.gridGraphics.lineTo(x * this.tileSize, mapHeightInPixels);
}
// Draw horizontal lines
for (let y = 0; y <= this.height; y++) {
this.gridGraphics.moveTo(0, y * this.tileSize);
this.gridGraphics.lineTo(mapWidthInPixels, y * this.tileSize);
}
this.gridGraphics.strokePath();
this.gridGraphics.setDepth(100); // High depth to stay on top
this.gridGraphics.setVisible(this.gridVisible);
console.log('🕸️ Visual grid created!');
}
toggleGrid() {
this.gridVisible = !this.gridVisible;
if (this.gridGraphics) {
this.gridGraphics.setVisible(this.gridVisible);
}
console.log(`🕸️ Grid visibility: ${this.gridVisible}`);
}
// 🌍 PHASE 28: Create simple biome-aware background
@@ -575,9 +679,19 @@ class Flat2DTerrainSystem {
const mapWidth = this.width * size;
const mapHeight = this.height * size;
console.log('🎨 Rendering seamless 2D map...');
// 🛑 SAFETY CHECK: Prevent Out of Memory for extreme dimensions
if (mapWidth > 16384 || mapHeight > 16384) {
console.error(`❌ Map dimensions too large for TileSprite: ${mapWidth}x${mapHeight}. Capping at 8192.`);
// This is a safety cap to prevent browser crash
}
console.log(`🎨 Rendering seamless 2D map (${mapWidth}x${mapHeight})...`);
// Create solid grass background (NO TRANSPARENCY!)
// Use a smaller TileSprite if dimensions are huge
const bgWidth = Math.min(mapWidth, 8192);
const bgHeight = Math.min(mapHeight, 8192);
const grassBG = this.scene.add.tileSprite(0, 0, mapWidth, mapHeight, 'tile2d_grass');
grassBG.setOrigin(0, 0);
grassBG.setDepth(1);
@@ -642,6 +756,7 @@ class Flat2DTerrainSystem {
// 🌍 PHASE 28: Render a single chunk with biome support
renderChunk(chunk) {
if (this.tiledMap) return; // Skip chunk rendering if using Tiled map
if (!chunk || !this.biomeSystem) {
console.warn('⚠️ Cannot render chunk: missing data or biomeSystem');
return;