// Project 2
// Nick Mercogliano 

//Global Variables

String title  = " Project 2";
String author = "Nick Mercogliano";

boolean day = true;

float left, right, top, bottom;
float sunX, sunY, sunDX;
float horizon;
float boatX, boatY, boatW, boatH, boatDX;
float fishX, fishY, fishW, fishH, fishDX;

int score = 0;

/// Setup Instructions
void setup() {
  size(600, 600);
  ellipseMode(CENTER);
  frameRate(30);
  noStroke();
  // Declaring Size Variables
  left=60; right=width-60; top=40; bottom=height-40;
  sunX = 25; sunY = 35;
  horizon = 200;
  boatW=150; boatH=60;
  fishW = 50; fishH = 30;
  resetAll();
  score = 0;
}
/// Reset Commands
void resetAll() {
  resetSun();
  resetBoatLeft();
  resetFishLeft();
  score = 0;
}
void resetSun() {
  sunX = 0;
  sunDX = 1;
}
void resetBoatLeft() {
  boatX=left+75;
  boatY=horizon-30;
  boatDX= random(2, 5);
}
void resetBoatRight() {
  boatX=right-75;
  boatY=horizon-30;
}
void resetFishLeft() {
  fishX=left;
  fishY= random(horizon+40, bottom);
  fishDX= random(2, 15);
}
void resetFishRight() {
  fishX=right;
  fishY= random(horizon+40, bottom);
  fishDX= random(2, 15);
}
/// Draw Scene
void draw() {
  scene();
  showSun();
  showBoat();
  showFish();
  action();
  messages();
}
/// Show Scenes, Background, Water, Mud, Sun, Boat, Fish
void scene() {
  if (day) { 
    background( 36, 160, 240 ); // Light Blue
  } 
    else { 
      background( 20, 47, 121 ); // Dark blue
  }
  fill( 39, 167, 164 );
  rect( 0, horizon, width, height);  // Horizon
  fill( 147, 92, 10 );
  rect( 0, height-8, width, height); // Mud or sand
}
void showSun() {
  fill( 250, 225, 35 );
  ellipse( sunX, sunY, 35, 35);
}
void showBoat() {
  rectMode(CENTER);
  fill( 200, 50, 50 );
  rect( boatX, boatY, boatW, boatH ); 
  if (boatDX>0) {
    float front = boatX+boatW/2;
    triangle( front-1, boatY-30, front-1, boatY+30, front+50, boatY-30 );
  } 
    else {
      float front = boatX-boatW/2;
      triangle( front+1, boatY-30, front+1, boatY+30, front-50, boatY-30 );
  }
  rectMode(CORNER);
}
void showFish() {
  fill( 255, 255, 0 );
  ellipse( fishX, fishY, fishW, fishH );
  if (fishDX>0) {
    float tail = fishX-35;
    triangle( tail+20, fishY, tail, fishY-20, tail, fishY+20 );
    fill(0);
    ellipse( fishX+18, fishY-5, 5, 5 );
  } 
    else {
     float tail = fishX+35;
     triangle( tail-20, fishY, tail, fishY-20, tail, fishY+20 );
     fill(0);
     ellipse( fishX-18, fishY-5, 5, 5 );
  }
}
/// Action
void action() {
  //Sun
  sunX = sunX+sunDX;               // Moving the sun
  if (sunX > width*2) sunX=0;      // Makes the sun come back
  if (sunX < width) day=true;
  if (sunX > width) day=false;
  //Boat
  boatX = boatX+boatDX;    // Moving the boat
  if (boatX > right-65) { 
    resetBoatRight();
    boatDX *= -1;  //Flips the Boat
  } 
  if (boatX < left+65) {  
    boatDX *= -1; //Flips the Boat
    resetBoatLeft();
  }
  //Fish
  fishX = fishX+fishDX;    // Moving the fish
  if (fishX > right+25) { 
    resetFishRight();
    fishDX *= -1;  //Flips the Fish
  } 
  if (fishX < left-10) {  
    fishDX *= -1; //Flips the Fish
    resetFishLeft();
  }
}
/// Messages: Title,Author,Score
void messages() {
  fill(255);
  textSize(28);
  text( title, width-350, top );
  textSize(20);
  text( author, right-125, height-20 );
  text( "Score", right - 20, top - 20 );
  text( score, right + 5, top + 5 );
}
/// Events
void keyPressed() {
  if (key == 'q') exit();
  if (key == 'r') resetAll();
  if (key == 'f') resetFishLeft();
  if (key == 'b') resetBoatLeft();
  if (key == 's') resetSun();
  if (key == 'c')  {
    if ( dist( boatX,boatY, fishX,fishY )  < 120 ) {
         if (day)  {
           score =  score + 5;
           resetFishLeft();
         }
         if (!day) {  
           score = score + 10;
           resetFishLeft();
         }
        else {
          score = score - 1;
        }
    }
  }
  if (key == '=' || key == '+' ) {  // Speeds up the action
    sunDX *=1.5;
    fishDX *=1.5;
    boatDX *=1.5;
  }
  if (key == '-' || key == '_' ) {  // Slows down the action
    sunDX *=0.75;
    fishDX *=0.75;
    boatDX *=0.75;
  }
}
void mousePressed() {
  if ( dist( mouseX, mouseY, boatX, boatY ) < 120 ) {
     if ( dist( boatX,boatY, fishX,fishY )  < 120 ) {
         if (day)  {
           score =  score + 5;
           resetFishLeft();
         }
         if (!day) {  
           score = score + 10;
           resetFishLeft();
         }
       }
        else {
          score = score - 1;
        }
  }
  if ( dist( mouseX, mouseY, fishX, fishY ) < 40 ) { 
    resetFishLeft();
  }
}
