/* Robot Clash - workshop shop (#shop in index.html): category tiles, part cards with sprites, detail panel with buy / fit / sell and stat comparison, robot stats, paint, mechanic tips. Needs parts.js, robot.js, kit-core.js. Sprites from ComfyGen (assets/Sprites/.png) with fallback glyphs while they are missing. RC.pic(name, glyph, cls) -> '' with the sprite (shared helper) const shop = RC.Shop({ t, getSave, onBuild(), onPaint(), onSave(), sfx: { click, buy, deny } }); shop.render() / shop.tip() redraw / next mechanic tip */ (function (root) { 'use strict'; const RC = root.RC, $ = Kit.$; RC.PAINTS = [0xd8452b, 0xff7a1a, 0xf2c230, 0x5fae3b, 0x2e9c8f, 0x2a6fd8, 0x6a3fb0, 0xd94f9a, 0xe6e6e6, 0x8c8c8c, 0x3a3d44, 0x15171b]; // ready-made [body, accent] combinations RC.SCHEMES = [[0xff7a1a, 0x15171b], [0x15171b, 0xff7a1a], [0x2a6fd8, 0xe6e6e6], [0xd8452b, 0xf2c230], [0x5fae3b, 0x15171b], [0x6a3fb0, 0xf2c230], [0xe6e6e6, 0xd8452b], [0x3a3d44, 0x2e9c8f]]; const hex = n => '#' + (n >>> 0).toString(16).padStart(6, '0'); const SELL = 0.5; const HOTKEY = { weapon: 'hk_weapon', tower: 'hk_tower', gadget: 'hk_gadget' }; const GLYPH = { chassis: '๐Ÿงฑ', motor: 'โš™๏ธ', wheels: '๐Ÿ›ž', battery: '๐Ÿ”‹', armour: '๐Ÿ›ก๏ธ', weapon: '๐Ÿช“', tower: '๐Ÿ”ฅ', gadget: '๐Ÿงฐ' }; const ICO = { hp: ['ico_hp', 'โค๏ธ'], armour: ['ico_armour', '๐Ÿ›ก๏ธ'], speed: ['ico_speed', 'โฑ๏ธ'], weight: ['ico_weight', 'โš–๏ธ'], power: ['ico_power', 'โšก'], money: ['ico_credits', '๐Ÿช™'], fame: ['ico_fame', 'โญ'], accel: [null, '๐Ÿš€'], turn: [null, 'โ†ป'] }; RC.pic = (name, glyph = '', cls = '') => `${name ? `` : ''}`; const ico = k => RC.pic(ICO[k][0], ICO[k][1]); RC.Shop = function (opt) { const t = opt.t; let slot = 'chassis', sel = undefined, tipN = 0, topTab = 'build'; function chipsFor(p) { const s = []; if (p.hp) s.push(`${ico('hp')}${p.hp * RC.HP_SCALE}`); if (p.slot === 'motor') s.push(`${ico('speed')}${p.speed.toFixed(1)} m/s`, `${ico('accel')}${p.force} N`); if (p.slot === 'wheels') s.push(`${t('st_grip', { n: p.grip })}`, `${ico('turn')}${p.turn}`, p.invertible ? `${t('st_invert')}` : ''); if (p.slot === 'armour') s.push(`${ico('armour')}-${Math.round(p.reduce * 100)}%`); 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 })}`); // which button works this part in a fight if (HOTKEY[p.slot]) s.push(`โŒจ ${t(HOTKEY[p.slot])}`); s.push(`${ico('weight')}${p.weight} kg`); if (p.slot === 'battery') s.push(`${ico('power')}+${p.power}`); else if (p.power) s.push(`${ico('power')}${p.power}`); return s.filter(Boolean).join(''); } // stat changes if this part (id or null) went into the current slot function deltas(build, id) { if (build[slot] === id) return ''; const a = RC.stats(build), b = RC.stats(Object.assign({}, build, { [slot]: id })); const va = RC.validate(build), vb = RC.validate(Object.assign({}, build, { [slot]: id })); const rows = [ ['hp', b.hp - a.hp, 0], ['armour', (b.reduce - a.reduce) * 100, 0, '%'], ['speed', b.top - a.top, 1], ['accel', b.accel - a.accel, 1], ['turn', b.turn - a.turn, 1], ['weight', vb.weight - va.weight, 0, ' kg', true], ['power', (vb.supply - vb.power) - (va.supply - va.power), 0], ]; return rows.filter(r => Math.abs(r[1]) >= (r[2] ? 0.05 : 0.5)).map(([k, v, d, unit = '', inv]) => { const good = inv ? v < 0 : v > 0; return `${ico(k)}${v > 0 ? '+' : ''}${v.toFixed(d)}${unit}`; }).join(''); } function render() { const save = opt.getSave(), b = save.build, v = RC.validate(b); if (sel === undefined || (sel && RC.PARTS[sel].slot !== slot)) sel = b[slot] || null; renderTopTabs(); // draw the 3 top tabs (Parts / Paint / Hangar) if (topTab !== 'paint' && opt.onPaintLeave) opt.onPaintLeave(); // close the inline painter when leaving const parts = topTab === 'build'; let editing = false; // category tabs + detail + fitted belong to the Parts tab only $('tabs').classList.toggle('hide', !parts); $('fittedwrap').classList.toggle('hide', !parts); { const d = $('designer'); if (d) d.classList.add('hide'); } if (parts) { // category tiles; the weapon tab shows the build editor inside the cards area (tabs stay visible) $('tabs').innerHTML = RC.SLOTS.map(sl => `
${RC.pic('slot_' + sl, GLYPH[sl])}${t('slot_' + sl)}
`).join(''); $('tabs').querySelectorAll('.tab').forEach(el => { el.onclick = () => { slot = el.dataset.slot; sel = b[slot] || null; opt.sfx.click(); render(); }; }); editing = slot === 'weapon'; if (opt.onSlot) opt.onSlot(slot); // game.js: freeze the robot while on the weapon tab $('cards').className = ''; // weapon catalog uses the same card grid as parts $('detail').classList.remove('hide'); // weapon tab also shows the detail/preview panel if (editing) { if (opt.onWeaponTab) opt.onWeaponTab($('cards')); // game.js fills #cards + #detail with the weapon editor } else { const locked = opt.locked || (() => false); // event/unlock: hide parts not released yet const items = (RC.REQUIRED[slot] ? [] : [null]).concat(RC.partsFor(slot).filter(p => !locked(p.id)).map(p => p.id)); $('cards').innerHTML = items.map(id => { const p = id && RC.PARTS[id], eq = b[slot] === id, own = !id || save.owned.includes(id); const badge = eq ? `${t('equipped')}` : own && id ? `${t('owned')}` : ''; const price = !p || own ? `
 
` : `
${ico('money')}${Kit.fmt(p.cost)}
`; return `
${badge}${RC.pic(p ? 'part_' + id : null, p ? GLYPH[slot] : 'โœ–')}
${p ? p.name : t('none')}
${price}
`; }).join(''); $('cards').querySelectorAll('.item').forEach(el => { el.onclick = () => { sel = el.dataset.id || null; opt.sfx.click(); render(); }; }); const p = sel && RC.PARTS[sel], eq = b[slot] === sel, own = !sel || save.owned.includes(sel); let act; if (eq) act = ``; else if (own) act = ``; else act = ``; if (p && own && !eq) act += ``; if (p && !own && save.money < p.cost) act += `${t('need_money', { n: Kit.fmt(p.cost - save.money) })}`; $('detail').innerHTML = `${RC.pic(p ? 'part_' + sel : null, p ? GLYPH[slot] : 'โœ–')}
${p ? p.name : t('none')}
${p ? t('pd_' + sel) : t('none_' + slot)}
${p ? chipsFor(p) : ''}
${deltas(b, sel)}
${act}
`; $('detail').querySelectorAll('button[data-act]').forEach(el => { el.onclick = () => act2(el.dataset.act); }); } } else if (topTab === 'paint') { if (opt.onSlot) opt.onSlot(''); // leave weapon mode / unfreeze $('detail').classList.add('hide'); $('cards').className = 'pcards'; if (opt.onPaintTab) opt.onPaintTab($('cards')); // game.js: slots + colours + inline painter } else { // hangar if (opt.onSlot) opt.onSlot(''); $('detail').classList.add('hide'); $('cards').className = 'hcards'; if (opt.onHangarView) opt.onHangarView($('cards')); } // robot stats (white ghost bar = value with the selected part) const st = RC.stats(b), alt = (parts && !editing && sel !== b[slot]) ? RC.stats(Object.assign({}, b, { [slot]: sel })) : null; const valt = alt ? RC.validate(Object.assign({}, b, { [slot]: sel })) : null; const bar = (val, max, over, ghost) => `
${ghost != null ? `` : ''}
`; // warn about the build you are PREVIEWING (selected part card / picked weapon) before you commit it, else the current build let shownB = b; if (parts && !editing && sel && sel !== b[slot]) shownB = Object.assign({}, b, { [slot]: sel }); else if (editing && opt.weaponPreview) { const pb = opt.weaponPreview(); if (pb) shownB = pb; } const vWarn = shownB === b ? v : RC.validate(shownB); const overW = vWarn.weight > RC.WEIGHT_LIMIT, overP = vWarn.power > vWarn.supply; $('meters').innerHTML = [ ['weight', t('m_weight'), bar(v.weight, RC.WEIGHT_LIMIT, overW, valt && valt.weight), `${v.weight}/${RC.WEIGHT_LIMIT} kg`, overW], ['power', t('m_power'), bar(v.power, Math.max(1, v.supply), overP, valt && valt.power), `${v.power}/${v.supply}`, overP], ['hp', t('m_hp'), bar(st.hp, 175 * RC.HP_SCALE, false, alt && alt.hp), Math.round(st.hp)], ['armour', t('m_armour'), bar(st.reduce, 0.4, false, alt && alt.reduce), Math.round(st.reduce * 100) + '%'], ['speed', t('m_speed'), bar(st.top, 7, false, alt && alt.top), st.top.toFixed(1) + ' m/s'], ['accel', t('m_accel'), bar(st.accel, 30, false, alt && alt.accel), st.accel.toFixed(1)], ['turn', t('m_turn'), bar(st.turn, 3.6, false, alt && alt.turn), st.turn.toFixed(1)], ].map(r => `${ico(r[0])}${r[2]}${r[3]}`).join(''); // build problem -> a loud, blinking message naming exactly what is too weak (battery / weight / motor) const gErr = $('gErr'); if (vWarn.errors.length) { const msg = vWarn.errors.map(e => { if (e === 'overweight') return t('err_overweight', { n: RC.WEIGHT_LIMIT }); if (e === 'no_power') return t('err_no_power'); if (e === 'too_many_weapons') return t('err_too_many_weapons'); return t('err_' + e.replace(/^(need|bad)_.*/, '$1'), { slot: t('slot_' + e.split('_')[1]) }); }).join(' ยท '); gErr.innerHTML = 'โš  ' + msg; gErr.classList.remove('alert'); void gErr.offsetWidth; gErr.classList.add('alert'); // restart the blink on every change/click } else { gErr.textContent = ''; gErr.classList.remove('alert'); } $('bLeague').disabled = !v.ok; $('bTrain').disabled = !v.ok; const name = $('rname'); if (document.activeElement !== name) name.value = save.robotName; if (!$('mechTip').textContent) tip(); renderFitted(); // live preview: show the selected part on the 3D robot (even parts you can't afford yet) if (opt.onPreview) opt.onPreview(parts && !editing && sel !== b[slot] ? Object.assign({}, b, { [slot]: sel }) : b); } // proportions (height/width/length) now live in the build editor (game.js), not here // top mode tabs: Parts / Paint / Hangar - all render into #cards (render() routes the content) function renderTopTabs() { const tabs = [['build', '๐Ÿ”ง ' + t('tab_build')], ['paint', '๐ŸŽจ ' + t('tab_paint')], ['hangar', '๐Ÿš— ' + t('tab_hangar')]]; $('shopTabs').innerHTML = tabs.map(([k, label]) => ``).join(''); $('shopTabs').querySelectorAll('button').forEach(bt => { bt.onclick = () => { setTab(bt.dataset.tab); opt.sfx.click(); }; }); } function setTab(k) { topTab = k; render(); } // fitted parts summary (left column): every slot with the equipped part + quick remove/jump function renderFitted() { const save = opt.getSave(), b = save.build; $('fitted').innerHTML = RC.SLOTS.map(sl => { if (sl === 'weapon') { // weapons are a list -> open the editor const ws = RC.weaponsOf(b), label = ws.length ? ws[0].part.name + (ws.length > 1 ? ' ร—' + ws.length : '') : t('none'); return `
${RC.pic('slot_weapon', GLYPH.weapon)}${label}๐Ÿ”ง
`; } const id = b[sl], p = id && RC.PARTS[id]; const rm = p && !RC.REQUIRED[sl] ? `โœ•` : ''; return `
${RC.pic('slot_' + sl, GLYPH[sl])}${p ? p.name : t('none')}${rm}
`; }).join(''); $('fitted').querySelectorAll('.frow').forEach(el => { el.onclick = e => { if (el.dataset.edit) { slot = 'weapon'; opt.sfx.click(); render(); return; } if (e.target.dataset.rm) { const s = e.target.dataset.rm; opt.getSave().build[s] = null; opt.sfx.click(); opt.onSave(); opt.onBuild(); render(); return; } slot = el.dataset.goto; sel = opt.getSave().build[slot] || null; topTab = 'build'; opt.sfx.click(); render(); }; }); } function act2(what) { const save = opt.getSave(), p = sel ? RC.PARTS[sel] : null; if (what === 'buy' && p && save.money >= p.cost && !save.owned.includes(sel)) { save.money -= p.cost; save.owned.push(sel); save.build[slot] = sel; opt.sfx.buy(); say(t('tip_bought', { name: p.name })); } else if (what === 'eq') { save.build[slot] = sel; opt.sfx.click(); } else if (what === 'sell' && p && save.build[slot] !== sel) { save.owned = save.owned.filter(x => x !== sel); save.money += Math.round(p.cost * SELL); opt.sfx.buy(); } else { opt.sfx.deny(); return; } opt.onSave(); opt.onBuild(); render(); } function say(text) { $('mechTip').textContent = text; } function tip() { const save = opt.getSave(), v = RC.validate(save.build); if (v.errors.includes('overweight')) return say(t('tip_overweight')); if (v.errors.includes('no_power')) return say(t('tip_power')); const n = Object.keys(window.LANG.en).filter(k => /^tip_\d+$/.test(k)).length; say(t('tip_' + (tipN++ % n))); } setInterval(() => { if (!$('shop').classList.contains('hide')) tip(); }, 14000); $('rname').addEventListener('input', () => { const s = opt.getSave(), n = Kit.cleanName($('rname').value).slice(0, 18); if (n && !Kit.badName(n)) { s.robotName = n; opt.onSave(); opt.onPaint(); } }); $('rname').addEventListener('keydown', e => { if (e.key === 'Enter') $('rname').blur(); e.stopPropagation(); }); $('mechPic').innerHTML = ``; return { render, tip, setTab, weaponTab() { slot = 'weapon'; render(); }, reset() { sel = undefined; topTab = 'build'; } }; }; })(window);