class Info {
    String textWin;
    String textGameOver;
    
    Info () {
       this.textWin = "La biotecnologia en mis\ncultivos y tu habilidad\nnos pusieron a salvo\nde las Plagas";
       this.textGameOver = "Gracias a la\nbiotecnologia, las perdidas\nno fueron grandes.\nTe espero en el\nproximo juego";
    }
  
      void menu () {
              image(menuBg, width/2, height/2);
              ScrollText();
              image(menuBgMk, width/2, height/2);
              renderStarField();
              menuTitlePic.display();
      
          if (mouseOver.pic(menuTitlePic)) {
                menuTitlePic2.display();
      
                if (clickOnce) {
                  menuOn = false;
                  menuInfoOn = true;
                  clickOnce = false;
                  playSound(clickSetting);
                }
          } else { menuTitlePic.display();  }
      
          if (mouseOver.pic(menuLogoPic)) { 
            menuLogoPic2.display();
            if (clickOnce) { 
                      goToLink(); 
                      clickOnce = false;
                      playSound(clickSetting);
                    }
          } else { menuLogoPic.display();  }
          
           invSeqASprite.x = 700;
           invSeqBSprite.x = invSeqASprite.x+50;
           invSeqCSprite.x = invSeqBSprite.x+50;
           invSeqASprite.loop(); invSeqASprite.update(); invSeqASprite.draw();
           invSeqBSprite.loop(); invSeqBSprite.update(); invSeqBSprite.draw();
           invSeqCSprite.loop(); invSeqCSprite.update(); invSeqCSprite.draw();
      }
      
      void menuInfo() {
          image(menuInfo, width/2, height/2);
          noStroke();
          if (mouseOver.rectan(575, 280, 200, 40)) {       /////// PRINCIPIANTE BUTTON
                fill(128,100,128);
                if (clickOnce) {
                    playSound(clickSetting);
                    restart();
                    avanzado = false;
                    menuInfoOn = false;
                    clickOnce = false;
                    playing = true;
                    limitShoot = 3;
                    invadersSpeed = 1.13;
                    clock = new Clock();
                    
                }
          } else { fill(128,0,128); }
          
          rect(575, 280, 200, 40);
              
          if (mouseOver.rectan(575, 325, 200, 40)) {        /////// EXPERTO BUTTON
              fill(128,100,128);
              if (clickOnce) {
                  playSound(clickSetting);
                  restart();
                  avanzado = true;
                  menuInfoOn = false;
                  clickOnce = false;
                  playing = true;
                  limitShoot = 1;
                  invadersSpeed = 1.18;
                  clock = new Clock();
                  
              }
          } else { fill(128,0,128);  }
          
          rect(575, 325, 200, 40);
          fill(255);
          textSize(18);
          text("Principiante", 575, 288);
          text("Avanzado", 575, 332);
      }
      
      void game() {
          fill(255);
          textFont(myFont);
          textSize(16);
          text("<PUNTOS: " + dead + ">" , 95, 30);
          text("<TIEMPO: " + clock.elapsed + ">" , 395, 30);
          if (avanzado) { text("Avanzado", 650, 30); } else { text("Principiante", 650, 30); }
          //text("<CREDITO: " + credit + ">" , 850, 30);
          fill(210,105,30);
          if (credit > 0) { 
              for(int a = 0; a < credit; a++) {
                  image(granjeroPicture, 850+(30*a), 27, granjeroPicture.width/2, granjeroPicture.height/2);
              }
          }
          image(refugioTxt, 495, 400, refugioTxt.width/2, refugioTxt.height/2);
      }
        
      void winner() {
          slideEnd.display(textWin);
        }
        
      void gameOver() {
          slideEnd.display(textGameOver);
      }
}


class Slide {
    
    int points;
    float[][] posRand;
    
    Picture winnerBgPic;
    Picture end_globoPic;
    Picture end_baseTransp;
    Picture hombreAlegrePic;
    Picture hombreTristePic;
    Picture choclos1Pic;
    Picture choclos2Pic;
    
    Slide() {
        this.points = 200;
        this.posRand = new float[points][3];
    }
    
    void init() {
        winnerBgPic = new Picture(winnerBg, width/2, height/2);
        end_globoPic = new Picture(end_globo, 507, 150);
        end_baseTransp = new Picture(end_base, 525, 340, end_base.width/1.4, end_base.height/1.4);
        hombreAlegrePic = new Picture(hombreAlegre, 237, 396);
        hombreTristePic = new Picture(hombreTriste, 237, 396);
        choclos1Pic = new Picture(choclos1, 752, 353);
        choclos2Pic = new Picture(choclos2, 752, 353);
        nube1Pic = new Picture(nube1, 160, 105);
        nube2Pic = new Picture(nube2, 743, 93);
        nube3Pic = new Picture(nube1, 1100, 93);
        nube4Pic = new Picture(nube1, 1400, 193);

        for (int y = 0; y < points; y++) {
            float [] pos = {random(0, width), random(0, height/2), random(0.5,4)};
            for (int x = 0; x < 3; x++) {
                posRand[y][x] = pos[x];
            }
        }   
    }

