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 JoeBubbleSmasher extends PApplet {

public void setup() {
  size(640, 480);
  colorMode(RGB, 1.0f);
  
  titleScreen = loadImage("joe_bubble_smasher.png");
  gameInit();
}

PImage titleScreen;
static final int TITLE = 0;
static final int GAME = 1;
int gameState = TITLE;
int score = 0;

//boolean collisionDetect = false;

public void draw() {
  switch(gameState) {
    case TITLE:
    title();
    break;
    case GAME:
    game();
    break;
    default:
    //Error!
    break;
  }
}

public void title() {
  image(titleScreen, 0, 0);
}

public void game() {
  
  for (int i=0; i<bubbles.length; i++) {
    bubbles[i].smash(mouseX, mouseY);
  }
  
  for (int i=0; i<bubbles.length; i++) {
    
    
    if (bubbles[i].hasBeenTapped) {
      fill(1);
      text("POP!", bubbles[i].x-20, bubbles[i].y+10);
      bubbles[i] = makeABubble();
      score += 1;
    }
    
    bubbles[i].update();
    bubbles[i].draw();
  }
  
  fill(0);
  rect(0, 0, 100, 40);
  fill(1, 0, 0);
  text("Score = " + score, 20, 20);
}


public void gameInit() {
  score = 0;
  
  bubbles = new Bubble[numBubbles];
  for (int i=0; i<numBubbles; i++) {
    bubbles[i] = makeABubble();
  }
}

public Bubble makeABubble() {
  float x = random(0, width-Bubble.bubbleWidth);
  float y = random(0, height-Bubble.bubbleHeight);
  return new Bubble(x, y);
}

public void mouseClicked() {
  gameState = GAME;
}

int numBubbles = 10;
Bubble[] bubbles;
public class Bubble {
  static final float bubbleWidth = 50;
  static final float bubbleHeight = 50; 
  
  float x = 0;
  float y = 0;
  float xVel = 1;
  float yVel = 1;
  float xSpeed = 2.5f;
  float ySpeed = 2.5f;
  
  int myColor;
  boolean hasBeenTapped = false;
  
  Bubble(float x, float y) {
    this.x = x;
    this.y = y;
    
    xVel = (random(0, 2) == 0) ? -1 : 1;
    yVel = (random(0, 2) == 0) ? -1 : 1;
    
    myColor = color(random(1.0f), random(1.0f), random(1.0f));
  }
  
  public void update() {
    this.x += xSpeed*xVel;
    this.y += ySpeed*yVel;
    
    if (x < 0 || x > width) {
      xVel *= -1;
    }
    
    if (y < 0 || y > height) {
      yVel *= -1;
    }
  }
  
  public void draw() {
    stroke(0);
    fill(myColor);
    ellipse(x, y, bubbleWidth, bubbleHeight);
    noStroke();
    fill(1, 1, 1);
    ellipse(x-bubbleWidth/8,y-bubbleHeight/8,bubbleWidth/8,bubbleHeight/8);
  }
  
  public void smash(float mx, float my) {
    if (
      (x - bubbleWidth/2.0f <= mx && mx <= x + bubbleWidth/2.0f) &&
      (y - bubbleHeight/2.0f <= my && my <= y + bubbleHeight/2.0f)
      ) {
        hasBeenTapped = true;
      }
  }
      
}
  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "JoeBubbleSmasher" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }
}
