// Terrain Generator System // Generira proceduralni isometrični teren in skrbi za optimizacijo (Culling, Object Pooling) class TerrainSystem { constructor(scene, width = 100, height = 100) { this.scene = scene; this.width = width; this.height = height; this.iso = new IsometricUtils(48, 24); this.noise = new PerlinNoise(Date.now()); this.tiles = []; this.decorations = []; this.decorationsMap = new Map(); this.cropsMap = new Map(); this.visibleTiles = new Map(); this.visibleDecorations = new Map(); this.visibleCrops = new Map(); // Pools this.tilePool = { active: [], inactive: [], get: () => { if (this.tilePool.inactive.length > 0) { const s = this.tilePool.inactive.pop(); s.setVisible(true); return s; } const s = this.scene.add.sprite(0, 0, 'dirt'); s.setOrigin(0.5, 0.5); return s; }, release: (sprite) => { sprite.setVisible(false); this.tilePool.inactive.push(sprite); } }; this.decorationPool = { active: [], inactive: [], get: () => { if (this.decorationPool.inactive.length > 0) { const s = this.decorationPool.inactive.pop(); s.setVisible(true); s.clearTint(); return s; } return this.scene.add.sprite(0, 0, 'tree'); }, release: (sprite) => { sprite.setVisible(false); this.decorationPool.inactive.push(sprite); } }; this.cropPool = { active: [], inactive: [], get: () => { if (this.cropPool.inactive.length > 0) { const s = this.cropPool.inactive.pop(); s.setVisible(true); return s; } return this.scene.add.sprite(0, 0, 'crop_stage_1'); }, release: (sprite) => { sprite.setVisible(false); this.cropPool.inactive.push(sprite); } }; this.terrainTypes = { WATER: { name: 'water', height: 0, color: 0x4444ff }, SAND: { name: 'sand', height: 0.2, color: 0xdddd44 }, GRASS_FULL: { name: 'grass_full', height: 0.35, color: 0x44aa44 }, GRASS_TOP: { name: 'grass_top', height: 0.45, color: 0x66cc66 }, DIRT: { name: 'dirt', height: 0.5, color: 0x8b4513 }, STONE: { name: 'stone', height: 0.7, color: 0x888888 }, PATH: { name: 'path', height: -1, color: 0xc2b280 }, FARMLAND: { name: 'farmland', height: -1, color: 0x5c4033 } }; this.offsetX = 0; this.offsetY = 0; } createTileTextures() { // Flat Grid Look (No depth) const tileWidth = 48; const tileHeight = 24; // Just the diamond const types = Object.values(this.terrainTypes); types.forEach((type) => { if (this.scene.textures.exists(type.name)) return; const graphics = this.scene.make.graphics({ x: 0, y: 0, add: false }); const x = 0; const top = 0; const midX = 24; const midY = 12; const bottomY = 24; graphics.fillStyle(type.color); // Diamond Only graphics.beginPath(); graphics.moveTo(midX, top); graphics.lineTo(x + 48, midY); graphics.lineTo(midX, bottomY); graphics.lineTo(x, midY); graphics.closePath(); graphics.fill(); // Grid Stroke (Black/Dark) graphics.lineStyle(1, 0x000000, 0.3); graphics.strokePath(); // Simple details if (type.name.includes('grass')) { graphics.fillStyle(0x339933); for (let i = 0; i < 5; i++) { const rx = x + 10 + Math.random() * 28; const ry = 5 + Math.random() * 14; graphics.fillRect(rx, ry, 2, 2); } } graphics.generateTexture(type.name, tileWidth, tileHeight); graphics.destroy(); }); } generate() { this.createTileTextures(); for (let y = 0; y < this.height; y++) { this.tiles[y] = []; for (let x = 0; x < this.width; x++) { const nx = x * 0.1; const ny = y * 0.1; const elevation = this.noise.noise(nx, ny); let terrainType = this.terrainTypes.WATER; if (elevation > this.terrainTypes.SAND.height) terrainType = this.terrainTypes.SAND; if (elevation > this.terrainTypes.GRASS_FULL.height) terrainType = this.terrainTypes.GRASS_FULL; if (elevation > this.terrainTypes.DIRT.height) terrainType = this.terrainTypes.DIRT; if (elevation > this.terrainTypes.STONE.height) terrainType = this.terrainTypes.STONE; this.tiles[y][x] = { type: terrainType.name, texture: terrainType.name, hasDecoration: false, hasCrop: false }; if (x > 5 && x < this.width - 5 && y > 5 && y < this.height - 5) { let decorType = null; let maxHp = 1; let scale = 1.0; if (terrainType.name.includes('grass')) { const rand = Math.random(); if (elevation > 0.6 && rand < 0.1) { decorType = 'bush'; maxHp = 5; } // Trees - Volumetric else if (rand < 0.15) { decorType = 'tree'; maxHp = 5; const sizeRand = Math.random(); if (sizeRand < 0.2) scale = 0.8; else if (sizeRand < 0.8) scale = 1.0 + Math.random() * 0.3; else scale = 1.3; } // Rocks - Volumetric else if (rand < 0.18) { decorType = 'rock'; // 'rock' texture from TextureGenerator maxHp = 8; scale = 1.5; // Big rocks } else if (rand < 0.19) { decorType = 'gravestone'; maxHp = 10; } else if (rand < 0.30) { decorType = 'flower'; maxHp = 1; } } else if (terrainType.name === 'dirt' && Math.random() < 0.05) { decorType = 'bush'; maxHp = 3; } if (decorType) { const key = `${x},${y}`; const decorData = { gridX: x, gridY: y, type: decorType, id: key, maxHp: maxHp, hp: maxHp, scale: scale }; this.decorations.push(decorData); this.decorationsMap.set(key, decorData); this.tiles[y][x].hasDecoration = true; } } } } console.log('✅ Terrain and decorations generated!'); } damageDecoration(x, y, amount) { const key = `${x},${y}`; const decor = this.decorationsMap.get(key); if (!decor) return false; decor.hp -= amount; if (this.visibleDecorations.has(key)) { const sprite = this.visibleDecorations.get(key); sprite.setTint(0xff0000); this.scene.time.delayedCall(100, () => sprite.clearTint()); this.scene.tweens.add({ targets: sprite, x: sprite.x + 2, duration: 50, yoyo: true, repeat: 1 }); } if (decor.hp <= 0) { this.removeDecoration(x, y); return 'destroyed'; } return 'hit'; } removeDecoration(x, y) { const key = `${x},${y}`; const decor = this.decorationsMap.get(key); if (!decor) return; if (this.visibleDecorations.has(key)) { const sprite = this.visibleDecorations.get(key); sprite.setVisible(false); this.decorationPool.release(sprite); this.visibleDecorations.delete(key); } this.decorationsMap.delete(key); const index = this.decorations.indexOf(decor); if (index > -1) this.decorations.splice(index, 1); if (this.tiles[y] && this.tiles[y][x]) this.tiles[y][x].hasDecoration = false; return decor.type; } placeStructure(x, y, structureType) { if (this.decorationsMap.has(`${x},${y}`)) return false; const decorData = { gridX: x, gridY: y, type: structureType, id: `${x},${y}`, maxHp: 5, hp: 5 }; this.decorations.push(decorData); this.decorationsMap.set(decorData.id, decorData); const tile = this.getTile(x, y); if (tile) tile.hasDecoration = true; this.lastCullX = -9999; return true; } setTileType(x, y, typeName) { if (!this.tiles[y] || !this.tiles[y][x]) return; this.tiles[y][x].type = typeName; const key = `${x},${y}`; if (this.visibleTiles.has(key)) { const sprite = this.visibleTiles.get(key); sprite.setTexture(typeName); } } addCrop(x, y, cropData) { const key = `${x},${y}`; this.cropsMap.set(key, cropData); this.tiles[y][x].hasCrop = true; this.lastCullX = -9999; } removeCrop(x, y) { const key = `${x},${y}`; if (this.cropsMap.has(key)) { if (this.visibleCrops.has(key)) { const sprite = this.visibleCrops.get(key); sprite.setVisible(false); this.cropPool.release(sprite); this.visibleCrops.delete(key); } this.cropsMap.delete(key); this.tiles[y][x].hasCrop = false; } } updateCropVisual(x, y, stage) { const key = `${x},${y}`; if (this.visibleCrops.has(key)) { const sprite = this.visibleCrops.get(key); sprite.setTexture(`crop_stage_${stage}`); } } init(offsetX, offsetY) { this.offsetX = offsetX; this.offsetY = offsetY; } getTile(x, y) { if (this.tiles[y] && this.tiles[y][x]) { return this.tiles[y][x]; } return null; } updateCulling(camera) { const view = camera.worldView; let buffer = 200; if (this.scene.settings && this.scene.settings.viewDistance === 'LOW') buffer = 50; const left = view.x - buffer - this.offsetX; const top = view.y - buffer - this.offsetY; const right = view.x + view.width + buffer - this.offsetX; const bottom = view.y + view.height + buffer - this.offsetY; const p1 = this.iso.toGrid(left, top); const p2 = this.iso.toGrid(right, top); const p3 = this.iso.toGrid(left, bottom); const p4 = this.iso.toGrid(right, bottom); const minGridX = Math.floor(Math.min(p1.x, p2.x, p3.x, p4.x)); const maxGridX = Math.ceil(Math.max(p1.x, p2.x, p3.x, p4.x)); const minGridY = Math.floor(Math.min(p1.y, p2.y, p3.y, p4.y)); const maxGridY = Math.ceil(Math.max(p1.y, p2.y, p3.y, p4.y)); const startX = Math.max(0, minGridX); const endX = Math.min(this.width, maxGridX); const startY = Math.max(0, minGridY); const endY = Math.min(this.height, maxGridY); const neededTileKeys = new Set(); const neededDecorKeys = new Set(); const neededCropKeys = new Set(); for (let y = startY; y < endY; y++) { for (let x = startX; x < endX; x++) { const key = `${x},${y}`; const tile = this.tiles[y][x]; // TILES if (tile) { neededTileKeys.add(key); if (!this.visibleTiles.has(key)) { const sprite = this.tilePool.get(); sprite.setTexture(tile.type); const screenPos = this.iso.toScreen(x, y); sprite.setPosition(screenPos.x + this.offsetX, screenPos.y + this.offsetY); sprite.setDepth(this.iso.getDepth(x, y)); this.visibleTiles.set(key, sprite); } } // DECORATIONS const decor = this.decorationsMap.get(key); if (decor) { neededDecorKeys.add(key); if (!this.visibleDecorations.has(key)) { const sprite = this.decorationPool.get(); const screenPos = this.iso.toScreen(x, y); sprite.setPosition(screenPos.x + this.offsetX, screenPos.y + this.offsetY); if (decor.type.includes('house_sprite') || decor.type.includes('market') || decor.type.includes('structure')) { sprite.setOrigin(0.5, 0.8); } else { // Volumetric trees/rocks origin adjustment // Usually volumetric needed (0.5, 1) or slightly different? sprite.setOrigin(0.5, 0.9); // Slight tweak } sprite.setTexture(decor.type); sprite.setScale(decor.scale || 1.0); sprite.setDepth(this.iso.getDepth(x, y) + 1); this.visibleDecorations.set(key, sprite); } } // CROPS const crop = this.cropsMap.get(key); if (crop) { neededCropKeys.add(key); if (!this.visibleCrops.has(key)) { const sprite = this.cropPool.get(); const screenPos = this.iso.toScreen(x, y); sprite.setPosition(screenPos.x + this.offsetX, screenPos.y + this.offsetY); sprite.setTexture(`crop_stage_${crop.stage}`); sprite.setDepth(this.iso.getDepth(x, y) + 0.5); this.visibleCrops.set(key, sprite); } } } } // Cleanup for (const [key, sprite] of this.visibleTiles) { if (!neededTileKeys.has(key)) { sprite.setVisible(false); this.tilePool.release(sprite); this.visibleTiles.delete(key); } } for (const [key, sprite] of this.visibleDecorations) { if (!neededDecorKeys.has(key)) { sprite.setVisible(false); this.decorationPool.release(sprite); this.visibleDecorations.delete(key); } } for (const [key, sprite] of this.visibleCrops) { if (!neededCropKeys.has(key)) { sprite.setVisible(false); this.cropPool.release(sprite); this.visibleCrops.delete(key); } } } }