class BuildSystem { constructor(scene) { this.scene = scene; this.buildMode = false; this.selectedBuilding = 'fence'; this.previewSprite = null; this.placedBuildings = []; // Building definitions this.buildings = { 'fence': { name: 'Fence (Old)', textureKey: 'fence_isometric', cost: { wood: 2 }, collision: false, scale: 0.3 }, 'fence_post': { name: 'Fence Post', textureKey: 'fence_post', cost: { wood: 1 }, collision: false, scale: 0.2 }, 'fence_horizontal': { name: 'Fence →', textureKey: 'fence_horizontal', cost: { wood: 2 }, collision: false, scale: 0.2 }, 'fence_vertical': { name: 'Fence ↓', textureKey: 'fence_vertical', cost: { wood: 2 }, collision: false, scale: 0.2 }, 'fence_corner': { name: 'Fence ⌞', textureKey: 'fence_corner', cost: { wood: 2 }, collision: false, scale: 0.2 }, 'barn': { name: 'Barn', textureKey: 'barn_isometric', cost: { wood: 40, stone: 20 }, collision: true, scale: 0.5 }, 'grave': { name: 'Grave', textureKey: 'grave_zombie', cost: { stone: 10 }, collision: false, scale: 0.3 }, 'farmhouse': { name: 'Farmhouse', textureKey: 'farmhouse_isometric', cost: { wood: 50, stone: 30, gold: 100 }, collision: true, scale: 0.5 }, 'blacksmith': { name: 'Blacksmith', textureKey: 'blacksmith_workshop', cost: { wood: 30, stone: 40, gold: 80 }, collision: true, scale: 0.45 } }; } toggleBuildMode() { this.buildMode = !this.buildMode; console.log(`Build Mode: ${this.buildMode ? 'ON' : 'OFF'}`); // Show tutorial on first time if (this.buildMode && !this.tutorialShown) { this.showTutorial(); this.tutorialShown = true; } // Notify UI const uiScene = this.scene.scene.get('UIScene'); if (uiScene) { uiScene.toggleBuildMenu(this.buildMode); } // Show/hide preview if (this.buildMode) { this.createPreview(); } else { this.destroyPreview(); } return this.buildMode; } selectBuilding(buildingId) { if (!this.buildings[buildingId]) return; this.selectedBuilding = buildingId; // Update UI const uiScene = this.scene.scene.get('UIScene'); if (uiScene) { uiScene.updateBuildSelection(this.buildings[buildingId].name); } // Refresh preview if (this.buildMode) { this.destroyPreview(); this.createPreview(); } } createPreview() { const building = this.buildings[this.selectedBuilding]; if (!this.scene.textures.exists(building.textureKey)) { console.warn(`Texture not found: ${building.textureKey}`); return; } this.previewSprite = this.scene.add.sprite(0, 0, building.textureKey); this.previewSprite.setOrigin(0.5, 1); this.previewSprite.setAlpha(0.6); this.previewSprite.setDepth(10000); // Always on top this.previewSprite.setScale(building.scale || 1.0); } destroyPreview() { if (this.previewSprite) { this.previewSprite.destroy(); this.previewSprite = null; } } update() { if (!this.buildMode || !this.previewSprite) return; // Follow mouse const pointer = this.scene.input.activePointer; const worldPoint = this.scene.cameras.main.getWorldPoint(pointer.x, pointer.y); const gridPos = this.scene.iso.toGrid(worldPoint.x, worldPoint.y); const screenPos = this.scene.iso.toScreen(gridPos.x, gridPos.y); this.previewSprite.setPosition(screenPos.x, screenPos.y); // Check if can place const canPlace = this.canPlaceAt(gridPos.x, gridPos.y); this.previewSprite.setTint(canPlace ? 0x00ff00 : 0xff0000); // Place on click if (pointer.isDown && canPlace) { this.placeBuilding(gridPos.x, gridPos.y); } } canPlaceAt(gridX, gridY) { // Check if already has building const exists = this.placedBuildings.find(b => b.gridX === gridX && b.gridY === gridY); if (exists) return false; // Check terrain (only on farm tiles) if (!this.scene.terrainSystem) return false; const tile = this.scene.terrainSystem.getTile(gridX, gridY); if (!tile || tile.type !== 'farm') return false; // Check cost const building = this.buildings[this.selectedBuilding]; if (!this.hasResources(building.cost)) return false; return true; } hasResources(cost) { const inv = this.scene.inventorySystem; if (!inv) return false; for (const [resource, amount] of Object.entries(cost)) { if (resource === 'gold') { if (inv.gold < amount) return false; } else { if (!inv.hasItem(resource, amount)) return false; } } return true; } placeBuilding(gridX, gridY) { const building = this.buildings[this.selectedBuilding]; // Consume resources const inv = this.scene.inventorySystem; for (const [resource, amount] of Object.entries(building.cost)) { if (resource === 'gold') { inv.gold -= amount; } else { inv.removeItem(resource, amount); } } // Create sprite const screenPos = this.scene.iso.toScreen(gridX, gridY); const sprite = this.scene.add.sprite(screenPos.x, screenPos.y, building.textureKey); sprite.setOrigin(0.5, 1); sprite.setScale(building.scale || 1.0); sprite.setDepth(this.scene.iso.getDepth(gridX, gridY) + 5); // Above terrain // Store this.placedBuildings.push({ gridX, gridY, type: this.selectedBuilding, sprite, collision: building.collision }); console.log(`🏗️ Placed ${building.name} at (${gridX}, ${gridY})`); // Update UI this.scene.events.emit('update-inventory'); } isCollisionAt(gridX, gridY) { const building = this.placedBuildings.find(b => b.gridX === gridX && b.gridY === gridY); return building ? building.collision : false; } showTutorial() { const uiScene = this.scene.scene.get('UIScene'); if (!uiScene) return; const width = this.scene.cameras.main.width; const height = this.scene.cameras.main.height; // Tutorial panel const panel = uiScene.add.container(width / 2, height / 2); panel.setDepth(10000); const bg = uiScene.add.rectangle(0, 0, 500, 300, 0x1a1a2e, 0.95); bg.setStrokeStyle(3, 0x00ff41); panel.add(bg); const title = uiScene.add.text(0, -120, '🏗️ BUILD MODE', { fontSize: '24px', fontFamily: 'Courier New', color: '#00ff41', fontStyle: 'bold' }).setOrigin(0.5); panel.add(title); const instructions = [ 'Controls:', '1-5: Select building type', 'Mouse: Move preview', 'Click: Place building', 'B: Exit build mode', '', 'Green = OK | Red = Blocked' ]; const text = uiScene.add.text(0, 0, instructions.join('\n'), { fontSize: '16px', fontFamily: 'Courier New', color: '#ffffff', align: 'center', lineSpacing: 8 }).setOrigin(0.5); panel.add(text); const closeBtn = uiScene.add.text(0, 120, '[Click to close]', { fontSize: '14px', color: '#888888' }).setOrigin(0.5); panel.add(closeBtn); // Auto-dismiss after 5 seconds uiScene.time.delayedCall(5000, () => { panel.destroy(); }); // Click to dismiss bg.setInteractive(); bg.on('pointerdown', () => { panel.destroy(); }); } }