big update
This commit is contained in:
172
game/shop.js
172
game/shop.js
@@ -25,7 +25,7 @@
|
||||
|
||||
RC.Shop = function (opt) {
|
||||
const t = opt.t;
|
||||
let slot = 'weapon', sel = undefined, tipN = 0, topTab = 'build', designerOpen = false;
|
||||
let slot = 'chassis', sel = undefined, tipN = 0, topTab = 'build';
|
||||
|
||||
function chipsFor(p) {
|
||||
const s = [];
|
||||
@@ -63,125 +63,125 @@
|
||||
const save = opt.getSave(), b = save.build, v = RC.validate(b);
|
||||
if (sel === undefined || (sel && RC.PARTS[sel].slot !== slot)) sel = b[slot] || null;
|
||||
|
||||
// category tiles
|
||||
$('tabs').innerHTML = RC.SLOTS.map(sl => `<div class="tab ${sl === slot ? 'on' : ''} ${RC.REQUIRED[sl] && !b[sl] ? 'miss' : ''}" data-slot="${sl}">${RC.pic('slot_' + sl, GLYPH[sl])}<span>${t('slot_' + sl)}</span></div>`).join('');
|
||||
$('tabs').querySelectorAll('.tab').forEach(el => { el.onclick = () => { slot = el.dataset.slot; sel = b[slot] || null; opt.sfx.click(); render(); }; });
|
||||
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'); }
|
||||
|
||||
// part cards
|
||||
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 ? `<span class="badge">${t('equipped')}</span>` : own && id ? `<span class="badge own">${t('owned')}</span>` : '';
|
||||
const price = !p || own ? `<div class="pr"> </div>` : `<div class="pr ${save.money < p.cost ? 'no' : ''}">${ico('money')}${Kit.fmt(p.cost)}</div>`;
|
||||
return `<div class="item ${id === sel ? 'sel' : ''}" data-id="${id || ''}">${badge}${RC.pic(p ? 'part_' + id : null, p ? GLYPH[slot] : '✖')}<div class="nm">${p ? p.name : t('none')}</div>${price}</div>`;
|
||||
}).join('');
|
||||
$('cards').querySelectorAll('.item').forEach(el => { el.onclick = () => { sel = el.dataset.id || null; opt.sfx.click(); render(); }; });
|
||||
|
||||
// detail of the selected card
|
||||
const p = sel && RC.PARTS[sel], eq = b[slot] === sel, own = !sel || save.owned.includes(sel);
|
||||
let act;
|
||||
if (eq) act = `<button disabled>${t('equipped')}</button>`;
|
||||
else if (own) act = `<button class="primary" data-act="eq">${t('equip')}</button>`;
|
||||
else act = `<button class="primary" data-act="buy" ${save.money < p.cost ? 'disabled' : ''}>${t('buy', { n: Kit.fmt(p.cost) })}</button>`;
|
||||
if (p && own && !eq) act += `<button data-act="sell">${t('sell', { n: Kit.fmt(Math.round(p.cost * SELL)) })}</button>`;
|
||||
if (p && !own && save.money < p.cost) act += `<span class="muted">${t('need_money', { n: Kit.fmt(p.cost - save.money) })}</span>`;
|
||||
$('detail').innerHTML = `${RC.pic(p ? 'part_' + sel : null, p ? GLYPH[slot] : '✖')}
|
||||
<div><div class="nm">${p ? p.name : t('none')}</div><div class="ds">${p ? t('pd_' + sel) : t('none_' + slot)}</div>
|
||||
<div class="chips">${p ? chipsFor(p) : ''}</div><div class="chips" style="margin-top:4px">${deltas(b, sel)}</div></div>
|
||||
<div class="act">${act}</div>`;
|
||||
$('detail').querySelectorAll('button[data-act]').forEach(el => { el.onclick = () => act2(el.dataset.act); });
|
||||
if (parts) {
|
||||
// category tiles; the weapon tab shows the build editor inside the cards area (tabs stay visible)
|
||||
$('tabs').innerHTML = RC.SLOTS.map(sl => `<div class="tab ${sl === slot ? 'on' : ''} ${RC.REQUIRED[sl] && !b[sl] ? 'miss' : ''}" data-slot="${sl}">${RC.pic('slot_' + sl, GLYPH[sl])}<span>${t('slot_' + sl)}</span></div>`).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 ? `<span class="badge">${t('equipped')}</span>` : own && id ? `<span class="badge own">${t('owned')}</span>` : '';
|
||||
const price = !p || own ? `<div class="pr"> </div>` : `<div class="pr ${save.money < p.cost ? 'no' : ''}">${ico('money')}${Kit.fmt(p.cost)}</div>`;
|
||||
return `<div class="item ${id === sel ? 'sel' : ''}" data-id="${id || ''}">${badge}${RC.pic(p ? 'part_' + id : null, p ? GLYPH[slot] : '✖')}<div class="nm">${p ? p.name : t('none')}</div>${price}</div>`;
|
||||
}).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 = `<button disabled>${t('equipped')}</button>`;
|
||||
else if (own) act = `<button class="primary" data-act="eq">${t('equip')}</button>`;
|
||||
else act = `<button class="primary" data-act="buy" ${save.money < p.cost ? 'disabled' : ''}>${t('buy', { n: Kit.fmt(p.cost) })}</button>`;
|
||||
if (p && own && !eq) act += `<button data-act="sell">${t('sell', { n: Kit.fmt(Math.round(p.cost * SELL)) })}</button>`;
|
||||
if (p && !own && save.money < p.cost) act += `<span class="muted">${t('need_money', { n: Kit.fmt(p.cost - save.money) })}</span>`;
|
||||
$('detail').innerHTML = `${RC.pic(p ? 'part_' + sel : null, p ? GLYPH[slot] : '✖')}
|
||||
<div><div class="nm">${p ? p.name : t('none')}</div><div class="ds">${p ? t('pd_' + sel) : t('none_' + slot)}</div>
|
||||
<div class="chips">${p ? chipsFor(p) : ''}</div><div class="chips" style="margin-top:4px">${deltas(b, sel)}</div></div>
|
||||
<div class="act">${act}</div>`;
|
||||
$('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 = sel !== b[slot] ? RC.stats(Object.assign({}, b, { [slot]: sel })) : null;
|
||||
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) => `<div class="bar">${ghost != null ? `<i class="ghost" style="width:${Math.min(100, ghost / max * 100)}%"></i>` : ''}<i class="${over ? 'over' : ''}" style="width:${Math.min(100, val / max * 100)}%;${ghost != null && ghost < val ? 'opacity:.55' : ''}"></i></div>`;
|
||||
// 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, v.weight > RC.WEIGHT_LIMIT, valt && valt.weight), `${v.weight}/${RC.WEIGHT_LIMIT} kg`],
|
||||
['power', t('m_power'), bar(v.power, Math.max(1, v.supply), v.power > v.supply, valt && valt.power), `${v.power}/${v.supply}`],
|
||||
['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 => `<span class="st" title="${r[1]}">${ico(r[0])}${r[2]}<span>${r[3]}</span></span>`).join('');
|
||||
].map(r => `<span class="st ${r[5] ? 'warn' : ''}" title="${r[1]}">${ico(r[0])}${r[2]}<span>${r[3]}</span></span>`).join('');
|
||||
|
||||
// paint
|
||||
for (const [id, key] of [['paintBody', 'body'], ['paintAccent', 'accent']]) {
|
||||
$(id).innerHTML = 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(); }; });
|
||||
}
|
||||
// ready-made two-colour schemes
|
||||
const sc = $('paintSchemes');
|
||||
if (sc) {
|
||||
sc.innerHTML = RC.SCHEMES.map(([b2, a2]) => {
|
||||
const on = save.paint.body === b2 && save.paint.accent === a2;
|
||||
return `<span class="sw duo ${on ? 'on' : ''}" data-b="${b2}" data-a="${a2}"><i style="background:${hex(b2)}"></i><i style="background:${hex(a2)}"></i></span>`;
|
||||
}).join('');
|
||||
sc.querySelectorAll('.sw').forEach(el => {
|
||||
el.onclick = () => { save.paint.body = +el.dataset.b; save.paint.accent = +el.dataset.a; 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(' · ');
|
||||
// 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();
|
||||
renderTopTabs(); renderFitted(); renderDesigner();
|
||||
renderFitted();
|
||||
// live preview: show the selected part on the 3D robot (even parts you can't afford yet)
|
||||
if (opt.onPreview) opt.onPreview(topTab === 'build' && sel !== b[slot] ? Object.assign({}, b, { [slot]: sel }) : b);
|
||||
if (opt.onPreview) opt.onPreview(parts && !editing && sel !== b[slot] ? Object.assign({}, b, { [slot]: sel }) : b);
|
||||
}
|
||||
|
||||
// proportions designer (height / width / length) - only on the chassis slot
|
||||
function renderDesigner() {
|
||||
const el = $('designer');
|
||||
if (!el) return;
|
||||
if (slot !== 'chassis') { el.classList.add('hide'); return; }
|
||||
el.classList.remove('hide');
|
||||
el.classList.toggle('open', designerOpen);
|
||||
el.classList.toggle('shut', !designerOpen);
|
||||
const save = opt.getSave(), b = save.build;
|
||||
if (!b.body) b.body = { hx: 1, wx: 1, lx: 1 };
|
||||
const row = (key, label, lo, hi) => `<div class="drow"><span>${t('des_' + label)}</span>`
|
||||
+ `<input type="range" data-k="${key}" min="${lo}" max="${hi}" step="0.05" value="${b.body[key] || 1}">`
|
||||
+ `<span class="dv" id="dv_${key}">${Math.round((b.body[key] || 1) * 100)}%</span></div>`;
|
||||
// collapsed by default: only the header; click the title to expand the three sliders
|
||||
el.innerHTML = `<div class="dh"><span class="dtitle" id="dToggle"><span class="caret">${designerOpen ? '▾' : '▸'}</span>${t('s_body')}</span>${designerOpen ? `<button id="dReset">${t('des_reset')}</button>` : ''}</div>`
|
||||
+ (designerOpen ? row('hx', 'height', 0.7, 1.6) + row('wx', 'width', 0.8, 1.4) + row('lx', 'length', 0.8, 1.3) : '');
|
||||
$('dToggle').onclick = () => { designerOpen = !designerOpen; opt.sfx.click(); renderDesigner(); };
|
||||
if (!designerOpen) return;
|
||||
el.querySelectorAll('input[type=range]').forEach(inp => {
|
||||
inp.oninput = () => {
|
||||
b.body[inp.dataset.k] = +inp.value;
|
||||
$('dv_' + inp.dataset.k).textContent = Math.round(inp.value * 100) + '%';
|
||||
opt.onSave(); opt.onPreview(b); // live 3D while dragging
|
||||
};
|
||||
inp.onchange = () => render(); // on release: refresh weight bar + stats
|
||||
});
|
||||
$('dReset').onclick = () => { b.body = { hx: 1, wx: 1, lx: 1 }; opt.sfx.click(); opt.onSave(); opt.onPreview(b); render(); };
|
||||
}
|
||||
// proportions (height/width/length) now live in the build editor (game.js), not here
|
||||
|
||||
// top mode tabs: Parts / Paint / Hangar
|
||||
// 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]) => `<button class="${k === topTab ? 'on' : ''}" data-tab="${k}">${label}</button>`).join('');
|
||||
$('shopTabs').querySelectorAll('button').forEach(bt => { bt.onclick = () => { setTab(bt.dataset.tab); opt.sfx.click(); }; });
|
||||
$('buildView').classList.toggle('hide', topTab !== 'build');
|
||||
$('paintView').classList.toggle('hide', topTab !== 'paint');
|
||||
$('hangarView').classList.toggle('hide', topTab !== 'hangar');
|
||||
if (topTab === 'hangar' && opt.onHangarView) opt.onHangarView($('hangarView'));
|
||||
}
|
||||
function setTab(k) { topTab = k; renderTopTabs(); }
|
||||
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 `<div class="frow" data-edit="1">${RC.pic('slot_weapon', GLYPH.weapon)}<span class="fn ${ws.length ? '' : 'empty'}">${label}</span><span class="frm">🔧</span></div>`;
|
||||
}
|
||||
const id = b[sl], p = id && RC.PARTS[id];
|
||||
const rm = p && !RC.REQUIRED[sl] ? `<span class="frm" data-rm="${sl}">✕</span>` : '<span></span>';
|
||||
return `<div class="frow" data-goto="${sl}">${RC.pic('slot_' + sl, GLYPH[sl])}<span class="fn ${p ? '' : 'empty'}">${p ? p.name : t('none')}</span>${rm}</div>`;
|
||||
}).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();
|
||||
}; });
|
||||
@@ -216,6 +216,6 @@
|
||||
});
|
||||
$('rname').addEventListener('keydown', e => { if (e.key === 'Enter') $('rname').blur(); e.stopPropagation(); });
|
||||
$('mechPic').innerHTML = `<img src="${Kit.asset('assets/Sprites/npc_mechanic.png')}" alt="" onerror="this.classList.add('missing')">`;
|
||||
return { render, tip, setTab, reset() { sel = undefined; topTab = 'build'; } };
|
||||
return { render, tip, setTab, weaponTab() { slot = 'weapon'; render(); }, reset() { sel = undefined; topTab = 'build'; } };
|
||||
};
|
||||
})(window);
|
||||
|
||||
Reference in New Issue
Block a user