// ======================================================== // NOVE GLOBALNE KONSTANTE ZA LOKACIJE // ======================================================== const FARM_SIZE = 8; const FARM_CENTER_X = 20; // Lokacija farme na X osi const FARM_CENTER_Y = 20; // Lokacija farme na Y osi const CITY_SIZE = 15; const CITY_START_X = 65; // Desni del mape (npr. med 65 in 80) const CITY_START_Y = 65; // Terrain Generator System 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(); 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 }, PAVEMENT: { name: 'pavement', height: 0.6, color: 0x777777 }, RUINS: { name: 'ruins', height: 0.6, color: 0x555555 }, PATH: { name: 'path', height: -1, color: 0xc2b280 }, FARMLAND: { name: 'farmland', height: -1, color: 0x5c4033 } }; this.offsetX = 0; this.offsetY = 0; } createTileTextures() { const tileWidth = 48; const tileHeight = 60; 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 midX = 24; const midY = 12; const bottomY = 24; const depth = 20; graphics.fillStyle(0x8B4513); graphics.beginPath(); graphics.moveTo(midX, bottomY); graphics.lineTo(midX, bottomY + depth); graphics.lineTo(x, midY + depth); graphics.lineTo(x, midY); graphics.closePath(); graphics.fill(); graphics.fillStyle(0x6B3410); graphics.beginPath(); graphics.moveTo(x + 48, midY); graphics.lineTo(x + 48, midY + depth); graphics.lineTo(midX, bottomY + depth); graphics.lineTo(midX, bottomY); graphics.closePath(); graphics.fill(); graphics.fillStyle(type.color); graphics.beginPath(); graphics.moveTo(midX, 0); graphics.lineTo(x + 48, midY); graphics.lineTo(midX, bottomY); graphics.lineTo(x, midY); graphics.closePath(); graphics.fill(); graphics.lineStyle(1, 0xffffff, 0.15); graphics.beginPath(); graphics.moveTo(x, midY); graphics.lineTo(midX, 0); graphics.lineTo(x + 48, midY); graphics.strokePath(); if (type.name.includes('grass')) { graphics.fillStyle(Phaser.Display.Color.IntegerToColor(type.color).lighten(10).color); for (let i = 0; i < 8; i++) { const rx = x + 10 + Math.random() * 28; const ry = 4 + Math.random() * 16; graphics.fillRect(rx, ry, 2, 2); } } if (type.name.includes('stone') || type.name.includes('ruins')) { graphics.fillStyle(0x444444); for (let i = 0; i < 6; i++) { const rx = x + 8 + Math.random() * 30; const ry = 4 + Math.random() * 16; graphics.fillRect(rx, ry, 3, 3); } } if (type.name.includes('pavement')) { graphics.lineStyle(1, 0x555555, 0.5); graphics.beginPath(); graphics.moveTo(x + 12, midY + 6); graphics.lineTo(x + 36, midY - 6); graphics.strokePath(); } 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.GRASS_FULL; if (x < 3 || x >= this.width - 3 || y < 3 || y >= this.height - 3) { terrainType = this.terrainTypes.GRASS_FULL; } else { if (elevation < -0.6) terrainType = this.terrainTypes.WATER; else if (elevation > 0.1) terrainType = this.terrainTypes.SAND; else if (elevation > 0.2) terrainType = this.terrainTypes.GRASS_FULL; else if (elevation > 0.3) terrainType = this.terrainTypes.GRASS_TOP; else if (elevation > 0.7) terrainType = this.terrainTypes.DIRT; else if (elevation > 0.85) terrainType = this.terrainTypes.STONE; } if (Math.abs(x - FARM_CENTER_X) <= FARM_SIZE / 2 && Math.abs(y - FARM_CENTER_Y) <= FARM_SIZE / 2) { terrainType = this.terrainTypes.DIRT; } if (x >= CITY_START_X && x < CITY_START_X + CITY_SIZE && y >= CITY_START_Y && y < CITY_START_Y + CITY_SIZE) { terrainType = this.terrainTypes.PAVEMENT; if (Math.random() < 0.2) { terrainType = this.terrainTypes.RUINS; } } this.tiles[y][x] = { type: terrainType.name, texture: terrainType.name, hasDecoration: false, hasCrop: false }; } } let treeCount = 0; let rockCount = 0; let flowerCount = 0; const validPositions = []; const isFarm = (x, y) => Math.abs(x - FARM_CENTER_X) <= (FARM_SIZE / 2 + 2) && Math.abs(y - FARM_CENTER_Y) <= (FARM_SIZE / 2 + 2); const isCity = (x, y) => x >= CITY_START_X - 2 && x < CITY_START_X + CITY_SIZE + 2 && y >= CITY_START_Y - 2 && y < CITY_START_Y + CITY_SIZE + 2; for (let y = 5; y < this.height - 5; y++) { for (let x = 5; x < this.width - 5; x++) { if (isFarm(x, y) || isCity(x, y)) continue; const tile = this.tiles[y][x]; if (tile.type !== 'water' && tile.type !== 'sand' && tile.type !== 'stone') { validPositions.push({ x, y }); } } } for (let i = validPositions.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [validPositions[i], validPositions[j]] = [validPositions[j], validPositions[i]]; } for (let i = 0; i < Math.min(25, validPositions.length); i++) { const pos = validPositions[i]; let treeType = 'tree_green_new'; const rand = Math.random(); if (rand < 0.15) treeType = 'tree_blue_new'; else if (rand < 0.25) treeType = 'tree_dead_new'; this.addDecoration(pos.x, pos.y, treeType); treeCount++; } for (let i = 25; i < Math.min(50, validPositions.length); i++) { const pos = validPositions[i]; // Uporabi uporabnikove kamne const rockType = Math.random() > 0.5 ? 'rock_1' : 'rock_2'; this.addDecoration(pos.x, pos.y, rockType); rockCount++; } const flowerNoise = new PerlinNoise(Date.now() + 3000); for (let y = 5; y < this.height - 5; y++) { for (let x = 5; x < this.width - 5; x++) { if (isFarm(x, y) || isCity(x, y)) continue; const tile = this.tiles[y][x]; const val = flowerNoise.noise(x * 0.12, y * 0.12); if (val > 0.85 && tile.type.includes('grass')) { this.addDecoration(x, y, 'flowers_new'); flowerCount++; } } } const roomSize = 5; const roomsAcross = Math.floor(CITY_SIZE / roomSize); for (let ry = 0; ry < roomsAcross; ry++) { for (let rx = 0; rx < roomsAcross; rx++) { if (Math.random() < 0.75) { const gx = CITY_START_X + rx * roomSize; const gy = CITY_START_Y + ry * roomSize; this.placeStructure(gx, gy, 'ruin_room'); } else { const gx = CITY_START_X + rx * roomSize + 2; const gy = CITY_START_Y + ry * roomSize + 2; const rockType = Math.random() > 0.5 ? 'rock_1' : 'rock_2'; this.addDecoration(gx, gy, rockType); } } } console.log(`✅ Teren generiran: ${treeCount} dreves, ${rockCount} kamnov.`); } 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; } init(offsetX, offsetY) { this.offsetX = offsetX; this.offsetY = offsetY; } setTile(x, y, type) { if (x >= 0 && x < this.width && y >= 0 && y < this.height) { this.tiles[y][x].type = type; } } placeStructure(gridX, gridY, type) { if (type === 'ruin') { for (let y = 0; y < 6; y++) { for (let x = 0; x < 6; x++) { if (Math.random() > 0.6) this.addDecoration(gridX + x, gridY + y, 'fence'); this.setTile(gridX + x, gridY + y, 'stone'); } } } if (type === 'arena') { const size = 12; for (let y = 0; y < size; y++) { for (let x = 0; x < size; x++) { const tx = gridX + x; const ty = gridY + y; this.setTile(tx, ty, 'stone'); if (x === 0 || x === size - 1 || y === 0 || y === size - 1) { if (!(x === Math.floor(size / 2) && y === size - 1)) { this.addDecoration(tx, ty, 'fence'); } } } } this.addDecoration(gridX + 6, gridY + 6, 'gravestone'); } if (type === 'ruin_room') { for (let y = 0; y < 5; y++) { for (let x = 0; x < 5; x++) { const tx = gridX + x; const ty = gridY + y; if (x > 0 && x < 4 && y > 0 && y < 4) { this.setTile(tx, ty, 'ruins'); } if (x === 0 || x === 4 || y === 0 || y === 4) { const isCenter = (x === 2 || y === 2); if (isCenter && Math.random() > 0.5) continue; if (Math.random() > 0.3) { this.addDecoration(tx, ty, 'fence'); } else { // User rocks in ruins if (Math.random() > 0.5) { const rType = Math.random() > 0.5 ? 'rock_1' : 'rock_2'; this.addDecoration(tx, ty, rType); } } } } } } } addDecoration(gridX, gridY, type) { if (gridX < 0 || gridX >= this.width || gridY < 0 || gridY >= this.height) return; const key = `${gridX},${gridY}`; if (this.decorationsMap.has(key)) return; let scale = 1.0; if (type === 'rock_1' || type === 'rock_2') scale = 1.5; // Povečano (bilo 0.5) else if (type === 'tree_green_new' || type === 'tree_blue_new' || type === 'tree_dead_new') scale = 0.04; else if (type === 'flowers_new') scale = 0.02; else if (type === 'fence') scale = 0.025; else if (type === 'gravestone') scale = 0.03; else if (type === 'hill_sprite') scale = 0.025; else { // Old Assets (Low Res) if (type.includes('tree')) scale = 1.2 + Math.random() * 0.4; else if (type.includes('rock')) scale = 0.8; else scale = 1.0; } const decorData = { gridX: gridX, gridY: gridY, type: type, id: key, maxHp: 10, hp: 10, scale: scale }; this.decorations.push(decorData); this.decorationsMap.set(key, decorData); if (this.tiles[gridY] && this.tiles[gridY][gridX]) { this.tiles[gridY][gridX].hasDecoration = 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; } 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}`); } } 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(); const voxelOffset = 12; for (let y = startY; y < endY; y++) { for (let x = startX; x < endX; x++) { const key = `${x},${y}`; const tile = this.tiles[y][x]; 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); } } 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 - voxelOffset); if (decor.type.includes('house') || decor.type.includes('market') || decor.type.includes('structure')) { sprite.setOrigin(0.5, 0.8); } else { sprite.setOrigin(0.5, 1.0); } sprite.setTexture(decor.type); sprite.setScale(decor.scale || 1.0); if (decor.alpha !== undefined) { sprite.setAlpha(decor.alpha); } sprite.setDepth(this.iso.getDepth(x, y) + 1); this.visibleDecorations.set(key, sprite); } } 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 - voxelOffset); sprite.setTexture(`crop_stage_${crop.stage}`); sprite.setOrigin(0.5, 1); sprite.setDepth(this.iso.getDepth(x, y) + 0.5); this.visibleCrops.set(key, sprite); } } } } 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); } } } }