import java.awt.*;
import ox.graphics.matrix.*;
import ox.graphics.shape.*;
import ox.graphics.viewportTransform.*;
import ox.graphics.windowTransform.*;
import ox.graphics.renderer.*;
import ox.graphics.animals.*;
import ox.graphics.background.*;
import perlin.*;

public class Assignment03 extends BufferedApplet {
    
    OXShape[] shapes = new OXShape[5];
    Tree tree00 = new Tree();
    Tree tree01 = new Tree();
    Tree tree02 = new Tree();
    Tree tree03 = new Tree();
    BirdMan bird = new BirdMan();

    WindowTransform windowTrans = new WindowTransform();
    ViewportTransform viewTrans = new ViewportTransform();
    Renderer renderer;
    
    Matrix3D matrix = new Matrix3D();
    int w, h;
    double count = 0; 
    public void init() {
        super.init();
        w = bounds().width;
        h = bounds().height;
        viewTrans.setWidth(w);
        viewTrans.setHeight(h);
        renderer = new LineRenderer(windowTrans, viewTrans);
        shapes[0] = tree00;
        shapes[1] = tree01;
        shapes[2] = tree02;
        shapes[3] = tree03;
        shapes[4] = bird;

        // init background
        tree00.getMatrix().translate(-.5, -.2, 0);
        tree00.getMatrix().scale(.5,.5,.5);
        tree01.getMatrix().translate(.6, -.3, 0);
        tree01.getMatrix().scale(.5,.5,.5);
        tree02.getMatrix().translate(-.75, -.1, 0);
        tree02.getMatrix().scale(.3,.3,.3);
        tree03.getMatrix().translate(0, 0, 0);
        tree03.getMatrix().scale(.4,.4,.4);
        bird.getMatrix().translate(-.75, -.75, 0);
        bird.getMatrix().scale(.5,.5,.5);
    }

    public void render(Graphics g) {
        g.setColor(Color.white);
        g.fillRect(0, 0, w, h);
        g.setColor(Color.black);
        g.fillRect(0,h/2,w,2);
        
        animate();
        g.drawString(Double.toString(count), 10, 10);
         
        g.setColor(Color.blue);
        
        // transform face 
        renderer.render(g, shapes);
        animating = true;
    }

    private void animate() {
        count++;
        if (count < 50) {
            bird.getMatrix().rotateY(Math.PI * Math.sin(.01));
        } else if (count < 150) {
            bird.getMatrix().translate(.5, 0, 0);
        } else if (count < 200) {
            bird.getMatrix().rotateY(Math.PI * Math.sin(.01));
        } else if (count < 350) {
            bird.getMatrix().translate(0, .01, 0);
        } else if (count < 400) {
            bird.getMatrix().rotateY(Math.PI * Math.sin(-.01));
        } else if (count < 450) {
            bird.getMatrix().translate(0, 0, .01);
        } else if (count < 500) {
            bird.getMatrix().rotateY(Math.PI * Math.sin(.01));
        } else if (count < 800) {
            bird.getMatrix().scale(.99, .99, .99);
            bird.getMatrix().translate(0, 0, .1);
        }
    }
}


