diff --git a/README.md b/README.md index a8fd4eb..302972b 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,10 @@ node prod/index.js ## How to play -The goal is to squash all beasts in each level. To squash a beast between two or more blocks. Press `q` to quit. +The goal is to squash all beasts in each level. To squash a beast between two or more blocks. +When a beast touches you, you die. You got four lives to spare. +Once all beasts are squashed you move on to the next level. There are four levels so far. Easily configurable if you want to extend them. +Press `q` to quit. ## Contributing @@ -61,6 +64,7 @@ TODO ## Release History +* 0.1.3 - Improved drawing * 0.1.2 - Fixed error message * 0.1.1 - Added level indicator, fixed lives display * 0.1.0 - alpha release diff --git a/dev/030-draw.js b/dev/030-draw.js index 84424d5..233ca65 100644 --- a/dev/030-draw.js +++ b/dev/030-draw.js @@ -17,23 +17,34 @@ //-------------------------------------------------------------------------------------------------------------------------------------------------------------- BEAST.draw = (() => { + +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Private function +// getSpaceLeft, Return the space we need left from the frame +// +// @return {integer} The amount of spaces we need to get inside the board from the left, rounded +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- + const getSpaceLeft = ( item ) => { + BEAST.debugging.report(`draw: getSpaceLeft`, 1); + + let spaceLeft = Math.floor( ( CliSize().columns - BEAST.MINWIDTH ) / 2 ) + 1; //horizontal alignment + + return spaceLeft + } + + //-------------------------------------------------------------------------------------------------------------------------------------------------------------- // Private function -// printLine, Print a row inside the board +// getSpaceTop, Return the space we need from the top to inside the board // -// @param item {string} The string to be written +// @return {integer} The amount of spaces we need to get inside the board from the top, not rounded //-------------------------------------------------------------------------------------------------------------------------------------------------------------- - const printLine = ( item ) => { - BEAST.debugging.report(`draw: printLine`, 1); + const getSpaceTop = ( item ) => { + BEAST.debugging.report(`draw: getSpaceTop`, 1); - //testing screen size and just printing on error - let error = BEAST.checkSize(); - if( error === '' ) { - let spaceLeft = Math.floor( ( CliSize().columns - BEAST.MINWIDTH ) / 2 ); //horizontal alignment - spaceLeft = ' '.repeat( spaceLeft ); + let spacetop = ( CliSize().rows - BEAST.MINHEIGHT ) / 2; //vertically alignment - BEAST.RL.write(`${spaceLeft}${Chalk.gray(`│`)}${item}\n`); //print line inside the frame - } + return spacetop } @@ -45,7 +56,7 @@ BEAST.draw = (() => { frame: () => { BEAST.debugging.report(`draw: frame`, 1); - customStdout.muted = false; + customStdout.muted = false; //allow output so we can draw Readline.cursorTo( BEAST.RL, 0, 0 ); //go to top of board Readline.clearScreenDown( BEAST.RL ); //clear screen @@ -53,18 +64,16 @@ BEAST.draw = (() => { //testing screen size and just printing on error let error = BEAST.checkSize(); if( error !== '' ) { - Readline.cursorTo( BEAST.RL, 0, 0 ); //go to top of board - Readline.clearScreenDown( BEAST.RL ); //clear screen BEAST.RL.write(`\n\n${error}`); } else { - let spaceLeft = Math.floor( ( CliSize().columns - BEAST.MINWIDTH ) / 2 ); //horizontal alignment + let spaceLeft = getSpaceLeft() - 1; //horizontal alignment spaceLeft = ' '.repeat( spaceLeft ); - let spacetop = Math.ceil( ( CliSize().rows - BEAST.MINHEIGHT ) / 2 ); //vertically alignment - spacetop = `\n`.repeat( spacetop ); + let spaceTop = Math.ceil( getSpaceTop() ); //vertically alignment + spaceTop = `\n`.repeat( spaceTop ); - BEAST.RL.write( spacetop ); + BEAST.RL.write( spaceTop ); BEAST.RL.write( `${spaceLeft}${Chalk.green(` ╔╗ ╔═╗ ╔═╗ ╔═╗ ╔╦╗`)}\n` + `${spaceLeft}${Chalk.cyan (` ╠╩╗ ║╣ ╠═╣ ╚═╗ ║`)}\n` + @@ -75,10 +84,10 @@ BEAST.draw = (() => { BEAST.RL.write(`${spaceLeft}${Chalk.gray(`│${' '.repeat( BEAST.MINWIDTH - 2 )}│`)}\n`.repeat( BEAST.MINHEIGHT - 7 )); BEAST.RL.write(`${spaceLeft}${Chalk.gray(`└${'─'.repeat( BEAST.MINWIDTH - 2 )}┘`)}\n\n`); - BEAST.RL.write( spacetop ); + BEAST.RL.write( spaceTop ); } - customStdout.muted = true; + customStdout.muted = true; //no more user output now! }, @@ -89,31 +98,29 @@ BEAST.draw = (() => { score: () => { BEAST.debugging.report(`draw: score`, 1); - customStdout.muted = false; + customStdout.muted = false; //allow output so we can draw //testing screen size let error = BEAST.checkSize(); if( error === '' ) { - let top = Math.floor( ( CliSize().rows - BEAST.MINHEIGHT ) / 2 ); - Readline.cursorTo( BEAST.RL, 0, (top + 4 + ( BEAST.MINHEIGHT - 6 )) ); //go to bottom of board + let spaceTop = Math.floor( getSpaceTop() ); + let spaceLeft = getSpaceLeft(); - let spaceLeft = Math.floor( ( CliSize().columns - BEAST.MINWIDTH ) / 2 ); //horizontal alignment - spaceLeft = ' '.repeat( spaceLeft ); + Readline.cursorTo( BEAST.RL, spaceLeft, (spaceTop + 4 + ( BEAST.MINHEIGHT - 6 )) ); //go to bottom of board //calculate the space between lives and beast count - let spaceMiddle = ( BEAST.MINWIDTH - 2 ) - ( 3 * BEAST.LIVES ) - 6 - ( Object.keys( BEAST.BEASTS ).length.toString().length ); + let spaceMiddle = ( BEAST.MINWIDTH - 2 ) - ( 3 * BEAST.LIVES ) - 3 - ( Object.keys( BEAST.BEASTS ).length.toString().length ); BEAST.RL.write( - `${spaceLeft}${Chalk.red(` ${BEAST.SYMBOLS.hero}`).repeat( BEAST.LIVES - BEAST.DEATHS )}` + - `${Chalk.gray(` ${BEAST.SYMBOLS.hero}`).repeat( BEAST.DEATHS )}` + `${Chalk.red(` ${BEAST.SYMBOLS.hero}`).repeat( BEAST.LIVES - BEAST.DEATHS )}` + + `${Chalk.gray(` ${BEAST.SYMBOLS.hero}`).repeat( BEAST.DEATHS )}` + + `${' '.repeat( spaceMiddle )} ${ Object.keys( BEAST.BEASTS ).length } x ${BEAST.SYMBOLS.beast}` ); - BEAST.RL.write(`${' '.repeat( spaceMiddle )} ${ Object.keys( BEAST.BEASTS ).length } x ${BEAST.SYMBOLS.beast}`); - Readline.cursorTo( BEAST.RL, 0, (CliSize().rows - 1) ); //go to bottom of board and rest cursor there } - customStdout.muted = true; + customStdout.muted = true; //no more user output now! }, @@ -124,23 +131,23 @@ BEAST.draw = (() => { level: () => { BEAST.debugging.report(`draw: level`, 1); - customStdout.muted = false; + customStdout.muted = false; //allow output so we can draw //testing screen size let error = BEAST.checkSize(); if( error === '' ) { - let top = Math.floor( ( CliSize().rows - BEAST.MINHEIGHT ) / 2 ); - let spaceLeft = Math.floor( ( CliSize().columns - BEAST.MINWIDTH ) / 2 ); //horizontal alignment - let spaceMiddle = ( BEAST.MINWIDTH - 2 ) - 9 - ( Object.keys( BEAST.LEVEL ).length.toString().length ); //calculate the space so we can right align + let spaceTop = Math.floor( getSpaceTop() ); + let spaceLeft = getSpaceLeft(); //horizontal alignment + let spaceMiddle = ( BEAST.MINWIDTH - 2 ) - 10 - ( Object.keys( BEAST.LEVEL ).length.toString().length ); //calculate the space so we can right align - Readline.cursorTo( BEAST.RL, (spaceLeft + spaceMiddle), (top + 2) ); //go to top above the board and right align + Readline.cursorTo( BEAST.RL, (spaceLeft + spaceMiddle), (spaceTop + 2) ); //go to top above the board and right align BEAST.RL.write(` Level: ${BEAST.LEVEL}`); Readline.cursorTo( BEAST.RL, 0, (CliSize().rows - 1) ); //go to bottom of board and rest cursor there } - customStdout.muted = true; + customStdout.muted = true; //no more user output now! }, @@ -153,8 +160,10 @@ BEAST.draw = (() => { customStdout.muted = false; //allow output so we can draw - let top = Math.floor( ( CliSize().rows - BEAST.MINHEIGHT ) / 2 ); - Readline.cursorTo( BEAST.RL, 0, (top + 4) ); //go to top of board + let spaceTop = Math.floor( getSpaceTop() ); + let spaceLeft = getSpaceLeft(); + + Readline.cursorTo( BEAST.RL, 0, (spaceTop + 4) ); //go to top of board for(let boardRow of BEAST.BOARD) { //iterate over each row let line = ''; //translate BEAST.BOARD to ASCII @@ -170,7 +179,8 @@ BEAST.draw = (() => { } } - printLine( line ); //print the compiled line onto the board + Readline.moveCursor(BEAST.RL, spaceLeft, 0); //move cursor into board + BEAST.RL.write(`${line}\n`); //print line inside the frame } Readline.cursorTo( BEAST.RL, 0, (CliSize().rows - 1) ); //go to bottom of board and rest cursor there @@ -189,20 +199,24 @@ BEAST.draw = (() => { message: ( message, color = 'black' ) => { customStdout.muted = false; //allow output so we can draw - let top = Math.floor( ( CliSize().rows - BEAST.MINHEIGHT ) / 2 ); - Readline.cursorTo( BEAST.RL, 0, (top + 4 + Math.floor( ( BEAST.MINHEIGHT - 7 ) / 2 ) - 2) ); //go to middle of board + let spaceTop = Math.floor( getSpaceTop() ); + let spaceLeft = getSpaceLeft(); //space left from frame - let spaceShoulder = Math.floor( ( CliSize().columns - BEAST.MINWIDTH ) / 2 ); //space left from frame - spaceShoulder = ' '.repeat( spaceShoulder ); + Readline.cursorTo( BEAST.RL, spaceLeft, (spaceTop + 4 + Math.floor( ( BEAST.MINHEIGHT - 7 ) / 2 ) - 2) ); //go to middle of board - let spaceLeft = Math.floor( ( (BEAST.MINWIDTH - 2) / 2 ) - ( (message.length + 2) / 2 ) ); - let spaceRight = Math.ceil( ( (BEAST.MINWIDTH - 2) / 2 ) - ( (message.length + 2) / 2 ) ); + let space = ( (BEAST.MINWIDTH - 2) / 2 ) - ( (message.length + 2) / 2 ); //rest space minus the message length + let spaceMiddleLeft = Math.floor( space ); + let spaceMiddleRight = Math.ceil( space ); - BEAST.RL.write(`${spaceShoulder}${Chalk.gray(`│`)}${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); - BEAST.RL.write(`${spaceShoulder}${Chalk.gray(`│`)}${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); - BEAST.RL.write(`${spaceShoulder}${Chalk.gray(`│`)}${' '.repeat( spaceLeft )}${Chalk[ color ].bgWhite.bold(` ${message} `)}${' '.repeat( spaceRight )}\n`); - BEAST.RL.write(`${spaceShoulder}${Chalk.gray(`│`)}${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); - BEAST.RL.write(`${spaceShoulder}${Chalk.gray(`│`)}${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); + BEAST.RL.write(`${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); //clear line + Readline.moveCursor(BEAST.RL, spaceLeft, 0); //move cursor into board + BEAST.RL.write(`${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); //clear line + Readline.moveCursor(BEAST.RL, spaceLeft, 0); //move cursor into board + BEAST.RL.write(`${' '.repeat( spaceMiddleLeft )}${Chalk[ color ].bgWhite.bold(` ${message} `)}${' '.repeat( spaceMiddleRight )}\n`); + Readline.moveCursor(BEAST.RL, spaceLeft, 0); //move cursor into board + BEAST.RL.write(`${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); //clear line + Readline.moveCursor(BEAST.RL, spaceLeft, 0); //move cursor into board + BEAST.RL.write(`${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); //clear line Readline.cursorTo( BEAST.RL, 0, (CliSize().rows - 1) ); //go to bottom of board and rest cursor there diff --git a/dev/dev.js b/dev/dev.js index 6c7d0d4..f685c71 100644 --- a/dev/dev.js +++ b/dev/dev.js @@ -309,23 +309,34 @@ BEAST.scaffolding = (() => { //-------------------------------------------------------------------------------------------------------------------------------------------------------------- BEAST.draw = (() => { + //-------------------------------------------------------------------------------------------------------------------------------------------------------------- // Private function -// printLine, Print a row inside the board +// getSpaceLeft, Return the space we need left from the frame // -// @param item {string} The string to be written +// @return {integer} The amount of spaces we need to get inside the board from the left, rounded //-------------------------------------------------------------------------------------------------------------------------------------------------------------- - const printLine = ( item ) => { - BEAST.debugging.report(`draw: printLine`, 1); + const getSpaceLeft = ( item ) => { + BEAST.debugging.report(`draw: getSpaceLeft`, 1); - //testing screen size and just printing on error - let error = BEAST.checkSize(); - if( error === '' ) { - let spaceLeft = Math.floor( ( CliSize().columns - BEAST.MINWIDTH ) / 2 ); //horizontal alignment - spaceLeft = ' '.repeat( spaceLeft ); + let spaceLeft = Math.floor( ( CliSize().columns - BEAST.MINWIDTH ) / 2 ) + 1; //horizontal alignment - BEAST.RL.write(`${spaceLeft}${Chalk.gray(`│`)}${item}\n`); //print line inside the frame - } + return spaceLeft + } + + +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Private function +// getSpaceTop, Return the space we need from the top to inside the board +// +// @return {integer} The amount of spaces we need to get inside the board from the top, not rounded +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- + const getSpaceTop = ( item ) => { + BEAST.debugging.report(`draw: getSpaceTop`, 1); + + let spacetop = ( CliSize().rows - BEAST.MINHEIGHT ) / 2; //vertically alignment + + return spacetop } @@ -337,7 +348,7 @@ BEAST.draw = (() => { frame: () => { BEAST.debugging.report(`draw: frame`, 1); - customStdout.muted = false; + customStdout.muted = false; //allow output so we can draw Readline.cursorTo( BEAST.RL, 0, 0 ); //go to top of board Readline.clearScreenDown( BEAST.RL ); //clear screen @@ -345,18 +356,16 @@ BEAST.draw = (() => { //testing screen size and just printing on error let error = BEAST.checkSize(); if( error !== '' ) { - Readline.cursorTo( BEAST.RL, 0, 0 ); //go to top of board - Readline.clearScreenDown( BEAST.RL ); //clear screen BEAST.RL.write(`\n\n${error}`); } else { - let spaceLeft = Math.floor( ( CliSize().columns - BEAST.MINWIDTH ) / 2 ); //horizontal alignment + let spaceLeft = getSpaceLeft() - 1; //horizontal alignment spaceLeft = ' '.repeat( spaceLeft ); - let spacetop = Math.ceil( ( CliSize().rows - BEAST.MINHEIGHT ) / 2 ); //vertically alignment - spacetop = `\n`.repeat( spacetop ); + let spaceTop = Math.ceil( getSpaceTop() ); //vertically alignment + spaceTop = `\n`.repeat( spaceTop ); - BEAST.RL.write( spacetop ); + BEAST.RL.write( spaceTop ); BEAST.RL.write( `${spaceLeft}${Chalk.green(` ╔╗ ╔═╗ ╔═╗ ╔═╗ ╔╦╗`)}\n` + `${spaceLeft}${Chalk.cyan (` ╠╩╗ ║╣ ╠═╣ ╚═╗ ║`)}\n` + @@ -367,10 +376,10 @@ BEAST.draw = (() => { BEAST.RL.write(`${spaceLeft}${Chalk.gray(`│${' '.repeat( BEAST.MINWIDTH - 2 )}│`)}\n`.repeat( BEAST.MINHEIGHT - 7 )); BEAST.RL.write(`${spaceLeft}${Chalk.gray(`└${'─'.repeat( BEAST.MINWIDTH - 2 )}┘`)}\n\n`); - BEAST.RL.write( spacetop ); + BEAST.RL.write( spaceTop ); } - customStdout.muted = true; + customStdout.muted = true; //no more user output now! }, @@ -381,31 +390,29 @@ BEAST.draw = (() => { score: () => { BEAST.debugging.report(`draw: score`, 1); - customStdout.muted = false; + customStdout.muted = false; //allow output so we can draw //testing screen size let error = BEAST.checkSize(); if( error === '' ) { - let top = Math.floor( ( CliSize().rows - BEAST.MINHEIGHT ) / 2 ); - Readline.cursorTo( BEAST.RL, 0, (top + 4 + ( BEAST.MINHEIGHT - 6 )) ); //go to bottom of board + let spaceTop = Math.floor( getSpaceTop() ); + let spaceLeft = getSpaceLeft(); - let spaceLeft = Math.floor( ( CliSize().columns - BEAST.MINWIDTH ) / 2 ); //horizontal alignment - spaceLeft = ' '.repeat( spaceLeft ); + Readline.cursorTo( BEAST.RL, spaceLeft, (spaceTop + 4 + ( BEAST.MINHEIGHT - 6 )) ); //go to bottom of board //calculate the space between lives and beast count - let spaceMiddle = ( BEAST.MINWIDTH - 2 ) - ( 3 * BEAST.LIVES ) - 6 - ( Object.keys( BEAST.BEASTS ).length.toString().length ); + let spaceMiddle = ( BEAST.MINWIDTH - 2 ) - ( 3 * BEAST.LIVES ) - 3 - ( Object.keys( BEAST.BEASTS ).length.toString().length ); BEAST.RL.write( - `${spaceLeft}${Chalk.red(` ${BEAST.SYMBOLS.hero}`).repeat( BEAST.LIVES - BEAST.DEATHS )}` + - `${Chalk.gray(` ${BEAST.SYMBOLS.hero}`).repeat( BEAST.DEATHS )}` + `${Chalk.red(` ${BEAST.SYMBOLS.hero}`).repeat( BEAST.LIVES - BEAST.DEATHS )}` + + `${Chalk.gray(` ${BEAST.SYMBOLS.hero}`).repeat( BEAST.DEATHS )}` + + `${' '.repeat( spaceMiddle )} ${ Object.keys( BEAST.BEASTS ).length } x ${BEAST.SYMBOLS.beast}` ); - BEAST.RL.write(`${' '.repeat( spaceMiddle )} ${ Object.keys( BEAST.BEASTS ).length } x ${BEAST.SYMBOLS.beast}`); - Readline.cursorTo( BEAST.RL, 0, (CliSize().rows - 1) ); //go to bottom of board and rest cursor there } - customStdout.muted = true; + customStdout.muted = true; //no more user output now! }, @@ -416,23 +423,23 @@ BEAST.draw = (() => { level: () => { BEAST.debugging.report(`draw: level`, 1); - customStdout.muted = false; + customStdout.muted = false; //allow output so we can draw //testing screen size let error = BEAST.checkSize(); if( error === '' ) { - let top = Math.floor( ( CliSize().rows - BEAST.MINHEIGHT ) / 2 ); - let spaceLeft = Math.floor( ( CliSize().columns - BEAST.MINWIDTH ) / 2 ); //horizontal alignment - let spaceMiddle = ( BEAST.MINWIDTH - 2 ) - 9 - ( Object.keys( BEAST.LEVEL ).length.toString().length ); //calculate the space so we can right align + let spaceTop = Math.floor( getSpaceTop() ); + let spaceLeft = getSpaceLeft(); //horizontal alignment + let spaceMiddle = ( BEAST.MINWIDTH - 2 ) - 10 - ( Object.keys( BEAST.LEVEL ).length.toString().length ); //calculate the space so we can right align - Readline.cursorTo( BEAST.RL, (spaceLeft + spaceMiddle), (top + 2) ); //go to top above the board and right align + Readline.cursorTo( BEAST.RL, (spaceLeft + spaceMiddle), (spaceTop + 2) ); //go to top above the board and right align BEAST.RL.write(` Level: ${BEAST.LEVEL}`); Readline.cursorTo( BEAST.RL, 0, (CliSize().rows - 1) ); //go to bottom of board and rest cursor there } - customStdout.muted = true; + customStdout.muted = true; //no more user output now! }, @@ -445,8 +452,10 @@ BEAST.draw = (() => { customStdout.muted = false; //allow output so we can draw - let top = Math.floor( ( CliSize().rows - BEAST.MINHEIGHT ) / 2 ); - Readline.cursorTo( BEAST.RL, 0, (top + 4) ); //go to top of board + let spaceTop = Math.floor( getSpaceTop() ); + let spaceLeft = getSpaceLeft(); + + Readline.cursorTo( BEAST.RL, 0, (spaceTop + 4) ); //go to top of board for(let boardRow of BEAST.BOARD) { //iterate over each row let line = ''; //translate BEAST.BOARD to ASCII @@ -462,7 +471,8 @@ BEAST.draw = (() => { } } - printLine( line ); //print the compiled line onto the board + Readline.moveCursor(BEAST.RL, spaceLeft, 0); //move cursor into board + BEAST.RL.write(`${line}\n`); //print line inside the frame } Readline.cursorTo( BEAST.RL, 0, (CliSize().rows - 1) ); //go to bottom of board and rest cursor there @@ -481,20 +491,24 @@ BEAST.draw = (() => { message: ( message, color = 'black' ) => { customStdout.muted = false; //allow output so we can draw - let top = Math.floor( ( CliSize().rows - BEAST.MINHEIGHT ) / 2 ); - Readline.cursorTo( BEAST.RL, 0, (top + 4 + Math.floor( ( BEAST.MINHEIGHT - 7 ) / 2 ) - 2) ); //go to middle of board + let spaceTop = Math.floor( getSpaceTop() ); + let spaceLeft = getSpaceLeft(); //space left from frame - let spaceShoulder = Math.floor( ( CliSize().columns - BEAST.MINWIDTH ) / 2 ); //space left from frame - spaceShoulder = ' '.repeat( spaceShoulder ); + Readline.cursorTo( BEAST.RL, spaceLeft, (spaceTop + 4 + Math.floor( ( BEAST.MINHEIGHT - 7 ) / 2 ) - 2) ); //go to middle of board - let spaceLeft = Math.floor( ( (BEAST.MINWIDTH - 2) / 2 ) - ( (message.length + 2) / 2 ) ); - let spaceRight = Math.ceil( ( (BEAST.MINWIDTH - 2) / 2 ) - ( (message.length + 2) / 2 ) ); + let space = ( (BEAST.MINWIDTH - 2) / 2 ) - ( (message.length + 2) / 2 ); //rest space minus the message length + let spaceMiddleLeft = Math.floor( space ); + let spaceMiddleRight = Math.ceil( space ); - BEAST.RL.write(`${spaceShoulder}${Chalk.gray(`│`)}${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); - BEAST.RL.write(`${spaceShoulder}${Chalk.gray(`│`)}${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); - BEAST.RL.write(`${spaceShoulder}${Chalk.gray(`│`)}${' '.repeat( spaceLeft )}${Chalk[ color ].bgWhite.bold(` ${message} `)}${' '.repeat( spaceRight )}\n`); - BEAST.RL.write(`${spaceShoulder}${Chalk.gray(`│`)}${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); - BEAST.RL.write(`${spaceShoulder}${Chalk.gray(`│`)}${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); + BEAST.RL.write(`${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); //clear line + Readline.moveCursor(BEAST.RL, spaceLeft, 0); //move cursor into board + BEAST.RL.write(`${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); //clear line + Readline.moveCursor(BEAST.RL, spaceLeft, 0); //move cursor into board + BEAST.RL.write(`${' '.repeat( spaceMiddleLeft )}${Chalk[ color ].bgWhite.bold(` ${message} `)}${' '.repeat( spaceMiddleRight )}\n`); + Readline.moveCursor(BEAST.RL, spaceLeft, 0); //move cursor into board + BEAST.RL.write(`${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); //clear line + Readline.moveCursor(BEAST.RL, spaceLeft, 0); //move cursor into board + BEAST.RL.write(`${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); //clear line Readline.cursorTo( BEAST.RL, 0, (CliSize().rows - 1) ); //go to bottom of board and rest cursor there diff --git a/package.json b/package.json index 70dc315..384a7dc 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "beast.js", "description": "ANSI Beast for node", - "version": "0.1.2", + "version": "0.1.3", "homepage": "https://github.com/dominikwilkowski/beast.js", "author": { "name": "Dominik Wilkowski", diff --git a/prod/index.js b/prod/index.js index d61e877..ebff866 100644 --- a/prod/index.js +++ b/prod/index.js @@ -309,23 +309,34 @@ BEAST.scaffolding = (() => { //-------------------------------------------------------------------------------------------------------------------------------------------------------------- BEAST.draw = (() => { + //-------------------------------------------------------------------------------------------------------------------------------------------------------------- // Private function -// printLine, Print a row inside the board +// getSpaceLeft, Return the space we need left from the frame // -// @param item {string} The string to be written +// @return {integer} The amount of spaces we need to get inside the board from the left, rounded //-------------------------------------------------------------------------------------------------------------------------------------------------------------- - const printLine = ( item ) => { - BEAST.debugging.report(`draw: printLine`, 1); + const getSpaceLeft = ( item ) => { + BEAST.debugging.report(`draw: getSpaceLeft`, 1); - //testing screen size and just printing on error - let error = BEAST.checkSize(); - if( error === '' ) { - let spaceLeft = Math.floor( ( CliSize().columns - BEAST.MINWIDTH ) / 2 ); //horizontal alignment - spaceLeft = ' '.repeat( spaceLeft ); + let spaceLeft = Math.floor( ( CliSize().columns - BEAST.MINWIDTH ) / 2 ) + 1; //horizontal alignment - BEAST.RL.write(`${spaceLeft}${Chalk.gray(`│`)}${item}\n`); //print line inside the frame - } + return spaceLeft + } + + +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- +// Private function +// getSpaceTop, Return the space we need from the top to inside the board +// +// @return {integer} The amount of spaces we need to get inside the board from the top, not rounded +//-------------------------------------------------------------------------------------------------------------------------------------------------------------- + const getSpaceTop = ( item ) => { + BEAST.debugging.report(`draw: getSpaceTop`, 1); + + let spacetop = ( CliSize().rows - BEAST.MINHEIGHT ) / 2; //vertically alignment + + return spacetop } @@ -337,7 +348,7 @@ BEAST.draw = (() => { frame: () => { BEAST.debugging.report(`draw: frame`, 1); - customStdout.muted = false; + customStdout.muted = false; //allow output so we can draw Readline.cursorTo( BEAST.RL, 0, 0 ); //go to top of board Readline.clearScreenDown( BEAST.RL ); //clear screen @@ -345,18 +356,16 @@ BEAST.draw = (() => { //testing screen size and just printing on error let error = BEAST.checkSize(); if( error !== '' ) { - Readline.cursorTo( BEAST.RL, 0, 0 ); //go to top of board - Readline.clearScreenDown( BEAST.RL ); //clear screen BEAST.RL.write(`\n\n${error}`); } else { - let spaceLeft = Math.floor( ( CliSize().columns - BEAST.MINWIDTH ) / 2 ); //horizontal alignment + let spaceLeft = getSpaceLeft() - 1; //horizontal alignment spaceLeft = ' '.repeat( spaceLeft ); - let spacetop = Math.ceil( ( CliSize().rows - BEAST.MINHEIGHT ) / 2 ); //vertically alignment - spacetop = `\n`.repeat( spacetop ); + let spaceTop = Math.ceil( getSpaceTop() ); //vertically alignment + spaceTop = `\n`.repeat( spaceTop ); - BEAST.RL.write( spacetop ); + BEAST.RL.write( spaceTop ); BEAST.RL.write( `${spaceLeft}${Chalk.green(` ╔╗ ╔═╗ ╔═╗ ╔═╗ ╔╦╗`)}\n` + `${spaceLeft}${Chalk.cyan (` ╠╩╗ ║╣ ╠═╣ ╚═╗ ║`)}\n` + @@ -367,10 +376,10 @@ BEAST.draw = (() => { BEAST.RL.write(`${spaceLeft}${Chalk.gray(`│${' '.repeat( BEAST.MINWIDTH - 2 )}│`)}\n`.repeat( BEAST.MINHEIGHT - 7 )); BEAST.RL.write(`${spaceLeft}${Chalk.gray(`└${'─'.repeat( BEAST.MINWIDTH - 2 )}┘`)}\n\n`); - BEAST.RL.write( spacetop ); + BEAST.RL.write( spaceTop ); } - customStdout.muted = true; + customStdout.muted = true; //no more user output now! }, @@ -381,31 +390,29 @@ BEAST.draw = (() => { score: () => { BEAST.debugging.report(`draw: score`, 1); - customStdout.muted = false; + customStdout.muted = false; //allow output so we can draw //testing screen size let error = BEAST.checkSize(); if( error === '' ) { - let top = Math.floor( ( CliSize().rows - BEAST.MINHEIGHT ) / 2 ); - Readline.cursorTo( BEAST.RL, 0, (top + 4 + ( BEAST.MINHEIGHT - 6 )) ); //go to bottom of board + let spaceTop = Math.floor( getSpaceTop() ); + let spaceLeft = getSpaceLeft(); - let spaceLeft = Math.floor( ( CliSize().columns - BEAST.MINWIDTH ) / 2 ); //horizontal alignment - spaceLeft = ' '.repeat( spaceLeft ); + Readline.cursorTo( BEAST.RL, spaceLeft, (spaceTop + 4 + ( BEAST.MINHEIGHT - 6 )) ); //go to bottom of board //calculate the space between lives and beast count - let spaceMiddle = ( BEAST.MINWIDTH - 2 ) - ( 3 * BEAST.LIVES ) - 6 - ( Object.keys( BEAST.BEASTS ).length.toString().length ); + let spaceMiddle = ( BEAST.MINWIDTH - 2 ) - ( 3 * BEAST.LIVES ) - 3 - ( Object.keys( BEAST.BEASTS ).length.toString().length ); BEAST.RL.write( - `${spaceLeft}${Chalk.red(` ${BEAST.SYMBOLS.hero}`).repeat( BEAST.LIVES - BEAST.DEATHS )}` + - `${Chalk.gray(` ${BEAST.SYMBOLS.hero}`).repeat( BEAST.DEATHS )}` + `${Chalk.red(` ${BEAST.SYMBOLS.hero}`).repeat( BEAST.LIVES - BEAST.DEATHS )}` + + `${Chalk.gray(` ${BEAST.SYMBOLS.hero}`).repeat( BEAST.DEATHS )}` + + `${' '.repeat( spaceMiddle )} ${ Object.keys( BEAST.BEASTS ).length } x ${BEAST.SYMBOLS.beast}` ); - BEAST.RL.write(`${' '.repeat( spaceMiddle )} ${ Object.keys( BEAST.BEASTS ).length } x ${BEAST.SYMBOLS.beast}`); - Readline.cursorTo( BEAST.RL, 0, (CliSize().rows - 1) ); //go to bottom of board and rest cursor there } - customStdout.muted = true; + customStdout.muted = true; //no more user output now! }, @@ -416,23 +423,23 @@ BEAST.draw = (() => { level: () => { BEAST.debugging.report(`draw: level`, 1); - customStdout.muted = false; + customStdout.muted = false; //allow output so we can draw //testing screen size let error = BEAST.checkSize(); if( error === '' ) { - let top = Math.floor( ( CliSize().rows - BEAST.MINHEIGHT ) / 2 ); - let spaceLeft = Math.floor( ( CliSize().columns - BEAST.MINWIDTH ) / 2 ); //horizontal alignment - let spaceMiddle = ( BEAST.MINWIDTH - 2 ) - 9 - ( Object.keys( BEAST.LEVEL ).length.toString().length ); //calculate the space so we can right align + let spaceTop = Math.floor( getSpaceTop() ); + let spaceLeft = getSpaceLeft(); //horizontal alignment + let spaceMiddle = ( BEAST.MINWIDTH - 2 ) - 10 - ( Object.keys( BEAST.LEVEL ).length.toString().length ); //calculate the space so we can right align - Readline.cursorTo( BEAST.RL, (spaceLeft + spaceMiddle), (top + 2) ); //go to top above the board and right align + Readline.cursorTo( BEAST.RL, (spaceLeft + spaceMiddle), (spaceTop + 2) ); //go to top above the board and right align BEAST.RL.write(` Level: ${BEAST.LEVEL}`); Readline.cursorTo( BEAST.RL, 0, (CliSize().rows - 1) ); //go to bottom of board and rest cursor there } - customStdout.muted = true; + customStdout.muted = true; //no more user output now! }, @@ -445,8 +452,10 @@ BEAST.draw = (() => { customStdout.muted = false; //allow output so we can draw - let top = Math.floor( ( CliSize().rows - BEAST.MINHEIGHT ) / 2 ); - Readline.cursorTo( BEAST.RL, 0, (top + 4) ); //go to top of board + let spaceTop = Math.floor( getSpaceTop() ); + let spaceLeft = getSpaceLeft(); + + Readline.cursorTo( BEAST.RL, 0, (spaceTop + 4) ); //go to top of board for(let boardRow of BEAST.BOARD) { //iterate over each row let line = ''; //translate BEAST.BOARD to ASCII @@ -462,7 +471,8 @@ BEAST.draw = (() => { } } - printLine( line ); //print the compiled line onto the board + Readline.moveCursor(BEAST.RL, spaceLeft, 0); //move cursor into board + BEAST.RL.write(`${line}\n`); //print line inside the frame } Readline.cursorTo( BEAST.RL, 0, (CliSize().rows - 1) ); //go to bottom of board and rest cursor there @@ -481,20 +491,24 @@ BEAST.draw = (() => { message: ( message, color = 'black' ) => { customStdout.muted = false; //allow output so we can draw - let top = Math.floor( ( CliSize().rows - BEAST.MINHEIGHT ) / 2 ); - Readline.cursorTo( BEAST.RL, 0, (top + 4 + Math.floor( ( BEAST.MINHEIGHT - 7 ) / 2 ) - 2) ); //go to middle of board + let spaceTop = Math.floor( getSpaceTop() ); + let spaceLeft = getSpaceLeft(); //space left from frame - let spaceShoulder = Math.floor( ( CliSize().columns - BEAST.MINWIDTH ) / 2 ); //space left from frame - spaceShoulder = ' '.repeat( spaceShoulder ); + Readline.cursorTo( BEAST.RL, spaceLeft, (spaceTop + 4 + Math.floor( ( BEAST.MINHEIGHT - 7 ) / 2 ) - 2) ); //go to middle of board - let spaceLeft = Math.floor( ( (BEAST.MINWIDTH - 2) / 2 ) - ( (message.length + 2) / 2 ) ); - let spaceRight = Math.ceil( ( (BEAST.MINWIDTH - 2) / 2 ) - ( (message.length + 2) / 2 ) ); + let space = ( (BEAST.MINWIDTH - 2) / 2 ) - ( (message.length + 2) / 2 ); //rest space minus the message length + let spaceMiddleLeft = Math.floor( space ); + let spaceMiddleRight = Math.ceil( space ); - BEAST.RL.write(`${spaceShoulder}${Chalk.gray(`│`)}${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); - BEAST.RL.write(`${spaceShoulder}${Chalk.gray(`│`)}${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); - BEAST.RL.write(`${spaceShoulder}${Chalk.gray(`│`)}${' '.repeat( spaceLeft )}${Chalk[ color ].bgWhite.bold(` ${message} `)}${' '.repeat( spaceRight )}\n`); - BEAST.RL.write(`${spaceShoulder}${Chalk.gray(`│`)}${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); - BEAST.RL.write(`${spaceShoulder}${Chalk.gray(`│`)}${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); + BEAST.RL.write(`${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); //clear line + Readline.moveCursor(BEAST.RL, spaceLeft, 0); //move cursor into board + BEAST.RL.write(`${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); //clear line + Readline.moveCursor(BEAST.RL, spaceLeft, 0); //move cursor into board + BEAST.RL.write(`${' '.repeat( spaceMiddleLeft )}${Chalk[ color ].bgWhite.bold(` ${message} `)}${' '.repeat( spaceMiddleRight )}\n`); + Readline.moveCursor(BEAST.RL, spaceLeft, 0); //move cursor into board + BEAST.RL.write(`${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); //clear line + Readline.moveCursor(BEAST.RL, spaceLeft, 0); //move cursor into board + BEAST.RL.write(`${' '.repeat( BEAST.MINWIDTH - 2 )}\n`); //clear line Readline.cursorTo( BEAST.RL, 0, (CliSize().rows - 1) ); //go to bottom of board and rest cursor there