import ddf.minim.*;
final  Minim minim = new Minim(this);
AudioPlayer click;
AudioPlayer clickSetting;
AudioPlayer rollOver;
AudioPlayer winSound;
AudioPlayer loseSound;
AudioPlayer shootSound;
AudioPlayer invaderDieSound;
AudioPlayer hitSound;
AudioPlayer[] moveSound;

PFont myFont;
PImage bg;
PImage nube1;
PImage nube2;
PImage menuBg;
PImage menuBgMk;
PImage menuRef;
PImage menuTitle;
PImage menuTitle2;
PImage menuLogo;
PImage menuLogo2;
PImage menuInfo;
PImage loseBg;
PImage txtInicial;
PImage choclitos;
PImage refugioTxt;
PImage end_base;
PImage end_globo;
PImage end_Bg;
PImage winnerBg;
PImage hombreAlegre;
PImage hombreTriste;
PImage choclos1;
PImage choclos2;
PImage chocloImg;
float xdir = 1;
float speed = 1;

AnimatedSprite txtIntroSprite;
AnimatedSprite invSeqASprite;
AnimatedSprite invSeqBSprite;
AnimatedSprite invSeqCSprite;
AnimatedSprite invSeqDSprite;

AnimatedSprite[] invSpriteGrp = new AnimatedSprite[4*12];
AnimatedSprite granjeroFront;
AnimatedSprite granjeroWalk;
AnimatedSprite granjeroWalkShoot;
AnimatedSprite granjeroStandShoot;
AnimatedSprite granjeroSprite;

Granjero granjero;
Invasores invasores;
Assets assets;
Choclo choclo;

//ArrayList<Choclo> choclos;
ArrayList<Choclo> choclos = new ArrayList();
ArrayList<Misil> misiles = new ArrayList();

////////////////////////////// GAME VAR
boolean playing = false;
boolean menuOn = true;
boolean gameOver = false;
boolean menuInfoOn = true;
boolean win = false;
boolean stand = true;
boolean edge = false;
boolean misilExist = true;

int[] numbers = new int[8];
float xdirMove = 0.1;
int elapsed = 0;
int lapse = 1000;
int chocloCorrec = 24;
    
int limitShoot = 1;
float invadersSpeed = 1.18;
////////////////////////////// INFO VAR
boolean avanzado = true;
boolean clickOnce = false;

//String[] txtString;
PGraphics graphText;
int txtFrames = 1;
// I create an array named "stars",
// it will be filled with 800 elements made with the Star() class.
Star[] stars = new Star[800];
// I create a variable "speed", it"ll be useful to control the speed of stars.

///////////////////////////// ROLL OVER IMAGES
Picture menuTitlePic;
Picture menuTitlePic2;
Picture menuLogoPic;
Picture menuLogoPic2;
Picture menuLogoBPic;
Picture menuLogoBPic2;
Picture menuLogoCPic;
Picture menuLogoCPic2;
Picture nube1Pic;
Picture nube2Pic;
Picture nube3Pic;
Picture nube4Pic;

MouseOver mouseOver;

long lastTime = 0;

