//ICM
//fall2005
//Jane Oh
//Final
//Bubbles

import processing.video.*;
Capture video;//video capturing

ArrayList bubbles;
PImage msk;
PImage bg;

import pitaru.sonia_v2_9.*;
Sample sound;//sound

void setup(){
size(300,600);
msk = loadImage("mask.jpg");
bg = loadImage("bg2.jpg");
video = new Capture(this,40,40);
Sonia.start(this);
sound = new Sample("16submarineping.wav");
smooth();
noStroke();
bubbles = new ArrayList();
}

void draw(){
image(bg,0,0,width,height);//background
for (int i = 0; i < bubbles.size(); i++){
Bubble b = (Bubble) bubbles.get(i);
b.render();
b.pop();
//create collision
if (i>0) {
for (int j = 0; j < i-1; j++){
Bubble c = (Bubble) bubbles.get(j);
if (b.collide(c)){
b.xspeed = (b.xspeed + c.xspeed + 30) /2;
c.xspeed = (b.xspeed + c.xspeed) /2;
b.yspeed = (b.yspeed + c.yspeed + 20) /2;
c.yspeed = (b.yspeed + c.yspeed) /2;
c.xdir = b.xdir;
b.xdir = c.xdir;
}
}
}
for (int k = i+1; k < bubbles.size(); k++){
Bubble d = (Bubble) bubbles.get(k);
if (b.collide(d)){
b.xspeed = (b.xspeed + d.xspeed + 30) /2;
d.xspeed = (b.xspeed + d.xspeed) /2;
b.yspeed = (b.yspeed + d.yspeed + 20) /2;
d.yspeed = (b.yspeed + d.yspeed) /2;
b.xdir = d.xdir;
d.xdir = b.xdir;
}
}
}//end of collision

if (bubbles.size() > 100){
bubbles.remove(0);
}
}

//create new bubbles for every mouse click
void mousePressed(){
for (int i = 0; i < 10; i++){
Bubble b = new Bubble(mouseX,height,video, 20);
bubbles.add(b);
}
//println(bubbles.size());
}

//read in the video
void captureEvent(Capture video){
video.read();
}

//stop the sound
public void stop(){
Sonia.stop();
super.stop();
}

class Bubble{
float x, y;
float xspeed, yspeed;
float r;
color c;
float popTrans;
float bubbleTrans;
float xdir;
float a;
float h;
PImage thisImg;
boolean start = false;
boolean popped = false;

Bubble(float x_, float y_, Capture thisVideo_,float h_){
x = x_;
y = y_;
h = h_;
h = .07;
thisImg = loadImage("bubble.gif");
thisImg.copy(thisVideo_,0,0,200,200,0,0,200,200);
thisImg.updatePixels();
r = random(40,70);
c = 255;
bubbleTrans = 200;
popTrans = 150;
a = 7;
xspeed = random(-.4,.3);
yspeed = random(-.6,-.1);
xdir = 1;
}

void render(){
smooth();
fill(c,bubbleTrans);
noStroke();
tint(c,bubbleTrans);
thisImg.mask(msk);
image(thisImg,x,y,r,r);

x+=xspeed*xdir;
y+=yspeed;

// make bubbles move right and left
if (y >= height*4/5 && y < height){
xdir = 1;
}
else if(y >= height*3/5 && y < height*4/5){
xdir = -.7;
}
else if(y >= height*2/5 && y < height*3/5){
xdir = 1.2;
}
else if(y >= height*1/5 && y < height*2/5){
xdir = -.5;
}
else if(y >= 0 && y < height*1/5){
xdir = 1;
}
}

//boolean for collision
boolean collide(Bubble bub){
//make bubbles stick together when collide
float d = dist(x,y,bub.x,bub.y);
if (d < h) {
return true;
}
else{
return false;
}
}

//make bubbles pop when arrived on the top
void pop(){
if (y<=20 || x<=10 || x>= width-30){
fill(c,popTrans);
bubbleTrans = 0; //bubbles disappear
popTrans-=3; //popping fades out

if (!popped) {
sound.play();
popped = true;
}

//draw shape of popping
smooth();
beginShape(POLYGON);
vertex(x-1.5*a, y+a);
vertex(x-4*a, y-2*a);
vertex(x, y-2*a);
vertex(x+2*a, y-5*a);
vertex(x+3*a, y-2*a);
vertex(x+7*a, y-a);
vertex(x+5*a, y+1*a);
vertex(x+6*a, y+4*a);
vertex(x+3*a, y+3*a);
vertex(x+3*a, y+7*a);
vertex(x+1*a, y+3.5*a);
vertex(x-3*a, y+5*a);
endShape();
}
}

}