The arrow keys and W A S D move the map while held

The map moves at 700 px a second on screen at any zoom. The keys run
their own frame loop only while one is down, so the game loop does no
extra work. Letting go, a window opening or leaving the browser window
stops it, and a slow frame moves at most 50 ms worth. Arrows still
scroll the NOC dashboard when it is open, and Alt or Cmd with an arrow
stays the browser's.
This commit is contained in:
Charlie Allom 2026-11-03 10:31:42 +00:00
parent d88684d86e
commit 2680f3d82b
No known key found for this signature in database
9 changed files with 52 additions and 5 deletions

View file

@ -1,13 +1,21 @@
/* ---------- Keyboard shortcuts: [ ] step through your POPs, - = zoom, n (then a letter) the NOC, ? lists every key ----------
/* ---------- Keyboard shortcuts: arrows/WASD move, [ ] step through your POPs, - = zoom, n (then a letter) the NOC, ? lists every key ----------
Space, 0–4 and Escape live with the map's input (96-map/90-input.js), / with Find a city. */
// Your POPs in the order Find a city lists them, so [ and ] walk the same list.
const popOrder=()=>S&&S.pops?Object.keys(S.pops).filter(isCity).sort((a,b)=>CITY[a].name.localeCompare(CITY[b].name)):[];
// n opens the NOC dashboard, and a letter within KEY_LEAD ms of it picks a tab (n c: Connections). n never closes the
// dashboard, so n c switches tabs without it flashing shut; Escape closes it.
const DASH_KEYS={o:'overview',u:'cust',c:'conn',t:'traffic',r:'infra',l:'lease'},KEY_LEAD=1000;let keyLead=0;
// Arrow keys and WASD move the map while held, at a steady speed on screen. Their own frame loop runs only while one is
// down, so the game loop does no extra work; letting go, a window opening or leaving the browser window stops it.
const PAN_KEYS={ArrowUp:[0,-1],w:[0,-1],ArrowDown:[0,1],s:[0,1],ArrowLeft:[-1,0],a:[-1,0],ArrowRight:[1,0],d:[1,0]},PAN_SPEED=700;
const panHeld=new Set();let panAt=null;
function panFrame(t){if(!panHeld.size||!$('modal').hidden||!$('dash').hidden){panHeld.clear();return;}
// A slow frame (a busy day, a background tab) moves at most 50 ms worth, not a jump across the map.
const dt=panAt==null?0:Math.min(0.05,(t-panAt)/1000);panAt=t;let dx=0,dy=0;for(const k of panHeld){dx+=PAN_KEYS[k][0];dy+=PAN_KEYS[k][1];}
cam.x+=dx*PAN_SPEED*dt/cam.z;cam.y+=dy*PAN_SPEED*dt/cam.z;wrapCam();requestAnimationFrame(panFrame);}
function showKeys(){const k=s=>`<span class="kbd">${s}</span>`,TN={overview:tr('common.overview'),cust:tr('common.customers'),conn:tr('common.connections'),traffic:tr('common.traffic'),infra:tr('common.links_routers'),lease:tr('common.leased_lines')};
const tabs=Object.entries(DASH_KEYS).map(([l,t])=>`<span class="keytab">${k(l)} ${TN[t]}</span>`).join(' ');
const rows=[[k(tr('keys.space')),tr('keys.pause_or_play')],[k('0')+' – '+k('4'),tr('keys.game_speed')],[k('/'),tr('find.find_a_city')],[k('[')+' '+k(']'),tr('keys.previous_or_next_of_your')],[k('-')+' '+k('='),tr('keys.zoom_the_map_out_or')],[k('n'),`${tr('keys.noc_dashboard_a_letter_right')}<br>${tabs}`],[k(tr('keys.esc')),tr('keys.close_a_window_or_deselect')],[k('?'),tr('keys.show_this_list')]];
const rows=[[[...'←↑↓→'].map(k).join(' ')+'<br>'+[...'wasd'].map(k).join(' '),tr('keys.move_around_the_map')],[k(tr('keys.space')),tr('keys.pause_or_play')],[k('0')+' – '+k('4'),tr('keys.game_speed')],[k('/'),tr('find.find_a_city')],[k('[')+' '+k(']'),tr('keys.previous_or_next_of_your')],[k('-')+' '+k('='),tr('keys.zoom_the_map_out_or')],[k('n'),`${tr('keys.noc_dashboard_a_letter_right')}<br>${tabs}`],[k(tr('keys.esc')),tr('keys.close_a_window_or_deselect')],[k('?'),tr('keys.show_this_list')]];
openModal(`<div style="display:flex;align-items:center"><h2 style="font-size:28px">⌨️ ${tr('keys.keyboard_shortcuts')}</h2><button class="x" data-act="helpClose" aria-label="${tr('common.close')}">×</button></div>
<dl class="keylist">${rows.map(([a,b])=>`<dt>${a}</dt><dd>${b}</dd>`).join('')}</dl><div class="btns" style="margin-top:14px">${btn(UI.started?tr('common.back_to_the_map'):tr('common.got_it'),'helpClose',{},'big')}</div>`,'keys');}
document.addEventListener('keydown',e=>{
@ -18,5 +26,9 @@ document.addEventListener('keydown',e=>{
if(lead&&DASH_KEYS[k]&&!$('dash').hidden){e.preventDefault();ACT.dashTab({k:DASH_KEYS[k]});return;}
if(k==='n'){if(!S||!UI.started||!$('modal').hidden)return;e.preventDefault();if($('dash').hidden)ACT.dash();if(!$('dash').hidden)keyLead=Date.now();return;}
if(k==='?'){if(e.repeat)return;if($('modal').hidden){if(S&&UI.started){e.preventDefault();ACT.keys();}}else if($('modal').className==='keys'){e.preventDefault();closeModal();}return;}
// Alt with an arrow is the browser's Back and Forward.
if(!e.altKey&&PAN_KEYS[k]){if(!S||!UI.started||!$('modal').hidden||!$('dash').hidden)return;e.preventDefault();if(!panHeld.size){panAt=null;requestAnimationFrame(panFrame);}panHeld.add(k);return;}
if(!(step||zoom)||!S||!UI.started||!$('modal').hidden)return;
e.preventDefault();if(step)ACT.popStep({d:step});else ACT.zoomStep({d:zoom});});
document.addEventListener('keyup',e=>panHeld.delete(e.key.length===1?e.key.toLowerCase():e.key));
addEventListener('blur',()=>panHeld.clear());

View file

@ -2373,6 +2373,7 @@
"keys.esc":"Esc",
"keys.game_speed":"Game speed: ½×, 1×, 2×, 4×, 8×",
"keys.keyboard_shortcuts":"Keyboard shortcuts",
"keys.move_around_the_map":"Move around the map",
"keys.noc_dashboard_a_letter_right":"NOC dashboard. A letter right after it opens a tab:",
"keys.pause_or_play":"Pause or play",
"keys.previous_or_next_of_your":"Previous or next of your POPs",