void setup() {
    settings();
    background(0,255,255);
    lastTime = millis();
    mouseOver = new MouseOver();
    //graphText = createGraphics(960, 960, P3D);
    //myFont = loadFont("font/space_invaders.ttf");  
    myFont = createFont("font/space_invaders.ttf", 32);
   
    bg = loadImage("img/_Game_BG.jpg");
    nube1 = loadImage("img/nube1.png");
    nube2 = loadImage("img/nube2.png");
    menuBg = loadImage("img/_Menu_BG.jpg");
    menuBgMk = loadImage("img/mk.png");
    menuRef = loadImage("img/_Menu.jpg");
    
    menuTitle = loadImage("img/_Menu_Title.png");
    menuTitle2 = loadImage("img/_Menu_Title2.png");
    menuTitlePic = new Picture(menuTitle, 482, 160);
    menuTitlePic2 = new Picture(menuTitle2, 482, 160);
    
    menuLogo = loadImage("img/_Menu_PresNac.png"); 
    menuLogo2 = loadImage("img/_Menu_PresNac2.png");
    menuLogoPic = new Picture(menuLogo, 476, 474, menuLogo.width/1.2, menuLogo.height/1.3);
    menuLogoPic2 = new Picture(menuLogo2, 476, 474, menuLogo2.width/1.2, menuLogo2.height/1.3);
    menuLogoCPic = new Picture(menuLogo, 171, 53, menuLogo.width/2, menuLogo.height/2);
    menuLogoCPic2 = new Picture(menuLogo2, 171, 53, menuLogo2.width/2, menuLogo2.height/2);
    menuLogoBPic = new Picture(menuLogo, 528, 483, menuLogo.width/2, menuLogo.height/2);
    menuLogoBPic2 = new Picture(menuLogo2, 528, 483, menuLogo2.width/2, menuLogo2.height/2);
    
    menuInfo = loadImage("img/_MenuInfo.jpg");
    loseBg = loadImage("img/_Lose.jpg");
    winnerBg = loadImage("img/_Win.jpg");
    
    end_base = loadImage("img/baseTransp.png");
    end_globo = loadImage("img/globo.png");
    end_Bg = loadImage("img/_End_BG.jpg");
    winnerBg = loadImage("img/_Win.jpg");
    hombreAlegre = loadImage("img/hombreAlegre.png");
    hombreTriste = loadImage("img/hombreTriste.png");
    chocloImg = loadImage("img/choclo.png");
    choclos1 = loadImage("img/choclos1.png");
    choclos2 = loadImage("img/choclos2.png");
    txtInicial = loadImage("img/textoInicial.png");
    choclitos = loadImage("img/chocloBarra.png");
    refugioTxt = loadImage("img/refugio.png");
    
    ////////////////////////////////// SOUND
    //minim = new Minim(this);
    click =  minim.loadFile("sound/Click.wav");
    //clickSetting = minim.loadFile("sound/ClickSetting.mp3");
    //rollOver =  minim.loadFile("sound/RollOver.mp3");
    //winSound =  minim.loadFile("sound/WinSound.mp3");
    //loseSound =  minim.loadFile("sound/LoseSound.mp3");
    //shootSound = minim.loadFile("sound/Destroy.wav");
    //invaderDieSound = minim.loadFile("sound/FireEq.mp3");
    //hitSound = minim.loadFile("sound/Die.wav");
    //for(int i = 0; i < 4; i++) {
    //    moveSound[i] = minim.loadFile("sound/"+(i+1)+".wav");
    //}
    
    //////////////////////////////////////////  ANIMACIONES 
    float sizeMult = 1.2;

    
    PImage[] txtIntroImgs = new PImage[txtFrames];
    for (int i = 0; i < txtFrames; i++) {
      String imageName = "img/txt/txt_" + nf(i, 4) + ".png";
      //println(imageName);
      //txtString = append(txtString, name); // Add "maple" to the end
      txtIntroImgs[i] = loadImage(imageName);
      //println((i%2)==0);
      if ((i%2)==0) { text("Loading...", width/2, height/2); }
      else { text("Loading", width/2, height/2); }
    }
    //loadAnimation(txtString, txtIntroImgs);
    txtIntroSprite = new AnimatedSprite(txtIntroImgs,  12, width/2,height/2,txtIntroImgs[0].width, txtIntroImgs[0].height);
    
    String[] invSeqA = {"img/inv0.png", "img/inv1.png"};
    PImage[] invSeqAImgs = new PImage[invSeqA.length];
    loadAnimation(invSeqA, invSeqAImgs);
    invSeqASprite = new AnimatedSprite(invSeqAImgs,  6, width/2, 60,invSeqAImgs[0].width*sizeMult, invSeqAImgs[0].height*sizeMult );
    
    String[] invSeqB = {"img/inv2.png", "img/inv3.png"};
    PImage[] invSeqBImgs = new PImage[invSeqB.length];
    loadAnimation(invSeqB, invSeqBImgs);
    invSeqBSprite = new AnimatedSprite(invSeqBImgs, 6, width/2, 60,invSeqAImgs[0].width*sizeMult, invSeqAImgs[0].height*sizeMult);
    
    String[] invSeqC = {"img/inv4.png", "img/inv5.png"};
    PImage[] invSeqCImgs = new PImage[invSeqC.length];
    loadAnimation(invSeqC, invSeqCImgs);
    invSeqCSprite = new AnimatedSprite(invSeqCImgs, 6, width/2, 60,invSeqAImgs[0].width*sizeMult, invSeqAImgs[0].height*sizeMult);
    
    String[] invSeqD = {"img/inv6.png", "img/inv7.png"};
    PImage[] invSeqDImgs = new PImage[invSeqD.length];
    loadAnimation(invSeqD, invSeqDImgs);
    invSeqDSprite = new AnimatedSprite(invSeqDImgs, 6, width/2, 60,invSeqAImgs[0].width*sizeMult, invSeqAImgs[0].height*sizeMult);
    
    invasores = new Invasores();
    invasores.init();
    granjero = new Granjero(width/2);
    granjero.init();
    
    assets = new Assets();
    assets.init();
    
    //choclos = new ArrayList<Choclo>();
    //String[] invColors = [[255,85,0,255], [255,0,106,255], [241,185,0,255],[0,85,231,255]];
    //String[] secuencias = [invSeqA, invSeqB, invSeqC, invSeqD];]]
    
    //invSpriteGrp = append(invSpriteGrp, invSeqASprite); 
    
    //invSpriteArray = {invSeqASprite, invSeqBSprite, invSeqCSprite, invSeqDSprite};
    
    String[] vaquitaWalkSeq = {"img/vaquitaWalk1.png", "img/vaquitaWalk2.png"};
    String[] vaquitaStandSeq = {"img/vaquitaStand1.png", "img/vaquitaStand2.png"};
    String[] abejaWalkSeq = {"img/Abeja_Camina1.png", "img/Abeja_Camina2.png"};
    String[] abejaFlySeq = {"img/Abeja_Vuela1.png", "img/Abeja_Vuela2.png"};
    String[] perroStandSeq = {"img/PerroStand1.png", "img/PerroStand2.png", "img/PerroStand3.png"};
    String[] perroWalkSeq = {"img/PerroWalk1.png", "img/PerroWalk2.png", "img/PerroWalk3.png"};
    String[] perroBarkUpSeq = {"img/PerroLadraArriba1.png", "img/PerroLadraArriba2.png", "img/PerroLadraArriba3.png"};
    String[] perroBarkSeq = {"img/PerroLadraAbajo1.png", "img/PerroLadraAbajo2.png", "img/PerroLadraAbajo3.png"};
    
    nube1Pic = new Picture(nube1, 160, 105);
    nube2Pic = new Picture(nube2, 743, 93);
    nube3Pic = new Picture(nube1, 1100, 93);
    nube4Pic = new Picture(nube1, 1400, 193);
    //refugioPic = new Picture(refugioTxt, 456, 360,  refugioTxt.width/2, refugioTxt.height/2);
    //menuRefPic = new Picture(menuRef, width/2, height/2);

    //menuTitlePic = new Picture(menuTitle, 482, 160);
    //menuTitlePic2 = new Picture(menuTitle2, 482, 160);

    //menuLogoPic = new Picture(menuLogo, 476, 474, menuLogo.width/1.2, menuLogo.height/1.3);
    //menuLogoPic2 = new Picture(menuLogo2, 476, 474, menuLogo2.width/1.2, menuLogo2.height/1.3);

    //menuLogoCPic = new Picture(menuLogo, 171, 53, menuLogo.width/2, menuLogo.height/2);
    //menuLogoCPic2 = new Picture(menuLogo2, 171, 53, menuLogo2.width/2, menuLogo2.height/2);

    //menuLogoBPic = new Picture(menuLogo, 528, 483, menuLogo.width/2, menuLogo.height/2);
    //menuLogoBPic2 = new Picture(menuLogo2, 528, 483, menuLogo2.width/2, menuLogo2.height/2);

    //menuBgPic = new Picture(menuBg, width/2, height/2);
    //menuInfoBg = new Picture(menuInfo, width/2, height/2);
    //lose = new Picture(loseBg, width/2, height/2);
    //winnerBgPic = new Picture(winnerBg, width/2, height/2);
    
    for (int i = 0; i < stars.length; i++) {
        stars[i] = new Star();
    }
    //info = new Info();
}



