> show canvas only <


/* built with Studio Sketchpad: 
 *   http://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   http://studio.sketchpad.cc/sp/pad/view/ro.Od-E6Gb$TNL/rev.19
 * 
 * authors: 
 *   GoToLoop

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   http://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



/** 
 * PopBubbles (v3.1)
 * by Andreas2436 & Chrisir (2013/Apr)
 * mod GoToLoop
 * 
 * http://forum.processing.org/topic/project-help-needed
 * http://studio.processingtogether.com/sp/pad/export/ro.9ZKbYJpM0CfPR/latest
 */

final static byte BUBBLE_X = 0, BUBBLE_Y = 1, BUBBLE_SPD = 2;
final static byte BUBBLE_SIZE = 3, BUBBLE_COLOR = 4;

final static byte  MIN_SPD = 3, MAX_SPD = 5; 
final static short MIN_SIZE = 0150, MAX_SIZE = 0250;
final static float FPS = 40, BOLD = 2;

final static byte  MAX_NUM = 010, FREQ = 020;
final static int[][] bubbles = new int[MAX_NUM][BUBBLE_COLOR + 1];
final static boolean[] bubblesState = new boolean[MAX_NUM];

final static String GFX = JAVA2D; // Use JAVA2D or P2D

static int bubblesCounter, score;

void setup() {
  size(600, 800, GFX);
  frameRate(FPS);
  smooth();

  strokeWeight(BOLD);
  ellipseMode(CENTER);
}

void draw() {
  background(-1);

  if (frameCount % FREQ == 0)   createBubbles();

  updateBubbles();
  drawBubbles();
}

void mousePressed() {
  for (int i=0; i!=MAX_NUM; i++)   if (bubblesState[i])
    if ( isMouseIntersect(i, mouseX, mouseY) ) {
      bubblesState[i] = false;
      return;
    }
}

static final boolean isMouseIntersect(int i, int mx, int my) {
  final int[] b = bubbles[i];
  return sq(b[BUBBLE_X] - mx) + sq(b[BUBBLE_Y] - my) < sq(b[BUBBLE_SIZE]>>1);
  //return dist(mx, my, b[BUBBLE_X], b[BUBBLE_Y]) < b[BUBBLE_SIZE]>>1;
}

static final void updateBubbles() {
  for (int i=0; i!=MAX_NUM; i++)   if (bubblesState[i]) {
    final int[] b = bubbles[i];
    b[BUBBLE_Y] -= b[BUBBLE_SPD];

    if (b[BUBBLE_Y] < -b[BUBBLE_SIZE]) {
      bubblesState[i] = false;
      --bubblesCounter;
    }
  }
}

void drawBubbles() {
  for (int i=0; i!=MAX_NUM; i++)   if (bubblesState[i]) {
    final int[] b = bubbles[i];
    fill(b[BUBBLE_COLOR]);
    ellipse(b[BUBBLE_X], b[BUBBLE_Y], b[BUBBLE_SIZE], b[BUBBLE_SIZE]);
  }
}

void createBubbles() {
  for (int i=0; i!=MAX_NUM; i++)   if (!bubblesState[i]) {
    final int[] b = bubbles[i];

    b[BUBBLE_X] = (int) random(width);
    b[BUBBLE_Y] = height + 0200;
    b[BUBBLE_SPD]   = (int) random(MIN_SPD, MAX_SPD + 1);
    b[BUBBLE_SIZE]  = (int) random(MIN_SIZE, MAX_SIZE + 1);
    b[BUBBLE_COLOR] = (color) random(#000000);

    bubblesState[i] = true;
    ++bubblesCounter;
    return;
  }
}