/////////////////////////////////////
//Leon Hong                        //
//Feed the Robot and Turn Him On!  //
//Ex 4C                            //
/////////////////////////////////////
/////////////////////////////////////
//RoBot Class                      //
/////////////////////////////////////
//Fields/////////////////////////////
//PImage bg      // body           //
//PImage bg2     //body w/glow     //
//PImage top2    //head            // 
//PImage top     //head w/glow     //
//PImage topalpha//head alpha      //
//float sinval   //smooth values   //
//                 for robot follow//
//float angle    //for sin         //
//float speed    //speed           //
/////////////////////////////////////
//Methods////////////////////////////
//Action     //Calculates numbers  //
//             for the robot follow//
//             No return value     //
//follow     //follows the battery //
//             No return value     //
//Displayeat //displays the robot  //
//             without glow        //
//             No return value     //
//Displayglow//displays robot with //
//             glow                //
//             No return value     //
/////////////////////////////////////
float mx=-100;
float my=-10;
float delay = 100.0;
int i =100;
Robot leon;
PImage batt = loadImage("batt.gif");
PImage battalpha = loadImage("battalpha.gif");
PImage batteat = loadImage("batteat.gif");
PImage batteatalpha = loadImage("batteatalpha.gif");
void setup(){
  angleMode(DEGREES);
  size(300,300);
  leon = new Robot (loadImage ("bg.gif"),loadImage ("head.gif"),
  loadImage ("bg.jpg"),loadImage ("head.jpg"),
  loadImage ("headalpha.gif"), 0.0, 2.5);
  alpha (batt, battalpha);
  alpha (batteat, batteatalpha);
}
void draw(){
  noCursor();
  background (172);
  push();
  leon.follow();
  leon.action();
  if (abs(my-mouseY)>5 && abs(mx-mouseX)>5){
    leon.displayeat();

  }
  else{
    leon.displayglow();
  }
  pop();
  push();
  if (abs(my-mouseY)>5 && abs(mx-mouseX)>5){
    battery(true);
  }
  else{
    battery (false);
  }
  pop();
}

class Robot
{
  PImage bg , bg2, top2, top, topalpha;
  float sinval;
  float angle;
  float speed;
  Robot(PImage body,PImage head,PImage body2,PImage head2,
  PImage headalpha, float iangle, float ispeed) {
    bg=body;
    top = head;
    bg2=body2;
    top2 = head2;
    topalpha = headalpha;
    alpha (top,topalpha);
    alpha (top2,topalpha);
    angle = iangle;
    speed = ispeed;
  }
  void action() {
    angle = angle + speed;
    sinval = sin(angle);
  }
  void displayeat() {
    image (bg,0,135);
    image (top,0,sinval*20+40);
  }
  void displayglow() {
    image (bg2,0,135);
    image (top2,0,sinval*20+40);
  }
  void follow(){
    interpolate();
    translate (mx-100,my-150);
  }

}

void battery(boolean test){
  if (test==true){
    image (batt, mouseX-50, mouseY-20);
  }
  else{
    image (batteat, mouseX-50+random(-20,20), mouseY-20+random(-20,20));
  }
}

void interpolate(){
  float diffx = mouseX-mx;
  if(abs(diffx) > 1) {
    mx = mx + diffx/delay;
  }
  float diffy = mouseY-my;
  if(abs(diffy) > 1) {
    my = my + diffy/delay;
  }
}
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.