// Desma 28: Project 3 [Final Build]

int numPpl = 50;
walk_c[] walk_a = new walk_c[numPpl];
int numFrames = 7;
PImage[] walk = new PImage[numFrames];
int numCloud = 10;
cloud_c[] cloud_a = new cloud_c[numCloud];


int ground = 400; // ground line
int count = 0; // number of people abducted 

void setup(){
  frameRate(20); 
  size(600, 600);
  for (int i=0; i<walk_a.length; i++){
    walk_a[i] = new walk_c(random(-50, 650), ground - 10 + random(-0.2, 0.2), random(-1.75, 1.75), int(random(0,2)));
  }
  for (int i=0; i<cloud_a.length; i++){
    cloud_a[i] = new cloud_c(int(random(0, 600)), int(random(-20, 150)), random(-0.25, 0.25));
  }
  for (int i = 0; i<walk.length; i++){
    String imageName = "walk" + nf(i, 2) + ".gif";
    walk[i] = loadImage(imageName);
  }
  
  // the loading dock 
  
  img_lamp = loadImage("lamp.gif"); 
  img_bench = loadImage("bench.gif");
  img_firehyd = loadImage("firehyd.gif");
  img_stores = loadImage("stores.gif");
  img_car = loadImage("car.gif");
  img_car2 = loadImage("car2.gif");
  img_cloud = loadImage("cloud.gif");
  img_ufo = loadImage("ufo.gif");
  font = loadFont("silk8.vlw");
}


float move; // scene move speed
float move_stores;
float move_speed;

float posx_car = random(700, 800);
float posx_car2 = random(600, 700);
float posx_car3 = random(-100, -200);
float posx_car4 = random(0, -100);

