Skip to content

Commit

Permalink
simplified draw
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Wilkowski <[email protected]>
  • Loading branch information
dominikwilkowski committed Oct 17, 2016
1 parent 0c224e9 commit 9f5a254
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 153 deletions.
116 changes: 65 additions & 51 deletions dev/030-draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand All @@ -45,26 +56,24 @@ 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

//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` +
Expand All @@ -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!
},


Expand All @@ -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!
},


Expand All @@ -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!
},


Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
Loading

0 comments on commit 9f5a254

Please sign in to comment.