import ddf.minim.*;

Minim minim;

void stop(){
  // always close Minim audio classes when you are done with them :-)
  closeSounds();
  minim.stop();
  super.stop();
}
int SAMPLECOUNT = 4;

AudioPlayer music;
AudioSnippet robot_boom;
AudioSnippet buzz;
AudioSnippet punch;
AudioSnippet swoosh;

void loadSounds(){
  minim = new Minim(this);
  music = minim.loadFile("bgmusic.mp3");
//    music = minim.loadFile("cjammer.wav");
  
 buzz = minim.loadSnippet("buzz.wav");
punch = minim.loadSnippet("punch.wav");
 swoosh = minim.loadSnippet("swoosh.wav");

}
void startMusic(){
   music.loop(); 
}



void fx(AudioSnippet a){
   a.pause();a.rewind();a.play(); 
}
void fx_buzz() { fx(buzz); }
void fx_punch() { fx(punch); }
void fx_swoosh() { fx(swoosh); }


void closeSounds(){
  buzz.close();
  punch.close();
  swoosh.close();
  music.close();

  
}









int CARD = 1;
int TITLE_BEFORE = 2;
int PLAY = 3;
int TITLE_AFTER;

ArrayList<Bee>bees = new ArrayList<Bee>();

int forcedWait;

ArrayList<Bee>fallingBees = new ArrayList<Bee>();
int gameMode = CARD;
PImage cardImage;
int killCount;
int lives;
Dino d;
void setup(){
   size(500,500); 
   cardImage = loadImage("smallbill.jpg");
//   startGame();
    d = new Dino();
//   noCursor();
loadSounds();
}

void draw(){
  if(forcedWait > 0) forcedWait--;
   if(gameMode == CARD){
      drawCard();
   } 
   if(gameMode == TITLE_BEFORE || gameMode == TITLE_AFTER){
      drawTitle();
   } 
   if(gameMode == PLAY){
    play();
   }
   
   if(gameMode == PLAY || gameMode == TITLE_AFTER){
      showScore(); 
   }
   
}


void showScore(){
   fill(0);stroke(0); strokeWeight(2);
   textAlign(CENTER,CENTER);
  text("score:"+killCount,0,GROUND,250,100);
if(gameMode == PLAY){
  text("lives:"+lives,250,GROUND,250,100);
}
}

int ticks;
void startGame(){
     gameMode = PLAY;
     ticks = 0;
    bees = new ArrayList<Bee>();
    fallingBees = new ArrayList<Bee>();
killCount = 0;
lives = 3;
}

void mousePressed(){
   if(gameMode == CARD){
      gameMode = TITLE_BEFORE;
      startMusic();
      return;
   } 
    if(gameMode == TITLE_BEFORE || gameMode == TITLE_AFTER){
      if(forcedWait <= 0){
        startGame();
        return;
      }
    }
    if(gameMode == PLAY){
       d.isPunch = true; 
       //bees.add(new Bee());
    }
}

void drawCard(){
  background(200);
    textAlign(CENTER);
    fill(0);
    image(cardImage,202,150);
   text("an alienbill.com /",250,145);
   text("kirkjerk joint\n\n\naudio by\nrenzo heredia\n\n\n\n(click)",250,310);
}

void drawTitle(){
   textAlign(CENTER,CENTER);

  background(50,50,100); 
noStroke();
fill(100,50,0);
rect(0,GROUND,500,500-GROUND);
fill(0);
textSize(20);
text("click to play",0,100,500,200);
textSize(50);
   text("DINOBEEBOXER",0,0,500,500);
   
}

int flash = 0;

void play(){
  ticks++;
  
  if(bees.size() == 0) bees.add(new Bee());
  
  if(random(100) < 1) bees.add(new Bee());

  
  if(flash > 0){
   background(100,100,50); 
    flash--;
  } else {
   background(50,50,100); 
  }

noStroke();
fill(100,50,0);
rect(0,GROUND,500,500-GROUND);
//println();
d.draw();
for(Bee b:bees){
  b.move();  
  b.draw();
}
for(Bee b:fallingBees){
  b.fall();  
  b.draw();
}

     ArrayList<Bee>toKill = new ArrayList<Bee>();
     for(Bee b : bees){
       if(b.x < -10){
        flash = 40; 
        toKill.add(b);
        lives--;
        fx_buzz();
        if(lives <= 0){
           gameMode = TITLE_AFTER;
          forcedWait = 250; 
        }
       }
     }
    bees.removeAll(toKill);


}

class Dino{
  float x,y;
      float fistX,  fistY;
boolean isPunch;
   Dino(){
    x = mouseX;
    y = mouseY;
   }
   