void draw(){
   move_speed = (mouseX - width/2)*0.0025;
  
  if(mouseX > width/2){
  move -= move_speed;  // move left (further right = faster)
  move_stores -= move_speed*0.85;
  } else {
  move += abs(move_speed);  // move right (further left = faster)
  move_stores += abs(move_speed)*0.85;
  }
  
  background(#ffffff);
  for(int i = 0; i<cloud_a.length; i++){
    cloud_a[i].move();
    cloud_a[i].display();
  }
  
  for(int i=-985; i<1; i += 985){
    if (move_stores > 985){
      move_stores = 0;
    }
    if (move_stores < -385){
      move_stores = 600;
    }
    stores(i + int(move_stores));
  }
  //println(move_stores);
  
  for (int i = 0; i<width+220; i = i + 110 ){ // 110 = lamp spacing
    if( move > 102){                     //lamp_space - img_lamp.width 
      move = -img_lamp.width;      // reset lamp loop
    }
    if ( move < - 102){
      move = img_lamp.width;
    }
    lamp(i + move);
    bench(i-90 + move);
    firehyd(i*3-20 + move);
  }
  line(0, ground, width, ground);        //draw ground line
  
  for (int i = 0; i<walk_a.length; i++){ // draw walking people
    walk_a[i].move();  
    walk_a[i].display();
  }
  
  posx_car -= 7;
  posx_car2 -= 8;
  posx_car3 += 7;
  if(posx_car < -100){
    posx_car = random(600, 800);
  }
  if(posx_car2 < -100){
    posx_car2 = random(600, 700);
  }
  if(posx_car3 > 700){
    posx_car3 = random(-100, -200);
  }
  car(posx_car, 1);
  car(posx_car2, 2);
  car2(posx_car3, 4);
  
  ufo();
  sig();
  
}
  

PImage img_lamp, img_bench, img_firehyd, img_stores, img_car, img_car2, img_cloud, img_ufo;

void lamp(float posx_lamp){
  
  image(img_lamp, posx_lamp, ground-img_lamp.height);
}

void bench(float posx_bench){
  
  image(img_bench, posx_bench, ground-img_bench.height);
}

void firehyd(float posx_firehyd){
  
  image(img_firehyd, posx_firehyd, ground-img_firehyd.height);
}

void stores(float posx_stores){
  image(img_stores, posx_stores, ground-img_stores.height);
}

void car(float posx_car, float posy_car){  
    image(img_car, posx_car, ground-img_car.height/2 + posy_car);
}
void car2(float posx_car, float posy_car){  
    image(img_car2, posx_car, ground-img_car.height/2 + posy_car);
}

float ufoX = 0;
float ufoY = 0;
float ufoDY = 0;
float easing;

void ufo(){
  float easing_mod = constrain(1+(count*0.0075), 0, 4);
  //println(easing_mod);
  easing = 0.1 / easing_mod ; // slow easing when gaining weight
  ufoY += 0.25;
  if(ufoDY < 100 + (count/2) && ufoDY < 250){ // lower flight when gaining weight
  ufoDY += 3;
  }
  float target_ufoX = mouseX-img_ufo.width/2;
  ufoX += (target_ufoX - ufoX) * easing;
  image(img_ufo, ufoX, ufoDY + int(sin(ufoY)*5));
  
}

class cloud_c {
  
  float posx_cloud;
  float posy_cloud;
  float speedx;
  float speedy;
  float direction;
  
  cloud_c (float vposx_cloud, float vposy_cloud, float vspeedx){
    posx_cloud = vposx_cloud;
    posy_cloud = vposy_cloud;
    speedx = vspeedx;
    direction = -1;
  }
  
  void move(){
     posx_cloud += speedx;
     if(posx_cloud < -100){
       posx_cloud = 650;
    }
    if(posx_cloud > 650){
       posx_cloud = -100;
    } 
  }
  
  void display(){
    image(img_cloud, posx_cloud , posy_cloud);
  }
}

 class walk_c {
  float posx_i;    // initial x position
  float posy_i;    // initial y position
  float speedx;    // walk speed
  float direction; // direction
  
  int frame;        // animation control
  int frame_offset = 0;
  
  boolean left;
  
  float contact;
  
  
  walk_c(float vposx_i, float vposy_i, float vspeedx, int vframe){
    posx_i = vposx_i;
    posy_i = vposy_i;
    speedx = vspeedx;
    direction = -1;
    frame = vframe;
  }
     
  void move(){

    float move_speed_act = move_speed; 
    posx_i += (speedx - move_speed_act); // accel / decel in respect to camera

    int turn = int(random(0, 200));
    
    if(posx_i < -60 || posx_i > width + 60 || turn == 100){ // turn around when hit
      speedx = (speedx * direction);                         // right or left bound 
      posx_i += (speedx - move_speed_act);
  }                                                        // or random value = 50.
    
    if (speedx > 0) { // direction of animation
      left = false;
    } else {
      left = true;
    }
     
    if (speedx < 0.5 && speedx > -0.5){ // if speed is too slow, stop and stand
      speedx = 0;
      frame = 6;
      move_speed_act = 0;    
      posx_i -= move_speed;
      image(walk[frame], posx_i, posy_i);
    }
    
    contact = dist(mouseX, mouseY, posx_i, posy_i);
    
    if (contact < 20 ){
      if ((speedx > 0 && posx_i < mouseX) || (speedx < 0 && posx_i > mouseX)){
      speedx = (speedx * direction);
      }
    }
    
    float ufoDist = dist(ufoX+img_ufo.width/2, posy_i, posx_i, posy_i);
    //println(ufoDist);
    
    if (ufoDist < 5 && mousePressed){ // lift off
      posy_i -= 0.5;
      count++;
    }
    if( posy_i < ground - 11){  // auto pilot
      posx_i = ufoX+img_ufo.width/2;
      posy_i -= 35;
      
    }
   if (posy_i < constrain(105 + (count/2), 0, 250)){ // redistribution
     posx_i = random(-50, 650);
     posy_i = ground - 10 + random(-0.2, 0.2);
   }
 }



void display(){
  
  if (frame < 6){
    frame++;
  if (frame > 2){
    frame = 0;
  } 
  if(left == false){
    frame_offset = 3;
  }
  image(walk[frame+frame_offset], posx_i, posy_i);
  }
}
}

PFont font;

void sig(){
  fill(200);
  textFont (font, 8);
  text("Wayne Fan 12.04.2006", width - 110, height - 5);
}

Project 3.3: