//// CST-112 FINAL EXAM -- Salvatore Randazzo ////

String student="Salvatore Randazzo";
String title="CST-112 FINAL EXAM:     " + student;
String news=  "Click to move objects";

//////// GLOBAL DECLARATIONS ////////
int score=0;
float left=50, top=50, right=750, bottom=550;

Animal zoog, cat, bird, bird2;//, bug, bug2, bug3, bug4, bug5;
Animal submarine;
Animal ant;

int maxbugs=5, numbugs=maxbugs;
Animal[] bugs=  new Animal[ maxbugs];

    
//////// SKETCH FUNCTIONS ////////
void setup() {
  //// Setup.
  size( 800, 600 );
  right=width-50;
  bottom=height-50;
  reset();
}
void reset() {
  //// initialize the menagarie.
  cat=  new Animal( 50, 30, 20, 4, 0 );
  cat.setRGB( 200,150,50 );
  cat.x=100;
  cat.y=100;
  cat.name=  "cat";
  //
  zoog=  new Animal( 40, 80, 0, 2, 7 );   
  zoog.setRGB( 200,150,150 );
  zoog.x=  200;
  zoog.y=  200;
  zoog.name=  "Zoog";
  //
  bird=  new Animal( 3,0,0, 50, 15, 12, 0, 0 );   
  bird.setRGB( 50,50,250 );
  bird.x=  300;
  bird.y=  300;
  //
  bird2=  new Animal( 3,0,0, 50, 15, 12, 0, 0 );   
  bird2.setRGB(100,50,250 );
  bird2.x=  300;
  bird2.y=  300;
  //  
  submarine=  new Animal( 2,0,1, 50, 15, 12,0, 0 );   
  submarine.setRGB( 155,0,255 );
  submarine.x=  400;
  submarine.y=  400;
  submarine.name=  "Nautilus";
  //
  ant=  new Animal( 6,0,3, 15,45, 255,50,50 ); 
  ant.y=  bottom ;
  //
  for (int i=0; i<numbugs; i++) {
    bugs[i]=  new Animal( 6,0,4, 15,55, 0,0,0 ); 
    bugs[i].y=  bottom ;
  }

/*
  //
  bug2=  new Animal( 6,0,4, 15,55, 0,0,0 ); 
  bug2.y=  bottom ;
  //
  bug3=  new Animal( 6,0,4, 15,55, 0,0,0 ); 
  bug3.y=  bottom ;
  //
  bug4=  new Animal( 6,0,4, 15,55, 0,0,0 ); 
  bug4.y=  bottom ;
  //
  bug5=  new Animal( 6,0,4, 15,55, 0,0,0 ); 
  bug5.y=  bottom ;
  */
  for (int i=0; i<maxbugs; i++)  {
    
  }
}
void draw() {
  //// Next frame.
  scene();
  action();        // Move all objects, check collisions, etc.
  show();          // Draw objects on screen.
  messages();
}
void action() {
  //// Move, check collisions, etc. ////
   if( cat.activate ) 
    cat.move();
 
  if( bird.activate  ) 
   bird.move();
  
  if( bird2.activate )      bird2.move();
  
  if( zoog.activate == true )      zoog.move();
  
  if( submarine.activate == true )       submarine.move();
/*
  bug.move();
  bug2.move();
  bug3.move();
  bug4.move();
  bug5.move();
  */
  //
  /*
  bug.move();
  if (bug.y < top+20) 
    bug.dy=0;                       // Dead bug -- Stop rising.
    bug.setRGB( 180,175,175 );    // Change color
    if (bug.x > right-20 )         // Float off end; new bug.
          bug=  new Animal( 6,0,3, 15,45, 0,0,0 ); 
          bug.y=  bottom;
  //
  bug2.move();
  if (bug2.y < top+20) 
    bug2.dy=0;                       // Dead bug -- Stop rising.
    bug2.setRGB( 180,175,175 );    // Change color
    if (bug2.x > right-20 )         // Float off end; new bug.
          bug2=  new Animal( 6,0,3, 15,45, 0,0,0 ); 
          bug2.y=  bottom;
  //
  bug3.move();
  if (bug3.y < top+20) 
    bug3.dy=0;                       // Dead bug -- Stop rising.
    bug3.setRGB( 180,175,175 );    // Change color
    if (bug3.x > right-20 )         // Float off end; new bug.
          bug3=  new Animal( 6,0,3, 15,45, 0,0,0 ); 
          bug3.y=  bottom;
  //
  bug4.move();
  if (bug4.y < top+20) 
    bug4.dy=0;                       // Dead bug -- Stop rising.
    bug4.setRGB( 180,175,175 );    // Change color
    if (bug4.x > right-20 )         // Float off end; new bug.
          bug4=  new Animal( 6,0,3, 15,45, 0,0,0 ); 
          bug4.y=  bottom;
  //
  bug5.move();
  if (bug5.y < top+20) 
    bug5.dy=0;                       // Dead bug -- Stop rising.
    bug5.setRGB( 180,175,175 );    // Change color
    if (bug5.x > right-20 )         // Float off end; new bug.
          bug5=  new Animal( 6,0,3, 15,45, 0,0,0 ); 
          bug5.y=  bottom;
  //
  */
  //// Move the ant.
  ant.move();
  if (ant.y < top+20) {
    ant.dy=0;                       // Dead ant -- Stop rising.
    ant.setRGB( 150, 50, 50 );    // Change color
    if (ant.x > right-20 ) {        // Float off end; new ant.
           ant=  new Animal( 6,0,3, 15,45, 255,0,0 ); 
           ant.y=  bottom;
    }
  }

  //Move the bugs

  for (int i=0; i<maxbugs; i++)  {{
      bugs[i].move();
    if (ant.y < top+20) { 
      bugs[i].dy=0;                       // Dead ant -- Stop rising.
      bugs[i].setRGB( 150, 50, 50 );    // Change color
      if (bugs[i].x > right-20 )         // Float off end; new bugs[i].
            bugs[i]=  new Animal( 6,0,3, 15,45, 255,0,0 ); 
            bugs[i].y=  bottom;
      }
    }
  }

  /*
  bugs[0].move();
  bugs[1]
  bugs[2]
  bugs[3]
  bugs[4]
*/

  crash();  
}
void crash() {
  //// Check for collisions ////

  // zoog kicks the cat (+50) -- move cat to right side.
  if (zoog.hit( cat ) ) { cat.x=right-20; score += 50;} 

  // cat catches bird (-100) -- move bird to left side.
        if( cat.hit( bird ) ) { bird.x = left + 20; score -= 100;} 

  // bird hits Zoog (-50) -- move Zoog to random position.
        if (bird.hit( zoog ) ) { zoog.randomXY(); score -= 50;} 

  // bird catches any bug (+10) -- start new bug at bottom 
        for (int i=0; i<maxbugs; i++)  {
          if (bird.hit( bugs[i] ) ) { bugs[i].x=bottom-20; score +=10;} 
        }

  // cat catches any bug (+25) -- start new bug at bottom 
//        if (cat.hit( bug ) ) { bug.x=bottom-20; score +=25;} 
  // zoog catches a bird (+50) ----- start bird at left
         if (zoog.hit( bird ) ) { bird.x = left + 20; score += 50;} 
  }
