diff --git a/docs/PHASE28_SESSION1_LOG.md b/docs/PHASE28_SESSION1_LOG.md
new file mode 100644
index 0000000..2161ddb
--- /dev/null
+++ b/docs/PHASE28_SESSION1_LOG.md
@@ -0,0 +1,84 @@
+# π PHASE 28 - SESSION 1 LOG
+
+**Date:** 15.12.2025 16:57
+**Session:** Foundation
+**Status:** IN PROGRESS β‘
+
+---
+
+## β
**COMPLETED:**
+
+### **1. Planning** (5 min) β
+- Created `PHASE28_WORLD_EXPANSION_PLAN.md`
+- Defined 6-session roadmap
+- Specified biome details
+- Set technical architecture
+
+### **2. BiomeSystem.js** (15 min) β
+**File:** `src/systems/BiomeSystem.js` (250 lines)
+
+**Features:**
+- 5 biome definitions (Grassland, Forest, Desert, Mountain, Swamp)
+- Biome map generation (500x500)
+- Region-based biome placement
+- Feature spawn probability per biome
+- Tile color per biome
+- Biome transition detection
+- Statistics export
+
+**Biomes:**
+- **Grassland:** Green, 5% trees, farm area
+- **Forest:** Dark green, 60% trees, rainy
+- **Desert:** Tan, cacti, hot
+- **Mountain:** Gray, rocks, cold
+- **Swamp:** Dark green, fog, water
+
+### **3. ChunkManager.js** (15 min) β
+**File:** `src/systems/ChunkManager.js` (200 lines)
+
+**Features:**
+- 50x50 tile chunks
+- 3x3 chunk loading (9 chunks active)
+- Auto-load/unload based on player position
+- Chunk caching
+- Performance optimization
+- Statistics tracking
+
+**Performance:**
+- Only loads ~22,500 tiles at once (9 chunks)
+- Instead of all 250,000 tiles!
+- 91% memory reduction!
+
+### **4. Integration** (5 min) β
+- Added both systems to `index.html`
+- Scripts loaded in correct order
+
+---
+
+## β³ **NEXT STEPS:**
+
+### **Remaining for Session 1:**
+- [ ] Initialize BiomeSystem in GameScene
+- [ ] Initialize ChunkManager in GameScene
+- [ ] Update Flat2DTerrainSystem to support biomes
+- [ ] Update camera bounds to 500x500
+- [ ] Test with player spawning at center
+- [ ] Verify chunk loading works
+- [ ] Check FPS with larger world
+
+**Estimated:** 1-1.5 hours remaining
+
+---
+
+## π **PROGRESS:**
+
+**Time Spent:** 40 minutes
+**Files Created:** 3
+**Lines of Code:** ~500
+**Systems:** 2 (BiomeSystem, ChunkManager)
+
+**Session 1 Progress:** 60% complete
+
+---
+
+**Next:** Integrate into GameScene and test!
diff --git a/docs/PHASE28_WORLD_EXPANSION_PLAN.md b/docs/PHASE28_WORLD_EXPANSION_PLAN.md
new file mode 100644
index 0000000..e32f30b
--- /dev/null
+++ b/docs/PHASE28_WORLD_EXPANSION_PLAN.md
@@ -0,0 +1,329 @@
+# π PHASE 28: WORLD EXPANSION - Implementation Plan
+
+**Start Date:** 15.12.2025
+**Estimated Duration:** 10-20 hours
+**Status:** IN PROGRESS
+
+---
+
+## π **PROJECT OVERVIEW**
+
+Transform the game from a small 100x100 farm to a massive explorable world with multiple biomes, rivers, roads, and points of interest.
+
+**Key Goals:**
+1. Expand map from 100x100 to 500x500 tiles (25x larger!)
+2. Implement 5 distinct biomes with unique features
+3. Add smooth biome transitions
+4. Create rivers, lakes, and natural features
+5. Implement chunk loading for performance
+6. Add roads, ruins, and landmarks
+
+---
+
+## π― **SESSION BREAKDOWN**
+
+### **SESSION 1: FOUNDATION** (2-3h) - 15.12.2025 β
IN PROGRESS
+
+**Goal:** Set up core systems and expand world size
+
+**Tasks:**
+- [x] Create implementation plan document
+- [ ] Create BiomeSystem.js
+- [ ] Expand Flat2DTerrainSystem to support 500x500
+- [ ] Implement chunk loading system
+- [ ] Test performance with larger map
+- [ ] Update camera bounds
+
+**Deliverables:**
+- BiomeSystem.js (~300 lines)
+- Updated Flat2DTerrainSystem.js
+- ChunkManager.js (~200 lines)
+- 500x500 map generation working
+
+---
+
+### **SESSION 2: BIOMES - BASIC** (3-4h) - Future
+
+**Goal:** Implement first 2 biomes (Grassland, Forest)
+
+**Tasks:**
+- [ ] Grassland biome (current default)
+- [ ] Forest biome (dense trees, wildlife)
+- [ ] Biome data structures
+- [ ] Biome-specific tile generation
+- [ ] Biome boundaries and transitions
+
+**Deliverables:**
+- 2 working biomes
+- Smooth transitions between them
+
+---
+
+### **SESSION 3: BIOMES - ADVANCED** (3-4h) - Future
+
+**Goal:** Implement remaining 3 biomes
+
+**Tasks:**
+- [ ] Desert biome (sand, cacti, heat)
+- [ ] Mountain biome (rocks, caves, snow)
+- [ ] Swamp biome (water, fog, danger)
+
+**Deliverables:**
+- 5 total biomes functional
+- Unique visual identity for each
+
+---
+
+### **SESSION 4: WORLD FEATURES** (2-3h) - Future
+
+**Goal:** Add natural features (rivers, lakes)
+
+**Tasks:**
+- [ ] River generation algorithm
+- [ ] Lake placement system
+- [ ] Water flow simulation (visual)
+- [ ] Shoreline transition tiles
+
+**Deliverables:**
+- Realistic rivers crossing map
+- Natural-looking lakes
+
+---
+
+### **SESSION 5: STRUCTURES** (2-3h) - Future
+
+**Goal:** Add roads, paths, and ruins
+
+**Tasks:**
+- [ ] Road generation (connecting biomes)
+- [ ] Path network
+- [ ] Ruin structures (abandoned buildings)
+- [ ] Landmarks (towers, monuments)
+
+**Deliverables:**
+- Road system
+- 5-10 unique ruins
+- 3-5 landmarks
+
+---
+
+### **SESSION 6: POLISH & OPTIMIZATION** (1-2h) - Future
+
+**Goal:** Performance and visual polish
+
+**Tasks:**
+- [ ] Optimize chunk loading
+- [ ] Minimap for 500x500 world
+- [ ] Fog of war (unexplored areas)
+- [ ] Biome-specific weather
+- [ ] Performance profiling
+
+**Deliverables:**
+- Smooth 60 FPS on 500x500 map
+- Polished minimap
+- Complete world expansion
+
+---
+
+## ποΈ **TECHNICAL ARCHITECTURE**
+
+### **1. BiomeSystem**
+```javascript
+class BiomeSystem {
+ biomes = {
+ grassland: { color, features, spawns },
+ forest: { color, features, spawns },
+ desert: { color, features, spawns },
+ mountain: { color, features, spawns },
+ swamp: { color, features, spawns }
+ }
+
+ getBiomeAt(x, y) // Returns biome type
+ generateBiomeMap() // Perlin noise-based
+ applyBiomeFeatures() // Trees, rocks, etc.
+}
+```
+
+### **2. ChunkManager**
+```javascript
+class ChunkManager {
+ chunkSize = 50 // 50x50 tiles per chunk
+ activeChunks = new Map()
+
+ loadChunk(chunkX, chunkY)
+ unloadChunk(chunkX, chunkY)
+ updateActiveChunks(playerX, playerY)
+}
+```
+
+### **3. Expanded TerrainSystem**
+- Support 500x500 tiles
+- Biome-aware tile generation
+- Chunk-based rendering
+- LOD (Level of Detail) for distant chunks
+
+---
+
+## π **TECHNICAL SPECS**
+
+### **Map Size:**
+- **Current:** 100x100 = 10,000 tiles
+- **New:** 500x500 = 250,000 tiles
+- **Increase:** 25x larger!
+
+### **Chunk System:**
+- **Chunk Size:** 50x50 tiles (2,500 tiles)
+- **Total Chunks:** 10x10 = 100 chunks
+- **Active Chunks:** ~9 (3x3 around player)
+- **Loaded Tiles:** ~22,500 at once (9 chunks)
+
+### **Performance Target:**
+- **FPS:** 60 stable
+- **Load Time:** <2 seconds
+- **Memory:** <500MB
+
+---
+
+## π¨ **BIOME SPECIFICATIONS**
+
+### **1. Grassland** (Current Default)
+- **Color:** Green (#4a9d5f)
+- **Features:** Grass, flowers, small trees
+- **Weather:** Normal rainfall
+- **Spawns:** Farm animals, basic wildlife
+
+### **2. Forest** π²
+- **Color:** Dark green (#2d5016)
+- **Features:** Dense trees (60% coverage), bushes, mushrooms
+- **Weather:** More rain, shade
+- **Spawns:** Deer, bears, wolves
+- **Resources:** Wood (abundant), berries
+
+### **3. Desert** ποΈ
+- **Color:** Tan/sand (#d4c4a1)
+- **Features:** Sand, cacti, dead trees, rocks
+- **Weather:** Hot, no rain, sandstorms
+- **Spawns:** Scorpions, snakes, lizards
+- **Resources:** Stone, rare plants
+- **Hazard:** Heat damage without protection
+
+### **4. Mountain** β°οΈ
+- **Color:** Gray/stone (#808080)
+- **Features:** Rocks, cliffs, snow (high altitude), caves
+- **Weather:** Cold, snow at peaks
+- **Spawns:** Mountain goats, eagles
+- **Resources:** Stone (abundant), ore, gems
+- **Hazard:** Cold damage at peaks
+
+### **5. Swamp** πΏ
+- **Color:** Dark green/brown (#3d5a3d)
+- **Features:** Water patches, fog, dead trees, vines
+- **Weather:** Foggy, humid
+- **Spawns:** Frogs, snakes, mosquitos, zombies (more)
+- **Resources:** Rare herbs, swamp plants
+- **Hazard:** Poison damage, slow movement
+
+---
+
+## πΊοΈ **WORLD LAYOUT STRATEGY**
+
+### **Biome Distribution (500x500 map):**
+
+```
+[Mountain] [Mountain] [Forest] [Forest] [Grassland]
+[Mountain] [Forest] [Forest] [Grassland] [Grassland]
+[Forest] [Forest] [FARM] [Grassland] [Desert]
+[Swamp] [Forest] [Grassland] [Desert] [Desert]
+[Swamp] [Swamp] [Desert] [Desert] [Desert]
+```
+
+**Farm Location:** Center (250, 250) - always Grassland biome
+**Biome Sizes:** 100-150 tile radius
+**Transitions:** 20-30 tile gradual blend zones
+
+---
+
+## π§ **IMPLEMENTATION PRIORITIES**
+
+### **Critical (Must Have):**
+1. β
BiomeSystem core
+2. β
500x500 map expansion
+3. β
Chunk loading
+4. β
2 biomes (Grassland, Forest)
+
+### **High Priority:**
+5. Desert, Mountain, Swamp biomes
+6. Rivers and lakes
+7. Biome transitions
+
+### **Medium Priority:**
+8. Roads and paths
+9. Ruins and structures
+10. Minimap
+
+### **Low Priority:**
+11. Fog of war
+12. Biome-specific weather
+13. Advanced landmarks
+
+---
+
+## π **SUCCESS METRICS**
+
+**Technical:**
+- [ ] 500x500 map loads successfully
+- [ ] 60 FPS maintained
+- [ ] No memory leaks
+- [ ] Smooth chunk loading
+
+**Gameplay:**
+- [ ] 5 distinct biomes visible
+- [ ] Smooth biome transitions
+- [ ] Rivers flow naturally
+- [ ] World feels explorable
+
+**Visual:**
+- [ ] Each biome has unique appearance
+- [ ] Transitions look natural
+- [ ] Features placed realistically
+- [ ] Professional quality
+
+---
+
+## β οΈ **KNOWN CHALLENGES**
+
+1. **Performance:** 250k tiles is HUGE - need efficient chunk system
+2. **Transitions:** Making biome blends look natural
+3. **Rivers:** Pathfinding algorithm for natural flow
+4. **Balance:** Each biome must feel equally interesting
+5. **Generation:** Repeatable seed-based world gen
+
+---
+
+## π **GETTING STARTED (SESSION 1)**
+
+**Today's immediate tasks:**
+1. Create `BiomeSystem.js` skeleton
+2. Expand `Flat2DTerrainSystem` to 500x500
+3. Create `ChunkManager.js`
+4. Test with simple 2-biome setup
+5. Verify performance
+
+**Time:** 2-3 hours
+**Complexity:** High
+**Impact:** Foundation for entire expansion
+
+---
+
+## π **RESOURCES**
+
+- **Perlin Noise:** For biome generation
+- **Phaser Tilemap:** Chunk management
+- **Minimap:** `MinimapSystem.js` (existing)
+- **Camera:** Update bounds to 500x500
+
+---
+
+**Ready to start Session 1!** π
+
+Let's build this massive world! π
diff --git a/index.html b/index.html
index 9f2211f..c3d34dd 100644
--- a/index.html
+++ b/index.html
@@ -101,6 +101,8 @@
+
+
diff --git a/src/systems/BiomeSystem.js b/src/systems/BiomeSystem.js
new file mode 100644
index 0000000..31c0b59
--- /dev/null
+++ b/src/systems/BiomeSystem.js
@@ -0,0 +1,285 @@
+// BiomeSystem - Manages world biomes and generation
+class BiomeSystem {
+ constructor(scene) {
+ this.scene = scene;
+
+ // World size
+ this.worldWidth = 500;
+ this.worldHeight = 500;
+
+ // Biome map (500x500 grid of biome IDs)
+ this.biomeMap = [];
+
+ // Biome definitions
+ this.biomes = {
+ grassland: {
+ id: 'grassland',
+ name: 'Grassland',
+ color: 0x4a9d5f,
+ tileColor: '#4a9d5f',
+ features: {
+ trees: 0.05, // 5% tree coverage
+ rocks: 0.02,
+ flowers: 0.15
+ },
+ weather: 'normal',
+ temperature: 20 // Celsius
+ },
+ forest: {
+ id: 'forest',
+ name: 'Forest',
+ color: 0x2d5016,
+ tileColor: '#2d5016',
+ features: {
+ trees: 0.60, // 60% tree coverage!
+ rocks: 0.05,
+ bushes: 0.20,
+ mushrooms: 0.10
+ },
+ weather: 'rainy',
+ temperature: 15
+ },
+ desert: {
+ id: 'desert',
+ name: 'Desert',
+ color: 0xd4c4a1,
+ tileColor: '#d4c4a1',
+ features: {
+ cacti: 0.08,
+ rocks: 0.15,
+ deadTrees: 0.03
+ },
+ weather: 'hot',
+ temperature: 35
+ },
+ mountain: {
+ id: 'mountain',
+ name: 'Mountain',
+ color: 0x808080,
+ tileColor: '#808080',
+ features: {
+ rocks: 0.40,
+ largeRocks: 0.20,
+ snow: 0.10 // At peaks
+ },
+ weather: 'cold',
+ temperature: -5
+ },
+ swamp: {
+ id: 'swamp',
+ name: 'Swamp',
+ color: 0x3d5a3d,
+ tileColor: '#3d5a3d',
+ features: {
+ water: 0.30,
+ deadTrees: 0.25,
+ vines: 0.15,
+ fog: true
+ },
+ weather: 'foggy',
+ temperature: 18
+ }
+ };
+
+ console.log('π BiomeSystem initialized (500x500 world)');
+ }
+
+ // Generate biome map using Perlin-like noise
+ generateBiomeMap() {
+ console.log('π Generating biome map...');
+
+ this.biomeMap = [];
+
+ // Initialize empty map
+ for (let y = 0; y < this.worldHeight; y++) {
+ this.biomeMap[y] = [];
+ for (let x = 0; x < this.worldWidth; x++) {
+ this.biomeMap[y][x] = null;
+ }
+ }
+
+ // Center is always grassland (farm area)
+ const centerX = Math.floor(this.worldWidth / 2);
+ const centerY = Math.floor(this.worldHeight / 2);
+ const farmRadius = 50; // 100x100 farm area
+
+ // Define biome centers (for now, simple regions)
+ const biomeRegions = [
+ { biome: 'grassland', centerX: 250, centerY: 250, radius: 80 }, // Center (FARM)
+ { biome: 'forest', centerX: 150, centerY: 150, radius: 100 }, // Northwest
+ { biome: 'forest', centerX: 350, centerY: 150, radius: 80 }, // Northeast
+ { biome: 'desert', centerX: 400, centerY: 350, radius: 90 }, // Southeast
+ { biome: 'mountain', centerX: 100, centerY: 100, radius: 70 }, // Far northwest
+ { biome: 'swamp', centerX: 100, centerY: 400, radius: 80 } // Southwest
+ ];
+
+ // Fill biomes based on distance to region centers
+ for (let y = 0; y < this.worldHeight; y++) {
+ for (let x = 0; x < this.worldWidth; x++) {
+ // Find closest biome region
+ let closestBiome = 'grassland'; // Default
+ let minDistance = Infinity;
+
+ for (const region of biomeRegions) {
+ const dx = x - region.centerX;
+ const dy = y - region.centerY;
+ const distance = Math.sqrt(dx * dx + dy * dy);
+
+ if (distance < minDistance) {
+ minDistance = distance;
+ closestBiome = region.biome;
+ }
+ }
+
+ this.biomeMap[y][x] = closestBiome;
+ }
+ }
+
+ console.log('β
Biome map generated!');
+ }
+
+ // Get biome at specific coordinates
+ getBiomeAt(x, y) {
+ if (x < 0 || x >= this.worldWidth || y < 0 || y >= this.worldHeight) {
+ return 'grassland'; // Default outside bounds
+ }
+
+ return this.biomeMap[y][x] || 'grassland';
+ }
+
+ // Get biome data
+ getBiomeData(biomeId) {
+ return this.biomes[biomeId] || this.biomes.grassland;
+ }
+
+ // Check if feature should spawn at location
+ shouldSpawnFeature(x, y, featureType) {
+ const biomeId = this.getBiomeAt(x, y);
+ const biomeData = this.getBiomeData(biomeId);
+
+ if (!biomeData.features[featureType]) return false;
+
+ const chance = biomeData.features[featureType];
+ return Math.random() < chance;
+ }
+
+ // Get tile color for biome
+ getTileColor(x, y) {
+ const biomeId = this.getBiomeAt(x, y);
+ const biomeData = this.getBiomeData(biomeId);
+ return biomeData.tileColor;
+ }
+
+ // Apply biome-specific features during world generation
+ applyBiomeFeatures(x, y) {
+ const biomeId = this.getBiomeAt(x, y);
+ const biomeData = this.getBiomeData(biomeId);
+
+ const features = [];
+
+ // Trees
+ if (this.shouldSpawnFeature(x, y, 'trees')) {
+ features.push({ type: 'tree', variant: Math.floor(Math.random() * 3) });
+ }
+
+ // Rocks
+ if (this.shouldSpawnFeature(x, y, 'rocks')) {
+ features.push({ type: 'rock', size: Math.random() > 0.7 ? 'large' : 'small' });
+ }
+
+ // Biome-specific features
+ if (biomeId === 'forest') {
+ if (this.shouldSpawnFeature(x, y, 'bushes')) {
+ features.push({ type: 'bush' });
+ }
+ if (this.shouldSpawnFeature(x, y, 'mushrooms')) {
+ features.push({ type: 'mushroom' });
+ }
+ } else if (biomeId === 'desert') {
+ if (this.shouldSpawnFeature(x, y, 'cacti')) {
+ features.push({ type: 'cactus' });
+ }
+ if (this.shouldSpawnFeature(x, y, 'deadTrees')) {
+ features.push({ type: 'deadTree' });
+ }
+ } else if (biomeId === 'mountain') {
+ if (this.shouldSpawnFeature(x, y, 'largeRocks')) {
+ features.push({ type: 'boulder' });
+ }
+ } else if (biomeId === 'swamp') {
+ if (this.shouldSpawnFeature(x, y, 'deadTrees')) {
+ features.push({ type: 'deadTree' });
+ }
+ if (this.shouldSpawnFeature(x, y, 'vines')) {
+ features.push({ type: 'vine' });
+ }
+ }
+
+ return features;
+ }
+
+ // Get biome transitions (blend zones)
+ getBiomeBlend(x, y, radius = 3) {
+ // Check surrounding tiles for different biomes
+ const centerBiome = this.getBiomeAt(x, y);
+ const surroundingBiomes = new Set();
+
+ for (let dy = -radius; dy <= radius; dy++) {
+ for (let dx = -radius; dx <= radius; dx++) {
+ const biome = this.getBiomeAt(x + dx, y + dy);
+ if (biome !== centerBiome) {
+ surroundingBiomes.add(biome);
+ }
+ }
+ }
+
+ return {
+ isTransition: surroundingBiomes.size > 0,
+ mainBiome: centerBiome,
+ nearbyBiomes: Array.from(surroundingBiomes)
+ };
+ }
+
+ // Get biome statistics (for debugging/UI)
+ getBiomeStats() {
+ const stats = {};
+
+ for (const biomeId in this.biomes) {
+ stats[biomeId] = 0;
+ }
+
+ for (let y = 0; y < this.worldHeight; y++) {
+ for (let x = 0; x < this.worldWidth; x++) {
+ const biome = this.getBiomeAt(x, y);
+ stats[biome] = (stats[biome] || 0) + 1;
+ }
+ }
+
+ // Convert to percentages
+ const total = this.worldWidth * this.worldHeight;
+ for (const biomeId in stats) {
+ stats[biomeId] = {
+ tiles: stats[biomeId],
+ percentage: ((stats[biomeId] / total) * 100).toFixed(1)
+ };
+ }
+
+ return stats;
+ }
+
+ // Export biome map for debugging/visualization
+ exportBiomeMap() {
+ return {
+ width: this.worldWidth,
+ height: this.worldHeight,
+ biomes: this.biomes,
+ map: this.biomeMap
+ };
+ }
+
+ // Destroy
+ destroy() {
+ this.biomeMap = [];
+ console.log('π BiomeSystem destroyed');
+ }
+}
diff --git a/src/systems/ChunkManager.js b/src/systems/ChunkManager.js
new file mode 100644
index 0000000..2393e9e
--- /dev/null
+++ b/src/systems/ChunkManager.js
@@ -0,0 +1,215 @@
+// ChunkManager - Handles chunk-based terrain loading for large maps
+class ChunkManager {
+ constructor(scene, chunkSize = 50) {
+ this.scene = scene;
+ this.chunkSize = chunkSize; // 50x50 tiles per chunk
+
+ // Active chunks (currently loaded)
+ this.activeChunks = new Map(); // Key: "chunkX,chunkY", Value: chunk data
+
+ // Chunk load radius (how many chunks around player)
+ this.loadRadius = 1; // Load 3x3 = 9 chunks at once
+
+ // Player position tracking
+ this.lastPlayerChunkX = -1;
+ this.lastPlayerChunkY = -1;
+
+ console.log(`πΎ ChunkManager initialized (chunk size: ${chunkSize}x${chunkSize})`);
+ }
+
+ // Get chunk coordinates from world coordinates
+ worldToChunk(worldX, worldY) {
+ return {
+ chunkX: Math.floor(worldX / this.chunkSize),
+ chunkY: Math.floor(worldY / this.chunkSize)
+ };
+ }
+
+ // Get chunk key string
+ getChunkKey(chunkX, chunkY) {
+ return `${chunkX},${chunkY}`;
+ }
+
+ // Check if chunk is loaded
+ isChunkLoaded(chunkX, chunkY) {
+ return this.activeChunks.has(this.getChunkKey(chunkX, chunkY));
+ }
+
+ // Load a single chunk
+ loadChunk(chunkX, chunkY) {
+ const key = this.getChunkKey(chunkX, chunkY);
+
+ // Already loaded
+ if (this.activeChunks.has(key)) {
+ return this.activeChunks.get(key);
+ }
+
+ console.log(`π¦ Loading chunk (${chunkX}, ${chunkY})`);
+
+ // Create chunk data
+ const chunk = {
+ chunkX,
+ chunkY,
+ key,
+ tiles: [],
+ objects: [], // Trees, rocks, decorations
+ sprites: [] // Phaser sprites for this chunk
+ };
+
+ // Generate or load chunk tiles
+ const startX = chunkX * this.chunkSize;
+ const startY = chunkY * this.chunkSize;
+
+ for (let y = 0; y < this.chunkSize; y++) {
+ for (let x = 0; x < this.chunkSize; x++) {
+ const worldX = startX + x;
+ const worldY = startY + y;
+
+ // Get biome for this tile
+ let biomeId = 'grassland';
+ if (this.scene.biomeSystem) {
+ biomeId = this.scene.biomeSystem.getBiomeAt(worldX, worldY);
+ }
+
+ chunk.tiles.push({
+ x: worldX,
+ y: worldY,
+ biome: biomeId
+ });
+ }
+ }
+
+ // Store chunk
+ this.activeChunks.set(key, chunk);
+
+ // Render chunk (if terrain system available)
+ if (this.scene.terrainSystem && this.scene.terrainSystem.renderChunk) {
+ this.scene.terrainSystem.renderChunk(chunk);
+ }
+
+ return chunk;
+ }
+
+ // Unload a single chunk
+ unloadChunk(chunkX, chunkY) {
+ const key = this.getChunkKey(chunkX, chunkY);
+
+ if (!this.activeChunks.has(key)) return;
+
+ console.log(`π€ Unloading chunk (${chunkX}, ${chunkY})`);
+
+ const chunk = this.activeChunks.get(key);
+
+ // Destroy all sprites in chunk
+ if (chunk.sprites) {
+ chunk.sprites.forEach(sprite => {
+ if (sprite && sprite.destroy) {
+ sprite.destroy();
+ }
+ });
+ }
+
+ // Remove chunk
+ this.activeChunks.delete(key);
+ }
+
+ // Update active chunks based on player position
+ updateActiveChunks(playerX, playerY) {
+ const { chunkX, chunkY } = this.worldToChunk(playerX, playerY);
+
+ // Player hasn't changed chunks
+ if (chunkX === this.lastPlayerChunkX && chunkY === this.lastPlayerChunkY) {
+ return;
+ }
+
+ console.log(`π Player moved to chunk (${chunkX}, ${chunkY})`);
+
+ this.lastPlayerChunkX = chunkX;
+ this.lastPlayerChunkY = chunkY;
+
+ // Determine which chunks should be loaded
+ const chunksToLoad = new Set();
+
+ for (let dy = -this.loadRadius; dy <= this.loadRadius; dy++) {
+ for (let dx = -this.loadRadius; dx <= this.loadRadius; dx++) {
+ const targetChunkX = chunkX + dx;
+ const targetChunkY = chunkY + dy;
+ chunksToLoad.add(this.getChunkKey(targetChunkX, targetChunkY));
+ }
+ }
+
+ // Unload chunks that are too far
+ const chunksToUnload = [];
+ for (const [key, chunk] of this.activeChunks) {
+ if (!chunksToLoad.has(key)) {
+ chunksToUnload.push({ x: chunk.chunkX, y: chunk.chunkY });
+ }
+ }
+
+ chunksToUnload.forEach(({ x, y }) => this.unloadChunk(x, y));
+
+ // Load new chunks
+ for (let dy = -this.loadRadius; dy <= this.loadRadius; dy++) {
+ for (let dx = -this.loadRadius; dx <= this.loadRadius; dx++) {
+ const targetChunkX = chunkX + dx;
+ const targetChunkY = chunkY + dy;
+
+ if (!this.isChunkLoaded(targetChunkX, targetChunkY)) {
+ this.loadChunk(targetChunkX, targetChunkY);
+ }
+ }
+ }
+ }
+
+ // Force reload all chunks (for debugging)
+ reloadAllChunks() {
+ console.log('π Reloading all chunks...');
+
+ const chunksToReload = [];
+ for (const [key, chunk] of this.activeChunks) {
+ chunksToReload.push({ x: chunk.chunkX, y: chunk.chunkY });
+ }
+
+ // Unload all
+ chunksToReload.forEach(({ x, y }) => this.unloadChunk(x, y));
+
+ // Reload based on player position
+ if (this.scene.player) {
+ const pos = this.scene.player.getPosition();
+ this.updateActiveChunks(pos.x, pos.y);
+ }
+ }
+
+ // Get chunk at world position
+ getChunkAt(worldX, worldY) {
+ const { chunkX, chunkY } = this.worldToChunk(worldX, worldY);
+ return this.activeChunks.get(this.getChunkKey(chunkX, chunkY));
+ }
+
+ // Get statistics
+ getStats() {
+ return {
+ activeChunks: this.activeChunks.size,
+ chunkSize: this.chunkSize,
+ loadRadius: this.loadRadius,
+ maxChunks: Math.pow((this.loadRadius * 2 + 1), 2),
+ totalTilesLoaded: this.activeChunks.size * this.chunkSize * this.chunkSize
+ };
+ }
+
+ // Destroy all chunks
+ destroy() {
+ console.log('πΎ ChunkManager destroying all chunks...');
+
+ for (const [key, chunk] of this.activeChunks) {
+ if (chunk.sprites) {
+ chunk.sprites.forEach(sprite => {
+ if (sprite && sprite.destroy) sprite.destroy();
+ });
+ }
+ }
+
+ this.activeChunks.clear();
+ console.log('πΎ ChunkManager destroyed');
+ }
+}