import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
Minim minim;

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

//AudioPlayer music;
AudioPlayer fanfare;

AudioSnippet[] scratch = new AudioSnippet[SAMPLECOUNT];
void loadSounds(){
  minim = new Minim(this);

 
   for(int i = 0; i < SAMPLECOUNT; i++){
    scratch[i] = minim.loadSnippet("yell"+(i%12)+".wav");  
  }
//  music = minim.loadFile("bgmusic.wav");
  fanfare = minim.loadFile("aff.mp3");
}

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

void winMusic(){
 fanfare.pause();
  fanfare.rewind();
   fanfare.play(); 
}

void scratch(){
  int i = int(random(SAMPLECOUNT));
  AudioSnippet s = scratch[i];
  s.pause();
  s.rewind();
  s.play();
}
void closeSounds(){
  
   for(int i = 0; i < SAMPLECOUNT; i++){
    scratch[i].close();
  }
  
//  music.close();
//  fanfare.close();
  
}














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

int gameMode = CARD;
PImage cardImage;

PImage handback;
PImage thumb;
PImage fingers;
PImage heman;
PImage spiderman;
PImage optimus;
PImage snakeeyes;
PImage openhand;

void setup(){
   size(500,500); 
   loadSounds();
   cardImage = loadImage("smallbill.jpg");

   handback = loadImage("handback.png");
   thumb = loadImage("thumb.png");
   fingers = loadImage("fingers.png");
   heman = loadImage("heman.png");
   spiderman = loadImage("spiderman.png");
   optimus = loadImage("optimus.png");
   snakeeyes = loadImage("snakeeyes.png");
   openhand = loadImage("openhand.png");
   //startGame();
}

void draw(){
   if(gameMode == CARD){
      drawCard();
   } 
   if(gameMode == TITLE_BEFORE || gameMode == TITLE_AFTER){
      drawTitle();
   } 
   if(gameMode == PLAY){
      play(); 
   }
}


void mousePressed(){
   if(gameMode == CARD){
      gameMode = TITLE_BEFORE;
      winMusic();
      return;
   } 
    if(gameMode == TITLE_BEFORE || gameMode == TITLE_AFTER){
      boolean left = true;
      boolean up = true;
      if(mouseX >= 250) left = false;
      if(mouseY >= 250) up = false;
      int area = 0;
      if(up && left) area = 1;
      if(up && !left) area = 2;
      if(!up && left) area = 3;
      if(!up && !left) area = 4;
      
     // image(getImage(area),150,0);    
      
      int enemy = area;
      while(enemy == area){
         enemy = floor(random(4))+1; 
      }
      
      startGame(area,enemy);
      return;
    }
}

String getName(int a){
   switch(a) {
     case 1: return "snakeeyes";
     case 2: return "heman";
     case 3: return "spiderman";
     case 4: return "optimus";

   }
   return "???";
}
PImage getImage(int a){
   switch(a) {
     case 1: return snakeeyes;
     case 2: return heman;
     case 3: return spiderman;
     case 4: return optimus;

   }
   return null;
}




Hand leftHand;
Hand rightHand;
int fightMode;
int messageTime;
String winner;
void startGame(int hero,int enemy){
   gameMode = PLAY; 
   leftHand = new Hand(LEFT,getImage(hero),getName(hero));
   rightHand = new Hand(RIGHT,getImage(enemy),getName(enemy));
   fightMode = TITLE_BEFORE;
   messageTime = 100;
}

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

void drawTitle(){
  background(255);
   
   
   pushMatrix();
   translate(100,120);
   if(mouseX < 250 && mouseY < 250) rotate(sin(frameCount/5.0)/10);
   image(snakeeyes,-60,-110,123,210);
   popMatrix();
   

   pushMatrix();
   translate(350,120);
   if(mouseX >= 250 && mouseY < 250) rotate(sin(frameCount/5.0)/10);
   image(heman,-70,-110,146,210);
   popMatrix();

   pushMatrix();
   translate(120,350);
   if(mouseX < 250 && mouseY >= 250) rotate(sin(frameCount/5.0)/10);
   image(spiderman,-65,-105,131,210);
   popMatrix();
   
   pushMatrix();
   translate(375,350);
   if(mouseX >= 250 && mouseY >= 250) rotate(sin(frameCount/5.0)/10);
   image(optimus,-106,-105,213,210);
   popMatrix();
   

   
   
   
   textSize(50);
   textAlign(CENTER,CENTER);
   fill(random(150,255),0,0);
   text("action figure fighter",0,-50,500,500);
   textSize(20);
   
   text("choose your warrior",0,125,500,250);
   
   
   

}