  boolean hit(Bee b){
     if(dist(fistX,fistY,b.x,b.y) < 25+16) return true;
    return false; 
  }
   
  void draw(){
    float MOVE = .5;
    if(x < mouseX) x+=MOVE;
    if(x > mouseX) x-=MOVE;
    if(y < mouseY) y+=MOVE;
    if(y > mouseY) y-=MOVE;
    
    x = constrain(x,180,220);
    //println(mouseY);
    y = constrain(y,235,280);
    color lightGreen = color(50,200,50);
    color darkGreen = color(25,150,25);
    stroke(lightGreen);
  strokeWeight(50);
    
    
    float baseOfTailX = x - 50;
    float baseOfTailY = y + 50;
    
    float a = map(mouseY,0,500,-PI,PI/2);
    //println(a);
    //neck
    float neckX = baseOfTailX+cos(a)*60; 
    float neckY = baseOfTailY+sin(a)*60;
    line(neckX,neckY,baseOfTailX,baseOfTailY);
    
    float tailLength = pow(120,2);;
     strokeWeight(40);
    float tailXLength = sqrt(abs(tailLength - pow(baseOfTailY - GROUND,2)));
    
    line(baseOfTailX,baseOfTailY,baseOfTailX-tailXLength,GROUND);
    line(baseOfTailX-tailXLength,GROUND,baseOfTailX-tailXLength-30,GROUND);
    
     strokeWeight(30);
     
    stroke(darkGreen);
     //top leg to knee
    line(baseOfTailX,baseOfTailY,baseOfTailX+(tailXLength/2),(GROUND + baseOfTailY) / 2);
    
    //kee down to ground
    line(baseOfTailX+(tailXLength/2),(GROUND + baseOfTailY) / 2,baseOfTailX+(tailXLength/2),GROUND+10);
    line(baseOfTailX+(tailXLength/2)+30,GROUND+10,baseOfTailX+(tailXLength/2),GROUND+10);
    line(baseOfTailX+(tailXLength/2)-30,GROUND+10,baseOfTailX+(tailXLength/2),GROUND+10);
    
    stroke(lightGreen);
    //line(fistX,fistY);
    //head
    rect(neckX,neckY-40,50,30);
    stroke(150,0,0);
    triangle(neckX+20,neckY-20,neckX+25,neckY-30,neckX+20,neckY-20);
    
    stroke(darkGreen);
     //arm
     if(mousePressed){
      fistX  = neckX+80;
      fistY = neckY;
      line(neckX,neckY,fistX,fistY);
     }else{ 
    line(neckX,neckY,neckX-20,neckY+20);
     fistX = neckX+20;
     fistY = neckY+20;
    line(neckX-20,neckY+20,fistX,fistY);
     } 
    fill(150,0,0);
    stroke(150,0,0);
    ellipse(fistX,fistY,25,25);
   
   if(isPunch){
     boolean playHit = false;
   
     isPunch = false;
     ArrayList<Bee>toKill = new ArrayList<Bee>();
     for(Bee b : bees){
       
       if(hit(b)){
         playHit = true;
       b.bam();
       killCount ++;
         toKill.add(b); 
       }
     }
     bees.removeAll(toKill);
     fallingBees.addAll(toKill);
   if(playHit) fx_punch();
 else fx_swoosh();  
 }
 
   
} 
  
}


float GROUND = 400;




float beeSpeed = 1;


class Bee{
  float x, y, speed;
  boolean dead = false;
  float c1,c2,c3,c4,a;
  
   Bee(){
    x = 500; 
    y = random(250,GROUND);
    
    
    beeSpeed += .03;
    beeSpeed = constrain(beeSpeed,1,5);
   // beeSpeed = 5;
    
    speed = -beeSpeed;
    
    
    c1 = random(-1,1);
    c2 = random(-2,2);
    c3 = random(-3,3);
    c4 = random(-1,1);
    
   } 
   
   void move(){
     a += c1;
      x += speed + c2*cos(a) ;
      y += c3*sin(a) ;
     //y = mouseY; x  
   }
  float ys;
  void bam(){
    
    dead = true;
    ys = random(-3,3);
  }
  void fall(){
   x += 3;
   ys += .1;
    y += ys;
  }
  
  
   void draw(){
   
    strokeWeight(2);
     stroke(0);
     pushMatrix();
     translate(x,y);
//    print(dead+" ");
     if(dead){
        rotate(x); 
     }
     line(0,0,0+12,0);   
     noStroke();
     fill(255,255,0);
     ellipse(0,0,16,16);
     stroke(0);
     line(0-4,0-6,0-4,0+6);
     line(0,0-8,0,0+8);
     line(0+4,0-6,0+4,0+6);
       popMatrix();
   }
}