grechka-game/src/ui/craft.js

116 lines
3.9 KiB
JavaScript

// Craft UI
import { state } from '../core/state.js';
import { BLOCKS } from '../data/blocks.js';
import { ITEMS } from '../data/items.js';
import { TOOLS } from '../data/tools.js';
import { RECIPES } from '../data/recipes.js';
import { addTool } from '../data/tools.js';
import { tex } from '../render/textures.js';
import { playSound } from '../audio/sound-engine.js';
import { rebuildHotbar } from './hotbar.js';
import { renderInventory } from './inventory.js';
export function canCraft(r) {
const inv = state.inv;
console.log('[CRAFT] Checking recipe for:', r.out, '- cost:', r.cost);
for (const res in r.cost) {
const have = inv[res] || 0;
const need = r.cost[res];
console.log('[CRAFT] Resource:', res, '- have:', have, '- need:', need, '- can craft:', have >= need);
if (have < need) return false;
}
return true;
}
export function renderCraft() {
const inv = state.inv;
const recipesEl = state.recipesEl;
recipesEl.innerHTML = '';
for (const r of RECIPES) {
const row = document.createElement('div');
row.className = 'recipe';
const icon = document.createElement('div');
icon.className = 'ricon';
// Иконка — блок, инструмент или предмет
if (tex[r.out]) {
icon.style.backgroundImage = `url(${tex[r.out].toDataURL()})`;
} else if (TOOLS[r.out]) {
icon.textContent = TOOLS[r.out].icon;
icon.style.fontSize = '24px';
icon.style.display = 'flex';
icon.style.alignItems = 'center';
icon.style.justifyContent = 'center';
} else if (ITEMS[r.out]) {
icon.textContent = ITEMS[r.out].icon;
icon.style.fontSize = '24px';
icon.style.display = 'flex';
icon.style.alignItems = 'center';
icon.style.justifyContent = 'center';
}
const info = document.createElement('div');
info.className = 'rinfo';
const nm = document.createElement('div');
nm.className = 'rname';
const itemName = BLOCKS[r.out]?.n || TOOLS[r.out]?.n || ITEMS[r.out]?.n || r.out;
nm.textContent = `${itemName} x${r.qty}`;
const cs = document.createElement('div');
cs.className = 'rcost';
cs.textContent = Object.keys(r.cost).map(x => {
const cn = BLOCKS[x]?.n || TOOLS[x]?.n || ITEMS[x]?.n || x;
return `${cn}: ${(inv[x] || 0)}/${r.cost[x]}`;
}).join(' ');
info.appendChild(nm); info.appendChild(cs);
const btn = document.createElement('button');
btn.className = 'rcraft';
btn.textContent = 'Создать';
btn.disabled = !canCraft(r);
btn.onclick = () => {
if (!canCraft(r)) return;
playSound('click');
for (const res in r.cost) inv[res] -= r.cost[res];
inv[r.out] = (inv[r.out] || 0) + r.qty;
if (TOOLS[r.out]) addTool(r.out);
rebuildHotbar();
renderCraft();
};
row.appendChild(icon); row.appendChild(info); row.appendChild(btn);
recipesEl.appendChild(row);
}
}
let craftOpen = false;
let inventoryOpen = false;
export function toggleCraft() {
playSound('click'); // Звук клика по кнопке
craftOpen = !craftOpen;
state.craftPanel.style.display = craftOpen ? 'block' : 'none';
if (craftOpen) {
renderCraft();
// Закрываем инвентарь если открыт крафт
inventoryOpen = false;
state.inventoryPanel.style.display = 'none';
}
}
export function closeCraft() {
playSound('click'); // Звук клика по кнопке
craftOpen = false;
state.craftPanel.style.display = 'none';
}
export function toggleInventory() {
playSound('click'); // Звук клика по кнопке
inventoryOpen = true;
state.inventoryPanel.style.display = 'block';
renderInventory();
// Закрываем крафт если открыт инвентарь
craftOpen = false;
state.craftPanel.style.display = 'none';
}
export function closeInventory() {
playSound('click'); // Звук клика по кнопке
inventoryOpen = false;
state.inventoryPanel.style.display = 'none';
}