217 lines
6.2 KiB
JavaScript
217 lines
6.2 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
|
|
}
|
|
};
|
|
}
|
|
|
|
// Till soil at grid position
|
|
tillSoil(gridX, gridY) {
|
|
if (!this.scene.terrainSystem) 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);
|
|
}
|
|
|
|
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++;
|
|
}
|
|
|
|
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}`;
|
|
|
|
// Fallback textures
|
|
if (!this.scene.textures.exists(textureKey)) {
|
|
textureKey = 'carrots_stages'; // Use carrot stages as fallback
|
|
}
|
|
|
|
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));
|
|
}
|
|
}
|
|
|
|
// 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) {
|
|
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);
|
|
|
|
console.log(`🌾 Harvested ${crop.type}! (+${goldEarned} gold)`);
|
|
|
|
// 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));
|
|
}
|
|
});
|
|
}
|
|
}
|