/*
 * @(#)Animation.java 01/12/07
 * Copyright (c) 2001 Joachim Diepstraten
 *
 * I grant you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software;

 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 */
 
import java.util.Enumeration;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.beans.PropertyChangeSupport;
import java.beans.PropertyChangeListener;


public class Animation extends Behavior {


    public Animation(Alpha a, TransformGroup tg, TransformGroup[] boxes) {
      
      this.alpha = a;    
      this.tg = tg;
      tempTrans = new Transform3D();
      tempTrans2 = new Transform3D();
      this.boxes = boxes;
      boxTrans = new Transform3D[boxes.length];
      propertyChange = new PropertyChangeSupport(this);      
      for (int i=0; i<boxes.length; i++)
        boxTrans[i] = new Transform3D();
      for (int i=0; i<360; i++) 
         randomNrs[i] = (float)(Math.sin(i*Math.PI/180.0f));
         
    }

    // Override Behavior's initialize method to setup wakeup criteria
    
    public void initialize() {
        alpha.setStartTime(System.currentTimeMillis());

        // Establish initial wakeup criteria
        wakeupOn(w);
    }

    // Override Behavior's stimulus method to handle the event

    public void processStimulus(Enumeration criteria) {
        tempTrans.rotX(rotx);
        tempTrans2.rotY(roty);
        tempTrans.mul(tempTrans2);
        tg.setTransform(tempTrans);
        // Set wakeup criteria for next time
        rotx+=0.05f;
        roty-=0.075f;
        for (int i=0; i<boxes.length; i++) {
          boxes[i].getTransform(boxTrans[i]);
          float scale = boxTrans[i].get(tempMat,tempVec);
          boxTrans[i].set(randomNrs[((i << 2)+counter)%360],tempVec);
          boxes[i].setTransform(boxTrans[i]);

        }
        counter++;
        wakeupOn(w);
    }

    public void addPropertyChangeListener(PropertyChangeListener l) {
      propertyChange.addPropertyChangeListener(l);
    }

    public void removePropertyChangeListener(PropertyChangeListener l) {
      propertyChange.removePropertyChangeListener(l);
    }
        
    private WakeupOnElapsedFrames w = new WakeupOnElapsedFrames(0);
    private Alpha alpha;
    private TransformGroup tg;
    private Transform3D tempTrans;
    private Transform3D tempTrans2;
    private Transform3D boxTrans[];
    private TransformGroup[] boxes;
    private Matrix3f tempMat = new Matrix3f();
    private Vector3f tempVec = new Vector3f();
    private int counter = 0;
    private float rotz = 0.0f;  
    private float roty = 0.0f;
    private float rotx = 0.0f;
    private float randomNrs[] = new float[3000];
    private PropertyChangeSupport propertyChange;
        
}
