Fix Biome System integration, memory optimization, and Tiled live sync workflow
This commit is contained in:
@@ -71,14 +71,21 @@ class GameScene extends Phaser.Scene {
|
||||
}
|
||||
|
||||
// 🎨 2D FLAT TERRAIN SYSTEM (NEW!)
|
||||
this.worldWidth = 100;
|
||||
this.worldHeight = 100;
|
||||
console.log('🎨 Initializing Flat 2D Terrain...');
|
||||
this.terrainSystem = new Flat2DTerrainSystem(this);
|
||||
|
||||
// 🏗️ PHASE 28-29 SYSTEMS INITIALIZATION
|
||||
try {
|
||||
// 🌍 PHASE 28: Initialize BiomeSystem and ChunkManager BEFORE terrain generation!
|
||||
console.log('🌍 Initializing Biome System (500x500 world)...');
|
||||
const w = this.worldWidth || 100;
|
||||
const h = this.worldHeight || 100;
|
||||
|
||||
console.log(`🌍 Initializing Biome System (${w}x${h} world)...`);
|
||||
this.biomeSystem = new BiomeSystem(this);
|
||||
this.biomeSystem.worldWidth = w;
|
||||
this.biomeSystem.worldHeight = h;
|
||||
this.biomeSystem.generateBiomeMap(); // Generate biome layout
|
||||
console.log('✅ Biome map generated!');
|
||||
|
||||
@@ -94,19 +101,19 @@ class GameScene extends Phaser.Scene {
|
||||
|
||||
// 🌊 PHASE 28 SESSION 5: RIVER SYSTEM
|
||||
console.log('🌊 Initializing River System...');
|
||||
this.riverSystem = new RiverSystem(500, 500, this.biomeSystem);
|
||||
this.riverSystem = new RiverSystem(w, h, this.biomeSystem);
|
||||
this.riverSystem.generateRivers();
|
||||
console.log('✅ River System ready!');
|
||||
|
||||
// 🏞️ PHASE 28 SESSION 5: LAKE SYSTEM
|
||||
console.log('🏞️ Initializing Lake System...');
|
||||
this.lakeSystem = new LakeSystem(500, 500, this.biomeSystem);
|
||||
this.lakeSystem = new LakeSystem(w, h, this.biomeSystem);
|
||||
this.lakeSystem.generateLakes(this.riverSystem);
|
||||
console.log('✅ Lake System ready!');
|
||||
|
||||
// 🏛️ PHASE 28 SESSION 6: STRUCTURE SYSTEM
|
||||
console.log('🏛️ Initializing Structure System...');
|
||||
this.structureSystem = new StructureSystem(500, 500, this.biomeSystem, this.riverSystem, this.lakeSystem);
|
||||
this.structureSystem = new StructureSystem(w, h, this.biomeSystem, this.riverSystem, this.lakeSystem);
|
||||
this.structureSystem.generateAll();
|
||||
const structStats = this.structureSystem.getStats();
|
||||
console.log(`✅ Structure System ready! (${structStats.structures} structures, ${structStats.landmarks} landmarks, ${structStats.roads.length} roads)`);
|
||||
@@ -157,7 +164,7 @@ class GameScene extends Phaser.Scene {
|
||||
}
|
||||
|
||||
// 🗺️ TRY TO LOAD USER'S TILED MAP FIRST
|
||||
let tiledMapLoaded = false;
|
||||
this.tiledMapLoaded = false;
|
||||
this.tiledSpawnPoint = null;
|
||||
|
||||
console.log('🔍 Checking for NovaFarma map...');
|
||||
@@ -176,7 +183,22 @@ class GameScene extends Phaser.Scene {
|
||||
if (map && map.width > 0) {
|
||||
console.log('✅ FOUND VALID USER MAP: NovaFarma.json');
|
||||
this.terrainSystem.loadFromTiled(map);
|
||||
tiledMapLoaded = true;
|
||||
this.tiledMapLoaded = true;
|
||||
|
||||
// UPDATE SYSTEM DIMENSIONS TO MATCH TILED MAP
|
||||
this.worldWidth = map.width;
|
||||
this.worldHeight = map.height;
|
||||
if (this.biomeSystem) {
|
||||
this.biomeSystem.worldWidth = map.width;
|
||||
this.biomeSystem.worldHeight = map.height;
|
||||
// Regenerate biome map for the new dimensions (optional, but consistent)
|
||||
this.biomeSystem.generateBiomeMap();
|
||||
}
|
||||
if (this.structureSystem) {
|
||||
this.structureSystem.worldWidth = map.width;
|
||||
this.structureSystem.worldHeight = map.height;
|
||||
this.structureSystem.generateAll(); // Re-generate for tiled map
|
||||
}
|
||||
|
||||
// 🕵️ FIND SPAWN POINT IN MAP
|
||||
const playerLayer = map.getLayer('Player');
|
||||
@@ -223,8 +245,10 @@ class GameScene extends Phaser.Scene {
|
||||
console.error('⚠️ CRITICAL ERROR loading NovaFarma map:', e);
|
||||
}
|
||||
|
||||
if (!tiledMapLoaded) {
|
||||
console.warn('⚠️ FALLBACK: Generating procedural terrain...');
|
||||
if (!this.tiledMapLoaded) {
|
||||
console.warn('⚠️ FALLBACK: Generating procedural terrain (100x100)...');
|
||||
this.terrainSystem.width = 100;
|
||||
this.terrainSystem.height = 100;
|
||||
await this.terrainSystem.generate();
|
||||
console.log('✅ Flat 2D terrain ready (Procedural)!');
|
||||
} else {
|
||||
@@ -240,9 +264,6 @@ class GameScene extends Phaser.Scene {
|
||||
this.buildSystem = new BuildSystem(this);
|
||||
console.log('🏗️ Build system initialized!');
|
||||
|
||||
// 🌱 Initialize Micro Farm System (PHASE 37!)
|
||||
this.microFarmSystem = new MicroFarmSystem(this);
|
||||
console.log('🌱 Micro Farm system initialized!');
|
||||
|
||||
|
||||
// ========================================================
|
||||
@@ -285,7 +306,7 @@ class GameScene extends Phaser.Scene {
|
||||
|
||||
|
||||
// Terrain offset
|
||||
if (tiledMapLoaded) {
|
||||
if (this.tiledMapLoaded) {
|
||||
// For Tiled maps, we usually start at 0,0 or map center is handled by camera
|
||||
this.terrainOffsetX = 0;
|
||||
this.terrainOffsetY = 0;
|
||||
@@ -551,30 +572,41 @@ class GameScene extends Phaser.Scene {
|
||||
console.log('👤 Initializing player...');
|
||||
const savedSpawn = localStorage.getItem('novafarma_spawn_point');
|
||||
|
||||
// 🌍 PHASE 28: Center of 500x500 world
|
||||
let playerSpawnX = 250, playerSpawnY = 250; // Default center
|
||||
// 🌍 PHASE 28: Initial spawn positioning
|
||||
let playerSpawnX, playerSpawnY;
|
||||
|
||||
if (this.tiledSpawnPoint) {
|
||||
// Use Tiled spawn point (converted to grid)
|
||||
// Note: Player class converts Grid -> Pixel (Grid * 48).
|
||||
// Tiled map gives Pixels.
|
||||
// We need to pass GRID coordinates that result in the correct Pixel.
|
||||
// TargetPixel = Grid * 48
|
||||
// Grid = TargetPixel / 48
|
||||
playerSpawnX = Math.floor(this.tiledSpawnPoint.x / 48);
|
||||
playerSpawnY = Math.floor(this.tiledSpawnPoint.y / 48);
|
||||
playerSpawnX = Math.floor(this.tiledSpawnPoint.x / this.terrainSystem.tileSize);
|
||||
playerSpawnY = Math.floor(this.tiledSpawnPoint.y / this.terrainSystem.tileSize);
|
||||
console.log(`👤 Spawning player at Tiled location: Grid(${playerSpawnX}, ${playerSpawnY}) -> Pixel(${this.tiledSpawnPoint.x}, ${this.tiledSpawnPoint.y})`);
|
||||
} else if (this.tiledMapLoaded) {
|
||||
// Default to map center for Tiled maps
|
||||
playerSpawnX = Math.floor(this.terrainSystem.width / 2);
|
||||
playerSpawnY = Math.floor(this.terrainSystem.height / 2);
|
||||
console.log(`👤 Spawning player at Tiled MAP CENTER: (${playerSpawnX}, ${playerSpawnY})`);
|
||||
} else if (savedSpawn) {
|
||||
[playerSpawnX, playerSpawnY] = savedSpawn.split(',').map(Number);
|
||||
console.log(`👤 Spawning player at saved location: (${playerSpawnX}, ${playerSpawnY})`);
|
||||
} else {
|
||||
console.log(`👤 Spawning player at world center: (${playerSpawnX}, ${playerSpawnY})`);
|
||||
playerSpawnX = 250;
|
||||
playerSpawnY = 250;
|
||||
console.log(`👤 Spawning player at default world center: (${playerSpawnX}, ${playerSpawnY})`);
|
||||
}
|
||||
|
||||
this.player = new Player(this, playerSpawnX, playerSpawnY, this.terrainOffsetX, this.terrainOffsetY);
|
||||
|
||||
// 🌍 PHASE 28: Load initial chunks around player
|
||||
if (this.chunkManager) {
|
||||
// 🌱 Initialize Micro Farm System (PHASE 37!)
|
||||
this.microFarmSystem = new MicroFarmSystem(this);
|
||||
if (playerSpawnX !== undefined && playerSpawnY !== undefined) {
|
||||
this.microFarmSystem.farmCenterX = playerSpawnX;
|
||||
this.microFarmSystem.farmCenterY = playerSpawnY;
|
||||
this.microFarmSystem.init(); // Re-initialize with correct center
|
||||
console.log(`🌱 Micro Farm system centered at (${playerSpawnX}, ${playerSpawnY})!`);
|
||||
}
|
||||
|
||||
// 🌍 PHASE 28: Load initial chunks around player (Procedural ONLY)
|
||||
if (this.chunkManager && !this.tiledMapLoaded) {
|
||||
console.log('💾 Loading initial chunks around player...');
|
||||
this.chunkManager.updateActiveChunks(playerSpawnX, playerSpawnY);
|
||||
const stats = this.chunkManager.getStats();
|
||||
@@ -1181,6 +1213,13 @@ class GameScene extends Phaser.Scene {
|
||||
if (this.buildSystem) this.buildSystem.toggleBuildMode();
|
||||
});
|
||||
|
||||
// 🕸️ Grid Toggle (G key)
|
||||
this.input.keyboard.on('keydown-G', () => {
|
||||
if (this.terrainSystem && this.terrainSystem.toggleGrid) {
|
||||
this.terrainSystem.toggleGrid();
|
||||
}
|
||||
});
|
||||
|
||||
this.input.keyboard.on('keydown-ONE', () => {
|
||||
if (this.buildSystem && this.buildSystem.buildMode) this.buildSystem.selectBuilding('fence_post');
|
||||
else if (this.scene.get('UIScene')) this.scene.get('UIScene').selectSlot(0);
|
||||
@@ -1860,8 +1899,8 @@ class GameScene extends Phaser.Scene {
|
||||
}
|
||||
}
|
||||
|
||||
// 🌍 PHASE 28: Update chunk loading based on player position
|
||||
if (this.chunkManager && this.player) {
|
||||
// 🌍 PHASE 28: Update chunk loading based on player position (ONLY for procedural maps!)
|
||||
if (this.chunkManager && this.player && !this.tiledMapLoaded) {
|
||||
const pos = this.player.getPosition();
|
||||
this.chunkManager.updateActiveChunks(pos.x, pos.y);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user