//// Samir Khater
//// 3/9/2012
//// CST 112
//// Professor Martin
//// Test 1XXXXX THIS IS #2


int score = 0;                      //// Set up the score.
int greenwater;                    //// Green water.
int fishMany = 6;                 //// How many fish.
int fishRed = 200;               //// Red color level for fish.
int sunDX = 3;                  //// Speed of the X coordinate for the sun.
int sunDY = 1;                  //// Speed of the Y coordinate for the sun.
float surface = 100;            //// Set the surface location.
float sunX = 300;              //// Set the X coordinate for the sun.
float sunY = 50;              //// Set the Y coordinate for the sun.
float fishSpacing = 50;            //// Set the spacing between the fish.
float fishX = 0;              //// Set the X coordinate for the leader fish.
float fishY = surface+100;    //// Set the Y coordinate for the leader fish.
float fishWidth = 60;        //// Set the width for the leader fish.
float fishHeight = 45;        //// Set the height for the leader fish.
float sharkX = 100;        //// Set the X coordinate for the shark.
float sharkY = 360;        //// Set the Y coordinate for the shark.
float sharkWidth = 120;    //// Set the width for the shark.
float sharkHeight = 70;    //// Set the height for the shark.
float octoX = 500;        //// Set the X coordinate for the octopus.
float octoY = 200;        //// Set the Y coordinate for the octopus.
float octoW = 80;        //// Set the width for the octopus.
float octoH = 120;        //// Set the height for the octopus.
float sr, sb, sg;        //// Set up colors for randomization in the sun.


void setup()      //// Initialize the background, set resolution for screen.
{
size(900,700);    //// Set resolution (window size).
frameRate=30;    //// Set the speed of the animation to 30 frames per second (fps).
}

void draw()      //// Set up functions for the sketch.
{
  environment();    //// Function for the environment.
  fish();        //// Function for the fish.
  octopus();      //// Function for the octopus.
  shark();        //// Function for the shark.
  move();
}

void environment()    //// Set up the environment of the sketch (Blue Sky, Green Water, Yellow Sun, etc.).
{
  fill(0);
  background(80,200,80);          //// Light-blue sky.
  smooth();                    //// Smooth out the edges of the rectangle, which is the green water.
  fill(100,190,255);              //// Green water.
  rectMode(CORNERS);      //// Set the mode of the rectangle to the center.
  rect(0,greenwater, width, height/4);      //// Draw the green water.
  fill(0);                              //// Use the color black for my name.
  text( "Samir Khater", 10, height-10 );  //// Print "Samir Khater" (my name) in the bottom left corner of the sketch.
  text(score, 800, 50);
  smooth();                  //// Smooth out the edges of the ellipse, which is the yellow sun.
  fill(sr, sg, 0);              //// Use the color Yellow for the sun.
  ellipse(sunX, sunY, 80, 80);      //// Draw an ellipse along the top of the screen, which is my Sun.
    }

void fish()        //// Set up the fish
{
 
  if ((fishX > width) || (fishY > height) || (fishMany < 1)){          //// Draw the school of fish
    fishMany = (2 + (int)random(6));
    fishX = (fishMany * fishSpacing);
    fishY = (surface + random(height/2));
    fishWidth = (20 + random(30));
    fishHeight = (0.75*fishWidth);
    fishRed = (150 + (int)random(100));
  }
  float fx = fishX;
  float fy = fishY;
  float fw = fishWidth;
  float fh = fishHeight;
  for (int i = 0; i < fishMany; i++){        //// Set the size and color of the school of fish
    
    noStroke();
    fill(fishRed, 100, 150);
    ellipseMode(3);
    ellipse(fx, fy, fw, fh);
    
    fx += 50;
    fy += 50;
    fw += 0;
    fh += 0;
  }
  
  noStroke();
  fill(fishRed, 100, 150);
  ellipseMode(3);
  ellipse(fishX, fishY, fishWidth, fishHeight);
}

void move()        //// Set up the movement of the fishes and the sun.
{
fishX += 3;        //// Make the fish move.
fishY += 2;
  { sunX += sunDX;
  if (sunX > width-38 ) sunDX=  -sunDX;    //// Make the sun bounce of the walls.
 if (sunX < 0+30/2+22 ) sunDX=  -sunDX;
  }
  if (sunX>width/2){                    //// Make the sun randomize its color as it is bouncing off the walls.
    sr = random(255);
    sg = random(255);
    sb = random(255);
  }
  else{
    if (sunX<width/2){
      sr = 255;
      sg = 255;
      sb = 0;
    }
  }
}

void octopus()      //// Set up the octopus
{

  float ox = octoX - 39;
  float oy = octoY +61;
  float sx = sharkX;
  float sy = sharkY;
  for (int i = 0; i < 8; i++){
    stroke(100, 0, 150);
    strokeWeight(3);
    line(ox,oy,ox,oy+15);
    ox += 11;
  }
  noStroke();
  octoX=mouseX;        //// Make the X coordinate of the purple rectangle (octopus) follow the mouse.
  octoY=mouseY;        //// Make the Y coordinate of the purple rectangle (octopus) follow the mouse.
  rectMode(CENTER);    //// Set up the rectangle mode to center, making the mouse appear in the center of the rectangle, and the rectangle will follow the mouse.
  fill(255,0,255);      //// Make the color of the rectangle (Octopus) purple.
  rect(octoX,octoY,80,120);    //// Draw the rectangle (Octopus).
  int spacing = 10;      //// Initialize the spacing value for the loop that will be the octopus' legs.
  int len = 20;        //// Initialize the length of each line.
  for(int octoX = 50; octoX <= 150; octoX += spacing)  {      //// Create a LOOP that gives the octopus its legs, and follows the the octopus.
    line(octoX=450,octoY-10,octoX,octoY+len);            //// Lines for the loop.
  }
  if ((fishMany > 0) && (dist(fishX, fishY, octoX, octoY) < 50)){      //// When the octopus touches the fish, eat the fish, Make background flash black and score +10.
    background(0);
    fishMany -= 1;
    score += 10;
    fishX += 50;
  }
}

void shark()        //// Set up the shark.
{
  fill(150);         //// Give the rectangle (Shark) a grey color.
  rectMode(3);
  rect(sharkX, sharkY, sharkWidth, sharkHeight);        //// Create a rectangle, which is the Shark.
  float sx = octoX - sharkX;
  float sy = octoY - sharkY;
  sharkX += sx / 40;
  sharkY += sy / 20;
  
  if (dist(sharkX, sharkY, octoX, octoY) < 50){                //// When the shark eats the octopus, make the score -100 and the background flash black.
  background(0);
  sharkX=width;
  sharkY=height;
  score -= 100;
}
}
 
