diff --git a/CHANGELOG.md b/CHANGELOG.md index fd6c7f6..4de926b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ All notable changes to this project are documented here. The project follows ## [Unreleased] - Fabric (NNI) ports now grow like your other ports: buy them at 10G, 100G or 400G (as fast as your router takes), and bundle up to eight into a LAG when one is not enough (#140). A fabric customer is no longer accepted, by hand or automatically, when its bandwidth would not fit on the port as well (#307). +- Pressing Space to play again after a pause now picks up at the speed you were playing, not always at 1× (#299). - Your own subsea cables now land right on the city's dot, also in Fujairah, Dubai, Karachi, Barcelona, Osaka and other cities the map shifts slightly so they do not overlap (#305). - Starting a new network no longer leaves the last one's "New POP!" and money pop-ups frozen all over the map until a reload. diff --git a/src/game/90-actions/10-interface.js b/src/game/90-actions/10-interface.js index 4224aac..ca643e8 100644 --- a/src/game/90-actions/10-interface.js +++ b/src/game/90-actions/10-interface.js @@ -36,7 +36,7 @@ const ACT={ meetGo:({city})=>{if(!isCity(city))return;$('dash').hidden=true;ACT.focusCity({city});if(S.pops[city]){UI.tab='peer';renderPanel();}}, toggleLeft:()=>{UI.leftOpen=!UI.leftOpen;renderLeft();}, phoneTab:({t})=>{if(!['goals','deals','noc','nets'].includes(t))return;if($('modal').classList.contains('link-check'))closeModal();haptic(8);if(UI.leftOpen&&UI.left===t&&!UI.sel){UI.leftOpen=false;}else{UI.left=t;UI.leftOpen=true;UI.sel=null;renderPanel();}$('left').classList.remove('full');renderLeft();}, - phonePlay:()=>{if(pzOn()||UI.clockLocked)return;if(S&&!rule('clock'))return;haptic(10);if(UI.speed){UI.lastSpeed=UI.speed;UI.speed=0;}else UI.speed=UI.lastSpeed||1;renderHUD();}, + phonePlay:()=>{if(playPause())haptic(10);}, phoneSpeed:()=>{if(pzOn()||UI.clockLocked)return;if(S&&!rule('clock'))return;haptic(8);const cur=UI.speed||UI.lastSpeed||1,i=SPEEDS.indexOf(cur),n=SPEEDS[(i+1)%SPEEDS.length];UI.speed=n;UI.lastSpeed=n;renderHUD();}, install:()=>{if(installEvt){installEvt.prompt();installEvt.userChoice.finally(()=>{installEvt=null;});return;} openModal(pitchHtml(false));}, diff --git a/src/game/96-map/30-settings.js b/src/game/96-map/30-settings.js index b2874fd..fcc7dac 100644 --- a/src/game/96-map/30-settings.js +++ b/src/game/96-map/30-settings.js @@ -16,7 +16,9 @@ const pref=k=>typeof PREFS[k]==='boolean'?PREFS[k]:PREF_DEF[k]; const quietOn=()=>typeof PREFS.quiet==='boolean'?PREFS.quiet:rulePreset()==='soft'; function setPref(k,v){if(!own(PREF_DEF,k))return;PREFS[k]=!!v;try{localStorage.setItem('sot-prefs',JSON.stringify(PREFS));}catch(e){}} // Stop the clock for something the player must see. Nothing happens if the game is already paused, or not the player's. -function autoPause(){if(!S||!UI.speed||UI.clockLocked)return false;UI.resumeSpeed=UI.speed;UI.speed=0;renderHUD();return true;} +function autoPause(){if(!S||!UI.speed||UI.clockLocked)return false;UI.resumeSpeed=UI.lastSpeed=UI.speed;UI.speed=0;renderHUD();return true;} +// Space and the phone's play button: pause, or play again at the speed it ran before (#299). +function playPause(){if(pzOn()||UI.clockLocked||(S&&!rule('clock')))return false;if(UI.speed){UI.lastSpeed=UI.speed;UI.speed=0;}else UI.speed=UI.lastSpeed||1;renderHUD();return true;} // Put the clock back at a speed (a new game, a loaded or resumed one): unless it is locked, then it runs as the lock says. function setSpeed(v){UI.speed=UI.clockLocked&&HOOKS.speed?HOOKS.speed():v;} const resumeAct=()=>({l:tr('common.resume'),act:'speed',args:{v:UI.resumeSpeed||1}}); diff --git a/src/game/96-map/90-input.js b/src/game/96-map/90-input.js index 58dc74d..f43b309 100644 --- a/src/game/96-map/90-input.js +++ b/src/game/96-map/90-input.js @@ -42,5 +42,5 @@ cv.addEventListener('pointerup',endPtr);cv.addEventListener('pointercancel',canc const ZMIN=0.35,ZMAX=32; function zoomAt(mx,my,f){const wx=cam.x+(mx-CW/2)/cam.z,wy=cam.y+(my-CH/2)/cam.z;cam.z=clamp(cam.z*f,ZMIN,ZMAX);cam.x=wx-(mx-CW/2)/cam.z;cam.y=wy-(my-CH/2)/cam.z;wrapCam();} cv.addEventListener('wheel',e=>{e.preventDefault();zoomAt(e.clientX,e.clientY,Math.exp(-e.deltaY*0.0015));},{passive:false}); -addEventListener('keydown',e=>{if(e.key==='Enter'&&e.target.id==='metIn'){ACT.metricSet({id:e.target.dataset.id});return;}if(e.target.closest&&e.target.closest('input,textarea,select,[contenteditable]'))return;if(e.ctrlKey||e.metaKey||e.altKey)return;if(e.code==='Space'&&e.target.closest&&e.target.closest('button,a'))return;if(e.code==='Space'&&(!S||rule('clock'))&&!pzOn()&&!UI.clockLocked){e.preventDefault();UI.speed=UI.speed?0:1;renderHUD();}if(e.key==='0')ACT.speed({v:0.5});if(e.key==='1')ACT.speed({v:1});if(e.key==='2')ACT.speed({v:2});if(e.key==='3')ACT.speed({v:4});if(e.key==='4')ACT.speed({v:8});if(e.key==='Escape'&&!$('bankBox').hidden){$('bankBox').hidden=true;return;}if(e.key==='Escape'&&!$('dash').hidden){$('dash').hidden=true;return;}if(e.key==='Escape'){if(!$('modal').hidden){if(S&&UI.started&&!S.lost)closeModal();}else{UI.sel=null;renderPanel();}}}); +addEventListener('keydown',e=>{if(e.key==='Enter'&&e.target.id==='metIn'){ACT.metricSet({id:e.target.dataset.id});return;}if(e.target.closest&&e.target.closest('input,textarea,select,[contenteditable]'))return;if(e.ctrlKey||e.metaKey||e.altKey)return;if(e.code==='Space'&&e.target.closest&&e.target.closest('button,a'))return;if(e.code==='Space'&&(!S||rule('clock'))&&!pzOn()&&!UI.clockLocked){e.preventDefault();playPause();}if(e.key==='0')ACT.speed({v:0.5});if(e.key==='1')ACT.speed({v:1});if(e.key==='2')ACT.speed({v:2});if(e.key==='3')ACT.speed({v:4});if(e.key==='4')ACT.speed({v:8});if(e.key==='Escape'&&!$('bankBox').hidden){$('bankBox').hidden=true;return;}if(e.key==='Escape'&&!$('dash').hidden){$('dash').hidden=true;return;}if(e.key==='Escape'){if(!$('modal').hidden){if(S&&UI.started&&!S.lost)closeModal();}else{UI.sel=null;renderPanel();}}}); diff --git a/tests/space-resume.test.js b/tests/space-resume.test.js new file mode 100644 index 0000000..6bfe617 --- /dev/null +++ b/tests/space-resume.test.js @@ -0,0 +1,32 @@ +// #299: Space pauses, and Space again plays at the speed you paused, not always at 1×. +const test = require('node:test'); +const assert = require('node:assert/strict'); +const { load } = require('./harness'); + +const start = "ACT.start({});S.nextFail=1e9;S.nextEvent=1e9;S.cash=1e9"; + +test('#299 Space resumes at the speed the game ran before the pause', () => { + const { ev, dom, errors } = load(); + try { + const space = () => dom.window.document.body.dispatchEvent(new dom.window.KeyboardEvent('keydown', { key: ' ', code: 'Space', bubbles: true })); + ev(start); + for (const [key, v] of [['3', 4], ['4', 8], ['0', 0.5], ['2', 2]]) { + dom.window.document.body.dispatchEvent(new dom.window.KeyboardEvent('keydown', { key, bubbles: true })); + assert.equal(ev('UI.speed'), v); + space(); assert.equal(ev('UI.speed'), 0, 'Space pauses'); + space(); assert.equal(ev('UI.speed'), v, `Space plays at ${v}× again`); + } + // A speed that came from a loaded game (not from a speed button) comes back too. + ev('UI.lastSpeed=undefined;setSpeed(4)'); + space(); space(); assert.equal(ev('UI.speed'), 4, 'a loaded game at 4× resumes at 4×'); + // The phone's play button and Space share the speed they come back to. + ev('ACT.speed({v:8})'); space(); ev('ACT.phonePlay()'); assert.equal(ev('UI.speed'), 8); + ev('ACT.phonePlay()'); space(); assert.equal(ev('UI.speed'), 8); + // A window that paused the game for you: Space plays at the speed it stopped. + ev('UI.lastSpeed=undefined;setSpeed(4);autoPause()'); assert.equal(ev('UI.speed'), 0); + space(); assert.equal(ev('UI.speed'), 4, 'after a pause the game made itself'); + // Nothing new playing for the first time: 1×. + ev('UI.lastSpeed=undefined;UI.speed=0'); space(); assert.equal(ev('UI.speed'), 1); + assert.deepEqual(errors, []); + } finally { dom.window.close(); } +});