float enemyUpVal = 0;

float enemyLeftVal = 0;
void play(){
   background(255);
  
  
  textAlign(CENTER,CENTER);
  textSize(20);
  
  
  noStroke();
  fill(128);
  rect(0,10,200,20);
  rect(300,10,200,20);
   fill(200,0,0);
  float lh = constrain(map(leftHand.health,0,1000,0,200),0,200);
  float rh = constrain(map(rightHand.health,0,1000,0,200),0,200);
  rect(0,10,lh,20);
  rect(500-rh,10,rh,20);
  
  
 fill(0);
  text(leftHand.who,0,0,200,30);
  text(rightHand.who,300,0,200,30);
   fill(200,0,0);
  
  
  
  textSize(30);
  text("VS",200,-5,100,40);
  
  
  enemyUpVal += .1;
  enemyLeftVal += .2;
 
  
  
  
  rightHand.draw(100+15*sin(enemyLeftVal)+15*cos(enemyLeftVal/3)+15*sin(2*(enemyLeftVal+.8)),250+5*sin(enemyUpVal)+5*cos(enemyUpVal/3)+5*sin(2*(enemyUpVal+.8)));
  leftHand.draw(mouseX,mouseY);;
  
  if(leftHand.health > 0 && rightHand.health > 0){
  
    if(rightHand.delta > 2){
       if(random(100)<10) scratch();
    leftHand.health -= rightHand.delta * leftHand.vuln/2;
  }
  if(leftHand.delta > 10){
   if(random(100)<10) scratch();
     rightHand.health -= leftHand.delta * rightHand.vuln/4;
  }
  }
  fill(0);
  
  
   // rotate(map(mouseX,0,250,-PI/2,PI/2));
  
  if(fightMode == PLAY){
     if(leftHand.health <= 0 || rightHand.health <=0){
       winMusic();
         fightMode = TITLE_AFTER;
         messageTime = 300;
       if(leftHand.health <= 0 && rightHand.health <=0){
         winner = "no one ";
       }else {
         if(leftHand.health <= 0) winner = rightHand.who;
         else  winner = leftHand.who;
       } 
       
     }
  }
  
 
  if(fightMode == TITLE_BEFORE){
    textSize(100);
    fill(random(150,255),50,50);
      text("FIGHT!",0,0,500,500);
      messageTime--;
      if(messageTime <= 0) fightMode = PLAY;
  }  
  if(fightMode == TITLE_AFTER){
    textSize(50);
    fill(random(150,255),50,50);
      String s = winner+" wins!";
      text(s.toUpperCase(),0,0,500,500);
      messageTime--;
      if(messageTime <= 0) gameMode = TITLE_AFTER;
  }  


 // line(0,0,mouseX,mouseY);
   
}



class Hand{
  
  float health = 1000;
  float drop,dropspeed;
  int dir;
  PImage hero;
  String who;
    Hand(int pdir, PImage phero,String pwho){
      dir = pdir;
      hero = phero;
      who = pwho;
    }
  float lastX = -1;
  float delta;
  float vuln;
  
  void draw(float xval,float yval){
 
      if(fightMode != PLAY) {
        xval = 100;
       yval = 300; 
      }
    if( lastX == -1) lastX = xval;
    delta = xval - lastX ;
    lastX = xval;
    
    
       xval = constrain(xval,0,200);
       vuln = map(xval,0,200,.1,1);
       
       float a = map(xval,0,500,-PI/4,PI/2);
        pushMatrix();
        float offset = -100;
        if(dir == RIGHT) {
          offset = 100;
           xval = 500 - xval;       
        }
 
      
      translate(xval+offset,yval);
        if(dir == RIGHT) scale(-1,1);
     
       rotate(a);
 
     if(health >0){  
       image(handback,-200,-250);
       image(hero,-9,-224);
       image(thumb,-39,-170);
       image(fingers,128,-84);
     } else {
       image(openhand,-200,-250);
              image(hero,-9,-224+drop);
              dropspeed += .1;
              drop+=dropspeed;
              
     }
       popMatrix();
    
  }
  
  
}