//

import render.*;
import java.awt.*;

public class SnitchTest extends RenderApplet
{
   Polly polly;
   Snitch snitch;
   boolean bulletTime = false;
   int lookingState = 1, noddingState = 0, turningState = 0;

   public boolean keyUp(Event e, int key) {  // SET ACTIONS VIA KEYBOARD
      if (! super.keyUp(e, key))
         setAction(key - 'a');
      return true;
   }

   public void setAction(int a) {
      switch (a) {
      case 't'-'a':
         bulletTime = ! bulletTime;
         break;
      case 'v'-'a':
	 lookingState = (lookingState + 1) % 3;
         polly.setGazeWeight(lookingState == 0 ? 0 : lookingState == 1 ? 1 : -1);
         break;
      case 'y'-'a':
	 noddingState = (noddingState + 1) % 3;
	 polly.setNodding(noddingState == 0 ? 0 : noddingState == 1 ? .5 : -.5);
         break;
      case 'z'-'a':
	 turningState = (turningState + 1) % 3;
	 polly.setTurning(turningState == 0 ? 0 : turningState == 1 ? .8 : -.8);
         break;
      default:
         polly.setAction(a);            // SELECT AN ACTION
         break;
      }
   }

   public void initialize() {           // INITIALIZE EVERYTHING

      addLight(1,1,1, 1,1,1);           // LIGHTS
      addLight(0,-1,0, 1,1,1);
      addLight(-1,0,-1, 1,1,1);
      setBgColor(.175,.175,.7);         // BACKGROUND COLOR
      setFOV(.8);
      setFL(8);                         // CAMERA FOCAL LENGTH

      push();
	 rotateX(Math.PI/6);
	 transform(world);              // INITIAL VIEW ANGLE
      pop();

      Material floorColor = (new Material()).setColor(.2,.2,.4);
      push();                           //  CREATE THE FLOOR
	 translate(0,-.03,0);
	 scale(2,.03,2);
	 rotateX(Math.PI/2);
	 transform(world.add().cube().setMaterial(floorColor));
      pop();

      world.add(polly = new Polly());   // CREATE POLLY
      world.add(snitch = new Snitch()); // CREATE SNITCH

      polly.setGazeWeight(1);
   }

   double oldTime = 0, bounce = 0, circle = 0;
   double xyz[] = {0,0,0};

   public void animate(double time) { // ANIMATE ONE FRAME

      double elapsed = oldTime == 0 ? 0 : time - oldTime;
      oldTime = time;

      circle += (bulletTime ? .3 : 1) * elapsed;

      xyz[0] = 0;
      xyz[1] = 0;
      xyz[2] = -2.5;
      Vec.rotate(xyz, 1, circle);

      snitch.setPosition(xyz[0], xyz[2] - .5);
      snitch.setDirection(circle);
      snitch.setRate(bulletTime ? 1.2 : 15);
      snitch.animate(time);

      polly.setPosition(-1,0);
      polly.setDirection(Math.PI/2);
      polly.animate(time);

      polly.setGaze(snitch);
   }
}