    void display(String txt) {

        image(bg, width/2, height/2);
        fill(150,140,255);  
        ////////// ESTRELLAS
        for (int i = 0; i < points; i++) {
            float m = 1;
            if (i % 10 == 0) { m = random(1,2); }
            rect(posRand[i][0], posRand[i][1], posRand[i][2] * m, posRand[i][2] * m);
        }

        if (nube2Pic.x >= width || nube2Pic.x <= 0) {   /////// NUBES
            xdirMove *= -1;
        }
        
        nube1Pic.x += xdirMove;
        nube2Pic.x += xdirMove * 1.6;
        nube3Pic.x += xdirMove;
        nube4Pic.x += xdirMove;
        nube1Pic.display();
        nube2Pic.display();
        nube3Pic.display();
        nube4Pic.display();
        if (mouseOver.pic(choclos1Pic)) { choclos1Pic.display();} else { choclos2Pic.display(); }
        end_baseTransp.display();

        if (mouseOver.pic(menuLogoBPic)) { 
      menuLogoBPic2.display();
      if (clickOnce) { 
                goToLink();
                clickOnce = false;
                playSound(click);
            }
    } else { menuLogoBPic.display();  }

    if (mouseOver.pic(menuLogoCPic)) { 
      menuLogoCPic2.display();
      if (clickOnce) { 
                goToLink();
                clickOnce = false;
                playSound(click);
            }
    } else { menuLogoCPic.display();  }

        if (gameOver) { hombreTristePic.display(); } else { hombreAlegrePic.display(); }
        
        end_globoPic.display();
        noStroke();
        fill(128,0,128);
        rect(605-158, 325, 110, 40);
        rect(605, 325, 110, 40);
        textAlign(CENTER);
        fill(255,200,0);
        textSize(10);
        textLeading(12);
        text("PLAGAS\nCONTROLADAS: ", 450, 363);
        text("TIEMPO\nDE JUEGO: ", 610, 363);
        //rect(523, 410, 233, 25);
        if (mouseOver.rectan(523, 410, 233, 25)) {
            textSize(26);
            if (clickOnce) {
                clickOnce = false;
                menuOn = false;
                menuInfoOn = true;
                clickOnce = false;
                playSound(click);
            }
        } else { textSize(24);  }
        text("<VOLVE A JUGAR>", 525, 419);
        textSize(24);
        text("TU PUNTAJE: ", 530, 293);
        
        if (!gameOver) { fill(25,160,25); }
        if (gameOver) { text("NO ES TAN GRAVE", 530, 100); } else { text("GANASTE", 530, 100); }
        fill(255,70,0);
        textSize(18);
        textLeading(26);
        text(txt, 530, 128);
        fill(255,255,255);
        textSize(18);
        text("FIN DE ESTA PARTIDA", 820, 53);
        text(dead, 450, 333);  ////////////// PUNTAJE
        text(clock.elapsed, 606, 333);
    }
}



class Clock {
    // this.hr;
    // this.mn;
    int sc;
    int lastSec;
    int totalSec;
    int elapsed;
    String total_hr;
    String total_mn;
    String total_sc;
    String elapsedString;
    
    void update() {
        // this.hr = hour();
        // this.mn = minute();
        this.sc = second();
        if (this.sc != lastSec) { totalSec++; }
        lastSec = this.sc;
        total_hr = pad(floor(totalSec / 3600));
        totalSec %= 3600;
        total_mn = pad(floor(totalSec / 60));
        total_sc = pad(totalSec % 60);
        elapsed = totalSec;
        elapsedString = total_hr + ":"+ total_mn + ":" + total_sc;
    }
}

String pad(int number) {
  if (number < 10) { return str('0' + number); } else { return str(number); } 
}

public class Picture {
    float x, y;
    float w, h;
    PImage pic;
    
    Picture(PImage pic, float x, float y) {
        this(pic, x, y, pic.width, pic.height); 
    }
    Picture(PImage pic, float x, float y, float w, float h) {
        this.x = x;
        this.y = y;
        this.pic = pic;
        this.w = w; 
        this.h = h;
    }
    void display() {
        image(this.pic, this.x, this.y, this.w, this.h);
    }
}


class MouseOver {
    float x;
    float xx;
    float y;
    float yy;

    boolean rectan(float cx, float cy, float cw, float ch) {
        this.x = cx - cw/2;
        this.xx = cx + cw/2;
        this.y = cy - ch/2;
        this.yy = cy + ch/2;
    
        if ((mouseX > this.x && mouseX < this.xx && mouseY > this.y && mouseY < this.yy)) {
            // if (roll) { playSound(rollOver); roll = false; }
            return true;
        } else {  return false; }
    }
    
    boolean pic(Picture img) {
          
          this.x = img.x - img.w/2;
          this.xx = img.x + img.w/2;
          this.y = img.y - img.h/2;
          this.yy = img.y + img.h/2;
        
          if ((mouseX > this.x && mouseX < this.xx && mouseY > this.y && mouseY < this.yy)) {
              return true;
          }  else { return false; }
    }
}

void soundInvaders(){
    long currentMillis = millis();
    
    if (currentMillis - previousMillis >= interval) {
        // save the last time you blinked the LED
        previousMillis = currentMillis;
        if (moveSound[playIndex].isPlaying() == false) {
            playSound(moveSound[playIndex]);
        }
    }
}

void playSound(AudioPlayer sound) {
      sound.pause();
      sound.rewind();
      sound.play();
}

void mousePressed() {
    clickOnce = true;
    //console.log("clickOnce: " + clickOnce  );
}


void mouseReleased() {
    clickOnce = false;
    //console.log("clickOnce: " + clickOnce );
}

void goToLink() {
  link("https://www.argentina.gob.ar/agroindustria"); 
}