void draw() {
  
  //background(bg);
  textFont(myFont);
  noStroke();
  imageMode(CENTER);
  rectMode(CENTER);
  textAlign(CENTER);

  if (playing) {
        game();
        //abejas();
        //drawSprites();                    //// RENDER ANIMACIONES
        //if (bloodOn) { explo.circle(granjeroSprite.position.x,granjeroSprite.position.y, 10); }
        //if (PixelOnce) { Fire(invPosX, invPosY, 50, invColor); }        //// EXPLOTA INVASOR INSECTO
        //clock.update();
     
   } else {
          if ( menuOn ) {
             menu();
          }
          else if ( menuInfoOn ) {
              menuInfo();
          }
          //else if ( gameOver ) {  
          //    gameOver();
          //}
          //else if ( win ) {
          //    winner();
          //}
  }
}
boolean armedOn = true;
boolean onFire = true;
boolean hitGranOn = false;
boolean loseOnce = true;
boolean bloodOn = true;
boolean hitOnce= true;
int dead = 0;
int credit = 3;

void restart() {
    //fireworks = [];
    //misiles = [];
    //choclos = [];
    xdir = 1;
    xdir *= -1;
    speed = 1;
    dead = 0;
    credit = 3;
    bloodOn = false;
    hitOnce = false;
    //allSprites.clear();
    //allSprites.remove();
    //granjero.init();
    //assets.init();
    //invasores.init();
    //BoidsInit();

    //for(let a = 0; a < 4; a++) {
    //    refugios[a] = new Refugio(200*a + 170, 350, 10);
    //    refugios[a].init();
    //}

    loseOnce = true;
    gameOver = false;
    win = false;
    hitGranOn = false;
    onFire = false;
    menuOn = false;
    menuInfoOn = false;
    playing = true;
}

