/*
 * Patrick Gillen
 * CST 112
 * March 14th 2012
 * MIDTERM TAKE HOME
 */


float sunX=300, sunY=50, sunW=80, sunH=80;
float surface=100;
float waveX=0, waveY=105, waveW=40, waveH=40;
float waveNumber=600;

int fishMany= 6;                // How many fish.
int fishRed=  200;             // Red color level for fish.
float fishSpacing=50;
float fishX=0, fishY=surface+100;
float fishWidth=60, fishHeight=45;

float sharkX=100, sharkY=360, sharkW=100, sharkH=60;
float octoX=500, octoY=200, octoW=80, octoH=120;
int score=0;

void setup()    //// setup
{
  size(800, 600);
  smooth();
}

void draw()    //// Next frame.
{
  scene();
  fish();       //// school of fish swim to the right.
  shark();      //// Shark chases fish.
  octopus();    //// Octopus -- 8 arms point toward shark.
}

void scene()    //// waves on surface, rocks at bottom.
{

  background( 127, 127, 255 );        // (Sky)
  //// moves sun
  float sunDX = 1;
  sunX += sunDX;
  /// moves waves 
  float waveDX = 2;
  waveX += waveDX;

  //// Sun.
  stroke(1);
  strokeWeight(1);
  fill( random(200, 255), random(200, 255), random(255) );	
  ellipse( sunX, sunY, sunW, sunH );
  //resets sun if it goes off screen
  if (sunX>width)
  {
    sunX = 350;
  }
  //// Water
  fill ( 50, 127, 50 );
  rectMode(CORNERS);
  noStroke();
  rect( 0, 100, 800, 800 );
  //displays score next to suns starting point
  fill(0);
  text(score, 250, 50);
  //tests if waves are off screen, if so set initial x back to zero and randomize number of wave sfor next run through of for next loop
  if( waveX>width)
  {
    waveX = 0;
    waveNumber = random(80,500);
  }	
  //// Waves on the surface.
  for (float x = 0; x <= waveNumber; x+= 40)
  {
    fill( 50, 127, 50 );
    ellipseMode(CENTER);
    ellipse(waveX + (x *random(1,1.02)), waveY, waveW, waveH + random(7));
  }
}

void fish()    //// school of fish.
{
  if (fishX>width || fishY>height || fishMany<1)   // Check if done.  Reset fish on left.
  {
    fishMany=  2 + (int) random(5);
    fishX=  fishMany*fishSpacing;        // Starting point for the leader.
    fishY=  surface+random(height/2);
    fishWidth=  20+random(20);          // Size of leader.
    fishHeight=  0.75 * fishWidth;
    fishRed=  150 + (int) random(100);        // Fish redness.
  }
  //// Move school of fish:  3 left, 2 down.	
  float fishDX = 3;
  float fishDY = 2;
  fishX += fishDX;
  fishY += fishDY;

  ///// octopus catches & eats fish
  if (dist(octoX, octoY, fishX, fishY) < 25) 
  {
    fishMany = fishMany - 1;
    score += 50;
  }

  //// Draw each fish - size gets smaller
  for (int x = 0; x <= fishMany; x+= 1)
  {
    fill(fishRed, 0, 0);
    ellipse(fishX +(x * fishSpacing), fishY + (x * fishSpacing), fishWidth - x, fishHeight - x);
  }
}

void octopus()    //// Octopus with 8 legs pointing toward shark.
{
  // draws octopus that follows mouse
  noStroke();
  octoX=mouseX;
  octoY=mouseY;
  fill( 160, 32, 240 );
  rectMode(CENTER);
  rect( octoX, octoY, octoW, octoH ); 
  // legs follow shark 
  float follow;
  follow = (octoX - sharkX) / 100;
  //loop to draw each leg
  for (int x = -40; x <= 40; x += 10)
  {
    strokeWeight(3);
    stroke( 160, 32, 240 );
    line(octoX + x, octoY + 60, octoX + x * follow, octoY + 80);
  }
}

void shark()        ////  Shark chases octopus.
{
  //// Move shark.	
  float xChase = octoX - sharkX;
  float yChase = octoY - sharkY;
  sharkX += xChase / 20;
  sharkY += yChase / 20;
  //// Draw shark.	
  fill( 127, 127, 127);
  rectMode(CENTER);
  rect(sharkX, sharkY, sharkW, sharkH);
  //// Shark catches Octopus.
  if (dist(sharkX, sharkY, octoX, octoY) < 50) 
  {
    background(0);
    sharkX = 750;
    sharkY = 550;
    score -= 100;
    return;
  }
}

void mousePressed() // moves shark into bottom right hand corner when mouse press
{

  sharkX = 750;
  sharkY = 550;
  score -= 50;
}
