package org.cg.aquarium.elements;

import com.sun.opengl.util.GLUT;
import java.util.Random;
import javax.media.opengl.GL;
import javax.media.opengl.glu.GLU;
import org.cg.aquarium.infrastructure.Environment;
import org.cg.aquarium.infrastructure.base.Mobile;
import org.cg.aquarium.infrastructure.representations.Vector;

/**
 * Bubbles.
 *
 * @author ldavid
 */
public class Bubbles extends Mobile {

    public final double CEIL = 100;
    public int bubblesCount = 4;

    @Override
    public void update() {
        if (position.getY() > CEIL) {
            setPosition(position.mirrorOnHorizontalAxis());
        }

        move();
    }

    @Override
    public void initializeProperties() {
        setPosition(Vector.DOWN.scale(20).add(
                Vector.random().normalize().scale(10)));
        
        setDirection(Vector.UP);
        setSpeed(.8f);
        setSize(Vector.RIGHT);
    }

    @Override
    public void display(GL gl, GLU glu, GLUT glut) {
        Random r = Environment.getEnvironment().getRandom();

        gl.glPushMatrix();
        gl.glTranslated(position.getX(), position.getY(), position.getZ());
        gl.glColor3f(1, 1, 1);

        for (int i = 0; i < bubblesCount; i++) {
            gl.glTranslated(position.getX() + r.nextDouble(),
                    position.getY() - 3,
                    position.getZ());

            for (int j = 0; j < 3; j++) {
                gl.glTranslated(
                        r.nextDouble() - .5,
                        r.nextDouble() - .5,
                        r.nextDouble() - .5
                );
                
                glut.glutSolidSphere(size.getX(), 10, 10);
            }
        }

        gl.glPopMatrix();
    }

}

@@ -417,58 +417,136 @@
 nal 
-double CEIL = 100;%0A    public int bubblesCount = 4
+static Vector VARIANCE = new Vector(40, 20, 4);%0A%0A    public static int N_BUBBLES = 4;%0A    public static int N_BUBBLES_GROUPS = 2
 ;%0A%0A 
@@ -619,12 +619,23 @@
 ) %3E 
-CEIL
+VARIANCE.getY()
 ) %7B%0A
@@ -662,40 +662,230 @@
 ion(
-position.mirrorOnHorizontalAxis(
+new Vector(%0A                    (Environment.getEnvironment().getRandom().nextDouble() - .5)%0A                    * 2 * VARIANCE.getX(),%0A                    -position.getY(),%0A                    position.getZ()%0A            
 ));%0A
@@ -969,24 +969,86 @@
 perties() %7B%0A
+        Random r = Environment.getEnvironment().getRandom();%0A%0A
         setP
@@ -1059,100 +1059,195 @@
 ion(
+new 
 Vector
-.DOWN.scale(20).add(%0A                Vector.random().normalize().scale(10)));%0A        
+(%0A                (r.nextDouble() - .5) * 2 * VARIANCE.getX(),%0A                -r.nextDouble() * VARIANCE.getY(),%0A                (r.nextDouble() - .5) * 2 * VARIANCE.getZ()));%0A
 %0A   
@@ -1633,20 +1633,24 @@
 i %3C 
-bubblesCount
+N_BUBBLES_GROUPS
 ; i+
@@ -1699,16 +1699,20 @@
 getX() +
+ 4 *
  r.nextD
@@ -1832,17 +1832,25 @@
  0; j %3C 
-3
+N_BUBBLES
 ; j++) %7B
@@ -1917,32 +1917,33 @@
 nextDouble() - .
+0
 5,%0A             
@@ -1971,16 +1971,17 @@
 le() - .
+0
 5,%0A     
@@ -2017,16 +2017,17 @@
 le() - .
+0
 5%0A      
@@ -2039,32 +2039,16 @@
      );%0A
-                
 %0A       
@@ -2077,16 +2077,33 @@
 dSphere(
+r.nextDouble() * 
 size.get
@@ -2173,11 +2173,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Improve Bubbles realism