Phone UI and touch precision overhaul, proxy fixes, Orbital Uplink, four new maps
Phone panels (reviewed + QA'd, matrix of 22 layouts with real taps/bounding boxes): - selected-tower action bar over the board (Upgrade/Sell/Target/close), info panel updated in place so taps are never lost, scrollable palette for 7+ towers, overlays with pinned footers (Help "Got it", Play solo, Join always visible), tappable room chip to invite, reduced-motion/contrast/forced-colors fixes, paused state, partner cursor labels, bigger level pips and owner flags - touch input rewritten around "pointerdown commits nothing": pinch never builds, 10px tap slop, palm-proof pinch, long-press pings with any tool armed, nudge arrows for precise placement, hotkey repeat guard, proportional wheel zoom - new e2e: select/sell, info-panel stability, UI states, touch precision; camera unit tests Proxy/server (reproduced against real nginx 1.26 with NPM v2.16 config): - Origin check works when the proxy drops a non-default port, TRUSTED_PROXIES for proxies on global IPv6/CGNAT addresses, service worker no longer serves nginx's 502 page, DEPLOY.md corrected (NPM default read timeout is 90 s, Advanced-tab limits, path-prefix, cookie size, per-IP cap behaviour) Content: - Orbital Uplink: 4-level endgame tower (450 + 500/800/1200 credits): piercing armour-ignoring beam, blast and lingering slow, ground+air, range 11-14; glyph, beam effect, sound, help text, unit tests - Maps: The Canyon, Archipelago, Fortress, Pinwheel (7 maps total) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Tb8pW2KAN4oFkh1aK1BfD8
This commit is contained in:
parent
dad0382e10
commit
a5bd09d505
22 changed files with 2423 additions and 405 deletions
|
|
@ -72,15 +72,23 @@
|
|||
clearTimeout(bannerT);
|
||||
bannerT = setTimeout(() => { b.className = 'banner' + (boss ? ' boss' : ''); }, 1500);
|
||||
}
|
||||
let hintText = '', hintKind = '';
|
||||
function hint(text, kind) {
|
||||
const h = $('hint');
|
||||
const h = $('hint'), ch = $('confirmHint');
|
||||
hintText = text || ''; hintKind = kind || 'info';
|
||||
if (!$('confirm').hidden && $('app').dataset.barrows !== '0') { // the touch build bar carries the route text itself: one overlay on the board instead of two (not when it is too small for it)
|
||||
h.hidden = true; h.textContent = hintText; ch.textContent = hintText; ch.className = 'chint ' + hintKind; return; // (#hint keeps the text, hidden, for readers and tests)
|
||||
}
|
||||
if (!text) { h.hidden = true; return; }
|
||||
h.hidden = false; h.textContent = text; h.className = 'hint ' + (kind || 'info');
|
||||
h.hidden = false; h.textContent = text; h.className = 'hint ' + hintKind;
|
||||
}
|
||||
const fmtLen = (v) => String(Math.round(v / Math.max(1, ui.spawns || 1)));
|
||||
|
||||
function showOverlay(id) {
|
||||
for (const o of document.querySelectorAll('.overlay')) o.hidden = o.id !== id;
|
||||
for (const o of document.querySelectorAll('.overlay')) {
|
||||
const wasHidden = o.hidden; o.hidden = o.id !== id;
|
||||
if (wasHidden && !o.hidden) { const body = o.querySelector('.card-body'); if (body) body.scrollTop = 0; }
|
||||
}
|
||||
}
|
||||
function setScreen(name) {
|
||||
app.screen = name;
|
||||
|
|
@ -163,15 +171,20 @@
|
|||
}
|
||||
}
|
||||
}
|
||||
let errSfxAt = -1e9;
|
||||
function onReject(r) {
|
||||
toast(REASONS[r.reason] || ('Not possible: ' + r.reason), 'bad');
|
||||
Audio.sfx('error');
|
||||
// One buzzer per wall stroke (a stroke can reject dozens of cells), and never a burst: co-op rejects arrive after the finger has lifted (touch-16)
|
||||
const now = performance.now();
|
||||
if (now - errSfxAt > 450 && !(painting && strokeErr)) { errSfxAt = now; Audio.sfx('error'); }
|
||||
if (painting) strokeErr = true;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ palette + info panel
|
||||
function buildPalette() {
|
||||
const pal = $('palette');
|
||||
pal.textContent = '';
|
||||
pal.classList.toggle('many', TD.TOWER_ORDER.length > 6);
|
||||
for (const type of TD.TOWER_ORDER) {
|
||||
const def = TD.TOWERS[type];
|
||||
const b = document.createElement('button');
|
||||
|
|
@ -183,8 +196,28 @@
|
|||
b.addEventListener('click', () => { Audio.sfx('click'); selectType(app.selType === type ? null : type); });
|
||||
pal.appendChild(b);
|
||||
}
|
||||
drawPaletteIcons();
|
||||
drawPaletteIcons(); updatePaletteHint();
|
||||
}
|
||||
// A palette that does not fit scrolls (sideways when stacked, down in the narrow landscape column). data-more says on which
|
||||
// side more tiles are hidden; the CSS fades that edge so the player can see there is more.
|
||||
function updatePaletteHint() {
|
||||
const p = $('palette'); let more = '';
|
||||
const edge = (pos, size, view) => { const a = pos > 2, b = pos < size - view - 2; return a && b ? 'both' : a ? 'start' : b ? 'end' : ''; };
|
||||
if (p.scrollWidth > p.clientWidth + 2) more = edge(p.scrollLeft, p.scrollWidth, p.clientWidth);
|
||||
else if (p.scrollHeight > p.clientHeight + 2) more = edge(p.scrollTop, p.scrollHeight, p.clientHeight);
|
||||
if (more) { if (p.dataset.more !== more) p.dataset.more = more; } else if (p.dataset.more) delete p.dataset.more;
|
||||
}
|
||||
function revealTile(b) { // scroll the palette (only the palette, never the page) so this tile is fully in view
|
||||
const p = $('palette'); if (!b) return;
|
||||
const pr = p.getBoundingClientRect(), br = b.getBoundingClientRect(), m = 34; // keep clear of the edge fade
|
||||
if (p.scrollWidth > p.clientWidth + 2) {
|
||||
if (br.left < pr.left + m) p.scrollLeft -= pr.left + m - br.left; else if (br.right > pr.right - m) p.scrollLeft += br.right - (pr.right - m);
|
||||
} else if (p.scrollHeight > p.clientHeight + 2) {
|
||||
if (br.top < pr.top + m) p.scrollTop -= pr.top + m - br.top; else if (br.bottom > pr.bottom - m) p.scrollTop += br.bottom - (pr.bottom - m);
|
||||
}
|
||||
updatePaletteHint();
|
||||
}
|
||||
$('palette').addEventListener('scroll', updatePaletteHint, { passive: true });
|
||||
function drawPaletteIcons() {
|
||||
for (const b of $('palette').children) NW.drawIcon(b.querySelector('canvas'), b.dataset.type, 1);
|
||||
}
|
||||
|
|
@ -193,6 +226,7 @@
|
|||
const sel = b.dataset.type === app.selType;
|
||||
b.classList.toggle('sel', sel);
|
||||
b.setAttribute('aria-pressed', sel ? 'true' : 'false');
|
||||
if (sel) revealTile(b);
|
||||
}
|
||||
}
|
||||
function selectType(type) {
|
||||
|
|
@ -218,46 +252,40 @@
|
|||
}
|
||||
function targetsText(def) { return def.ground && def.air ? 'ground + air' : def.ground ? 'ground only' : 'none'; }
|
||||
|
||||
let infoSig = '';
|
||||
// The info panel is built ONCE per selection (tower id + owner, or armed build type) and then updated in place, so a
|
||||
// button is never replaced under a finger: partner actions bump view.rev constantly in co-op (code-2). A rebuild that
|
||||
// would replace nodes is deferred while a pointer is down inside the panel.
|
||||
const info = { shape: '', sig: '', lv: 0, pressed: false, dirty: false };
|
||||
|
||||
function renderInfo(force) {
|
||||
const box = $('info'), ctrl = app.ctrl, view = ctrl.view;
|
||||
let sig, tower = null;
|
||||
let tower = null, ownerP = null, shape = '';
|
||||
if (app.selTower) {
|
||||
tower = view.towers.find((t) => t.id === app.selTower);
|
||||
if (!tower) { app.selTower = 0; }
|
||||
tower = view.towers.find((t) => t.id === app.selTower) || null;
|
||||
if (!tower) app.selTower = 0;
|
||||
}
|
||||
if (tower) sig = 't' + tower.id + ':' + tower.level + ':' + tower.mode;
|
||||
else if (app.selType) sig = 'b' + app.selType;
|
||||
else sig = '';
|
||||
if (!force && sig === infoSig) return;
|
||||
infoSig = sig;
|
||||
if (!sig) { box.textContent = ''; return; }
|
||||
if (tower) {
|
||||
const def = TD.TOWERS[tower.type], i = tower.level - 1;
|
||||
const cur = statsFor(tower.type, i), nxt = tower.level < def.levels.length ? statsFor(tower.type, i + 1) : null;
|
||||
let kv = '';
|
||||
cur.forEach((row, k) => {
|
||||
const n = nxt && nxt[k] && nxt[k][1] !== row[1] ? ' <span class="up">→ ' + nxt[k][1] + '</span>' : '';
|
||||
kv += '<div class="kvp"><span>' + row[0] + '</span><span>' + row[1] + n + '</span></div>';
|
||||
});
|
||||
if (tower.type !== 'wall') kv += '<div class="kvp"><span>Targets</span><span>' + targetsText(def) + '</span></div>';
|
||||
const sellVal = def.refund ? tower.spent : Math.floor(tower.spent * TD.SELL_RATIO);
|
||||
const upCost = tower.level < def.levels.length ? def.levels[tower.level].cost : null;
|
||||
const owner = ctrl.kind === 'remote' && ctrl.players.length > 1;
|
||||
const ownerP = owner ? ctrl.players.find((p) => p.slot === tower.owner) : null;
|
||||
if (ctrl.kind === 'remote' && ctrl.players.length > 1) ownerP = ctrl.players.find((p) => p.slot === tower.owner) || null;
|
||||
shape = 't' + tower.id + tower.type + ':' + (ownerP ? ownerP.slot + ownerP.name : '');
|
||||
} else if (app.selType) shape = 'b' + app.selType;
|
||||
if (!force && shape === info.shape) { if (tower) syncInfo(); return; }
|
||||
if (info.pressed) { info.dirty = true; return; } // never replace a node while a pointer is down on it
|
||||
info.shape = shape; info.sig = ''; info.lv = 0; info.dirty = false;
|
||||
if (!shape) { box.textContent = ''; return; }
|
||||
if (tower) {
|
||||
const def = TD.TOWERS[tower.type], wall = tower.type === 'wall';
|
||||
box.innerHTML =
|
||||
'<div class="panel info"><div class="info-head"><canvas></canvas><div><div class="nm"></div><div class="lv"></div></div></div>' +
|
||||
'<div class="desc"></div><div class="kv">' + kv + '</div><div class="actions">' +
|
||||
(tower.type === 'wall' ? '' :
|
||||
'<button type="button" data-act="up"' + (upCost === null ? ' disabled' : '') + '><svg class="ic"><use href="#i-up"/></svg>' + (upCost === null ? 'Max level' : 'Upgrade <span class="cost" id="upCost">' + upCost + '</span>') + '</button>' +
|
||||
'<button type="button" data-act="mode" title="Which packet to shoot"><span>Target: ' + MODE_NAMES[tower.mode] + '</span></button>') +
|
||||
'<button type="button" data-act="sell" class="danger"><svg class="ic"><use href="#i-trash"/></svg>Sell <span class="cost">' + sellVal + '</span></button></div>' +
|
||||
'<div class="byline">' + (tower.type === 'wall' ? '' : '<span id="infoKills"></span>') + (ownerP ? '<span class="dot" style="background:' + NW.SLOT_COLORS[ownerP.slot] + '"></span><span id="infoOwner"></span>' : '') + '</div></div>';
|
||||
'<div class="desc"></div><div class="kv"></div><div class="actions">' +
|
||||
(wall ? '' : '<button type="button" data-act="up"><svg class="ic"><use href="#i-up"/></svg><span class="w"></span><span class="cost" id="upCost"></span></button>') +
|
||||
'<button type="button" data-act="sell" class="danger"><svg class="ic"><use href="#i-trash"/></svg><span class="w">Sell</span><span class="cost"></span></button>' +
|
||||
(wall ? '' : '<button type="button" data-act="mode" title="Which packet to shoot"><svg class="ic"><use href="#i-target"/></svg><span class="w">Target:</span><span class="mv"></span></button>') +
|
||||
'<button type="button" data-act="close" class="icon" aria-label="Close" title="Close"><svg class="ic"><use href="#i-x"/></svg></button></div>' +
|
||||
'<div class="byline">' + (wall ? '' : '<span id="infoKills"></span>') + (ownerP ? '<span class="dot" style="background:' + NW.SLOT_COLORS[ownerP.slot] + '"></span><span id="infoOwner"></span>' : '') + '</div></div>';
|
||||
box.querySelector('.nm').textContent = def.name;
|
||||
box.querySelector('.lv').textContent = tower.type === 'wall' ? '' : 'Level ' + tower.level + ' / ' + def.levels.length;
|
||||
box.querySelector('.desc').textContent = def.desc;
|
||||
if (ownerP) box.querySelector('#infoOwner').textContent = 'Built by ' + ownerP.name;
|
||||
NW.drawIcon(box.querySelector('canvas'), tower.type, tower.level);
|
||||
syncInfo();
|
||||
} else {
|
||||
const def = TD.TOWERS[app.selType];
|
||||
let kv = '';
|
||||
|
|
@ -270,23 +298,65 @@
|
|||
NW.drawIcon(box.querySelector('canvas'), app.selType, 1);
|
||||
}
|
||||
}
|
||||
function updateInfoDynamic() {
|
||||
// numbers of the selected tower: only text/disabled changes, never a replaced element
|
||||
function syncInfo() {
|
||||
if (!app.selTower) return;
|
||||
const t = app.ctrl.view.towers.find((x) => x.id === app.selTower);
|
||||
if (!t) return;
|
||||
const k = $('infoKills'); if (k) k.textContent = t.kills + (t.kills === 1 ? ' kill' : ' kills');
|
||||
const def = TD.TOWERS[t.type];
|
||||
const up = $('info').querySelector('[data-act="up"]');
|
||||
if (up && t.level < def.levels.length) up.disabled = app.ctrl.view.gold < def.levels[t.level].cost;
|
||||
const box = $('info'), view = app.ctrl.view, t = view.towers.find((x) => x.id === app.selTower);
|
||||
if (!t || !box.firstChild) return;
|
||||
const def = TD.TOWERS[t.type], wall = t.type === 'wall', i = t.level - 1;
|
||||
const canUp = t.level < def.levels.length, upCost = canUp ? def.levels[t.level].cost : 0;
|
||||
const affordable = canUp && view.gold >= upCost;
|
||||
const sellVal = def.refund ? t.spent : Math.floor(t.spent * TD.SELL_RATIO);
|
||||
const sig = t.level + ':' + t.mode + ':' + sellVal + ':' + t.kills + ':' + (affordable ? 1 : 0);
|
||||
if (sig === info.sig) return;
|
||||
info.sig = sig;
|
||||
const setText = (el, v) => { if (el && el.textContent !== v) el.textContent = v; };
|
||||
if (info.lv !== t.level) { // level changed: stats table and icon
|
||||
info.lv = t.level;
|
||||
const cur = statsFor(t.type, i), nxt = canUp ? statsFor(t.type, i + 1) : null;
|
||||
let kv = '';
|
||||
cur.forEach((row, k) => {
|
||||
const n = nxt && nxt[k] && nxt[k][1] !== row[1] ? ' <span class="up">→ ' + nxt[k][1] + '</span>' : '';
|
||||
kv += '<div class="kvp"><span>' + row[0] + '</span><span>' + row[1] + n + '</span></div>';
|
||||
});
|
||||
if (!wall) kv += '<div class="kvp"><span>Targets</span><span>' + targetsText(def) + '</span></div>';
|
||||
box.querySelector('.kv').innerHTML = kv;
|
||||
setText(box.querySelector('.lv'), wall ? '' : 'Level ' + t.level + ' / ' + def.levels.length);
|
||||
NW.drawIcon(box.querySelector('canvas'), t.type, t.level);
|
||||
}
|
||||
const up = box.querySelector('[data-act="up"]');
|
||||
if (up) {
|
||||
setText(up.querySelector('.w'), canUp ? 'Upgrade' : 'Max level');
|
||||
setText(up.querySelector('.cost'), canUp ? String(upCost) : '');
|
||||
up.disabled = !affordable;
|
||||
up.setAttribute('aria-label', canUp ? 'Upgrade for ' + upCost + ' credits' : 'Max level');
|
||||
}
|
||||
const sell = box.querySelector('[data-act="sell"]');
|
||||
setText(sell.querySelector('.cost'), String(sellVal));
|
||||
sell.setAttribute('aria-label', 'Sell for ' + sellVal + ' credits');
|
||||
const mode = box.querySelector('[data-act="mode"]');
|
||||
if (mode) { setText(mode.querySelector('.mv'), MODE_NAMES[t.mode]); mode.setAttribute('aria-label', 'Target: ' + MODE_NAMES[t.mode] + '. Tap to change'); }
|
||||
setText(box.querySelector('#infoKills'), t.kills + (t.kills === 1 ? ' kill' : ' kills'));
|
||||
}
|
||||
let infoTimer = 0;
|
||||
$('info').addEventListener('pointerdown', () => { clearTimeout(infoTimer); info.pressed = true; }); // (a new press cancels the previous release timer)
|
||||
const infoRelease = () => {
|
||||
if (!info.pressed) return;
|
||||
clearTimeout(infoTimer);
|
||||
infoTimer = setTimeout(() => { info.pressed = false; if (info.dirty) renderInfo(false); }, 90); // after the click has been delivered
|
||||
};
|
||||
document.addEventListener('pointerup', infoRelease, true);
|
||||
document.addEventListener('pointercancel', infoRelease, true);
|
||||
$('info').addEventListener('click', (e) => {
|
||||
const btn = e.target.closest('button[data-act]'); if (!btn || !app.selTower) return;
|
||||
info.pressed = false;
|
||||
const ctrl = app.ctrl, tower = ctrl.view.towers.find((t) => t.id === app.selTower);
|
||||
if (!tower) return;
|
||||
Audio.sfx('click');
|
||||
switch (btn.dataset.act) {
|
||||
case 'up': ctrl.send({ c: 'upgrade', id: tower.id }); break;
|
||||
case 'sell': ctrl.send({ c: 'sell', id: tower.id }); app.selTower = 0; renderInfo(true); app.previewDirty = true; break;
|
||||
case 'close': selectTower(0); break;
|
||||
case 'mode': {
|
||||
const modes = TD.TARGET_MODES; ctrl.send({ c: 'target', id: tower.id, mode: modes[(modes.indexOf(tower.mode) + 1) % modes.length] });
|
||||
break;
|
||||
|
|
@ -308,7 +378,7 @@
|
|||
for (const g of sum) {
|
||||
const d = TD.ENEMIES[g.k];
|
||||
if (d.air) air = true;
|
||||
html += '<span class="e"><i style="background:' + d.color + '"></i>' + g.c + '× ' + d.name + '</span>';
|
||||
html += '<span class="e" title="' + d.name + (d.air ? ' (flies)' : '') + '"><i class="sh-' + g.k + '" style="--c:' + d.color + '"></i>' + g.c + '× ' + d.name + '</span>';
|
||||
}
|
||||
if (air) html += '<span class="tag air">AIR</span>';
|
||||
if (n % 10 === 0) html += '<span class="tag boss">BOSS</span>';
|
||||
|
|
@ -328,7 +398,7 @@
|
|||
}
|
||||
app.prevPlayers = list.map((p) => Object.assign({}, p));
|
||||
renderPlayers();
|
||||
if (ctrl.kind === 'remote') renderInfo(true);
|
||||
if (ctrl.kind === 'remote') renderInfo(false); // the shape includes the owner, so only a changed roster rebuilds
|
||||
}
|
||||
function renderPlayers() {
|
||||
const ctrl = app.ctrl, box = $('players');
|
||||
|
|
@ -379,7 +449,12 @@
|
|||
if (L.attn !== attn) { L.attn = attn; $('btnWave').classList.toggle('attn', attn); }
|
||||
if (L.auto !== view.auto) { L.auto = view.auto; $('btnAuto').setAttribute('aria-pressed', view.auto ? 'true' : 'false'); }
|
||||
if (L.speed !== view.speed) { L.speed = view.speed; $('speedText').textContent = view.speed + '×'; }
|
||||
if (L.paused !== view.paused) { L.paused = view.paused; $('pausedTag').hidden = !view.paused; $('btnPause').innerHTML = '<svg class="ic"><use href="#i-' + (view.paused ? 'play' : 'pause') + '"/></svg>'; $('btnPause').setAttribute('aria-label', view.paused ? 'Resume' : 'Pause'); }
|
||||
if (L.paused !== view.paused) { // paused: dimmed board, a resume pill, and an amber pressed pause button (not a second play triangle)
|
||||
L.paused = view.paused; $('pausedTag').hidden = !view.paused; $('stage').classList.toggle('paused', !!view.paused);
|
||||
$('btnPause').setAttribute('aria-pressed', view.paused ? 'true' : 'false'); $('btnPause').setAttribute('aria-label', view.paused ? 'Resume' : 'Pause');
|
||||
}
|
||||
const offline = !$('reconnect').hidden;
|
||||
if (L.offline !== offline) { L.offline = offline; $('app').classList.toggle('offline', offline); }
|
||||
if (L.muted !== Audio.muted) { L.muted = Audio.muted; $('btnSound').innerHTML = '<svg class="ic"><use href="#i-' + (Audio.muted ? 'mute' : 'sound') + '"/></svg>'; }
|
||||
// palette affordability
|
||||
const gold = view.gold;
|
||||
|
|
@ -391,11 +466,16 @@
|
|||
const chip = $('netChip');
|
||||
chip.hidden = false;
|
||||
$('netRoom').textContent = ctrl.room || '';
|
||||
const alone = ctrl.players.length < 2;
|
||||
if (L.alone !== alone || L.room !== ctrl.room) {
|
||||
L.alone = alone; L.room = ctrl.room; chip.classList.toggle('waiting', alone);
|
||||
chip.setAttribute('aria-label', 'Co-op room ' + (ctrl.room || '') + (alone ? '. Waiting for a partner: tap to invite them' : '. Tap to invite your partner'));
|
||||
}
|
||||
const p = ctrl.rtt ? ctrl.rtt + ' ms' : '';
|
||||
if (L.ping !== p) { L.ping = p; $('netPing').textContent = p; chip.classList.toggle('bad', ctrl.rtt > 220); const pp = $('plPing'); if (pp) pp.textContent = p ? '\u00b7 ' + p : ''; }
|
||||
}
|
||||
renderWavePanel(view);
|
||||
updateInfoDynamic();
|
||||
syncInfo();
|
||||
}
|
||||
|
||||
function endScreen(view) {
|
||||
|
|
@ -457,19 +537,62 @@
|
|||
function updateConfirm() {
|
||||
const show = !!(app.pending && app.selType && app.selType !== 'wall');
|
||||
$('confirm').hidden = !show;
|
||||
if (show) {
|
||||
$('confirmText').textContent = 'Build · ' + TD.TOWERS[app.selType].levels[0].cost;
|
||||
// keep the bar clear of the cell being placed: show it on the opposite half of the stage
|
||||
const p = renderer.cellToScreen(app.pending.x, app.pending.y);
|
||||
$('confirm').classList.toggle('top', p.y > $('stage').clientHeight * 0.5);
|
||||
}
|
||||
if (show) { const cost = TD.TOWERS[app.selType].levels[0].cost; $('confirmText').textContent = 'Build · ' + cost; $('btnConfirmBuild').setAttribute('aria-label', 'Build for ' + cost + ' credits'); hint(hintText, hintKind); }
|
||||
else if (!ptrs.size) clearCursor(); // (no pending cell, no finger down: the partner's view of our touch cursor goes)
|
||||
layoutBars(true);
|
||||
}
|
||||
$('btnConfirmBuild').addEventListener('click', () => { if (app.pending && placeAt(app.pending)) { app.pending = null; updateConfirm(); } });
|
||||
// Where the floating bars go (the touch confirm bar and the selected-tower action bar): on the half of the stage away from the
|
||||
// cell they act on, so they never cover it. Runs every frame (cheap, writes only on change) so rotation, resize, pan and zoom
|
||||
// keep it right (touch-14). The stage rectangle goes into CSS variables because the tower bar is position:fixed.
|
||||
// The bar does not hop to the other half while the same cell is being worked on (nudging across the middle must not move the buttons
|
||||
// out from under the thumb): it flips only when the cell would end up under it.
|
||||
let barKey = '', barSide = '', barSubject = '';
|
||||
function layoutBars(force) {
|
||||
const r = $('stage').getBoundingClientRect(), a = $('app'), conf = !$('confirm').hidden && !!app.pending;
|
||||
let cell = null;
|
||||
if (conf) cell = app.pending;
|
||||
else if (app.selTower && app.ctrl) cell = app.ctrl.view.towers.find((t) => t.id === app.selTower) || null;
|
||||
let side = '';
|
||||
if (cell) {
|
||||
const sp = renderer.cellToScreen(cell.x, cell.y), bar = conf ? $('confirm') : $('info').querySelector('.actions');
|
||||
const guard = (bar ? bar.offsetHeight : 54) + 8 + renderer.cellSizePx() / 2 + 8; // the strip a bar there would cover, plus the cell itself
|
||||
const hit = (s) => (s === 'top' ? sp.y < guard : sp.y > r.height - guard);
|
||||
const subject = conf ? 'pending' : 'tower' + app.selTower;
|
||||
const mid = sp.y > r.height * 0.5 ? 'top' : 'bottom', other = mid === 'top' ? 'bottom' : 'top';
|
||||
side = subject === barSubject && barSide && !hit(barSide) ? barSide : hit(mid) && !hit(other) ? other : mid;
|
||||
barSubject = subject; barSide = side;
|
||||
} else { barSubject = ''; barSide = ''; }
|
||||
const key = side + '|' + (conf ? 1 : 0) + '|' + Math.round(r.left) + ',' + Math.round(r.top) + ',' + Math.round(r.width) + ',' + Math.round(r.height);
|
||||
if (!force && key === barKey) return;
|
||||
barKey = key;
|
||||
const st = a.style;
|
||||
st.setProperty('--st-l', r.left.toFixed(1) + 'px'); st.setProperty('--st-t', r.top.toFixed(1) + 'px');
|
||||
st.setProperty('--st-w', r.width.toFixed(1) + 'px'); st.setProperty('--st-h', r.height.toFixed(1) + 'px');
|
||||
if (side) a.dataset.bar = side; else delete a.dataset.bar;
|
||||
if (conf) a.dataset.conf = '1'; else delete a.dataset.conf; // the build bar is taller than the tower bar (it has the nudge row)
|
||||
a.dataset.barrows = r.width >= 484 ? '1' : r.height >= 262 ? '2' : '0'; // build bar: nudge buttons in the bar's row on a wide stage, a second row on a phone, no text at all on a tiny one
|
||||
a.dataset.barw = r.width < 440 ? 's' : 'm'; // narrow stage: the bar drops the words, keeps icons + numbers
|
||||
$('confirm').classList.toggle('top', side === 'top');
|
||||
}
|
||||
$('btnConfirmBuild').addEventListener('click', () => { if (app.pending && placeAt(app.pending)) setPending(null); });
|
||||
$('btnCancelBuild').addEventListener('click', () => { selectType(null); });
|
||||
|
||||
// ------------------------------------------------------------------ pointer input
|
||||
// Touch rules. pointerdown never builds and never selects: every pinch starts with one finger down. A TAP (the finger stays within
|
||||
// TAP_SLOP of where it landed and lifts before the long press) acts on the cell it LANDED on, when it lifts. A DRAG acts as soon as it
|
||||
// passes the slop: it paints a Firewall line, aims the pending turret, or pans a zoomed board. A second finger turns the whole gesture
|
||||
// into pinch/pan and undoes whatever the first finger had begun; extra fingers (a palm) are ignored. A long press pings the map,
|
||||
// whatever is armed, and never builds. Cells are only 12 to 20 px on a phone, which is why the build bar also has nudge buttons.
|
||||
const TAP_SLOP = 10, MOUSE_SLOP = 8, LONG_MS = 520;
|
||||
const ptrs = new Map();
|
||||
let painting = false, paintCell = null, panning = false, longT = 0, longFired = false, rightDown = null, prevPending = null, pinch = null;
|
||||
let painting = false, paintCell = null, panning = false, longT = 0, longFired = false, rightDown = null, pinch = null;
|
||||
let multi = false; // a second finger landed: pinch/pan owns the gesture until every finger is up
|
||||
let tg = null; // the one-finger touch gesture in progress: { id, cell (where it landed), pend (pending cell then), hover0, dragged }
|
||||
let tapSelectOnUp = false;
|
||||
let tapTowerId = 0; // a wall stroke that began on a turret: a stationary click or tap selects that turret instead (touch-17)
|
||||
let pendFree = true; // the pending cell held no tower when it was chosen (a tower appearing there later is the partner's, coop-7)
|
||||
let curSent = false; // a touch cursor is showing on the partner's board
|
||||
let strokeErr = false; // this wall stroke already played its error sound (touch-16)
|
||||
|
||||
function cancelLong() { clearTimeout(longT); longT = 0; }
|
||||
function setHover(cell) {
|
||||
|
|
@ -478,6 +601,50 @@
|
|||
if (c && app.hover && c.x === app.hover.x && c.y === app.hover.y) return;
|
||||
app.hover = c; app.previewDirty = true;
|
||||
}
|
||||
// The partner's view of where this touch is aiming. `force` skips the 70 ms throttle (the last position must get through).
|
||||
function touchCursor(cell, force) {
|
||||
if (!cell || !app.ctrl.board.inBounds(cell.x, cell.y)) return;
|
||||
app.ctrl.sendCursor(cell.x + 0.5, cell.y + 0.5, force); curSent = true;
|
||||
}
|
||||
function clearCursor() { if (curSent) { curSent = false; app.ctrl.sendCursor(null, null); } }
|
||||
// The only way the touch-pending build cell changes. An off-board cell is no cell (code-23).
|
||||
function setPending(cell, commit) {
|
||||
const c = cell && app.ctrl.board.inBounds(cell.x, cell.y) ? { x: cell.x, y: cell.y } : null;
|
||||
app.pending = c; pendFree = !c || !towerAtCell(c); app.previewDirty = true;
|
||||
if (c) { setHover(c); touchCursor(c, commit); }
|
||||
updateConfirm();
|
||||
}
|
||||
// the finger-down preview ends without a commit (long press, second finger, cancel): hover goes back, the touch cursor goes away
|
||||
function unaim() {
|
||||
if (tg) { app.hover = tg.hover0; app.previewDirty = true; }
|
||||
if (!app.pending) clearCursor();
|
||||
}
|
||||
function abortTouch() { // forget what the first finger began: a second finger landed, or the touch was cancelled
|
||||
cancelLong(); painting = false; paintCell = null; tapSelectOnUp = false; tapTowerId = 0; longFired = false;
|
||||
if (tg) { if (tg.dragged) setPending(tg.pend); unaim(); tg = null; }
|
||||
}
|
||||
function startStroke(cell) { painting = true; paintCell = cell; strokeErr = false; }
|
||||
function seedPinch() { // pinch/pan between the first two fingers only; the pair is re-seeded when one of them lifts
|
||||
const ids = [...ptrs.keys()].slice(0, 2), a = ptrs.get(ids[0]), b = ptrs.get(ids[1]);
|
||||
pinch = { ids, d: Math.hypot(a.x - b.x, a.y - b.y) || 1, mx: (a.x + b.x) / 2, my: (a.y + b.y) / 2 };
|
||||
}
|
||||
// nudge buttons: move the pending cell one cell in a screen direction (rotation-safe), and keep it in view when zoomed in
|
||||
function nudge(dx, dy) {
|
||||
const p = app.pending; if (!p) return;
|
||||
const c = renderer.stepCell(p.x, p.y, dx, dy);
|
||||
if (!app.ctrl.board.inBounds(c.x, c.y)) return; // the edge of the board: stay put
|
||||
setPending(c, true);
|
||||
if (renderer.zoom > 1.01) {
|
||||
const s = renderer.cellToScreen(c.x, c.y), m = renderer.cellSizePx() * 1.5;
|
||||
const mx = s.x < m ? m - s.x : s.x > renderer.W - m ? renderer.W - m - s.x : 0, my = s.y < m ? m - s.y : s.y > renderer.H - m ? renderer.H - m - s.y : 0;
|
||||
if (mx || my) { renderer.panBy(mx, my); updateFit(); }
|
||||
}
|
||||
}
|
||||
$('nudge').addEventListener('click', (e) => {
|
||||
const b = e.target.closest('button[data-dx]'); if (!b) return;
|
||||
Audio.sfx('click'); nudge(+b.dataset.dx, +b.dataset.dy);
|
||||
});
|
||||
|
||||
function paintLine(from, to) { // place on every cell crossed by a fast drag (Bresenham)
|
||||
let x0 = from.x, y0 = from.y; const x1 = to.x, y1 = to.y;
|
||||
const dx = Math.abs(x1 - x0), dy = -Math.abs(y1 - y0), sx = x0 < x1 ? 1 : -1, sy = y0 < y1 ? 1 : -1;
|
||||
|
|
@ -511,47 +678,42 @@
|
|||
e.preventDefault();
|
||||
try { canvas.setPointerCapture(e.pointerId); } catch (err) { /* ignore */ }
|
||||
Audio.unlock();
|
||||
const touch = e.pointerType !== 'mouse';
|
||||
if (touch && e.isPrimary) { ptrs.clear(); abortTouch(); multi = false; pinch = null; } // (a lost pointerup must never leave half a gesture behind)
|
||||
const r = canvas.getBoundingClientRect(), sx = e.clientX - r.left, sy = e.clientY - r.top;
|
||||
ptrs.set(e.pointerId, { x: sx, y: sy, sx, sy, type: e.pointerType, moved: false, btn: e.button });
|
||||
if (ptrs.size === 2) { // second finger: pinch/pan, abandon whatever the first finger started
|
||||
cancelLong(); painting = false; tapSelectOnUp = false;
|
||||
const [a, b] = [...ptrs.values()];
|
||||
pinch = { d: Math.hypot(a.x - b.x, a.y - b.y) || 1, mx: (a.x + b.x) / 2, my: (a.y + b.y) / 2 };
|
||||
if (ptrs.size >= 2) { // second finger: pinch/pan; whatever the first finger had begun is undone, a third finger is ignored
|
||||
if (!multi) { multi = true; abortTouch(); }
|
||||
if (!pinch) seedPinch();
|
||||
return;
|
||||
}
|
||||
if (ptrs.size > 2) return;
|
||||
const cell = renderer.screenToCell(sx, sy);
|
||||
longFired = false;
|
||||
if (e.pointerType === 'mouse') {
|
||||
if (!touch) {
|
||||
if (e.button === 2) { rightDown = { x: sx, y: sy, moved: false }; return; }
|
||||
if (e.button === 1) { panning = true; return; }
|
||||
if (e.altKey) { markAt(cell); return; }
|
||||
const ex = towerAtCell(cell);
|
||||
if (app.selType && ex && !(app.selType === 'wall' && ex.type === 'wall')) { selectTower(ex.id); Audio.sfx('click'); }
|
||||
if (app.selType === 'wall' && ex && ex.type !== 'wall') { setHover(cell); startStroke(cell); tapTowerId = ex.id; } // a Firewall stroke may start on a turret; a plain click selects it
|
||||
else if (app.selType && ex && !(app.selType === 'wall' && ex.type === 'wall')) { selectTower(ex.id); Audio.sfx('click'); }
|
||||
else if (app.selType) {
|
||||
setHover(cell);
|
||||
if (!ex) placeAt(cell);
|
||||
if (app.selType === 'wall') { painting = true; paintCell = cell; }
|
||||
if (app.selType === 'wall') startStroke(cell);
|
||||
} else {
|
||||
tapSelectOnUp = true; panning = renderer.zoom > 1.01;
|
||||
}
|
||||
return;
|
||||
}
|
||||
// touch / pen
|
||||
prevPending = app.pending ? { x: app.pending.x, y: app.pending.y } : null;
|
||||
const ex = towerAtCell(cell);
|
||||
if (app.selType && ex && !(app.selType === 'wall' && ex.type === 'wall')) {
|
||||
selectTower(ex.id); Audio.sfx('click');
|
||||
} else if (app.selType === 'wall') {
|
||||
setHover(cell); if (!ex) placeAt(cell); painting = true; paintCell = cell;
|
||||
} else if (app.selType) {
|
||||
app.pending = { x: cell.x, y: cell.y }; setHover(cell); updateConfirm();
|
||||
} else {
|
||||
tapSelectOnUp = true;
|
||||
longT = setTimeout(() => { longFired = true; tapSelectOnUp = false; markAt(cell); }, 520);
|
||||
}
|
||||
// touch / pen: remember what is under the finger and preview it; commit nothing until the finger lifts or drags
|
||||
tg = { id: e.pointerId, cell, pend: app.pending ? { x: app.pending.x, y: app.pending.y } : null, hover0: app.hover, dragged: false };
|
||||
if (app.selType) { setHover(cell); touchCursor(cell); }
|
||||
tapSelectOnUp = !app.selType;
|
||||
longT = setTimeout(() => { // a long press pings the map, whatever is armed
|
||||
longT = 0; if (!tg || multi || tg.id !== e.pointerId) return;
|
||||
longFired = true; tapSelectOnUp = false; unaim(); markAt(tg.cell);
|
||||
}, LONG_MS);
|
||||
});
|
||||
let tapSelectOnUp = false;
|
||||
|
||||
canvas.addEventListener('pointermove', (e) => {
|
||||
const r = canvas.getBoundingClientRect(), sx = e.clientX - r.left, sy = e.clientY - r.top;
|
||||
|
|
@ -567,9 +729,10 @@
|
|||
}
|
||||
const dx = sx - p.x, dy = sy - p.y;
|
||||
p.x = sx; p.y = sy;
|
||||
if (Math.hypot(sx - p.sx, sy - p.sy) > 8) p.moved = true;
|
||||
if (ptrs.size === 2 && pinch) {
|
||||
const [a, b] = [...ptrs.values()];
|
||||
if (Math.hypot(sx - p.sx, sy - p.sy) > (e.pointerType === 'mouse' ? MOUSE_SLOP : TAP_SLOP)) p.moved = true;
|
||||
if (pinch) {
|
||||
if (!pinch.ids.includes(e.pointerId)) return; // a third finger (palm) neither pans nor zooms
|
||||
const a = ptrs.get(pinch.ids[0]), b = ptrs.get(pinch.ids[1]); if (!a || !b) return;
|
||||
const d = Math.hypot(a.x - b.x, a.y - b.y) || 1, mx = (a.x + b.x) / 2, my = (a.y + b.y) / 2;
|
||||
renderer.panBy(mx - pinch.mx, my - pinch.my);
|
||||
renderer.setZoom(renderer.zoom * d / pinch.d, mx, my);
|
||||
|
|
@ -577,50 +740,76 @@
|
|||
updateFit();
|
||||
return;
|
||||
}
|
||||
if (multi) return; // one finger of a pinch is still down: it must not paint, aim or pan
|
||||
const cell = renderer.screenToCell(sx, sy);
|
||||
if (e.pointerType === 'mouse') {
|
||||
if (rightDown) { if (Math.hypot(sx - rightDown.x, sy - rightDown.y) > 5) rightDown.moved = true; if (rightDown.moved) { renderer.panBy(dx, dy); updateFit(); } rightDown.x = rightDown.moved ? sx : rightDown.x; return; }
|
||||
if (panning && p.moved) { renderer.panBy(dx, dy); updateFit(); tapSelectOnUp = false; return; }
|
||||
setHover(cell);
|
||||
app.ctrl.sendCursor(cell.fx, cell.fy);
|
||||
if (painting && app.selType === 'wall' && paintCell && (cell.x !== paintCell.x || cell.y !== paintCell.y)) { paintLine(paintCell, cell); paintCell = cell; }
|
||||
if (painting && app.selType === 'wall' && paintCell && (cell.x !== paintCell.x || cell.y !== paintCell.y)) { tapTowerId = 0; paintLine(paintCell, cell); paintCell = cell; }
|
||||
return;
|
||||
}
|
||||
// touch / pen
|
||||
if (p.moved) { cancelLong(); tapSelectOnUp = tapSelectOnUp && !p.moved; }
|
||||
if (painting && app.selType === 'wall') {
|
||||
if (!tg || tg.id !== e.pointerId || !p.moved) return; // inside the tap slop it is still a tap: nothing moves
|
||||
cancelLong(); tapSelectOnUp = false;
|
||||
if (app.selType === 'wall') {
|
||||
if (!painting) { // the stroke starts here; paintLine skips its first cell, so the origin goes in now (unless a tower is on it)
|
||||
startStroke(tg.cell);
|
||||
if (!towerAtCell(tg.cell)) placeAt(tg.cell);
|
||||
}
|
||||
setHover(cell);
|
||||
if (paintCell && (cell.x !== paintCell.x || cell.y !== paintCell.y)) { paintLine(paintCell, cell); paintCell = cell; }
|
||||
} else if (app.selType) {
|
||||
app.pending = { x: cell.x, y: cell.y }; setHover(cell); updateConfirm();
|
||||
app.ctrl.sendCursor(cell.fx, cell.fy);
|
||||
} else if (p.moved && renderer.zoom > 1.01) {
|
||||
if (cell.x !== paintCell.x || cell.y !== paintCell.y) { paintLine(paintCell, cell); paintCell = cell; }
|
||||
} else if (app.selType) { // aim: the pending cell follows the finger (off the board it stays on the last cell)
|
||||
if (app.ctrl.board.inBounds(cell.x, cell.y)) { tg.dragged = true; setPending(cell); }
|
||||
} else if (renderer.zoom > 1.01) {
|
||||
renderer.panBy(dx, dy); updateFit();
|
||||
}
|
||||
});
|
||||
|
||||
function endPointer(e) {
|
||||
const p = ptrs.get(e.pointerId);
|
||||
const p = ptrs.get(e.pointerId), cancelled = e.type === 'pointercancel';
|
||||
ptrs.delete(e.pointerId);
|
||||
try { canvas.releasePointerCapture(e.pointerId); } catch (err) { /* ignore */ }
|
||||
if (!p || !app.inGame) return;
|
||||
if (!p || !app.inGame) { if (!ptrs.size) { multi = false; pinch = null; } return; }
|
||||
cancelLong();
|
||||
if (ptrs.size < 2) pinch = null;
|
||||
const r = canvas.getBoundingClientRect(), cell = renderer.screenToCell(e.clientX - r.left, e.clientY - r.top);
|
||||
if (e.pointerType === 'mouse') {
|
||||
const r = canvas.getBoundingClientRect(), cell = renderer.screenToCell(e.clientX - r.left, e.clientY - r.top);
|
||||
if (e.button === 2 && rightDown) { if (!rightDown.moved) { selectType(null); selectTower(0); } rightDown = null; return; }
|
||||
if (e.button === 1) { panning = false; return; }
|
||||
if (tapSelectOnUp && !p.moved && e.type === 'pointerup') tapSelect(cell);
|
||||
tapSelectOnUp = false; painting = false; panning = false;
|
||||
if (tapTowerId && !p.moved && e.type === 'pointerup') { selectTower(tapTowerId); Audio.sfx('click'); }
|
||||
tapSelectOnUp = false; tapTowerId = 0; painting = false; panning = false;
|
||||
return;
|
||||
}
|
||||
if (e.type === 'pointercancel') { painting = false; tapSelectOnUp = false; return; }
|
||||
if (painting) { painting = false; return; }
|
||||
if (ptrs.size > 0) return; // lifted one finger of a pinch
|
||||
if (app.selType && app.pending && !p.moved && prevPending && prevPending.x === app.pending.x && prevPending.y === app.pending.y) {
|
||||
if (placeAt(app.pending)) { app.pending = null; updateConfirm(); } // tapping the previewed cell again builds
|
||||
} else if (!app.selType && tapSelectOnUp && !p.moved && !longFired) tapSelect(cell);
|
||||
// touch / pen
|
||||
if (pinch && pinch.ids.includes(e.pointerId)) { if (ptrs.size >= 2) seedPinch(); else pinch = null; }
|
||||
const wasMulti = multi;
|
||||
if (!ptrs.size) multi = false;
|
||||
if (wasMulti) return; // the end of a pinch/pan is never a tap
|
||||
const g = tg;
|
||||
if (!g || g.id !== e.pointerId) return;
|
||||
if (cancelled) { abortTouch(); return; } // pointercancel: no action, nothing half done
|
||||
tg = null;
|
||||
const wasPainting = painting; painting = false; paintCell = null;
|
||||
if (wasPainting || p.moved || longFired) { // a stroke, an aim drag, a pan or a long press: lifting the finger adds nothing
|
||||
tapSelectOnUp = false;
|
||||
if (longFired) unaim(); else if (!app.pending) clearCursor();
|
||||
return;
|
||||
}
|
||||
// a tap, on the cell the finger landed on (not wherever it drifted to)
|
||||
const cell = g.cell, ex = towerAtCell(cell);
|
||||
if (app.selType === 'wall') {
|
||||
if (ex && ex.type !== 'wall') { selectTower(ex.id); Audio.sfx('click'); } // (a drag that starts on a turret is a stroke: it keeps the tool)
|
||||
else if (!ex) placeAt(cell);
|
||||
} else if (app.selType) {
|
||||
if (ex) { selectTower(ex.id); Audio.sfx('click'); }
|
||||
else if (!app.ctrl.board.inBounds(cell.x, cell.y)) setPending(null);
|
||||
else if (g.pend && g.pend.x === cell.x && g.pend.y === cell.y) { if (placeAt(cell)) setPending(null); } // tapping the previewed cell again builds
|
||||
else setPending(cell, true);
|
||||
} else if (tapSelectOnUp) tapSelect(cell);
|
||||
tapSelectOnUp = false;
|
||||
if (!app.pending) clearCursor();
|
||||
}
|
||||
canvas.addEventListener('pointerup', endPointer);
|
||||
canvas.addEventListener('pointercancel', endPointer);
|
||||
|
|
@ -629,9 +818,13 @@
|
|||
if (!app.inGame) return;
|
||||
e.preventDefault();
|
||||
const r = canvas.getBoundingClientRect();
|
||||
renderer.setZoom(renderer.zoom * (e.deltaY < 0 ? 1.15 : 1 / 1.15), e.clientX - r.left, e.clientY - r.top);
|
||||
// proportional to the scroll distance (a mouse notch is about the old 1.15x; a trackpad glides). ctrl+wheel is a trackpad pinch: much smaller deltas.
|
||||
const dy = clamp(e.deltaY * (e.deltaMode === 1 ? 16 : e.deltaMode === 2 ? 400 : 1), -300, 300);
|
||||
renderer.setZoom(renderer.zoom * Math.exp(-dy * (e.ctrlKey ? 0.01 : 0.0015)), e.clientX - r.left, e.clientY - r.top);
|
||||
updateFit();
|
||||
}, { passive: false });
|
||||
// (the window lost focus mid-gesture: the pointerup may never arrive)
|
||||
root.addEventListener('blur', () => { ptrs.clear(); abortTouch(); multi = false; pinch = null; rightDown = null; panning = false; });
|
||||
|
||||
function updateFit() {
|
||||
const z = renderer.zoom > 1.02;
|
||||
|
|
@ -643,12 +836,15 @@
|
|||
root.addEventListener('keydown', (e) => {
|
||||
if (e.target && (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA')) return;
|
||||
if (e.ctrlKey || e.metaKey) return;
|
||||
if (e.repeat) return; // holding a key is one press: Space must not start 20 waves (code-6)
|
||||
if (!app.inGame) return;
|
||||
if (document.querySelector('.overlay:not([hidden])')) { if (e.key === 'Escape') { closeOverlayToGame(); } return; }
|
||||
const ctrl = app.ctrl, view = ctrl.view;
|
||||
const k = e.key.toLowerCase();
|
||||
const idx = '123456'.indexOf(k);
|
||||
if (idx >= 0 && k.length === 1) { selectType(app.selType === TD.TOWER_ORDER[idx] ? null : TD.TOWER_ORDER[idx]); Audio.sfx('click'); e.preventDefault(); return; }
|
||||
// Space / Enter on a focused button or link press THAT control (keyboard users), not the next-wave hotkey (code-8)
|
||||
if ((k === ' ' || k === 'enter') && e.target && e.target.closest && e.target.closest('button, a, select, [role="button"]')) return;
|
||||
const keyType = k.length === 1 ? TD.TOWER_ORDER.find((t) => TD.TOWERS[t].key === k) : null; // 1-9, 0: one key per tower in the palette
|
||||
if (keyType) { selectType(app.selType === keyType ? null : keyType); Audio.sfx('click'); e.preventDefault(); return; }
|
||||
switch (k) {
|
||||
case 'escape': case 'q': selectType(null); selectTower(0); app.pending = null; updateConfirm(); break;
|
||||
case ' ': case 'enter': ctrl.send({ c: 'wave' }); e.preventDefault(); break;
|
||||
|
|
@ -673,6 +869,8 @@
|
|||
$('btnPause').addEventListener('click', () => { Audio.sfx('click'); app.ctrl.ctl('pause', !app.ctrl.view.paused); });
|
||||
$('btnSound').addEventListener('click', () => { Audio.unlock(); Audio.toggleMuted(); Audio.sfx('click'); });
|
||||
$('btnMenu').addEventListener('click', () => { Audio.sfx('click'); openMenu(); });
|
||||
$('pausedTag').addEventListener('click', () => { Audio.sfx('click'); app.ctrl.ctl('pause', false); });
|
||||
$('netChip').addEventListener('click', () => { if (app.ctrl && app.ctrl.kind === 'remote') { Audio.sfx('click'); invite(); } }); // room code chip: tap to invite
|
||||
|
||||
// ------------------------------------------------------------------ menu / lobby
|
||||
function buildMapPicker() {
|
||||
|
|
@ -818,13 +1016,45 @@
|
|||
}
|
||||
document.addEventListener('visibilitychange', () => { if (!document.hidden && app.inGame) requestWakeLock(); });
|
||||
|
||||
// ------------------------------------------------------------------ app layout (the JS half of the layout modes in style.css)
|
||||
const mq = (q) => (root.matchMedia ? root.matchMedia(q) : { matches: false, addEventListener() {} });
|
||||
const mqStacked = mq('(orientation: portrait), (max-width: 760px) and (min-height: 521px)');
|
||||
const mqCompact = mq('(pointer: coarse), (orientation: portrait), (max-width: 760px) and (min-height: 521px), (orientation: landscape) and (max-height: 520px)');
|
||||
// Stacked (portrait / narrow) layout: the stage hugs the board, so there is no dead band above and below it and the side panel gets
|
||||
// everything else. The board is drawn turned 90 degrees when that makes the cells clearly bigger (tall phones), otherwise upright
|
||||
// and short (small phones: the freed height goes to the palette and info panel). Inputs are the viewport and top bar only (never
|
||||
// the stage's own size), so this cannot feed back into the ResizeObserver.
|
||||
function layoutApp() {
|
||||
const a = $('app'), b = app.ctrl && app.ctrl.board;
|
||||
a.dataset.ui = mqCompact.matches ? 'compact' : 'wide';
|
||||
if (!mqStacked.matches || !b) { a.style.removeProperty('--stage-h'); app.rotate = null; return; }
|
||||
const cs = root.getComputedStyle(a);
|
||||
const W = a.clientWidth - (parseFloat(cs.paddingLeft) || 0) - (parseFloat(cs.paddingRight) || 0);
|
||||
const H = a.clientHeight - (parseFloat(cs.paddingTop) || 0) - (parseFloat(cs.paddingBottom) || 0) - $('topbar').offsetHeight;
|
||||
const mg = (NW.BOARD_MARGIN || 0) * 2, dw = b.cols * NW.CELL + mg, dh = b.rows * NW.CELL + mg, pad = 8;
|
||||
const minSide = clamp(Math.round(H * 0.25), 140, 210); // room for the palette row and a window onto the info panel
|
||||
const maxStage = Math.max(120, H - minSide);
|
||||
const sU = Math.min((W - pad) / dw, (maxStage - pad) / dh), sR = Math.min((W - pad) / dh, (maxStage - pad) / dw);
|
||||
const rot = sR > sU * 1.08;
|
||||
const bh = rot ? dw : dh, bw = rot ? dh : dw;
|
||||
const stageH = Math.round(Math.min(maxStage, Math.ceil(bh * ((W - pad) / bw) + pad)));
|
||||
app.rotate = rot;
|
||||
if (a.style.getPropertyValue('--stage-h') !== stageH + 'px') a.style.setProperty('--stage-h', stageH + 'px');
|
||||
}
|
||||
function resizeCanvas() {
|
||||
layoutApp();
|
||||
const st = $('stage'), r = st.getBoundingClientRect();
|
||||
renderer.setRotation(r.height > r.width * 1.05); // portrait: draw the board turned 90 degrees (bigger cells)
|
||||
renderer.setRotation(app.rotate !== null && app.rotate !== undefined ? app.rotate : r.height > r.width * 1.05); // portrait: board turned 90 degrees (bigger cells)
|
||||
const dpr = Math.min(root.devicePixelRatio || 1, root.matchMedia && root.matchMedia('(pointer: coarse)').matches ? 2 : 2.5);
|
||||
renderer.resize(r.width, r.height, dpr);
|
||||
updatePaletteHint(); layoutBars(true); updateFit(); // (a rotation or board swap resets the camera: hide a stale Reset-zoom button)
|
||||
}
|
||||
if (root.ResizeObserver) new ResizeObserver(resizeCanvas).observe($('stage')); else root.addEventListener('resize', resizeCanvas);
|
||||
let resizeRaf = 0;
|
||||
function scheduleResize() { if (!resizeRaf) resizeRaf = root.requestAnimationFrame(() => { resizeRaf = 0; resizeCanvas(); }); } // (a frame later: never resize inside the observer callback)
|
||||
if (root.ResizeObserver) { const ro = new ResizeObserver(scheduleResize); ro.observe($('stage')); ro.observe($('topbar')); ro.observe($('palette')); }
|
||||
root.addEventListener('resize', scheduleResize);
|
||||
mqStacked.addEventListener && mqStacked.addEventListener('change', scheduleResize);
|
||||
mqCompact.addEventListener && mqCompact.addEventListener('change', scheduleResize);
|
||||
root.addEventListener('orientationchange', () => setTimeout(() => { resizeCanvas(); drawPaletteIcons(); }, 250));
|
||||
// keep the page from scrolling/bouncing or zooming on iOS
|
||||
document.addEventListener('gesturestart', (e) => e.preventDefault());
|
||||
|
|
@ -843,8 +1073,13 @@
|
|||
else if (app.endShown) { app.endShown = false; $('end').hidden = true; }
|
||||
}
|
||||
if (app.selTower && !view.towers.some((t) => t.id === app.selTower)) { app.selTower = 0; renderInfo(true); }
|
||||
if (app.last.rev !== view.rev) { app.last.rev = view.rev; app.previewDirty = true; if (app.selTower) renderInfo(true); }
|
||||
if (app.pending && pendFree) { // the partner built on the cell being previewed: drop it and say so (coop-7)
|
||||
const t = towerAtCell(app.pending);
|
||||
if (t) { setPending(null); toast(((ui.names && ui.names[t.owner]) || 'Someone') + ' just built there', 'bad'); }
|
||||
}
|
||||
if (app.last.rev !== view.rev) { app.last.rev = view.rev; app.previewDirty = true; if (app.selTower) renderInfo(false); }
|
||||
updateHud(view);
|
||||
layoutBars(false);
|
||||
if (now - app.lastBossCheck > 600 && view.state === 'playing') {
|
||||
app.lastBossCheck = now;
|
||||
const boss = view.monsters.some((m) => m.kind === 'rootkit');
|
||||
|
|
@ -861,6 +1096,7 @@
|
|||
// ------------------------------------------------------------------ boot
|
||||
function boot() {
|
||||
buildPalette(); buildMapPicker();
|
||||
{ const keys = TD.TOWER_ORDER.map((t) => TD.TOWERS[t].key); $('helpKeys').textContent = keys[0] + '\u2013' + keys[keys.length - 1]; }
|
||||
previewMap(app.mapId);
|
||||
setScreen('menu'); showOverlay('menu');
|
||||
$('btnCloseMenu').hidden = true;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue