// walker2
String title=  "5 Walkers";

Walker a, b, c, d, e;

void setup()
{
  size(600,800);
  a=  new Walker(255,200,100);
  a.y=  100;
  b=  new Walker(200,255,200);
  b.y=  300;
  b.dx=2;
  //
  c=  new Walker(50,500, 1,-1, 200,200,255);
  d=  new Walker(100,200, 3,2, 255,255,0);
  e=  new Walker(400,400, 4,-2, 200,255,255);
}
void draw()
{
  background(255);
  text( title, width/3,20 );

  //// Check for collisions
  a.checkHit(b);
  a.checkHit(c);
  a.checkHit(d);
  a.checkHit(e);
  
  b.checkHit(c);
  b.checkHit(d);
  b.checkHit(e);
  
  c.checkHit(d);
  c.checkHit(e);
  
  d.checkHit(e);

  //// Move and show
  a.update();
  b.update();
  c.update();
  d.update();
  e.update();
}







class Walker
{
  //// Animated figure.
  int step=0;        // Step number.
  float x=50,y=50;
  float dx=1,dy=0;
  float w=50, h=80;
  int r=255,g=200,b=100;
  String name="?";

  float foot=8;
  
  float near=20.0;
  
  
  //// CONSTRUCTORS ////
  Walker( int rr, int gg, int bb)
  {
    setcolors(rr,gg,bb);
  }
  void setcolors( int rr, int gg, int bb )
  {
    r=rr;
    g=gg;
    b=bb;
  }    
  Walker( float xx, float yy, int rr, int gg, int bb)
  {
    x=xx; y=yy;
    setcolors(rr,gg,bb);
  }
  Walker( float xx, float yy, float dxdx, float dydy, int rr, int gg, int bb)
  {
    x=xx; y=yy;
    dx=dxdx; dy=dydy;
    setcolors(rr,gg,bb);
  }
    
  //// METHODS ////
  void update()
  {
    move();
    show();
  }
  void move()
  {
    //// Take a step.
    if (x<10 || x>width-10) dx=  -dx;
    x += dx;
    if (y<10 || y>height-10) dy=  -dy;
    y += dy;
  }
  void show()
  {
    // Draw the body & head.
    noStroke();
    rectMode(CORNER);
    fill(r,g,b);
    rect(x,y, w, h);
    ellipseMode (CENTER );
    ellipse(x+w/2, y-20, 30,40);
    // Now, draw the legs.
    stroke(r,g,b);
    strokeWeight(12);
    if (step++ % 10 < 5)    // Check if odd or even
    {
      leg( x+8, y+h, 0);
      leg( x+w-8, y+h, foot);
      /*
      line(x+5,y+h, x-5,y+h+40);
      line(x+w-5,y+h, x+w-5,y+h+40);
      */
    } else {
      leg( x+5, y+h, -foot);
      leg( x+w-5, y+h, 0);

      /*
      line(x+5,y+h, x+5,y+h+40);
      line(x+w-5,y+h, x+w+5,y+h+40);
      */
    }
    fill(0);
    text( name, x+w/2-5, y+20);

  }
  void leg(float x, float y, float offset)
 {
   //// Draw one leg:  start at (x,y), go down 30 & offset to one side.
   line(x,y, x+offset,y+40);   
 }


  //// METHOD TO DETECT COLLISION
  void checkHit( Walker q ) {
    //// Check for hit; swap velocities if hit.
    if (hit( q )) 
    {
      float tmp=  q.dx;          // Swap velocities (completely elastic)
      q.dx=  this.dx;
      this.dx=  tmp;
      tmp=  q.dy;
      q.dy=  this.dy;
      this.dy=  tmp;
      //
      background( r, g, b );
      fill(0);
      ellipse(x,y, 100,100);
    }
  }
  boolean hit( Walker q  )
  {
    //// return true if this object collides with 
    return abs( this.x - q.x ) < 20   &&   abs( this.y - q.y ) < 20;

    /*
    boolean result;
    if ( abs(x-q.x) < 20   &&   abs(y-q.y) < 20 )
    {
      result=true;
    } else {
      result=false;
    }
    return result;
    */
    
  }

}