void youLose() {
    hitGranOn = true;                   //// DETIENE A LOS INSECTOS
    //loseSound.amp(0.6);
    //loseSound.play();
    delay(1000);
}


void youWin() {
    winSound.play();
    delay(1000);
}


void winner() {
    win = true;
    playing = false;
    gameOver = false;
}


void theEnd() {
    playing = false;
    win = false;
    gameOver = true;
}


//boolean delay_(int range) {
//    if ( millis() - lastTime > range ) {
//        println( "Pasaron: " + range + " milisegundos." );
//        lastTime = millis();
//        return true;
//    } else { return false; }
//}


void keyPressed() {
    switch(keyCode) {

        case 37:        ////LEFT
            granjero.moveLeft = true;
            granjeroSprite = granjeroWalk;
            dogSprite = perroWalk;
            break;
        case 39:    ////// RIGHT
            granjero.moveRight = true;
            granjeroSprite = granjeroWalk;
            dogSprite = perroWalk;
            break;
        case 32:         ///// SPACEBAR
            if(armedOn) {
                granjero.shoot();
            }
            break; 
        case 38:        /////    UP ARROW
                granjero.shoot();
            break;
        case 13:                ////// ENTER
            if (gameOver || win) { restart(); }
            //moveOn = !moveOn;
            break;
    }
}


void keyReleased() {
    switch(keyCode) {
        case 37:
            granjero.moveLeft = false;
            break;
        case 39:
            granjero.moveRight = false;
            break;
        //case (37 || 39):
        //    break;

    }

    if (!granjero.moveLeft && !granjero.moveRight) {
       granjeroSprite = granjeroFront;
       
        granjeroSprite = granjeroFront;
        dogSprite = perroStand;
        stand = true;
    }
}



void settings() {
  size(960, 540);
}
