String gameState;

//herramientas de juego
PImage macri;
PImage dolar;

float tamaniomacri = 0;
float anchopaddle = 70;

//movimiento MACRI
float X = 0;
float Y = 0;

//velocidad MACRI
float velocidadX = 2;
float velocidadY = 0;

//contador
int hit;
int miss;

//fondo
PImage img;
int direccion = 1;
float senial;

float xPos, yPos;
float pixelSize;

//fuente
PFont miFuente1;

void setup() {

  //musica
  minim = new Minim(this);
  player = minim.loadFile("FlyingMicrotonalBanana.mp3");
  player.play();

  //lienzo
  size(800, 600);
  frameRate(35);

  //contador
  hit = 0;
  miss = 0;

  //imagenes
  img = loadImage("Fondo.jpg");
  macri = loadImage("MACRI.png");
  dolar = loadImage("DOLAR.png");

  //fuente
  miFuente1 = createFont("Minecraft", 30);

  //estado
  gameState = "START";
}

void draw () {

  //ESTADO
  if (gameState == "START") {
    startGame();
  } else if (gameState == "PLAY") {
    playGame();
  } else if (gameState == "WIN") {
    winGame();
  } else if (gameState == "LOSE") {
    loseGame();
  }
}

void startGame() {
  background(184, 255, 102);
  textAlign(CENTER);
  textFont(miFuente1);
  fill(173, 102, 255);
  textSize(50);
  text("PRESS ENTER TO START", width/2, height/2);

  if (keyCode==ENTER) {
    gameState = "PLAY";
  }
}

void playGame() {

  //COMO SE MUEVEN HERRAMIENTAS DE JUEGO
  X += velocidadX;
  Y += velocidadY;

  float paddle = 1000 / (hit+10); // Se achica la barra donde rebota la pelota


  // rebota lateralmente
  if (X < 0 || X > width) velocidadX = -velocidadX;

  // si Y es mayor al alto de la pantalla no deberia rebotar...
  // ... deberia por ejemplo, aparecer por arriba
  if (Y > height && velocidadY > 0) {

    Y = -50;
    velocidadY = 1;

    miss += 1;
  } else {
    velocidadY += 1;
  }

  // deberia revisar la distancia con la paleta si Y 
  // es A: menor al alto de la pantalla, 
  // y es B: mayor a la posicion de la paleta
  if (Y < height && Y > height-90) {
    // y rebotar si X    
    // es A: mayor al X inicial de la paleta
    // es B: mayor al X final de la paleta
    if (X > mouseX && X < mouseX+2*paddle) {
      hit +=1;
      velocidadY = -velocidadY;
    }
  }


  // FONDO CAMBIA DE COLOR
  if (senial > img.width*img.height-1 || senial < 0) { 
    direccion = direccion * -1;
  }

  if (mouseButton == RIGHT) {
    int mx = constrain(mouseX, 0, img.width-1);
    int my = constrain(mouseY, 0, img.height-1);
    senial = my*img.width + mx;
  } else {
    senial += 0.33*direccion;
  }

  int sx = int(senial) % img.width;
  int sy = int(senial) / img.width;

  if (keyPressed) {
    set(0, 0, img);
    point(sx, sy);
    rect(sx - 5, sy - 5, 10, 10);
  } else {
    color c = img.get(sx, sy);
    background(c);
  }

  //HERRAMIENTAS DE JUEGO

  //dolar
  imageMode(CORNER); 
  image(dolar, mouseX, height-90, 2*paddle, anchopaddle);

  //macri 
  pushMatrix(); 
  imageMode(CENTER); 
  translate(X, Y); 
  rotate(frameCount % 360 * 0.1); 
  image(macri, 0, 0); 
  popMatrix();

  //CONTADOR
  textFont(miFuente1, 25);
  fill(255);
  text("Move the mouse to play.", 600, 50);
  text("HIT: " + hit, 70, 60);
  text("MISS: " + miss, 70, 100);

  textFont(miFuente1, 20);
  fill(220, 20, 60);
  text("Want more fun?", 600, 80);
  text("Press right click while playing!", 600, 100);


  if (hit == 10) {
    gameState = "WIN";
  }

  if (miss == 10) {
    gameState = "LOSE";
  }
}

void winGame () {

  //pelotas de colores
  pixelSize = 100;
  strokeWeight(pixelSize);
  stroke(random(255), random(255), random(255));    
  point (xPos, yPos);
  xPos += pixelSize;

  if (xPos > width) {
    stroke(random(255), random(255), random(255));
    xPos = 0;
    yPos += pixelSize;
  }
  if (yPos > height ) {
    yPos = 0;
  }  

  textSize(60);
  fill(255, 255, 36);
  textAlign(CENTER);
  text("CONGRATS!", width/2, height/2);

  textSize(20);
  fill(0);
  text("Left click to restart game.", 300, 200);


  if (mouseButton == LEFT) {
    resetGame();
    gameState = "PLAY";
  }
}

void loseGame () {
  background(255, 140, 0);
  textSize(60);
  fill(255);
  textAlign(CENTER);
  fill(0, 115, 255);
  text("YOU LOST...", width/2, height/2);

  textSize(20);
  fill(223, 255, 0);
  text("Left click to restart game.", 300, 200);

  if (mouseButton == LEFT) {
    resetGame();
    gameState = "PLAY";
  }
}

void resetGame() {
  hit = 0;
  miss = 0;
}