import processing.core.*; import java.applet.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.text.*; import java.util.*; import java.util.zip.*; public class blockvaders extends PApplet {tankclass tank;
HashSet enemies = new HashSet();
HashSet deadenemies = new HashSet();
HashSet badbullets = new HashSet();
boolean gameover;

int nextEnemyDue;
int nextEnemyDueReset = 300;

int score = 0;

int playerLives = 3;
public void playerKilled(){
    tank.hitcolors = 100;
    playerLives --;
  if(playerLives <= 0){
    gameover = true; 

    } else {

    badbullets.clear();
    }
}

public void mouseReleased(){
if(gameover){
     gameover = false; 
      enemies.clear();
      badbullets.clear();
     tank.bullets.clear();
     nextEnemyDueReset = 300;
     nextEnemyDue = nextEnemyDueReset;
      enemyStartSpeed = .1f;
 chanceOfFire = 100;
 playerLives = 3;
 score = 0;
}
}


public void setup(){
  size(400,400,P3D);
  rectMode(CENTER);
  tank = new tankclass();
  gameover = false;
  addEnemy();
  framerate(50);
  
  textFont(loadFont("Arial-Black-14.vlw"),14);
}

float enemyStartSpeed = .1f;
float chanceOfFire = 100;

public void addEnemy(){
  enemies.add(new enemyclass(enemyStartSpeed,chanceOfFire));
  if(chanceOfFire > 2){
    chanceOfFire -=3;
  }
  enemyStartSpeed += .03f;



}


public void keyPressed(){
  if(keyCode==37){
    tank.xs= -1;
  }
  if(keyCode==39){
    tank.xs = 1;
  }
  if(keyCode==38){
    tank.ys = -1;
  }
  if(keyCode==40){
    tank.ys = 1;
  }
  if(keyCode==32 || keyCode == 17){
    tank.fire();
  }
  // println(keyCode); 
}


float xAngle = 0;
float yAngle = 0;
float zAngle = 0;
float x=0,y=0;


public void draw(){




  background(128);

  Iterator i;
  if(! gameover){
    nextEnemyDue--;
    if( nextEnemyDue <= 0){
      addEnemy();
      nextEnemyDue = nextEnemyDueReset;
      if(nextEnemyDueReset > 20) nextEnemyDueReset -= 3;
//println(nextEnemyDueReset);

    }



    tank.move();
    tank.bounds();


    i = enemies.iterator();
    while(i.hasNext()){
      enemyclass enemy  =(enemyclass) i.next();
      enemy.move();
      enemy.bounds();

      if(enemy.h >= 90){
        gameover = true; 
      }

      if(enemy.checkfire()){
        badbullets.add(new badbulletclass(enemy.x,enemy.y,enemy.h)); 
      }
    }


    i = deadenemies.iterator();
    while(i.hasNext()){
      deadenemyclass enemy  =(deadenemyclass) i.next();
      enemy.move();
      enemy.bounds();
    }
boolean playerKilled = false;
    i = badbullets.iterator();
    while(i.hasNext()){
      badbulletclass badbullet  =(badbulletclass) i.next();
      badbullet.move();
      if(badbullet.h > 100){
        i.remove(); 
      }

      if(  badbullet.hit(tank)){
          playerKilled = true;
      }

      // badbullet.bounds();
    }

    if(playerKilled){
         playerKilled(); 
    }
  yAngle += .0002f;
  } //not gameover so move shit



  fill(0,128);
  stroke(200);

  //"camera" translation
  translate(200,200,0);


  if(! gameover){
    noFill();
  } else {
     fill(0,255,0,30); 
  }
  rotateY(yAngle);


  //xAngle += .0001;

  //zAngle += .0001;
  box(200,200,200);

stroke(0,128);
fill(0,128);
text("score: "+score+"  lives:"+playerLives,-100,-100,0);
if(gameover){
text("GAME OVER   click to restart",-100,-75,0);
}

  tank.show();

  i = enemies.iterator();
  while(i.hasNext()){
    enemyclass enemy  =(enemyclass) i.next();
    enemy.show();

  }

  i = badbullets.iterator();
  while(i.hasNext()){
    badbulletclass badbullet  =(badbulletclass) i.next();
    badbullet.show();

  }

  i = deadenemies.iterator();
  while(i.hasNext()){
    deadenemyclass enemy  =(deadenemyclass) i.next();
    enemy.show();
  }


}





class deadenemyclass{
  public float x,y,h,xs,ys,hs;

  deadenemyclass(float px,float py, float ph){

    x = px;
    y = py;
    h = ph;
    hs = -10;
  }
  float s = 20;
  public void show(){
    pushMatrix();
    stroke(0,200);
    noFill();
    translate(x,h,y);
    box(s,s,s);
    popMatrix();

  }


  public void move(){
    h += hs;
  }


  public void bounds(){
    if(h < -200){
//      println("whoo");
      deadenemies.remove(this); 
    } 
  }


}




class enemyclass{
  public float x,y,h,xs,ys,hs;



  enemyclass(float phs, float pr){
    h = -90; 
    x = random(160) - 80;
    y = random(160) - 80;
    hs = phs;

    r = pr;
  }
  float r;
  float s = 20;
  public void show(){
    pushMatrix();
    stroke(0,200);
    fill(150,255,150,128);
    translate(x,h,y);
    box(s,s,s);
    popMatrix();

    pushMatrix();
    noStroke();
    fill(0,10);
    translate(x,100,y);
    box(s,0,s);
    popMatrix();

  }

  public boolean checkfire(){
    if(PApplet.toInt(random(r))==0){
      return true;

    }
    return false;
  }

  public void move(){
    h += hs;
    xs += (random(2)-1)/10;
    ys += (random(2)-1)/10;
    x+=xs;
    y += ys;
  }



  public void bounds(){
    if(x < -90) {  
      x = -90; 
      xs = xs * -.5f;
    }
    if(x > 90) { 
      x = 90;
      xs = xs * -.5f;
    }
    if(y < -90) {
      y = -90;  
      ys = ys * -.5f; 
    }
    if(y > 90) { 
      y = 90;
      ys = ys * -.5f;
    }

  }


}

public boolean overlap(float line1val1,float line1val2,float line2val1,float line2val2){
  float line1min = (line1val1 < line1val2)?line1val1:line1val2;
  float line1max = (line1val1 > line1val2)?line1val1:line1val2;
  float line2min = (line1val1 < line2val2)?line2val1:line2val2;
  float line2max = (line1val1 > line2val2)?line2val1:line2val2;

  if(line1min < line2min && line1max < line2min){
    return false; 
  }
  if(line1min > line2max && line1max > line2max){
    return false; 
  }  


  return true;
}

class badbulletclass{

  public  float x,y, h;
  badbulletclass(float px,float py,float ph){
    x = px;
    y = py;
    h = ph;
  }
  float  s = 20;
  public void move(){
    h += 1;
  }

  public boolean hit(tankclass t){
    if(h < 90){
      return false; 
    }
    if(! overlap(x-10,x+10,tank.x-10,tank.x+10)){
      return false;
    }
    if(! overlap(y-10,y+10,tank.y-10,tank.y+10)){
      return false;
    }


    return true;
  }

  public void show(){
    pushMatrix();
    stroke(255,200);
    fill(255,150,150,128);
    translate(x,h,y);
    box(s,0,s);
    popMatrix();
    //shadow
    /*
    pushMatrix();
     noStroke();
     fill(255,150,150,50);
     translate(x,100,y);
     box(s/2,0,s/2);
     popMatrix();
     */
  }

}


class bulletclass{
  public  float x,y, h;
  public boolean hit(enemyclass e){
    if(! overlap(h,h,e.h-10,e.h+10)){
      return false;
    }
    if(! overlap(x-10,x+10,e.x-10,e.x+10)){
      return false;
    }
    if(! overlap(y-10,y+10,e.y-10,e.y+10)){
      return false;
    }
    //  println("OUCH" + random(10000));
    return true;
  }



  bulletclass(float px,float py){
    x = px;
    y = py;
    h = 95;
  }
  float  s = 20;
  public void move(){
    h -= 2;
  }

  public void show(){
    pushMatrix();
    stroke(255,200);
    fill(150,150,255,128);
    translate(x,h,y);
    box(s,0,s);
    popMatrix();
  }

}

class tankclass {
  public float x,y,xs,ys; 

  public HashSet bullets = new HashSet();
  public void move(){
    x += xs;
    y += ys;

    Iterator i = bullets.iterator();
    while(i.hasNext()){
      bulletclass bullet  =(bulletclass) i.next();
      bullet.move();
      if(bullet.h < -100){
        i.remove();
      }
      Iterator j = enemies.iterator();
      while(j.hasNext()){
        enemyclass enemy  =(enemyclass) j.next();
        if(bullet.hit(enemy)){
          deadenemies.add(new deadenemyclass(enemy.x,enemy.y,enemy.h));
score++;
                    j.remove();
          //TODO score
        }
      }      

    }


  }

  public void fire(){
    bullets.add(new bulletclass(x,y)); 
  }
public float hitcolors = 255;
  float s = 20;

  public void show(){
    pushMatrix();
    stroke(255,200);
    
    if(hitcolors < 255){
       hitcolors++; 
    }
    
    fill(255,hitcolors,hitcolors,128);
    translate(x,90,y);
    box(s,s,s);
    popMatrix();

    Iterator i = bullets.iterator();
    while(i.hasNext()){
      bulletclass bullet  =(bulletclass) i.next();
      bullet.show();
    }

  }




  public void bounds(){
    if(x < -90) {  
      x = -90;
    }
    if(x > 90) { 
      x = 90;
    }
    if(y < -90) {
      y = -90;   
    }
    if(y > 90) { 
      y = 90;
    }
  }

}


static public void main(String args[]) {   PApplet.main(new String[] { "blockvaders" });}}