//// bam:8a03:  example of random, etc.

String title=  "Project 2";
String author=  "Deblin Ortega";
String points= "Score";

//GLOBAL DECLARATIONS//
boolean day=true;

float horizon;
float skyR=100, skyG=200, skyB=255;
float sunR=255, sunG=255, sunB=0;
float sunX=50, sunY=50, sunDX=4, sunDY=0;
float surf, bott;
float boatX=50, boatY=50, boatDX=2, boatDY=0;
float boatW=100, boatH=40;
float fishX=100, fishY=300, fishDX=3, fishDY=0;
float fishW=80, fishH=30;
int score=0;

//SETUP//
void setup() {
  size(800, 600);
  horizon=  height/4;
  rectMode(CENTER);
  surf= height/3;
  bott= height-12;
  boatY=surf;
}

void resetFish() {
  fishY=  random(surf, bott);
  //--  println(fishY);
  if (fishDX>0)  
  {
    fishDX=  -random(5, 10);
    fishX=  width-50;
  }
  else {
    fishDX=  random(5, 10);
    fishX=50;
  }
}

void draw() {
  scene();
  sun();
  salmon();
  boat();
  messages();
}
//Scene: Ocean, Sky
void scene() {
  background( skyR, skyG, skyB );
  fill( 0, 100, 200 );  // grass
  rectMode(CORNER);
  rect( 0, surf, width, bott );
  rectMode(CENTER);
}  

//Sun move and change sky: Day to nite and viceversa
void sun() {
  // Sun.
  sunX=  sunX+sunDX;
  if (sunX>width ) {
    day=  ! day;
    sunX=50;
    if (day) { 
      skyR=100; 
      skyG=200; 
      skyB=255;
    }   // Day sky
    else { 
      skyR=20; 
      skyG=40; 
      skyB=80;
    }   // Night sky
    if (day) { 
      sunR=255; 
      sunG=255; 
      sunB=0;
    }   // Sun
    else { 
      sunR=20; 
      sunG=40; 
      sunB=80;
    }   // Moon
  } 
  fill( sunR, sunG, sunB );
  ellipse( sunX, sunY, 30, 30 );
}

//Salmon//
void salmon() {
  noStroke();
  fill( 200, 0, 100 );
  ellipse( fishX, fishY, fishW, fishH );

/*
  rect( fishX-15, fishY, fishW-20, fishH-5);
  fill(255, 255, 255);
  ellipse( fishX+20, fishY-2, 10, 10);
*/

  float head=fishX+20, tail=fishX-15;
  if (fishDX<0)    // Moving right
  {
    //// Swap two values;
    float tmp;
    tmp = head;
    head=tail;
    tail=tmp;
  }
  
  rect( tail, fishY, fishW-20, fishH-5);
  fill(255, 255, 255);
  ellipse( head, fishY-2, 10, 10);

  // Move
  if (fishX<20) {    resetFish();  }
  else if (fishX>width-20)   {    resetFish();  }
  else  {    fishX += fishDX;  }
}

//Boat//
void boat() {
  fill(255, 0, 0);
  rectMode(CORNERS);
  rect(boatX, boatY, boatX+boatW, boatY-boatH);
  // Prow of boat
  float front=  boatX+boatW, deck=surf-boatH;
  triangle( front, surf, front, deck, front+25, deck );
  fill(10, 10, 10);
  triangle( front, surf-50, front-25, deck, front-25, deck );
  // Move
  boatX += boatDX;
  if (boatX>width-20 || boatX<20) {
    boatDX *= -1;
  }
}
//Messages: Author, Title, Scoreboard
void messages() {
  textSize(24);
  text( title, width/3, 20 );
  textSize(14);
  text( author, width-100, height-20 );
  fill(0);
  textSize(24);
  text( points, width*3/4, 40 );
  if (score > 0) {
    text( score, width * 3/4, 65 );
  }
}

//Keys//
void keyPressed() {
  if (key == 'q') exit(); //Exit Program//
  if (key == 'f') resetFish(); //Reset Fish to random location//
  if (key == 'c') { //Press C to catch fish!//
    if ( dist( boatX, boatY, fishX, fishY ) < 50) { 
      score=score+5; //Catch Fish by clicking boat//
    }
  }
}

void mousePressed() {
  if ( dist( mouseX, mouseY, fishX, boatY ) < 50) { 
    score=score+5; //Catch Fish by clicking boat//
  }
  if ( dist( mouseX, mouseY, fishX, fishY ) < 50) { 
    resetFish(); //reset fish on random location//
  }
}
