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;

HashSet boxes = new HashSet();

PPWorld world = new PPWorld();
Thing t1;
;
Thing t2;
void setup(){
  size(500,500);
  textFont(createFont("",24),24);




}
static    Color gr = new Color(200,200,200);
static  Color bl = new Color(0,0,0);
static Color BLUE = new Color(0,0,200);
static Color RED = new Color(200,0,0);
class Box extends PPBox {
  int loc  = 0;
  Box(float x, float y, float w, float h){
    super(w,h);
    setFillColor(gr);
    setStrokeColor(bl);
    setPosition(x,y);

  }
  void check(){
    if(loc == 0){
      if(getX() < 125){
        setFillColor(RED);
        loc = -1;
      }
      if(getX() > 375){
        setFillColor(BLUE);
        loc = 1;
      }
    } else {
       if(getX() > 125 && getX() < 375){
          loc = 0;
                  setFillColor(gr);
       } 
    }
  }


}
    int redscore = 0;
    int bluescore = 0;


String winmsg; color wincolor;
void draw(){
  background(128);
  if(gamemode == PREGAME || gamemode == POSTGAME) {
    drawPregame();
  }
  if(gamemode == POSTGAME){
    drawPostgame();
  }

  noStroke();
  rectMode(CORNER);
  fill(255,200,200);
  rect(0,0,125,500);
  fill(200,200,255);
  rect(375,0,125,500);

  if(gamemode == GAME  || gamemode == POSTGAME){



    t1.move();
    t2.move();

    world.draw(this);
    /* if(mousePressed){
     mouseDragged(); 
     }*/
      if(gamemode == GAME){
     redscore = bluescore = 0;
    Iterator i  = boxes.iterator();
    while(i.hasNext()){
      Box b = (Box)i.next(); 
      b.check();
      if(b.loc == -1){
        redscore++; 
      }
      if(b.loc == 1){
        bluescore++; 
      }
    } //iterator
      } // is game
    if(bluescore >= 8 || redscore >= 8){
       gamemode = POSTGAME;
      if(bluescore > redscore) {
         winmsg = "BLUE WINS!";
         wincolor = color(0,0,200);
      } 
      if(bluescore < redscore) {
         winmsg = "RED WINS!";
         wincolor = color(200,0,0);
      } 
      if(bluescore == redscore) {
         winmsg = "IT'S A TIE?!";
         wincolor = color(0,0,0);
      } 
      
    }
    
  }//game going 
  rectMode(CORNER);
  textAlign(CENTER);
  fill(200,0,0);
  text(""+redscore,0,125,125,50);
  fill(0,0,200);
  text(""+bluescore,375,125,125,50);
  if(gamemode == POSTGAME){
  fill(wincolor);
    text(winmsg ,0,250,500,50);
  }
  
}//draw
float POWA = 800000;

float startX,startY;
/*`
 void mousePressed(){
 startX = mouseX;
 startY = mouseY;
 }
 void mouseDragged(){
 float midX = (startX+mouseX)/2;
 float midY = (startY+mouseY)/2;
 float w = abs(startX-mouseX);
 float h = abs(startY-mouseY);
 rectMode(CENTER);
 stroke(255);
 noFill();
 rect(midX,midY,w,h);
 
 }*/
int rInt(){
  return floor(random(255));
}/*
void mouseReleased(){
 float midX = (startX+mouseX)/2;
 float midY = (startY+mouseY)/2;
 float w = abs(startX-mouseX);
 float h = abs(startY-mouseY);
 
 PPBox box = new PPBox(w,h);
 box.setFillColor(new Color(rInt(),rInt(),rInt()));
 box.setStrokeColor(new Color(0,0,0));
 box.setPosition(midX,midY);
 world.add(box);
 }*/

class Thing extends PPBox{
  int myUp,myLeft,myRight;
  Thing(int what){
   super(10,40);



    
    if(what == 1){
      setPosition(100,450);
      setFillColor(new Color(250,0,0));   
      myUp = 87;
      myLeft = 65;
      myRight = 68; 
      attachImage(loadImage("redrocket1.gif"));
    } 
    else {
      setPosition(400,450);    
      setFillColor(new Color(0,0,250));    
      myUp = 38;
      myLeft = 37;
      myRight = 39;
      attachImage(loadImage("bluerocket1.gif"));

    }

    setRestitution(.5);

  }
  void move(){
    setAngularVelocity(0);
    if(isKeyDown(myUp)){
      float rotation = getRotation()-PI/2.0;
      float force = 40000.0f;
      addForce(cos(rotation)*force, sin(rotation)*force); 
    }
    if(isKeyDown(myLeft)){//left{
      setRotation(getRotation()-.1);

    }
    if(isKeyDown(myRight)){//right
      setRotation(getRotation()+.1);

    }

  }


}


void resetGame(){
  world.clear();
  world.setEdges(this, new Color (0,0,0));  

  boxes = new HashSet();
  t1 = new Thing(-1);
  t2 = new Thing(1);
  world.add(t1);
  world.add(t2);
  world.setEdgesRestitution(.8);
  float ptr = 480;
  while(ptr > 0){
    float h = random(20,50);
    float w = random(20,100);
    Box b = new Box(250,ptr - h/2,w,h);
    ptr -= h;
    world.add(b);
    boxes.add(b);
  }
}

void drawPregame(){
  fill(0);
  rectMode(CORNER);
  textAlign(CENTER);
  text("ROCKET DORKICUS\n\nbring as many\nBLOCKS\nto your side\nas you can.\n\nfirst to 8 wins\n\n<-W A S D    \n    arrow keys->\nspace starts",50,0,400,500);
}
void drawPostgame(){

}

HashSet keysDown = new HashSet();
void keyPressed(){
  int k = this.keyEvent.getKeyCode();
  if(k == 32&&gamemode != GAME ) {
    resetGame();
    gamemode = GAME;
  }
  this.keysDown.add(k);
}
void keyReleased(){
  this.keysDown.remove(this.keyEvent.getKeyCode());  
}
boolean isKeyDown(int keyCode){
  if(keysDown.contains(keyCode)) return true;
  return false; 
}