Files
novafarma/EMERGENCY_SYSTEMS_RECOVERY/FarmingSystem.js
2026-01-16 02:43:46 +01:00

279 lines
8.4 KiB
JavaScript

class FarmingSystem {
constructor(scene) {
this.scene = scene;
this.crops = []; // Active crops in world
// Crop definitions
this.cropTypes = {
'carrot': {
name: 'Carrot',
growthStages: 4,
daysPerStage: 1,
sellPrice: 10,
seedCost: 5
},
'wheat': {
name: 'Wheat',
growthStages: 4,
daysPerStage: 2,
sellPrice: 15,
seedCost: 8
},
'cannabis': {
name: 'Cannabis',
growthStages: 4,
daysPerStage: 2,
sellPrice: 60,
seedCost: 20
}
};
}
// Till soil at grid position
tillSoil(gridX, gridY) {
if (!this.scene.terrainSystem) return false;
// 🌱 CHECK MICRO FARM BOUNDARY!
if (this.scene.microFarmSystem && !this.scene.microFarmSystem.isTileUnlocked(gridX, gridY)) {
console.log('❌ Cannot till outside farm boundary!');
// Show error message
if (this.scene.events) {
this.scene.events.emit('show-floating-text', {
x: gridX * 48,
y: gridY * 48,
text: '🚫 Unlock farm first!',
color: '#ff0000'
});
}
return false;
}
// Check if already tilled
const key = `${gridX},${gridY}`;
if (this.isTilled(gridX, gridY)) {
console.log('Already tilled!');
return false;
}
// Mark as tilled in terrain
if (!this.scene.terrainSystem.tilledSoil) {
this.scene.terrainSystem.tilledSoil = new Set();
}
this.scene.terrainSystem.tilledSoil.add(key);
// Visual feedback - place tilled soil sprite
const screenPos = this.scene.iso.toScreen(gridX, gridY);
if (this.scene.textures.exists('soil_tilled')) {
const sprite = this.scene.add.sprite(screenPos.x, screenPos.y, 'soil_tilled');
sprite.setOrigin(0.5, 1);
sprite.setScale(1.0);
sprite.setDepth(this.scene.iso.getDepth(gridX, gridY) - 1); // Below crops
// Store reference
if (!this.scene.terrainSystem.tilledSprites) {
this.scene.terrainSystem.tilledSprites = new Map();
}
this.scene.terrainSystem.tilledSprites.set(key, sprite);
}
// Play dig sound
if (this.scene.soundManager) {
this.scene.soundManager.playDig();
}
// ✨ PARTICLE EFFECT - Dig particles
if (this.scene.particleEnhancements) {
this.scene.particleEnhancements.digParticles(screenPos.x, screenPos.y);
}
console.log(`✅ Tilled soil at (${gridX}, ${gridY})`);
return true;
}
isTilled(gridX, gridY) {
if (!this.scene.terrainSystem || !this.scene.terrainSystem.tilledSoil) return false;
return this.scene.terrainSystem.tilledSoil.has(`${gridX},${gridY}`);
}
// Plant seed
plantSeed(gridX, gridY, cropType) {
if (!this.isTilled(gridX, gridY)) {
console.log('Need to till soil first!');
return false;
}
// Check if already has crop
if (this.getCropAt(gridX, gridY)) {
console.log('Already has a crop!');
return false;
}
// Create crop
const crop = {
gridX,
gridY,
type: cropType,
stage: 0,
daysInStage: 0,
plantedDay: this.scene.timeSystem ? this.scene.timeSystem.day : 0
};
this.crops.push(crop);
// Visual - create sprite
this.updateCropSprite(crop);
// Update farm stats
if (this.scene.farmStats) {
this.scene.farmStats.cropsPlanted++;
}
// Play plant sound
if (this.scene.soundManager) {
this.scene.soundManager.playPlant();
}
// ✨ PARTICLE EFFECT - Plant sparkle
if (this.scene.particleEnhancements) {
const screenPos = this.scene.iso.toScreen(gridX, gridY);
this.scene.particleEnhancements.plantSparkle(screenPos.x, screenPos.y);
}
console.log(`🌱 Planted ${cropType} at (${gridX}, ${gridY})`);
return true;
}
getCropAt(gridX, gridY) {
return this.crops.find(c => c.gridX === gridX && c.gridY === gridY);
}
updateCropSprite(crop) {
const screenPos = this.scene.iso.toScreen(crop.gridX, crop.gridY);
// Remove old sprite
if (crop.sprite) {
crop.sprite.destroy();
}
// Determine texture based on stage
let textureKey = `crop_${crop.type}_stage_${crop.stage}`;
if (this.scene.textures.exists(textureKey)) {
crop.sprite = this.scene.add.sprite(screenPos.x, screenPos.y, textureKey);
crop.sprite.setOrigin(0.5, 1);
crop.sprite.setScale(0.8);
crop.sprite.setDepth(this.scene.iso.getDepth(crop.gridX, crop.gridY));
// Apply tilts/fallback tints
if (textureKey === 'carrots_stages' && crop.type === 'cannabis') {
crop.sprite.setTint(0x55ff55); // Green tint for cannabis fallback
} else if (crop.type === 'cannabis') {
crop.sprite.setTint(0x88ff88); // Subtle green boost for specific texture
}
}
}
// Harvest crop
harvestCrop(gridX, gridY) {
const crop = this.getCropAt(gridX, gridY);
if (!crop) return false;
const cropDef = this.cropTypes[crop.type];
const isRipe = crop.stage >= (cropDef.growthStages - 1);
if (!isRipe) {
console.log('Crop not ready yet!');
return false;
}
// Give items
if (this.scene.inventorySystem) {
if (crop.type === 'cannabis') {
this.scene.inventorySystem.addItem('cannabis_buds', 2);
this.scene.inventorySystem.addItem('hemp_fiber', 3);
} else {
this.scene.inventorySystem.addItem(crop.type, 1);
}
}
// Give gold
const goldEarned = cropDef.sellPrice;
if (this.scene.inventorySystem) {
this.scene.inventorySystem.gold += goldEarned;
}
// Update farm stats
if (this.scene.farmStats) {
this.scene.farmStats.totalHarvested++;
this.scene.farmStats.goldEarned += goldEarned;
}
// Remove crop
if (crop.sprite) crop.sprite.destroy();
this.crops = this.crops.filter(c => c !== crop);
// Play harvest sound
if (this.scene.soundManager) {
this.scene.soundManager.playHarvest();
}
console.log(`🌾 Harvested ${crop.type}! (+${goldEarned} gold)`);
// ✨ PARTICLE EFFECT - Harvest burst
const screenPos = this.scene.iso.toScreen(gridX, gridY);
if (this.scene.particleEnhancements) {
this.scene.particleEnhancements.harvestBurst(screenPos.x, screenPos.y, crop.type);
}
// Show floating text
if (this.scene.events) {
this.scene.events.emit('show-floating-text', {
x: screenPos.x,
y: screenPos.y - 30,
text: `+${goldEarned}g`,
color: '#FFD700'
});
}
return true;
}
// Update - called each game day
updateDay() {
this.crops.forEach(crop => {
const cropDef = this.cropTypes[crop.type];
crop.daysInStage++;
// Advance stage?
if (crop.daysInStage >= cropDef.daysPerStage) {
crop.daysInStage = 0;
crop.stage++;
// Cap at max stage
if (crop.stage >= cropDef.growthStages) {
crop.stage = cropDef.growthStages - 1;
}
// Update visual
this.updateCropSprite(crop);
console.log(`🌱 Crop grew! Stage ${crop.stage}/${cropDef.growthStages - 1}`);
}
});
}
// Update - called each frame
update() {
// Update sprite depths if camera moved
this.crops.forEach(crop => {
if (crop.sprite) {
crop.sprite.setDepth(this.scene.iso.getDepth(crop.gridX, crop.gridY));
}
});
}
}