
// Bounce.java
// Andrew Davison, ad@coe.psu.ac.th, March 2023

/* A character bounces around the window, playing a tone
   when it hits a 'wall'.

   Uses Lanterna 3
   https://github.com/mabe02/lanterna

   Usage (Windows):
     > compile.bat Bounce.java
     > run.bat Bounce

   Usage (Linux):
     > ./compile.sh Bounce.java
     > ./run.sh Bounce

*/


import java.io.*;
import java.util.*;

import com.googlecode.lanterna.*;
import com.googlecode.lanterna.input.*;
import com.googlecode.lanterna.screen.*;
import com.googlecode.lanterna.terminal.*;


public class Bounce
{

  public static void main(String[] args)
  {
    Screen scr = Utils.createScreen();
    scr.setCursorPosition(null);
    
    TerminalSize termSz = scr.getTerminalSize();
    int numCols = termSz.getColumns();
    int numRows = termSz.getRows();

    int x = numCols/2;   // start at the center of the window
    int y = numRows/2;

    int xStep = 1;   // to the right and
    int yStep = 1;   // going down
    boolean hasHit = false;

    while (true) {
      KeyStroke ks = Utils.pollInput(scr);
      if (Utils.hasQuit(ks))
        break;

      // bounce by reversing the associated step value
      if ((x == 0) || (x == numCols-1)) {   // left or right
        xStep = -xStep;
        hasHit = true;
      }
      if ((y == 0) || (y == numRows-1)) {   // top or bottom
        yStep = -yStep;
        hasHit = true;
      }

      if (hasHit) {
        SoundUtils.tone(1000, 100);
        hasHit = false;
      }

      // move the ball
      x += xStep;
      y += yStep;
  
      // draw the window
      // scr.clear();
      Utils.fill(scr, TextColor.ANSI.BLACK);
      Utils.putString(scr, x, y, 
                     // ""+Symbols.WHITE_CIRCLE, 
                     ""+Symbols.SOLID_SQUARE, 
                     TextColor.ANSI.WHITE_BRIGHT);
      Utils.pause(100);
    }
    
    Utils.close(scr);
  }  

}  // end of Bounce class
