// ==================== ПЕЧЬ (ОБЖИГ) ====================
import { state } from '../core/state.js';
import { BLOCKS } from '../data/blocks.js';
import { ITEMS } from '../data/items.js';
import { SMELTING_RECIPES } from '../data/recipes.js';
import { playSound } from '../audio/sound-engine.js';
import { rebuildHotbar } from '../ui/hotbar.js';
import { getBlock } from '../world/world-storage.js';
const furnacePanel = document.getElementById('furnacePanel');
const furnaceContent = document.getElementById('furnaceContent');
export function initFurnace() {
document.getElementById('furnaceClose').onclick = () => {
furnacePanel.style.display = 'none';
state.currentFurnaceKey = null;
};
}
export function openFurnaceUI(gx, gy) {
state.currentFurnaceKey = `${gx},${gy}`;
furnacePanel.style.display = 'block';
renderFurnaceUI();
}
export function renderFurnaceUI() {
if (!state.currentFurnaceKey) return;
// Проверяем что печь всё ещё существует
const [fgx, fgy] = state.currentFurnaceKey.split(',').map(Number);
const fb = getBlock(fgx, fgy);
if (!fb || fb.t !== 'furnace') {
furnacePanel.style.display = 'none';
state.currentFurnaceKey = null;
return;
}
// Текущий процесс обжига
const active = state.activeFurnaces.get(state.currentFurnaceKey);
let html = '
';
// Доступные рецепты — показываем только те, для которых есть ресурсы
for (let i = 0; i < SMELTING_RECIPES.length; i++) {
const recipe = SMELTING_RECIPES[i];
const haveCount = state.inv[recipe.in] || 0;
const canSmelt = haveCount >= recipe.qty;
// Иконка результата
const outDef = BLOCKS[recipe.out];
const outItem = ITEMS[recipe.out];
const iconStr = outItem ? outItem.icon : (outDef ? '🧱' : '❓');
const nameStr = outItem ? outItem.n : (outDef ? outDef.n : recipe.out);
const inItem = ITEMS[recipe.in];
const inDef = BLOCKS[recipe.in];
const inName = inItem ? inItem.n : (inDef ? inDef.n : recipe.in);
html += `
`;
html += `
${iconStr}
`;
html += `
`;
html += `
${nameStr}
`;
html += `
${inName} x${recipe.qty} (есть: ${haveCount}) • ${recipe.time}с
`;
html += `
`;
html += `
`;
html += `
`;
}
// Текущий прогресс
if (active) {
const pct = Math.min(100, Math.floor((active.progress / active.recipe.time) * 100));
html += `
`;
html += `
🔥 Обжиг: ${pct}%
`;
html += `
`;
}
html += '
';
furnaceContent.innerHTML = html;
}
// Глобальная функция для кнопки обжига
window._smelt = (recipeIdx) => {
if (!state.currentFurnaceKey) return;
const recipe = SMELTING_RECIPES[recipeIdx];
if ((state.inv[recipe.in] || 0) < recipe.qty) return;
// Уже обжигаем в этой печи?
if (state.activeFurnaces.has(state.currentFurnaceKey)) return;
// Забираем ресурсы
state.inv[recipe.in] -= recipe.qty;
// Запускаем обжиг
state.activeFurnaces.set(state.currentFurnaceKey, {
recipe: recipe,
progress: 0
});
playSound('fire');
rebuildHotbar();
renderFurnaceUI();
};
// Тик печей — вызывается в главном цикле
export function tickFurnaces(dt) {
for (const [key, furnace] of state.activeFurnaces) {
furnace.progress += dt;
if (furnace.progress >= furnace.recipe.time) {
// Обжиг завершён — выдаём результат
const outItem = furnace.recipe.out;
if (ITEMS[outItem]) {
state.inv[outItem] = (state.inv[outItem] || 0) + furnace.recipe.outQty;
} else if (BLOCKS[outItem]) {
state.inv[outItem] = (state.inv[outItem] || 0) + furnace.recipe.outQty;
}
playSound('stone_build');
state.activeFurnaces.delete(key);
// Если эта печь открыта — обновляем UI
if (key === state.currentFurnaceKey) {
renderFurnaceUI();
}
}
}
}