// Renato Briceno - Project 6


  Person a,b,c,d,e;
  
  
    void setup () {
      
       size (800,600);
       frameRate(30);
       a = new Person(15,103,188);
       a.y= 300;
       b= new Person(500,100,6, 4,188,15,186);
       c= new Person(300,120,-3,-8,15,188,28);
       d= new Person(350,300,5,-4,237,231,29);
       e= new Person(250,170,-7,-6,239,39,29);


}


     void draw () {
        background(255);    
      
      a.collision(b);
      a.collision(c);
      a.collision(d);
      a.collision(e);
      b.collision(c);
      b.collision(d);
      b.collision(e);
      d.collision(e);
      
      
       a.update();
       b.update();
       c.update();
       d.update();
       e.update();
       
       
       
}

        //Class Character Defined
        
        class Person {

   //VARIABLES

    float x= 70,y=70;
    float dx=5,dy=3;
    float w= 50,h=70;
    int r=74,g=188,b=18;
    int step = 0;    
    
    int manyhits=0;
          
          
  //CONSTRUCTOR
  Person (int R,int G,int B) {
     
       colors(R,G,B);
  }
  
  
  void colors ( int R, int G, int B ) {
   
     r = R;
     g= G;
     b= B;
  }
    
  Person ( float X,float Y, float DX, float DY, int R, int G, int B) {  // Constructor defines for color ,speed,location
    
    x= X; y= Y;
    dx=DX; dy=DY;
    colors (R,G,B);
  }
       
    // Function Methods
    
    void update() {
      
      move();
      show();
    }
    
    void show() {
      
                                  //DRAW CHARACTER 
      fill(r,g,b);      
      rectMode(CENTER);
      rect (x,y,w,h);
      eyes();
     
}
          
    void eyes () {              //Eyes are drawn 
      
      fill(0);
      noStroke();

      float ouch=0;
      if (manyhits % 2 > 0) 
      {
        ouch=30;
        fill(255,255,0);
        stroke(255,0,0);
      }

      
      ellipse(x-10,y-10+ouch, 20,20);    
      ellipse(x+15,y-10+ouch,20,20);  
      stroke(1);  
      fill(r,g,b);
            
      if ( x < width/2 ) {                        //Eyes react to collision
        ellipse(x-13,y-10+ouch, 8,8);
        ellipse(x+12,y-10+ouch, 8,8);
     }
     
     if (x >width/2) {
        ellipse(x+20,y-10+ouch,8,8);
        ellipse(x-5,y-10+ouch,8,8);
    }
    }
      
void move() {
  // Move in x and y, also check walls
    
   if (x <20 || x > width-10) {
      dx = -dx;
      x += 2*dx;
      manyhits++;
   }
   
    x += dx;
   if (y<0 || y >height-10) {                   
     dy= -dy;
     y += 2*dy;
      manyhits++;
   }
    y += dy;
    
}
      
      
      //CHECK COLLISIONS?
      
    void collision (Person z) {
       
      if (hit( z )) 
    {
      manyhits++;
      
      float tmp=  z.dx;          // Swap velocities 
      z.dx=  this.dx;
      this.dx=  tmp;
      tmp=  z.dy;
      z.dy=  this.dy;
      this.dy=  tmp;
      fill(0);
     
     ellipse(x,y,30,30);
     /*
      ellipse(x,y, 100,100);
       */        
  }  
    }  
  
  
    
     boolean hit (Person z) {
       
       return abs ( this.x - z.x ) < 20 && (this.y - z.y) <20;
     }
        }
         
         
