//BAM:  OK
//Project 3 version 1
//Thomas Montanaro

//global variables
float x=400,y=300,w=50,h=80;
float octDX = 2;
float octDY = 1;
float r=200, g=0, b=200;
float sealevel = 100;
boolean debug=false;


void setup()
{   size (800,600); //init.
}
void draw(){ //Next frame
  scene();
  button();
  action();

  
}
void scene(){//sky button grass sea
  background(200,100,100);
  noStroke();
  fill(50,100,250);
  rectMode(CORNERS);
  rect(0,sealevel,width,height);
  rectMode(CORNER);
  grass();
  noStroke();
  waves();
  fill(0);
  text ( "Project 3", 400, 20);
  text ("Thomas Montanaro" , 20 , height-20);
  sun();
}

void action(){
  //octopus
  fill(r,g,b);
  rectMode(CORNER);
  rect(x,y,w,h);
  ellipseMode (CENTER);
  ellipse ( x+25,y-10,55,55);
  float legX = x;
    for(int l=0;l<8;l++){
      stroke(r,g,b);
      strokeWeight(4);
      line(legX+2,y+75,(legX+2)-(octDX*2),y+100);
      legX += w/8;
    }
  //eyeball
  fill(255);
  ellipse(x+25,y-10,30,30);
  //pupil
  if(octDX>0){
   fill(0);
  ellipse(x+35,y-10,10,10);
  }
  if(octDX <0){
   fill(0);
  ellipse(x+15,y-10,10,10);
  }
  if (debug) {
    fill(0);
    text( "("+  x  +","+  y  +")",   x+50, y );
    text( "DX="+  octDX  +", DY="+  octDY,   x+50, y+20 );
  }
  //bouncing off walls
  if (x < 50 || x > width-50) octDX  *= -1; 
  if (y < sealevel || y > height-75) octDY  *= -1; 
  //movement
  if (key != 'p') {
    x += octDX;
    y += octDY;
  }
}

void grass(){
  //setting up grass accross the screen
 float grassX=20;
  while ( grassX < (width - 5))
  {
    stroke (0,255,0);
    strokeWeight(4);
    //setting grass to lean in the direction of movement
        if (octDX>0){
        line (grassX,height-50,grassX-10,height-5);
        }
      if (octDX<0){
      line (grassX,height-50,grassX+10,height-5);
      }
    grassX += 20;
  }
}


void waves(){
  //waves
  noStroke();
  float waveX=20;
  fill(50,100,250);
  ellipseMode(CENTER);
      while (waveX<width){
      ellipse(waveX,sealevel,70,70);
      waveX+=120;
      }
  //wave dips
  float dipX=80;
  fill(200,100,100);
  ellipseMode(CENTER);
  
            while (dipX<width+100)
            {
              ellipse(dipX,sealevel,50,50);
              dipX+=120;
            }
}


void sun(){
  fill(255,255,0);
  ellipse( (frameCount/2)%width+15 , height/12, 30,30);
}



void button(){
  noStroke();
  fill(50,100,250);
  rectMode(CORNERS);
  rect(600,height-100,750,height-50);
  fill(255);
  textSize(18);
  text("Click here",620,height-75);
  
}


void mousePressed(){
  if (mouseX<750 && mouseX>600 && mouseY<height-50 && mouseY>height-100) {
    x=random(100,width-100);
    y=random(sealevel,height-100);

   //hit detection background(0);
  }
    if (mouseX>x-35 && mouseX<x+35 && mouseY>y-60 && mouseY<y+60){
     //hit detection background(0);
      r=random(150,200);
      g=random(0,100);
      b=random(150,200);
  
    }
}


void keyPressed( ) {
  //// Key events
  if (key == '@') debug = ! debug;
}
