164 lines
5.1 KiB
JavaScript
164 lines
5.1 KiB
JavaScript
/**
|
|
* PERENNIAL CROPS SYSTEM
|
|
* Handles multi-season crops like Apple Trees
|
|
*/
|
|
class PerennialCropSystem {
|
|
constructor(scene) {
|
|
this.scene = scene;
|
|
this.perennials = []; // Array of perennial objects
|
|
|
|
// Perennial definitions
|
|
this.perennialData = {
|
|
'apple_tree': {
|
|
name: 'Apple Tree',
|
|
growthTime: 120000, // 2 minutes to maturity
|
|
harvestSeason: 'autumn', // Only harvest in autumn
|
|
harvestYield: { 'apple': 5 },
|
|
regrowthTime: 30000, // 30s to regrow apples
|
|
texture: 'apple_tree',
|
|
stages: ['sapling', 'young', 'mature', 'fruiting']
|
|
}
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Plant a perennial crop
|
|
*/
|
|
plant(gridX, gridY, type) {
|
|
if (!this.perennialData[type]) {
|
|
console.error('Unknown perennial type:', type);
|
|
return false;
|
|
}
|
|
|
|
const perennial = {
|
|
type,
|
|
gridX,
|
|
gridY,
|
|
plantedTime: Date.now(),
|
|
stage: 0, // 0=sapling, 1=young, 2=mature, 3=fruiting
|
|
canHarvest: false,
|
|
sprite: null,
|
|
healthBar: null
|
|
};
|
|
|
|
this.perennials.push(perennial);
|
|
this.createSprite(perennial);
|
|
console.log(`🌳 Planted ${type} at ${gridX},${gridY}`);
|
|
return true;
|
|
}
|
|
|
|
createSprite(perennial) {
|
|
const data = this.perennialData[perennial.type];
|
|
const screenPos = this.scene.iso.toScreen(perennial.gridX, perennial.gridY);
|
|
|
|
// Create tree sprite
|
|
perennial.sprite = this.scene.add.sprite(
|
|
screenPos.x + this.scene.terrainOffsetX,
|
|
screenPos.y + this.scene.terrainOffsetY,
|
|
data.texture || 'tree'
|
|
);
|
|
perennial.sprite.setOrigin(0.5, 1);
|
|
perennial.sprite.setScale(0.5);
|
|
perennial.sprite.setDepth(this.scene.iso.getDepth(perennial.gridX, perennial.gridY, this.scene.iso.LAYER_OBJECTS));
|
|
|
|
this.updateSprite(perennial);
|
|
}
|
|
|
|
updateSprite(perennial) {
|
|
const data = this.perennialData[perennial.type];
|
|
|
|
// Update tint based on stage
|
|
if (perennial.stage === 0) {
|
|
perennial.sprite.setTint(0x88aa88); // Light green (sapling)
|
|
perennial.sprite.setScale(0.3);
|
|
} else if (perennial.stage === 1) {
|
|
perennial.sprite.setTint(0x44aa44); // Medium green (young)
|
|
perennial.sprite.setScale(0.4);
|
|
} else if (perennial.stage === 2) {
|
|
perennial.sprite.clearTint(); // Normal (mature)
|
|
perennial.sprite.setScale(0.5);
|
|
} else if (perennial.stage === 3) {
|
|
perennial.sprite.setTint(0xff8844); // Orange tint (fruiting)
|
|
perennial.sprite.setScale(0.5);
|
|
}
|
|
}
|
|
|
|
update(delta, currentSeason) {
|
|
this.perennials.forEach(perennial => {
|
|
const data = this.perennialData[perennial.type];
|
|
const age = Date.now() - perennial.plantedTime;
|
|
|
|
// Growth stages
|
|
if (perennial.stage < 3) {
|
|
const stageTime = data.growthTime / 3;
|
|
const newStage = Math.min(3, Math.floor(age / stageTime));
|
|
|
|
if (newStage !== perennial.stage) {
|
|
perennial.stage = newStage;
|
|
this.updateSprite(perennial);
|
|
|
|
if (perennial.stage === 3) {
|
|
console.log(`🌳 ${data.name} reached maturity!`);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fruiting logic (only in correct season and when mature)
|
|
if (perennial.stage === 3 && currentSeason === data.harvestSeason) {
|
|
if (!perennial.canHarvest) {
|
|
perennial.canHarvest = true;
|
|
perennial.sprite.setTint(0xff6644); // Bright orange (ready to harvest)
|
|
}
|
|
} else if (perennial.canHarvest && currentSeason !== data.harvestSeason) {
|
|
perennial.canHarvest = false;
|
|
perennial.sprite.clearTint();
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Attempt to harvest perennial
|
|
*/
|
|
harvest(gridX, gridY) {
|
|
const perennial = this.perennials.find(p =>
|
|
p.gridX === gridX && p.gridY === gridY
|
|
);
|
|
|
|
if (!perennial) return null;
|
|
|
|
if (!perennial.canHarvest) {
|
|
return { error: 'Not ready to harvest' };
|
|
}
|
|
|
|
const data = this.perennialData[perennial.type];
|
|
|
|
// Reset harvest state (will regrow)
|
|
perennial.canHarvest = false;
|
|
perennial.sprite.setTint(0x88aa88); // Back to green
|
|
|
|
console.log(`🍎 Harvested ${data.name}!`);
|
|
return data.harvestYield;
|
|
}
|
|
|
|
/**
|
|
* Check if position has perennial
|
|
*/
|
|
hasPerennial(gridX, gridY) {
|
|
return this.perennials.some(p => p.gridX === gridX && p.gridY === gridY);
|
|
}
|
|
|
|
/**
|
|
* Get perennial at position
|
|
*/
|
|
getPerennial(gridX, gridY) {
|
|
return this.perennials.find(p => p.gridX === gridX && p.gridY === gridY);
|
|
}
|
|
|
|
destroy() {
|
|
this.perennials.forEach(p => {
|
|
if (p.sprite) p.sprite.destroy();
|
|
});
|
|
this.perennials = [];
|
|
}
|
|
}
|