package com.lloydm.geosword;

import java.util.Random;

import com.threed.jpct.Camera;
import com.threed.jpct.FrameBuffer;
import com.threed.jpct.Interact2D;
import com.threed.jpct.Matrix;
import com.threed.jpct.Object3D;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.World;

public class SpaceDebris
{
     @SuppressWarnings("unused")
     private final static String TAG = "com.lloydm.geosword.SpaceDebris";
     public final static int MAXDEBRIS = 60;

     public SimpleVector currentpos = null;
     public SimpleVector currentrot = null;
     public SimpleVector rotvector = null;

     public Matrix wmatrix = null;

     public Object3D pivot = null;
     public Object3D[] mesh = null;
     public String meshfile = null;
     public int meshfileindex = -1;

     public Random rand;

     public SimpleVector screenvec = null;
     public SimpleVector[] randomveclist;

     public int index = 0;

     // public float angle=0;

     public SpaceDebris(World world, long i)
     {

                     rotvector = new SimpleVector(SimpleVector.ORIGIN);
                     pivot = (Object3D.createDummyObj());
                     world.addObject(pivot);
                     pivot.setOrigin(SimpleVector.ORIGIN);
                     rand = new Random();
                     rand.setSeed(100 + System.currentTimeMillis() % (i + 1));
                     randomveclist = new SimpleVector[8];
                     for (int j = 0; j < 8; j++)
                     {
                                     float x
=
256.0f * (rand.nextFloat() - 0.5f);
                                     float y = 256.0f * (rand.nextFloat() - 0.5f);
                                     float z = 256.0f * (rand.nextFloat() - 0.5f);
                                     // angle = rand.nextFloat();
                                     randomveclist[j] = new SimpleVector(x, y, z);
                     }
     }

     public SimpleVector getrandomposition()
     {
                     index++;
                     if (index > 7)
                     {
                                     index = 0;
                     }
                     return pivot.getWorldTransformation().getTranslation().calcAdd(randomveclist[index]);
     }

     public void update(Camera camera, FrameBuffer fbuffer, float camx, float camy, float camz, float camdx, float camdy, float camdz)
     {
                     if (inview(camera, fbuffer))
                     {
                                     // pivot.rotateX(0.01f);
                                     // pivot.rotateY(0.02f);
                                     // pivot.rotateZ(0.03f);
                     }
                     else
                     {
                                     index++;
                                     if (index > 7)
                                     {
                                                     index = 0;

                                     }
                                     if (wmatrix == null)
                                     {
                                                     wmatrix = pivot.getWorldTransformation();
                                     }
                                     else
                                     {
                                                     wmatrix = pivot.getWorldTransformation(wmatrix);
                                     }
                                     if (currentpos == null)
                                     {
                                                     currentpos = wmatrix.getTranslation();
                                     }
                                     currentpos = wmatrix.getTranslation(currentpos);
                                     currentpos.scalarMul(-1.0f);
                                     pivot.translate(currentpos); // ie put it back to the origin....
                                     currentpos.x = camx + randomveclist[index].x + camdx;
                                     currentpos.y = camy + randomveclist[index].y + camdy;
                                     currentpos.z = camz + randomveclist[index].z + camdz;
                                     // camera.getPosition().calcAdd(randomveclist[index]);
                                     // currentpos.x+= camera.getDirection().x*100f;
                                     // currentpos.y+= camera.getDirection().y*100f;
                                     // currentpos.z+= camera.getDirection().z*100f;

                                     pivot.translate(currentpos);
                                     wmatrix = pivot.getWorldTransformation(wmatrix);
                                     currentpos = wmatrix.getTranslation(currentpos);

                     }
     }

     private boolean
inview(Camera camera, FrameBuffer fbuffer)
     {
                     if (currentpos == null)
                     {
                                     currentpos = pivot.getWorldTransformation().getTranslation();
                     }
                     if (screenvec == null)
                     {
                                     Interact2D.project3D2D(camera, fbuffer, currentpos);
                     }
                     screenvec = Interact2D.project3D2D(camera, fbuffer, currentpos, screenvec);
                     if (screenvec == null)
                     {
                                     return false;
                     }
                     if (screenvec.x < 0 || screenvec.y < 0)
                     {
                                     return false;
                     }
                     if (screenvec.x >= fbuffer.getWidth() || screenvec.y >= fbuffer.getHeight())
                     {
                                     return false;
                     }

                     return true;
     }
}