//Doodle Jump :o
float x, y;
float vy = 10;
float a = 0.5;
ArrayList<Platform> platforms = new ArrayList<Platform>();
ArrayList<Enemy> enemies = new ArrayList<Enemy>();
ArrayList<Spring> springs = new ArrayList<Spring>();
ArrayList<Bullet> bullets = new ArrayList<Bullet>();
PImage aliendude = loadImage("http://img1.wikia.nocookie.net/__cb20130403202424/doodle-jump/images/5/5c/Doodler.png");
PImage bad = loadImage ("http://icons.iconarchive.com/icons/snaky-reports/summer-sweets/32/Pudding-icon.png");
PImage spring = loadImage("http://i.imgur.com/n6oSv7H.jpg");
boolean experimentjump, left, right;
int screen = 2;
int score = 0;
int hscore = 0;
int kscore = 0;
float bulletSpeed = 15;
class Bullet {
  float x, y, vx, vy;
  Bullet( float x, float y, float vx, float vy ) {
    this.x = x;
    this.y = y;
    this.vx = vx;
    this.vy = vy;
  }
  void move() {
    x += vx;  
    y += vy;
  }
  void display() {
    fill(255, 0, 0);
    ellipse(x, y - scrolly, 5, 5);
  }
}
class Platform { 
  float x, y;
  Platform (float x, float y) { 
    this.x= x;
    this.y = y;
  }
  void display() {
    fill(0, 255, 100);
    rect(x, y - scrolly, 60, 10);
  }
}
class Spring { 
  float x, y;
  Spring (float x, float y) { 
    this.x= x;
    this.y = y;
  }
  void display() {
    image(spring, x, y-scrolly, 20, 20);
  }
}
class Enemy {
  float x, y, vx, vy;
  Enemy ( float x, float y) {
    this.x = x;
    this.y = y;
  }
  void display() { 
    image(bad, x, y- scrolly);
  }
}
void setup() { 
  size(400, 500);
  // spring.resize(20, 20);
  //bad.resize(50,50);
  y = 150;
  x = 200;  
  for (int i = 0; i<30; i++) {
    platforms.add(new Platform( random(0, width), random(0, height)));
  }
  rectMode(CENTER);
  imageMode(CENTER);
}
int scrolly = 0;
void draw() {
  if (screen == 3) {
    background(255);
    fill(0);
    text("Oh nose! You dead!", width/3, height/2.5);
    text("Score was: "+score+". Click to restart", width/4, height/2);
    if (mousePressed) {
      screen = 2;
      scrolly=0;
      x = 45;
      y = 45;
      bullets.clear();
    }
  }
  else {
    if (screen == 2) {
      background( 255, 255, 255);
      text("Score: "+score, width-100, 30);
      image(aliendude, x, y - scrolly, 45, 45);
      hscore = Math.round(-scrolly/10);
      score = hscore + kscore;
      y += vy;
      vy += a;
      println(scrolly);
      if (y - scrolly < height / 2 && vy < 0) {
        scrolly += vy;
        float platformProb = 0;
        if (hscore < 200) {
          platformProb = 0.2;
        } 
        else if (hscore < 400) {
          platformProb = 0.19;
        } 
        else if (hscore < 1000) {
          platformProb = 0.175;
        } 
        else {
          platformProb = 0.155;
        }
        if (random(0, 1) < platformProb) {
          float platformX = random(0, width);
          float platformY = random(scrolly, scrolly - height);
          platforms.add(new Platform(platformX, platformY));
          if (random(0, 1) < 0.1) {
            enemies.add(new Enemy (platformX, platformY -10));
          }
          if (random(0, 1) < 0.1 ) {
            springs.add(new Spring (platformX, platformY -10));
          }
        }
      }
      if (x > 400) x = 0;
      if (x < 0) x = 400;     
      if (y - scrolly > 500) screen = 3;
    }
    for (int i = 0 ; i < bullets.size(); i++) {
      Bullet b = bullets.get(i);
      b.move();
      b.display();
      //      if ( b.y < 0 ) bullets.remove(i);
    }
    for (int i = 0 ; i < platforms.size(); i++) {
      Platform p = platforms.get(i);
      p.display();
      if ( p.y > height ) platforms.remove(i); 
      if ( p.y - 5 <= y + 45 /2 && y + 45 / 2 <= p.y + 5 && abs(x - p.x) < (45 + 60) / 2) {
        if (vy > 0) {
          vy = -14;
        }
      }
    }
    for (int i = 0 ; i < springs.size(); i++) {
      Spring s = springs.get(i);
      s.display();
      if ( s.y > height ) springs.remove(i); 
      if ( s.y - 5 <= y + 45 /2 && y + 45 / 2 <= s.y + 5 && abs(x - s.x) < (45 + 60) / 2) {
        if (vy > 0) {
          vy = -30;
        }
      }
    }
    for (int i = 0 ; i < enemies.size(); i++) {
      Enemy e = enemies.get(i);
      e.display();
      if ( e.y > height ) {
        enemies.remove(i);
      }
      if ( e.y - 5 <= y + 45 /2 && y + 45 / 2 <= e.y + 5 && abs(x - e.x) < (45 + 60) / 2) {
        if (vy > 0) {
          vy = -20;
          enemies.remove(i);
          kscore+=100;
        }
      }
      else {
        for (int j = 0 ; j < bullets.size(); j++) {
          Bullet b = bullets.get(j);
          if (abs(b.x-e.x) < 20 && abs(b.y-e.y) < 20) {
            enemies.remove(i);
            kscore+=100;
            break;
          }
        }
      }
    }
    if (mousePressed && frameCount % (60*0.2) == 0) {
      float dx = mouseX - x;
      float dy = mouseY - y + scrolly;
      float hyp = sqrt(dx * dx + dy * dy);
      bullets.add( new Bullet( x+20, y, bulletSpeed * dx / hyp, bulletSpeed * dy / hyp) );
    }
  }
  if (left) x-= 5;
  if (right) x+= 5;
  if (experimentjump) vy=-14;
}
void keyPressed() { 
  if (key==' ') experimentjump = true;
  if (key=='a') left = true; 
  if (key=='d') right = true;
}
void keyReleased() { 
  if (key == ' ') experimentjump = false;
  if (key=='a') left = false;
  if (key=='d') right = false;
}