Files
novafarma/old_logic/src_backup_1768938138/entities/LootChest.js
2026-01-21 01:08:21 +01:00

128 lines
4.4 KiB
JavaScript

class LootChest {
constructor(scene, gridX, gridY, lootTable = 'basic') {
this.scene = scene;
this.gridX = gridX;
this.gridY = gridY;
this.lootTable = lootTable;
this.isOpened = false;
this.createSprite();
}
createSprite() {
const screenPos = this.scene.iso.toScreen(this.gridX, this.gridY);
const x = screenPos.x + this.scene.terrainOffsetX;
const y = screenPos.y + this.scene.terrainOffsetY;
// Create chest sprite (using existing chest texture)
this.sprite = this.scene.add.sprite(x, y, 'chest');
this.sprite.setOrigin(0.5, 1);
this.sprite.setScale(0.1); // Tiny size!
this.sprite.setDepth(this.scene.iso.getDepth(this.gridX, this.gridY, this.scene.iso.LAYER_OBJECTS));
// Golden glow for unopened chests
if (!this.isOpened) {
this.sprite.setTint(0xFFDD00);
// Gentle floating animation
this.scene.tweens.add({
targets: this.sprite,
y: y - 5,
duration: 1500,
yoyo: true,
repeat: -1,
ease: 'Sine.easeInOut'
});
}
}
getLootTable() {
const tables = {
'basic': [
{ item: 'seeds', count: 10, chance: 1.0 },
{ item: 'seeds_wheat', count: 5, chance: 0.8 },
{ item: 'wood', count: 10, chance: 0.6 },
{ item: 'stone', count: 10, chance: 0.5 }
],
'farm_starter': [
{ item: 'seeds_wheat', count: 15, chance: 1.0 },
{ item: 'seeds_corn', count: 10, chance: 1.0 },
{ item: 'hoe', count: 1, chance: 1.0 },
{ item: 'watering_can', count: 1, chance: 0.8 },
{ item: 'wood', count: 20, chance: 0.9 }
],
'city': [
{ item: 'gold', count: 50, chance: 1.0 },
{ item: 'scrap_metal', count: 5, chance: 0.8 }, // City-specific
{ item: 'chips', count: 2, chance: 0.6 }, // Electronics
{ item: 'stone', count: 30, chance: 0.9 },
{ item: 'iron', count: 10, chance: 0.7 },
{ item: 'seeds_corn', count: 5, chance: 0.6 },
{ item: 'axe', count: 1, chance: 0.3 },
{ item: 'pickaxe', count: 1, chance: 0.3 }
],
'elite': [
{ item: 'gold', count: 100, chance: 1.0 },
{ item: 'scrap_metal', count: 15, chance: 1.0 }, // Lots of scrap
{ item: 'chips', count: 5, chance: 0.9 }, // Rare electronics
{ item: 'iron', count: 25, chance: 1.0 },
{ item: 'diamond', count: 3, chance: 0.5 },
{ item: 'seeds_corn', count: 20, chance: 0.8 }
]
};
return tables[this.lootTable] || tables['basic'];
}
open(player) {
if (this.isOpened) return false;
this.isOpened = true;
this.sprite.clearTint();
this.sprite.setTint(0x888888); // Gray for opened
// Stop animation
this.scene.tweens.killTweensOf(this.sprite);
// Spawn loot
const loot = this.getLootTable();
for (const entry of loot) {
if (Math.random() < entry.chance) {
if (entry.item === 'gold') {
if (this.scene.inventorySystem) {
this.scene.inventorySystem.addGold(entry.count);
}
} else {
if (this.scene.interactionSystem) {
this.scene.interactionSystem.spawnLoot(
this.gridX,
this.gridY,
entry.item,
entry.count
);
}
}
}
}
// Effects
this.scene.events.emit('show-floating-text', {
x: this.gridX * 48,
y: this.gridY * 48,
text: '📦 Chest Opened!',
color: '#FFD700'
});
if (this.scene.soundManager) {
this.scene.soundManager.playHarvest();
}
console.log(`📦 Opened ${this.lootTable} chest at ${this.gridX},${this.gridY}`);
return true;
}
interact(player) {
return this.open(player);
}
}