farma updejt
This commit is contained in:
@@ -1,10 +1,30 @@
|
||||
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 = 5000; // Check growth every 5 seconds (real time)
|
||||
// Or better: based on TimeSystem days?
|
||||
// For fast testing: rapid growth.
|
||||
this.growthTickRate = 1000;
|
||||
}
|
||||
|
||||
// Called by InteractionSystem
|
||||
@@ -14,40 +34,45 @@ class FarmingSystem {
|
||||
|
||||
if (!tile) return false;
|
||||
|
||||
// 1. HARVEST (Right click or just click ripe crop?)
|
||||
// Let's say if it has crop and it is ripe, harvest it regardless of tool.
|
||||
// 1. HARVEST
|
||||
if (tile.hasCrop) {
|
||||
const crop = terrain.cropsMap.get(`${gridX},${gridY}`);
|
||||
console.log('🌾 Check harvest:', crop);
|
||||
if (crop && crop.stage === 4) {
|
||||
if (crop && crop.stage === CROP_DATA[crop.type].stages) {
|
||||
this.harvest(gridX, gridY);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. TILLING (Requires Hoe)
|
||||
// 2. TILLING
|
||||
if (toolType === 'hoe') {
|
||||
const typeName = tile.type.name || tile.type;
|
||||
if (typeName.includes('grass') || typeName === 'dirt') {
|
||||
if (!tile.hasDecoration && !tile.hasCrop) {
|
||||
console.log('🚜 Tilling soil...');
|
||||
terrain.setTileType(gridX, gridY, 'farmland'); // This sets it to string 'farmland' usually? or object? Assuming method handles it.
|
||||
// Play sound
|
||||
terrain.setTileType(gridX, gridY, 'farmland');
|
||||
if (this.scene.soundManager) this.scene.soundManager.playDig();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. PLANTING (Requires Seeds)
|
||||
if (toolType === 'seeds') {
|
||||
// 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) {
|
||||
console.log('🌱 Planting seeds...');
|
||||
this.plant(gridX, gridY);
|
||||
// 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';
|
||||
|
||||
// Remove 1 seed from inventory
|
||||
this.plant(gridX, gridY, cropType);
|
||||
|
||||
// Consumption
|
||||
if (this.scene.inventorySystem) {
|
||||
this.scene.inventorySystem.removeItem('seeds', 1);
|
||||
this.scene.inventorySystem.removeItem(toolType, 1);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -56,57 +81,72 @@ class FarmingSystem {
|
||||
return false;
|
||||
}
|
||||
|
||||
plant(x, y) {
|
||||
plant(x, y, type = 'wheat') {
|
||||
const terrain = this.scene.terrainSystem;
|
||||
const data = CROP_DATA[type];
|
||||
|
||||
const cropData = {
|
||||
gridX: x,
|
||||
gridY: y,
|
||||
stage: 1, // Seeds
|
||||
type: 'wheat', // Default for now
|
||||
stage: 1,
|
||||
type: type,
|
||||
timer: 0,
|
||||
maxTime: 10 // Seconds per stage?
|
||||
maxTime: data.growthTime
|
||||
};
|
||||
terrain.addCrop(x, y, cropData);
|
||||
|
||||
// Plant Sound
|
||||
if (this.scene.soundManager) {
|
||||
this.scene.soundManager.playPlant();
|
||||
}
|
||||
|
||||
// Quest Tracking
|
||||
if (this.scene.questSystem) this.scene.questSystem.trackAction('plant');
|
||||
console.log(`🌱 Planted ${type} at ${x},${y}`);
|
||||
}
|
||||
|
||||
harvest(x, y) {
|
||||
const terrain = this.scene.terrainSystem;
|
||||
console.log('🌾 Harvesting!');
|
||||
const crop = terrain.cropsMap.get(`${x},${y}`);
|
||||
if (!crop) return;
|
||||
|
||||
// Harvest Sound
|
||||
const data = CROP_DATA[crop.type];
|
||||
|
||||
// Sound
|
||||
if (this.scene.soundManager) {
|
||||
this.scene.soundManager.playHarvest();
|
||||
}
|
||||
|
||||
// Spawn loot
|
||||
// Loot
|
||||
if (this.scene.interactionSystem) {
|
||||
this.scene.interactionSystem.spawnLoot(x, y, 'wheat');
|
||||
this.scene.interactionSystem.spawnLoot(x, y, 'seeds'); // Return seeds
|
||||
// 50% chance for extra seeds
|
||||
if (Math.random() > 0.5) this.scene.interactionSystem.spawnLoot(x, y, 'seeds');
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove crop
|
||||
terrain.removeCrop(x, y);
|
||||
|
||||
// Revert to dirt? Or keep farmland? Usually keeps farmland.
|
||||
// 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) {
|
||||
// Growth Logic
|
||||
// Iterate all crops? Expensive?
|
||||
// Better: Random tick like Minecraft or list iteration.
|
||||
// Since we have cropsMap, iteration is easy.
|
||||
|
||||
// Only run every 1 second (1000ms) to save PERF
|
||||
this.growthTimer += delta;
|
||||
if (this.growthTimer < 1000) return;
|
||||
const secondsPassed = this.growthTimer / 1000;
|
||||
@@ -116,20 +156,15 @@ class FarmingSystem {
|
||||
if (!terrain) return;
|
||||
|
||||
for (const [key, crop] of terrain.cropsMap) {
|
||||
if (crop.stage < 4) {
|
||||
const data = CROP_DATA[crop.type];
|
||||
if (!data) continue;
|
||||
|
||||
if (crop.stage < data.stages) {
|
||||
crop.timer += secondsPassed;
|
||||
|
||||
// Growth thresholds (fast for testing)
|
||||
// Stage 1 -> 2: 5s
|
||||
// Stage 2 -> 3: 10s
|
||||
// Stage 3 -> 4: 15s
|
||||
const needed = 5;
|
||||
|
||||
if (crop.timer >= needed) {
|
||||
if (crop.timer >= data.growthTime) {
|
||||
crop.stage++;
|
||||
crop.timer = 0;
|
||||
terrain.updateCropVisual(crop.gridX, crop.gridY, crop.stage);
|
||||
// Particle effect?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user