/* Robot Clash - workshop extras: weapon/shape editor, paint tab with paint slots, hangar (several robots). Part of game.js (split for size): created once by game.js with the shared context G. Live state (save, mode, F, ...) is read as G.; services/helpers are taken from G at creation. */ (function (root) { 'use strict'; const RC = (root.RC = root.RC || {}); RC.GameWorkshop = function (G) { const { $, GLYPH_SLOT, MAX_ROBOTS, SFX, act, esc, hud, layer, paintObj, painter, persist, shop, syncActive, t, weaponName } = G; // ---- weapon/shape editor: rendered inside the shop's #cards (tabs stay visible), robot held still ---- function refreshEditorRobot() { G.view.showGarage(G.save.build, paintObj()); G.view.freeze(true); G.view.setEditWeapon(G.editSel); } function reDrawEditor() { persist(); hud(); refreshEditorRobot(); shop.render(); } // after a change: 3D + tabs + meters + editor // weapon detail chips (mirrors the shop's part chips, weapon stats only) function wChips(p) { const s = []; if (p.dmg) s.push(`${t('st_dmg', { n: p.dmg })}`); if (p.dps) s.push(`${t('st_dps', { n: p.dps })}`); if (p.range) s.push(`${t('st_range', { n: p.range })}`); s.push(`⌨ ${t('hk_weapon')}`); s.push(`⚖️ ${p.weight} kg`); if (p.power) s.push(`⚡ ${p.power}`); return s.join(''); } // show the clicked weapon live on the robot (a mounted one, else a temporary front preview) function previewWeapon(id) { const weps = G.save.build.weapons || []; const idx = weps.findIndex(w => w.id === id); if (idx >= 0) { G.editSel = idx; refreshEditorRobot(); } else { const pv = Object.assign({}, G.save.build, { weapons: weps.concat([{ id, side: 'front', up: 0 }]) }); G.view.showGarage(pv, paintObj()); G.view.freeze(true); G.view.setEditWeapon(weps.length); } } function renderWeaponEditor() { const cards = $('cards'), wp = $('wplace'); if (!G.save || !cards) return; const b = G.save.build, weps = b.weapons || []; G.editSel = Math.min(G.editSel, Math.max(0, weps.length - 1)); const cat = RC.partsFor('weapon'); const canAdd = weps.length < RC.MAX_WEAPONS; if (!G.wPrev || !RC.PARTS[G.wPrev]) G.wPrev = (weps[G.editSel] && weps[G.editSel].id) || cat[0].id; // right (#cards): the weapon catalog, same card grid as the other slots — click = preview cards.className = ''; cards.innerHTML = cat.map(p => { const mounted = weps.filter(w => w.id === p.id).length; const badge = mounted ? `×${mounted}` : ''; const price = `
🪙 ${Kit.fmt(p.cost)}
`; return `
${badge}${RC.pic('part_' + p.id, GLYPH_SLOT.weapon)}
${p.name}
${price}
`; }).join(''); cards.querySelectorAll('[data-prev]').forEach(x => x.onclick = () => { G.wPrev = x.dataset.prev; SFX.click(); previewWeapon(G.wPrev); renderWeaponEditor(); }); // detail panel: previewed weapon + Montieren / Entfernen (like the other slots) const dp = $('detail'), pv = RC.PARTS[G.wPrev]; if (dp && pv) { const mounted = weps.filter(w => w.id === G.wPrev).length, poor = G.save.money < pv.cost; let act = ``; if (mounted) act += ``; if (!canAdd) act += `${t('ed_max', { n: RC.MAX_WEAPONS })}`; dp.innerHTML = `${RC.pic('part_' + G.wPrev, GLYPH_SLOT.weapon)}
${pv.name}${mounted ? ` ×${mounted}` : ''}
${t('pd_' + G.wPrev)}
${wChips(pv)}
${act}
`; dp.querySelectorAll('button[data-wact]').forEach(x => x.onclick = () => { if (x.dataset.wact === 'add') addWeapon(G.wPrev); else { const i = weps.map(w => w.id).lastIndexOf(G.wPrev); if (i >= 0) removeWeapon(i); } }); } // left (#wplace): placement of the active mounted weapon + shape (no sell list) if (wp) { const sel = weps[G.editSel]; const sides = [['front', t('mt_front')], ['back', t('mt_back')], ['left', t('mt_left')], ['right', t('mt_right')]]; const srow = (key, label, lo, hi) => { const val = (b.body && b.body[key]) || 1; return `
${t('des_' + label)}${Math.round(val * 100)}%
`; }; wp.classList.remove('hide'); wp.innerHTML = `
${t('ed_weapons')} ${weps.length}/${RC.MAX_WEAPONS}
` + (sel ? `
${t('ed_place')} — ${RC.PARTS[sel.id].name}
` + `
${sides.map(([k, lb]) => ``).join('')}
` + `
${t('des_height')}${Math.round(((sel.up || 0) + 0.3) / 0.9 * 100)}%
` : `${t('ed_noweapons')}`) + `
${t('s_body')}
${srow('hx', 'height', 0.7, 1.6)}${srow('wx', 'width', 0.8, 1.4)}${srow('lx', 'length', 0.8, 1.3)}`; wp.querySelectorAll('[data-side]').forEach(x => x.onclick = () => { if (!sel) return; act({ type: 'weaponPlace', index: G.editSel, side: x.dataset.side }); SFX.click(); reDrawEditor(); }); if ($('edUp')) $('edUp').oninput = e => { if (sel) { sel.up = +e.target.value; refreshEditorRobot(); } }; // live preview while dragging if ($('edUp')) $('edUp').onchange = e => { if (sel) act({ type: 'weaponPlace', index: G.editSel, up: +e.target.value }); reDrawEditor(); }; wp.querySelectorAll('[data-body]').forEach(x => { x.oninput = e => { b.body = b.body || { hx: 1, wx: 1, lx: 1 }; b.body[e.target.dataset.body] = +e.target.value; refreshEditorRobot(); }; x.onchange = e => { act({ type: 'body', [e.target.dataset.body]: +e.target.value }); reDrawEditor(); }; }); } if (weps[G.editSel] && weps[G.editSel].id === G.wPrev) G.view.setEditWeapon(G.editSel); } function addWeapon(id) { if (!act({ type: 'weaponAdd', part: id }).ok) { SFX.deny(); return; } G.editSel = G.save.build.weapons.length - 1; G.wPrev = id; G.view.setEditWeapon(G.editSel); SFX.buy(); reDrawEditor(); } function removeWeapon(i) { if (!act({ type: 'weaponRemove', index: i }).ok) { SFX.deny(); return; } if (G.editSel >= G.save.build.weapons.length) G.editSel = Math.max(0, G.save.build.weapons.length - 1); G.view.setEditWeapon(G.editSel); SFX.buy(); reDrawEditor(); } // ---- paint tab: 3 save slots + quick colours + the inline painter (all inside #cards) ---- const phex = c => '#' + (c >>> 0).toString(16).padStart(6, '0'); function paintTab(el) { let hdr = $('paintHdr'); if (!hdr) { el.innerHTML = ''; hdr = document.createElement('div'); hdr.id = 'paintHdr'; hdr.className = 'phdr'; el.appendChild(hdr); } $('shop').classList.add('paintmode'); // hide category tabs / detail / fitted bar while painting renderPaintHeader(hdr); if (!painter.isOpen) painter.open(el); // mount the painter inline, below the header } function paintLeave() { $('shop').classList.remove('paintmode'); if (!painter.isOpen) return; const art = painter.hasArt(); G.save.paint.decal = art ? layer.toDataURL('image/png') : null; G.save.paint.drawn = art; painter.close(); persist(); const hdr = $('paintHdr'); if (hdr) hdr.remove(); } function renderPaintHeader(hdr) { const slots = G.save.paintSlots || []; hdr.innerHTML = `
${slots.map((s, i) => `
${s.decal ? '🖌' : ''}
`).join('')}
` + `

${t('paint_body')}

` + `

${t('paint_accent')}

` + `

${t('paint_schemes')}

`; const lockP = c => (RC.PAINT_REWARDS || []).includes(c) && !(G.save.unlockedPaints || []).includes(c); // reward colour, not earned yet for (const [id, key] of [['paintBody', 'body'], ['paintAccent', 'accent']]) { $(id).innerHTML = RC.PAINTS.map(c => `${lockP(c) ? '🔒' : ''}`).join(''); $(id).querySelectorAll('.sw').forEach(sw => sw.onclick = () => { if (sw.classList.contains('locked')) { SFX.deny && SFX.deny(); return; } G.save.paint[key] = +sw.dataset.c; SFX.click(); persist(); G.view.showGarage(G.save.build, paintObj()); if (painter.sync) painter.sync(); renderPaintHeader(hdr); }); } $('paintSchemes').innerHTML = RC.SCHEMES.map(([b2, a2]) => { const on = G.save.paint.body === b2 && G.save.paint.accent === a2; return ``; }).join(''); $('paintSchemes').querySelectorAll('.sw').forEach(sw => sw.onclick = () => { G.save.paint.body = +sw.dataset.b; G.save.paint.accent = +sw.dataset.a; SFX.click(); persist(); G.view.showGarage(G.save.build, paintObj()); if (painter.sync) painter.sync(); renderPaintHeader(hdr); }); hdr.querySelectorAll('[data-load]').forEach(x => x.onclick = e => { if (e.target.dataset.save) return; loadPaintSlot(+x.dataset.load); }); hdr.querySelectorAll('[data-save]').forEach(x => x.onclick = e => { e.stopPropagation(); savePaintSlot(+x.dataset.save); }); } function loadPaintSlot(i) { const s = G.save.paintSlots[i]; if (!s) return; G.save.paintSlot = i; G.save.paint.body = s.body; G.save.paint.accent = s.accent; G.save.paint.decal = s.decal || null; G.save.paint.drawn = !!s.drawn; SFX.click(); persist(); RC.layerFromData(layer, G.save.paint.decal, () => { G.view.showGarage(G.save.build, paintObj()); if (painter.sync) painter.sync(); }); const hdr = $('paintHdr'); if (hdr) renderPaintHeader(hdr); } function savePaintSlot(i) { const art = painter.hasArt(); G.save.paintSlots[i] = { body: G.save.paint.body, accent: G.save.paint.accent, decal: art ? layer.toDataURL('image/png') : (G.save.paint.decal || null), drawn: art || !!G.save.paint.drawn }; G.save.paintSlot = i; SFX.buy(); persist(); const hdr = $('paintHdr'); if (hdr) renderPaintHeader(hdr); } // hangar: switch / add / delete robot slots (owned parts are shared) function switchRobot(i) { if (i === G.save.active || !G.save.robots[i]) return; syncActive(); if (!act({ type: 'robotSwitch', index: i }).ok) return; persist(); RC.layerFromData(layer, G.save.paint.decal, () => { if (G.mode === 'garage') G.view.showGarage(G.save.build, paintObj()); }); shop.reset(); shop.render(); G.view.showGarage(G.save.build, paintObj()); renderHangar(); } function addRobot() { syncActive(); if (!act({ type: 'robotNew', name: t('robot_n', { n: G.save.robots.length + 1 }) }).ok) { SFX.deny(); return; } persist(); RC.layerFromData(layer, G.save.paint.decal, () => { if (G.mode === 'garage') G.view.showGarage(G.save.build, paintObj()); }); shop.reset(); shop.render(); G.view.showGarage(G.save.build, paintObj()); renderHangar(); SFX.buy(); } function delRobot(i) { syncActive(); if (!act({ type: 'robotDelete', index: i }).ok) { SFX.deny(); return; } persist(); RC.layerFromData(layer, G.save.paint.decal, () => {}); shop.reset(); shop.render(); G.view.showGarage(G.save.build, paintObj()); renderHangar(); } // hangar management (shop top-tab): list of robots with weapon, switch / delete + add button function renderHangar(el) { el = el || $('cards'); if (!el) return; const rows = G.save.robots.map((r, i) => `
${esc(r.name)}
${esc(weaponName(r.build))}
${i === G.save.active ? `${t('active_robot')}` : ``} ${G.save.robots.length > 1 ? `` : ''}
`).join(''); el.innerHTML = `
${rows}
${G.save.robots.length < MAX_ROBOTS ? `` : `

${t('hangar_full')}

`}`; el.querySelectorAll('[data-use]').forEach(b => { b.onclick = () => { SFX.click(); switchRobot(+b.dataset.use); shop.setTab('build'); } ; }); el.querySelectorAll('[data-del]').forEach(b => { b.onclick = () => { SFX.click(); delRobot(+b.dataset.del); renderHangar(); } ; }); if ($('hAdd')) $('hAdd').onclick = () => { addRobot(); shop.setTab('build'); }; } return { refreshEditorRobot, reDrawEditor, wChips, previewWeapon, renderWeaponEditor, addWeapon, removeWeapon, phex, paintTab, paintLeave, renderPaintHeader, loadPaintSlot, savePaintSlot, switchRobot, addRobot, delRobot, renderHangar }; }; })(window);