/*****************************************************************
NanoBots
by Keith Pasko
11.30.04

Nanos look for each other and form groups
When Nanos form a group, the extend their 
arms and create a network of lines implying
streams of information.

Press 1-5 on keypad for various grouping modes

Click to disperse nanos at any time

oh and also, this code's not optimised...at all...
*****************************************************************/


/***********************************
Class Outline Table

Nano

Variables:
float x - x position
float y - y position
float siz - nano size
float col  - nano color
float angle - used to modulate "feet"
float t - used to stretch "feet"

Methods:
void update () - nano's own motion
void display () - creates nano

************************************/

int numNanos=500;
Nano[] myNano = new Nano[numNanos];
float lag;
float influence;
int rad;


void setup()
{
  size(300, 300);
  for(int i=0; i<numNanos; i++) {
    float xpos=random(0,300);
    float ypos=random(0,300);
    float scl=random(2,7);
    float  c= random(30,120);
    myNano[i]= new Nano(xpos,ypos,scl,c);
  }
  framerate(60);
  smooth();
  angleMode(DEGREES);
  rectMode(CENTER_RADIUS);

}

void draw()
{
  background(255);
  for(int i=0; i<numNanos; i++) {
    myNano[i].update();
    myNano[i].display();
    for (int j=0;j<numNanos;j++){
      //influence=30;  //how far away Nanos influence other Nanos
      //lag=230*influence/myNano[i].siz;  //speed they clump at, larger Nanos attract faster
      //rad=4;  //size of clumps
      
      if (mousePressed==true){
        lag=-14*influence;
        myNano[i].range=2.5;
     }else{
       keyPressed=true;
       myNano[i].range=.7;
       }   
      
      if (keyPressed==true&&mousePressed==false){
        influence=30;
        lag=250*influence/myNano[i].siz;
        rad=4;
        if (key=='1'){
        influence=30;
        lag=250*influence/myNano[i].siz;
        rad=4;
        }else if (key=='2'){
        influence=40;
        lag=240*influence/myNano[i].siz;
        rad=7;
        }else if (key=='3'){
        influence=75;
        lag=250*influence/myNano[i].siz;
        rad=10;
        }else if (key=='4'){
        influence=90;
        lag=640*influence/myNano[i].siz;
        rad=13;
        }else if (key=='5'){
        influence=110;
        lag=530*influence/myNano[i].siz;
        rad=12;
        }
        }
      
      
      
      float diffx=myNano[j].x-myNano[i].x;
      float diffy=myNano[j].y-myNano[i].y;
      if (dist(myNano[i].x,myNano[i].y,myNano[j].x,myNano[j].y)<influence){

        if (abs(diffy)>rad && abs(diffx)>rad){
          myNano[i].y+=diffy/lag;
          myNano[i].x+=diffx/lag;
          myNano[i].t=0;
        }else{
          myNano[i].t+=.81;
        }
      }
    }

  }
}


class Nano
{
  float range=.7;

  float x;
  float y;
  float siz;
  float col;
  float angle;
  float t;

  Nano (float xpos, float ypos, float scl, float c)
  {
    x=xpos;
    y=ypos;
    siz=scl;
    col=c;
  }

  void update()
  {

    x+= random(-range,range);
    y+= random(-range,range);
    x = constrain(x, 0, width);
    y = constrain(y, 0, height);
    angle+=90*2/siz;
    if (angle>360){
      angle=0;
    }
  }

  void display()
  {
    push();
    translate(x,y);
    fill (col);
    noStroke();
    rect (0,0,siz/2,siz/2);
    fill (col+190);
    rect (0,0,siz/4,siz/4);
    stroke (col);
    for (float i=-siz/2;i<siz/2;i+=2){
      line (i,-siz/2,i,siz/1.4+sin(angle)+t);
    }
    for (float i=-siz/2;i<siz/2;i+=2){
      line (i,siz/2,i,-siz/1.4+sin(angle-60)-t);
    }
    for (float i=-siz/2;i<siz/2;i+=2){
      line (siz/2,i,siz/1.4+sin(angle-30)+t,i);
    }
    for (float i=-siz/2;i<siz/2;i+=2){
      line (-siz/2,i,-siz/1.4+sin(angle-90)-t,i);
    }

    pop();

  }


}
Exercise 4.C: Objects
Design a class which displays, animates, and defines the behavior of a machine or organism. Give your invention a goal (e.g. finding other creatures, searching for food, climbing the screen) and have it react when it reaches its goal.