////Project #2 Ver 1.1////
////M.Andrychowski////

////GLOBAL DECLARATIONS////
boolean day=true;
float sealevel,mud;
float sunX=20,sunY=50,sunDX=2,sunDY=0;
float boatX=50,boatY=50,boatDX=2,boatDY=0;
float boatW=80,boatH=30;
float fishX=100,fishY=100,fishDX=3,fishDY=0;
float fishW=80,fishH=30;
int score=0;
String title= "Project #2 Catch the Fish";
String author= "M.Andrychowski";

void setup() {
  size( 800,600 );
  sealevel= height/3;
  mud= height-8;
  boatY=sealevel;
  textAlign(CENTER);
  //noStroke();
}
void resetFish() {
  fishY=  random(sealevel,mud);
  println(fishY);
  if (fishDX>0)  fishDX=  -random(2,7);
  else fishDX=  random(2,7);
}

void draw() {
  scene();
  boat();
  fish();
  message();
  keypressed();
  
}

void scene() {
  if (day) { background(150,200,255); }
  else { background(50,100,150); }
  fill(255,255,0);
  ellipse( sunX,sunY, 30,30 );
  sunX += sunDX;
  if (sunX > width*2) sunX=0;
  if (sunX > width) day=false;
  fill( 100,200,150 );
  rectMode(CORNERS);
  rect( 0,sealevel, width,mud );
  fill( 150,85,0 );
  rect( 0,mud, width,height ); 
}

void boat() {
  fill(255,0,0);
  rectMode(CORNERS);
  rect(boatX,boatY, boatX+boatW,boatY-boatH);
  float front=  boatX+boatW, deck=sealevel-boatH, back=boatX;
  triangle( front,sealevel, front,deck , front+40,deck );
  triangle( back,sealevel, back,deck, back-40,deck );
  boatX += boatDX;
  if (boatX>width-20 || boatX<20) {
    boatDX *= -1;
  }
}

void fish() {
  fill( 200,0,0 );
  ellipse( fishX,fishY, fishW,fishH );
  fishX += fishDX;
  if (fishX>width-20 || fishX<20) {
    resetFish();
  }
}

void catchfish() {
  if ( (dist( boatX,boatY, fishX,fishY )  <  10 ) && (day)) {
     score=  score + 5;
     resetFish();
   }
}

void message() {
  fill( 255 );
  textSize( 14 );
  text( title, width/2, 20 );
  text( author, width-75, height-20 );
  text( "SCORE", width * 9/10, 25 );
  textSize( 24 );
  text( score, width * 9/10, 50 );
}

void keypressed() {
  if (key == 'q') exit();
  if (key == 'f') resetFish();
  if (key == 'c') catchfish();
}