import processing.core.*; 
import processing.data.*; 
import processing.event.*; 
import processing.opengl.*; 

import java.util.HashMap; 
import java.util.ArrayList; 
import java.io.File; 
import java.io.BufferedReader; 
import java.io.PrintWriter; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.IOException; 

public class sketch_201117a_jeu_pour_projet extends PApplet {

int gameScreen = 0;
int ballX, ballY;
int ballSize =20;
int ballColor = color(0);
obstacle [] listeObstacle;

float gravity = 1;
float ballSpeedVert = 0;

int compteur = 0;
int compteurTotale =0;
PImage bg;
PImage ig;
PImage ug;


public void setup(){
  
  bg = loadImage("gamestart.jpg");
  ig = loadImage("mainscreen.jpg");
  ug = loadImage("gameover.jpg");
  
  ballX = width/4;
  ballY = height/5;
  
  listeObstacle = new obstacle [20];
}



public void draw(){
  if (gameScreen == 0){
    initScreen();
  }
  
  else if (gameScreen ==1 ){
     gameScreen();
    
    
  }
  
  else if (gameScreen ==2){
   gameOverScreen();
   }
}



public void initScreen(){
   background(bg);
  textAlign (CENTER);
  text("Cliquer Pour Commencer", height/2, width/2);
  
}


public void gameOverScreen()
{
  background(ug);
}
  
  
public void mousePressed(){
  if (gameScreen ==0){
    gameScreen =1;
  }
}






public void gameScreen(){
  background(ig);
  creationMur();
  
  drawRaquette();
  drawBall();
  makeBounceRaquette();
  
  for(int i = 0;  i < listeObstacle.length ; i += 1){
    if(listeObstacle[i] != null){
      listeObstacle[i].drawObstacle();
      listeObstacle[i].deplacement();
      
    }
  }
  int nbrObstacle=0;
   for(int i = 0;  i < listeObstacle.length ; i += 1){
     nbrObstacle+=1;
      if(listeObstacle[i] != null){
      
        if( (listeObstacle[i].obstacleX <= ballX) && ( listeObstacle[i].obstacleY<=ballY) && (ballY < listeObstacle[i].sizeY + listeObstacle[i].obstacleY ) )
         {
         
        background(ug);
        //gameOverScreen();

         }
                  textAlign (CENTER);
    text("Score:"+nbrObstacle, height/2, width/2);
          fill(255,255,255);
       }

    }
   
  applyGravity();
  keepInScreen();
 
  
  if(ballSpeedVert == 0 && ballY > height - 20){
    gameScreen = 2;
  }
  
  compteur += 1;
}




public void drawBall(){
  fill (ballColor);
  ellipse(ballX, ballY, ballSize, ballSize);
}



public void drawRaquette(){
  rect(mouseX-20, mouseY, 50, 5);
}






public void creationMur(){
  
  if (compteur>=60){
    listeObstacle[compteurTotale] = new obstacle();
    compteur = 0;
    compteurTotale +=1;
    
    if(compteurTotale >= listeObstacle.length){
      compteurTotale = 0;
    }
    
  }
}





public void applyGravity(){
  ballSpeedVert += gravity;
  ballY += ballSpeedVert;
}




public void makeBounceBottom(float surface) {
  ballY = PApplet.parseInt(surface-(ballSize/2));
  ballSpeedVert = (ballSpeedVert - 5) * -1 ;
}




public void makeBounceTop(float surface) {
  ballY = PApplet.parseInt(surface+(ballSize/2));
  ballSpeedVert*=-1;
}


public void makeBounceRaquette(){
  
  if(ballX <= mouseX+30 && ballX >= mouseX - 30 && ballY <= mouseY + ballSize/2 && ballY >= mouseY -ballSize/2){
    ballSpeedVert = -15;
  }
}




public void keepInScreen() {
  
  
  if (ballY+(ballSize/2) > height) { 
    makeBounceBottom(height);
  }
  
  
  if (ballY-(ballSize/2) < 0) {
    makeBounceTop(0);
  }
}

class obstacle{
  
  int obstacleX;
  int obstacleY;
  int sizeY;
  int sizeX;
  
  
  obstacle(){
    
    sizeY = PApplet.parseInt(random(25, 500) );
    sizeX = PApplet.parseInt(random(50, 100) );
    
    obstacleX = width;
    obstacleY = sizeY;
  }
  
  
  
  public void drawObstacle(){
    
    rect(obstacleX, obstacleY, sizeX, sizeY);
  }
  
  
  
  public void deplacement(){
    obstacleX -= 5;
  }
}
  public void settings() {  size (800, 500); }
  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "sketch_201117a_jeu_pour_projet" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }
}