void show() {
  //// Draw objects on screen. ////
  cat.show();
  bird.show();
  bird2.show();
  zoog.show();
  submarine.show();
  ant.show();  

        for (int i=0; i<maxbugs; i++)  {
            bugs[i].show();
        }
  for (int i=0; i<5; i++) {
   }
}
void scene() {
  //// Background, scene, etc. ////
  background( 255,255,200 );
  fill( 150,200,150 );
  rectMode(CORNERS);
  rect( left, top, right, bottom );
  //// Blades of grass across the bottom ////
  stroke(0,150,0);    // Grass color.
  strokeWeight(1);
  if( cat.dx > 0 ) 
    for(int i = 5; i < right / 10; i++) 
      line(i * 10, bottom, i * 10 + 10, bottom - 20);
    else if ( cat.dx < 0 ) 
    for(int i = 6; i < right/ 10 + 1; i++) 
       line(i * 10, bottom, i * 10 - 10, bottom - 20);
    else 
    for(int i = 5; i < right/ 10; i++) 
       line(i * 10, bottom, i * 10, bottom - 20);
}
 void messages() {
  //// Display messages
  fill(0);
  textSize( 20 );
  text( title, width/3, 25 );
  textSize( 14 );
  text( news, left, 40 );
  if(score != 0) 
    text("your score is " + score, width/2, height*19/20);
}
//////// EVENT HANDLERS ////////
 void keyPressed()  {
  //// Handle keys ////
  if (key == 'q') {   exit();  }
  if (key == 'r') {   reset();  }
  if (key == 'c') {   cat.randomXY();  }
  if (key == 'z') {   zoog.randomXY();  }
  if (key == 'b') {   }
    bird.randomXY(); 
    bird.dx=  12;
    bird.dy=  .5;
  }


void mousePressed() {
  if (cat.hit( mouseX, mouseY ) ) {
    cat.activate=  true;
  }
}

