@@ -16,6 +16,10 @@ const _3s = _1s * 3;
16
16
const _4s = _1s * 4 ;
17
17
const _5s = _1s * 5 ;
18
18
19
+ function sec ( x ) {
20
+ return _1s * x ;
21
+ }
22
+
19
23
const styles = {
20
24
default : "\x1b[0m" ,
21
25
bold : "\x1b[1m" ,
@@ -277,7 +281,15 @@ function getKeyString(key) {
277
281
return "Anykey" ;
278
282
}
279
283
280
- // TODO: Semi-infinite "....." prompt
284
+ async function readAutoKey ( ) {
285
+ setStyle ( styles . faint + styles . white ) ;
286
+ const key = await waitAutoKey ( sec ( 5 ) , randomInt ( sec ( 8 ) , sec ( 16 ) ) , "\r" ) ;
287
+ await moveCursorHome ( ) ;
288
+ resetStyle ( ) ;
289
+
290
+ return key ;
291
+ }
292
+
281
293
async function readKey ( prompt = ".." , echo = false ) {
282
294
while ( true ) {
283
295
@@ -286,7 +298,6 @@ async function readKey(prompt = "..", echo = false) {
286
298
await type ( prompt ) ;
287
299
resetStyle ( ) ;
288
300
289
- // TODO: Add option to don't await input indefinitely - e.g. set timer and "run" CLS command from time to time.
290
301
const key = await waitKey ( ) ;
291
302
292
303
if ( echo ) {
@@ -389,11 +400,12 @@ async function puzzle1() {
389
400
await command ( "CLS" ) ;
390
401
391
402
await typeln ( `You ${ randomMsg ( [ "see a" , "enter the" , "step into the" ] ) } small, dark room covered in old webs` ) ;
403
+ await wait ( _hs ) ;
392
404
await typeln ( "with just table, chair and rusty terminal on it." ) ;
393
405
await typeln ( ) ;
394
406
await typeln ( "There is no doors or even windows!" ) ;
395
407
396
- await readKey ( ) ;
408
+ await readAutoKey ( ) ;
397
409
398
410
return true ;
399
411
}
@@ -444,7 +456,7 @@ async function scene2_greeting() {
444
456
while ( true ) {
445
457
const choice = await menu ( [
446
458
{ text : "" , choice : "showMenu" } ,
447
- { text : " Continue protogame" , choice : "continue" } ,
459
+ { text : ` Continue ${ randomMsg ( [ "adventure" , "your journey" ] ) } ` , choice : "continue" } ,
448
460
{ text : "You have emails: (1)" , choice : "email" } ,
449
461
{ text : "New protogame (resets progress)" , choice : "newGame" }
450
462
] , showMenu ) ;
@@ -481,7 +493,7 @@ async function scene2_greeting() {
481
493
await typeln ( randomMsg ( [ "Done." , "Done." , "Yes!" , "Meow!" , "Wow!" , "Clap!" , "Zzz..." , "Shhh..." , "Flip!" , "Flop!" , "Slap!" , "Plop!" , "Boom!" , "Ding!" ] ) ) ;
482
494
await typeln ( ) ;
483
495
484
- await readKey ( ) ;
496
+ await readAutoKey ( ) ;
485
497
486
498
// to the world
487
499
if ( game . isNewGame ( ) ) {
@@ -604,7 +616,7 @@ async function scene4_world(note) {
604
616
await typeNote ( note , noteColor ) ;
605
617
showNote = false ;
606
618
607
- await readKey ( ) ;
619
+ await readAutoKey ( ) ;
608
620
}
609
621
610
622
// TODO: Positive/negative switch: "Don't follow"
@@ -813,6 +825,58 @@ async function scene4_world(note) {
813
825
return false ;
814
826
}
815
827
828
+ /**
829
+ * @param {* } silentTime Initial time to wait w/o typing anything
830
+ * @param {* } maxWaitTime Max. time to wait for user input before simulating key press
831
+ * @param {* } autoKey Key to pass as if user pressed it
832
+ * @returns
833
+ */
834
+ async function waitAutoKey ( silentTime , maxWaitTime , autoKey ) {
835
+ t . focus ( ) ;
836
+ await type ( ".." ) ;
837
+
838
+ const pollInterval = 100 ;
839
+ const typeDelay = 500 ;
840
+ let totalWaitTime = 0 ;
841
+ let typeWaitTime = 0 ;
842
+
843
+ // use it like a buffer
844
+ game . lastKey = null ;
845
+ return new Promise ( ( resolve ) => {
846
+ const interval = setInterval ( ( ) => {
847
+ totalWaitTime += pollInterval ;
848
+
849
+ if ( totalWaitTime > silentTime ) {
850
+ typeWaitTime += pollInterval ;
851
+ if ( typeWaitTime > typeDelay ) {
852
+ t . write ( "." ) ;
853
+ typeWaitTime = 0 ;
854
+ }
855
+ }
856
+
857
+ if ( totalWaitTime > maxWaitTime ) {
858
+ game . lastKey = autoKey ;
859
+ }
860
+
861
+ const key = game . lastKey ;
862
+ if ( key ) {
863
+ clearInterval ( interval ) ;
864
+ console . log ( { key : game . lastKey } ) ;
865
+ resolve ( game . lastKey ) ;
866
+ }
867
+ } , pollInterval ) ;
868
+ } ) ;
869
+ }
870
+
871
+ /** CSI Ps G: Moves cursor to column #1, without cleanup */
872
+ async function moveCursorHome ( ) {
873
+ await type ( "\x9B1G" ) ;
874
+ }
875
+
876
+ async function test ( ) {
877
+ return true ;
878
+ }
879
+
816
880
export class App {
817
881
init ( ) {
818
882
const t = new Terminal ( {
@@ -841,6 +905,7 @@ export class App {
841
905
}
842
906
843
907
async main ( ) {
908
+ //await test();
844
909
await scene1_puzzlebox ( ) ;
845
910
await command ( "CLS" ) ;
846
911
setTheme ( bowTheme ) ;
0 commit comments