import pphys2d.bodies.*;
import pphys2d.joints.*;
import pphys2d.shapes.*;
import pphys2d.phys2d.raw.collide.*;
import pphys2d.phys2d.raw.strategies.*;
import pphys2d.phys2d.raw.forcesource.*;
import pphys2d.phys2d.util.*;
import pphys2d.phys2d.raw.shapes.*;
import pphys2d.*;
import pphys2d.phys2d.raw.*;
import pphys2d.phys2d.math.*;

int PREGAME = 0;
int GAME = 1;
int POSTGAME = 2;
int gamemode = PREGAME;

int score = 0;

PPWorld world;
Hammer h;
Diamond d;
HashSet bees = new HashSet();
void setup(){
  size(500,500);
  textFont(createFont("",24),24);
  world  = new PPWorld();

  h = new Hammer();
  d = new Diamond();
  world.add(h);
  world.add(d);


}
void draw(){
  background(128,128,255);
  if(gamemode == PREGAME || gamemode == POSTGAME) {
    drawPregame();
  }
  //  if(gamemode == GAME || gamemode == POSTGAME){

  world.draw(this);
  showscore();
  h.draw();
  int addcounter = 0;
  Iterator i = bees.iterator();
  while(i.hasNext()){
    Bee b = (Bee)i.next(); 

    b.draw();
    if(b.move()) {
      addcounter += 2; 
    }
  }
  h.move();

  while(addcounter  > 0){
    addBee(); 
    addcounter--;
  }
  //  }

  if(gamemode == GAME){
    score++;
  }
  if(d.getX()< 0 || d.getX()>500 || d.getY() <0 || d.getY()> 500){
    gamemode = POSTGAME; 
  }
}



class Diamond  extends PPBox{
  Diamond(){
    super(30,30);
    setRotation(PI/4);
    setPosition(250,250);
    setDamping(10);
    setRotDamping(10);
    setFillColor(new Color(255,255,255));
    setStrokeColor(new Color(0,0,0));
    setImageAlpha(200);
  }


}


class Hammer extends PPBox{
  Hammer(){
    super(40,40); 
    setFillColor(new Color(128,128,128));
    setStrokeColor(new Color(0,0,0));
    setRotDamping(5);
  }
  void move(){
    float deltaX, deltaY;
    deltaX =( mouseX - getX() )/5; 
    deltaY = (mouseY - getY() )/5; 

    setVelocity(getVelocityX() + deltaX,getVelocityY()+deltaY);
    setVelocity(getVelocityX()*.99,getVelocityY()*.99);


  }
  void draw(){
    strokeWeight(2);
    stroke(0);
    line(getX(),getY(),mouseX,mouseY); 
  }
}



void mouseClicked(){
  if(gamemode == PREGAME || gamemode == POSTGAME){
    resetGame();
  } 
}
float BEESIZE =20;
class Bee extends PPCircle{
  Bee(){
    super(BEESIZE);
    setDrawable(false);
    
    
  }
  boolean dead = false;
  float counter ;
  float wingoff;
  void draw(){
    strokeWeight(2);
    stroke(0);
    pushMatrix();
    translate(getX(),getY());
    rotate(getRotation());
    
    noFill();
    ellipse(-3,-3+wingoff,15,15);
    ellipse(3,-3+wingoff,15,15);

    if(dead) {fill(128); }
    else {
      fill(255,255,0);
      wingoff = sin(counter+=.3)*5;  
  }
    line(0,0,-BEESIZE/2,-BEESIZE/2);
    line(0,0,BEESIZE/2,-BEESIZE/2);
    
    ellipse(0,0,BEESIZE,BEESIZE);
    line(-BEESIZE/2,0,BEESIZE/2,0);
    line(-BEESIZE/2,BEESIZE/4,BEESIZE/2,BEESIZE/4);
    popMatrix();
  }
  boolean move(){
    if(dead) return false;
    float deltaX, deltaY;
    float mul = 2;
    if(getX() < d.getX()){
      deltaX = 1;
    } 
    else{
      deltaX = -1;       
    }
    if(getY() < d.getY()){
      deltaY = 1;
    } 
    else{
      deltaY = -1;       
    }
    setVelocity(getVelocityX() + deltaX*mul,getVelocityY()+deltaY*mul);

    if(isTouchingBody(h)){
      //        world.remove(this);
      //   return true;

      dead = true; 
      return true; 
    }
    return false;
  }
}




void addBee(){
  Bee b = new Bee();

  float a = random(PI* 20);

  b.setPosition(250+(400*cos(a)),250+(400*sin(a)));
  world.add(b);
  bees.add(b);
}

void resetGame(){
  score = 0;
  world.clear();
  h = new Hammer();
  d = new Diamond();
  world.add(d);
  world.setGravity(0,0);
  world.add(h);
  bees = new HashSet();
  gamemode = GAME;
  for(int i = 0; i < 7; i++){
    addBee();
  }
}

void showscore(){
  fill(0);
  rectMode(CORNER);
  textAlign(CENTER);
  text("score:"+score,0,0,500,50);
}

void drawPregame(){
  fill(0);
  rectMode(CORNER);
  textAlign(CENTER);
  if(gamemode == PREGAME){
    text("BEEBASH\n\nthe bees! the bees! they're trying to steal your precious diamond. use your yoyo hammer of justice to fend them off - but bee warned for every one you kill two bees shall rise up to take its place\n\nclick to play",50,100,400,400);
  } else {
    text("BEEBASH\n\nthe bees! the bees! they stole your precious diamond.\n\nclick to play again",50,100,400,400);
    
  }
}
void drawPostgame(){

}

HashSet keysDown = new HashSet();
void keyPressed(){
  this.keysDown.add(this.keyEvent.getKeyCode());
}
void keyReleased(){
  this.keysDown.remove(this.keyEvent.getKeyCode());  
}
boolean isKeyDown(int keyCode){
  if(keysDown.contains(keyCode)) return true;
  return false; 
}