const CROP_DATA = { 'wheat': { name: 'Wheat', seedItem: 'seeds_wheat', harvestItem: 'wheat', stages: 4, growthTime: 10, // Seconds per stage (Total 30s) regrow: false, color: 0xffdd00 }, 'corn': { name: 'Corn', seedItem: 'seeds_corn', harvestItem: 'corn', stages: 4, growthTime: 15, // Slower (Total 45s) regrow: true, // Corn regrows! regrowStage: 2, // Goes back to stage 2 color: 0xffaa00 } }; class FarmingSystem { constructor(scene) { this.scene = scene; this.growthTimer = 0; this.growthTickRate = 1000; } // Called by InteractionSystem interact(gridX, gridY, toolType) { const terrain = this.scene.terrainSystem; const tile = terrain.getTile(gridX, gridY); if (!tile) return false; // 1. HARVEST if (tile.hasCrop) { const crop = terrain.cropsMap.get(`${gridX},${gridY}`); if (crop && crop.stage === CROP_DATA[crop.type].stages) { this.harvest(gridX, gridY); return true; } } // 2. TILLING if (toolType === 'hoe') { const typeName = tile.type.name || tile.type; if (typeName.includes('grass') || typeName === 'dirt') { if (!tile.hasDecoration && !tile.hasCrop) { terrain.setTileType(gridX, gridY, 'farmland'); if (this.scene.soundManager) this.scene.soundManager.playDig(); return true; } } } // 3. PLANTING // Check if tool is a seed const isSeed = toolType.startsWith('seeds_') || toolType === 'seeds'; if (isSeed) { const typeName = tile.type.name || tile.type; if (typeName === 'farmland' && !tile.hasCrop && !tile.hasDecoration) { // Determine crop type from seed name // "seeds_corn" -> "corn", "seeds" -> "wheat" (default) let cropType = 'wheat'; if (toolType === 'seeds_corn') cropType = 'corn'; if (toolType === 'seeds_wheat') cropType = 'wheat'; this.plant(gridX, gridY, cropType); // Consumption if (this.scene.inventorySystem) { this.scene.inventorySystem.removeItem(toolType, 1); } return true; } } return false; } plant(x, y, type = 'wheat') { const terrain = this.scene.terrainSystem; const data = CROP_DATA[type]; const cropData = { gridX: x, gridY: y, stage: 1, type: type, timer: 0, maxTime: data.growthTime }; terrain.addCrop(x, y, cropData); if (this.scene.soundManager) { this.scene.soundManager.playPlant(); } if (this.scene.questSystem) this.scene.questSystem.trackAction('plant'); console.log(`🌱 Planted ${type} at ${x},${y}`); } harvest(x, y) { const terrain = this.scene.terrainSystem; const crop = terrain.cropsMap.get(`${x},${y}`); if (!crop) return; const data = CROP_DATA[crop.type]; // Sound if (this.scene.soundManager) { this.scene.soundManager.playHarvest(); } // Loot if (this.scene.interactionSystem) { this.scene.interactionSystem.spawnLoot(x, y, data.harvestItem, 1); // Chance for seeds if (Math.random() > 0.4) { this.scene.interactionSystem.spawnLoot(x, y, data.seedItem, 1); } // Extra harvest chance if (Math.random() > 0.5) { this.scene.interactionSystem.spawnLoot(x, y, data.harvestItem, 1); } // XP Gain if (this.scene.statsSystem) { this.scene.statsSystem.addXP(data.harvestItem === 'corn' ? 15 : 10); } } // Regrow or Destroy if (data.regrow) { crop.stage = data.regrowStage; crop.timer = 0; terrain.updateCropVisual(x, y, crop.stage); console.log(`🔄 ${crop.type} regrowing...`); } else { terrain.removeCrop(x, y); } } update(delta) { this.growthTimer += delta; if (this.growthTimer < 1000) return; const secondsPassed = this.growthTimer / 1000; this.growthTimer = 0; const terrain = this.scene.terrainSystem; if (!terrain) return; for (const [key, crop] of terrain.cropsMap) { const data = CROP_DATA[crop.type]; if (!data) continue; if (crop.stage < data.stages) { crop.timer += secondsPassed; if (crop.timer >= data.growthTime) { crop.stage++; crop.timer = 0; terrain.updateCropVisual(crop.gridX, crop.gridY, crop.stage); } } } } }