//////// CLASS DEFINITION ////////
  class Animal {
  //// Animal has a body, an optional head, legs, pairs of arms, etc.
  //// Animal is wide and tall, and has other properties such as color.  
  //// Also:  position (x,y) and velocity (dx,dy).   
  //// Desription and drawing info.   
  float wide=30, tall=50, head=0, tail=0;
  int legs=0, armpairs=0, reach=20;
  String name="";
  int r,g,b;                                // Color.
  int shape=4;
  //// Position, velocity, boundaries
  float x=400,y=300, dx=0,dy=0;
  boolean activate=  true;


  //// CONSTRUCTORS (and set functions) ////
  Animal( int shape, int legs ) { 
    //// 2-arg Constructor:  shape, wide, tall ////
    this.shape=  shape;
    randomXY();            //Random x,y, dx,dy
  }
  Animal( int shape, int legs, int armpairs, float wide, float tall, int r, int g, int b) {
    //// 8-arg Constructor:  shape, wide, tall ////
    this.shape=  shape;
    this.legs=  legs;
    this.armpairs=  armpairs;
    this.wide=  wide;
    this.tall=  tall;
    randomXY();            //Random x,y, dx,dy
    setRGB( r, g, b );
  }
  
  Animal( float wide, float tall, float head, int legs, int armpairs ) {
    //// 5-arg Constructor ////
    this.wide=  wide;
    this.tall=  tall;
    this.head=  head;
    this.legs=  legs;
    this.armpairs=  armpairs;
  }
  Animal( int shape, float wide, float tall, int armpairs ) {
    //// 4 - arg Constructor
    this.shape=  shape;
    this.wide=  wide;
    this.tall=  tall;
    this.armpairs=  armpairs;
  }  
  void setRGB( int r, int g, int b) {
    this.r=  r;
    this.g=  g;
    this.b=  b;
  }
  void randomXY() {
    x=  random(100,700);
    y=  random(100,500);
    dx=  random(1.5,2.5);
    dy=  random(0.5,1.5);
  }
    
  //// METHODS to move and show ////
  void move() {
    //// Check for boundaries, and move to new coordinates (x+dx, y+dy).
    if (x<left || x>right) dx= -dx;
    if (y<top || y>bottom) dy= -dy;
    ////
    x += dx;
    y += dy;
  }
  void show() {
    //// Draw the Animal on the screen ////
    rectMode( CENTER );
    ellipseMode( CENTER );
    fill(r,g,b);
    //// Front & back depend on dx ////
    float front=x+wide/2, rear=x-wide/2;
    if (dx<0) {
        front=  x-wide/2;
        rear=  x+wide/2;  
      }
    //// Draw different shapes. ////
    if (shape == 0) {
      ellipse( x, y, wide, tall );      
    } else if (shape == 4) {
      rect( x, y, wide, tall );
    } else if (shape == 3) {
        triangle( front,y, rear,y-tall/2, rear,y+tall/2 );  
    } else if (shape == 2) {

          // ++++++++++ ADD YOUR CODE HERE. +++++++++++++ //  
    //flap of the birds wings
      if(( millis() / 1000) % 2 == 1 ) {
            if( dx > 0 ) {
              triangle( front - wide / 2, y, rear - wide / 2, y - tall / 2, rear, y + tall / 2 );
          } else {
              triangle( front + wide / 2, y, rear + wide / 2, y - tall / 2, rear, y + tall / 2 );
          }
      }else {
              if( dx > 0 ) {
                triangle( front - wide / 2, y, rear + wide, y + tall, rear, y + tall / 2 );
            } else {
                triangle( front + wide / 2, y, rear - wide, y + tall, rear, y + tall / 2 );
            }
      }    
      
    } else if (shape == 6) {
        ellipse(x,y-tall*2/3, wide, tall/3 ); 
        ellipse(x,y-tall/3, wide, tall/3 ); 
        ellipse(x,y, wide, tall/3 ); 
        reach=  8;      
    }     else if (shape == 2) {
    // ellipse with rectangle on top, and one pair of "arms".   
      ellipse( x, y, wide, tall );
        rect( x, y - tall / 2, wide / 3, tall / 5 );
        text( "undefined object is here!", x, y );

    } else {
        text( "undefined shape!!!!!", x, y );
    }
    //// Head 
    if (head>0) {                  // Head (if any) //
      ellipseMode( CENTER );
      ellipse( front, y-tall/2-head/2, head, head );    // Head at front
      rect( rear, y-tall/2, 20, 6 );                    // Tail at rear
    }
    limbs();
    //// Name
    fill( 256-r, 256-g, 256-b ); 
    text( name, x-wide/3, y-tall/6 );
  }
  void limbs() {
    //// Legs & Arms ////
    float legX=  x - wide/2;        // First leg is at the left
    float legY=  y + tall/2;        // Legs start at bottom of rectangle.
    if (legs > 0) {
      line( legX,legY, legX,legY+reach );     // First leg.
    }
    if (legs > 1) {
      float legSpacing=  wide / (legs-1);    
      for (int i=1; i<legs; i++) {
        legX=  legX + legSpacing;
        line( legX,legY, legX,legY+reach );     // First leg.        
      }
    }
    //// Pairs of arms. ////
    float armY=  y - tall/2;        // Arms start at top of rectangle.
    float armSpacing=  tall / (armpairs);    
    for (int i=0; i<armpairs; i++) {
        line( x-wide/2,armY, x-reach-wide/2,armY );     // Left arm        
        line( x+wide/2,armY, x+reach+wide/2,armY );     // Right  arm        
        armY=  armY + armSpacing;
    }
  }  
  boolean hit( float x, float y ) {
    //// Return true iff x,y is inside this box.
    if (x < this.x - this.wide/2) return false;
    if (x > this.x + this.wide/2) return false;
    if (y < this.y - this.tall/2) return false;
    if (y > this.y + this.tall/2) return false;
    return true;
  }
  boolean hit( Animal other ) {
    //// Return true if other hits this one.
    return hit( other.x, other.y );
  }

}//class Animal//
