Files
Robot-Clash/game/garage.js
2026-11-13 00:41:54 +01:00

96 lines
6.0 KiB
JavaScript

/* Robot Clash - workshop panel (#garage in index.html): slot tabs, buy / equip / sell parts,
weight + power meters, paint. Needs parts.js, robot.js, kit-core.js.
const gar = RC.Garage({ t, getSave, onBuild(), onPaint(), onSave(), sfx: { click, buy, deny } });
gar.render() redraw everything (after language switch, money change)
save fields used: money, owned [ids], build {slot: id|null}, paint {body, accent}, robotName */
(function (root) {
'use strict';
const RC = root.RC, $ = Kit.$;
RC.PAINTS = [0xd8452b, 0xff7a1a, 0xf2c230, 0x5fae3b, 0x2e9c8f, 0x2a6fd8, 0x6a3fb0, 0xd94f9a, 0xe6e6e6, 0x8c8c8c, 0x3a3d44, 0x15171b];
const hex = n => '#' + n.toString(16).padStart(6, '0');
const SELL = 0.5;
RC.Garage = function (opt) {
const t = opt.t;
let slot = 'weapon';
function statLine(p) {
const s = [];
if (p.hp) s.push(t('st_hp', { n: p.hp * RC.HP_SCALE }));
if (p.slot === 'motor') s.push(t('st_speed', { n: p.speed.toFixed(1) }), t('st_force', { n: p.force }));
if (p.slot === 'wheels') s.push(t('st_grip', { n: p.grip }), t('st_turn', { n: p.turn }), p.invertible ? t('st_invert') : '');
if (p.slot === 'armour') s.push(t('st_reduce', { n: 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 }));
s.push(t('st_weight', { n: p.weight }));
if (p.slot === 'battery') s.push(t('st_supply', { n: p.power }));
else if (p.power) s.push(t('st_power', { n: p.power }));
return s.filter(Boolean).join(' · ');
}
function render() {
const save = opt.getSave(), b = save.build, v = RC.validate(b);
// tabs
$('tabs').innerHTML = RC.SLOTS.map(sl => `<button data-slot="${sl}" class="${sl === slot ? 'on' : ''} ${RC.REQUIRED[sl] && !b[sl] ? 'miss' : ''}">${t('slot_' + sl)}</button>`).join('');
$('tabs').querySelectorAll('button').forEach(el => { el.onclick = () => { slot = el.dataset.slot; opt.sfx.click(); render(); }; });
// parts of this slot
const rows = [];
if (!RC.REQUIRED[slot]) rows.push(`<div class="part ${b[slot] ? '' : 'eq'}"><span class="pn">${t('none')}</span><span class="pb">${b[slot] ? `<button data-act="eq" data-id="">${t('equip')}</button>` : `<span class="muted">${t('equipped')}</span>`}</span><span class="pd">${t('none_' + slot)}</span></div>`);
for (const p of RC.partsFor(slot)) {
const own = save.owned.includes(p.id), eq = b[slot] === p.id;
let btns;
if (eq) btns = `<span class="muted">${t('equipped')}</span>`;
else if (own) btns = `<button data-act="eq" data-id="${p.id}">${t('equip')}</button><button data-act="sell" data-id="${p.id}">${t('sell', { n: Kit.fmt(Math.round(p.cost * SELL)) })}</button>`;
else btns = `<button class="buy" data-act="buy" data-id="${p.id}" ${save.money < p.cost ? 'disabled' : ''}>${t('buy', { n: Kit.fmt(p.cost) })}</button>`;
rows.push(`<div class="part ${eq ? 'eq' : ''}"><span class="pn">${p.name}</span><span class="pb">${btns}</span><span class="pd">${t('pd_' + p.id)}</span><span class="ps">${statLine(p)}</span></div>`);
}
$('parts').innerHTML = rows.join('');
$('parts').querySelectorAll('button[data-act]').forEach(el => { el.onclick = () => act(el.dataset.act, el.dataset.id || null); });
// meters
const st = RC.stats(b);
const bar = (val, max, over) => `<div class="bar"><i class="${over ? 'over' : ''}" style="width:${Math.min(100, val / max * 100)}%"></i></div>`;
$('meters').innerHTML = [
[t('m_weight'), bar(v.weight, RC.WEIGHT_LIMIT, v.weight > RC.WEIGHT_LIMIT), `${v.weight}/${RC.WEIGHT_LIMIT} kg`],
[t('m_power'), bar(v.power, Math.max(1, v.supply), v.power > v.supply), `${v.power}/${v.supply}`],
[t('m_hp'), bar(st.hp, 175 * RC.HP_SCALE), Math.round(st.hp)],
[t('m_armour'), bar(st.reduce, 0.4), Math.round(st.reduce * 100) + '%'],
[t('m_speed'), bar(st.top, 7), st.top.toFixed(1) + ' m/s'],
[t('m_accel'), bar(st.accel, 30), st.accel.toFixed(1)],
[t('m_turn'), bar(st.turn, 3), st.turn.toFixed(1)],
].map(r => `<span>${r[0]}</span>${r[1]}<span>${r[2]}</span>`).join('');
// paint
for (const [id, key] of [['paintBody', 'body'], ['paintAccent', 'accent']]) {
$(id).innerHTML = `<span style="width:58px">${t('paint_' + key)}</span>` + RC.PAINTS.map(c => `<span class="sw ${save.paint[key] === c ? 'on' : ''}" data-c="${c}" style="background:${hex(c)}"></span>`).join('');
$(id).querySelectorAll('.sw').forEach(el => { el.onclick = () => { save.paint[key] = +el.dataset.c; opt.sfx.click(); opt.onSave(); opt.onPaint(); render(); }; });
}
$('gErr').textContent = v.errors.map(e => t('err_' + e.replace(/^(need|bad)_.*/, '$1'), { slot: t('slot_' + e.split('_')[1]) })).join(' · ');
$('bLeague').disabled = !v.ok; $('bTrain').disabled = !v.ok;
const name = $('rname');
if (document.activeElement !== name) name.value = save.robotName;
}
function act(what, id) {
const save = opt.getSave(), p = id ? RC.PARTS[id] : null;
if (what === 'buy' && p && save.money >= p.cost && !save.owned.includes(id)) {
save.money -= p.cost; save.owned.push(id); save.build[slot] = id; opt.sfx.buy();
} else if (what === 'eq') {
save.build[slot] = id; opt.sfx.click();
} else if (what === 'sell' && p && save.build[slot] !== id) {
save.owned = save.owned.filter(x => x !== id); save.money += Math.round(p.cost * SELL); opt.sfx.buy();
} else { opt.sfx.deny(); return; }
opt.onSave(); opt.onBuild(); render();
}
$('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(); });
return { render, get slot() { return slot; } };
};
})(window);