/**
 * 
 * You can add code anywhere in this file - except in the areas that are
 * written by the editor. You should not change the relative ordering of
 * the code.
 * 
 * You can remove this comment block or replace it with another.
 * 
 * @see		
 * @version	
 * @author	
 */

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
import java.net.*;
import java.util.*;
import com.supercede.forms.*;
import java.applet.*;

public class Roaster extends SuperCedeApplet implements Runnable // (formerly "extends SuperCedeApplet implements Serializable")
{
    Playfield playfield;
    //AnimatedSprite sprite;
    //Sprite sprite;
    Image output;
    Thread animate;
    
	void roasterPreInit()
	{
		// You can add code anywhere in this method.
		// This method is called PRIOR to form initialization.
		// NOTE: The form has NOT been initialized yet. Do not modify the form itself in this method.
	}

	void roasterPostInit()
	{
		// You can add code anywhere in this method.
		// This method is called AFTER form initialization.
	}

    public void init() {
		// Get the images that will be used...
        Image jpgimg = getImage(getCodeBase(),"field.jpg");
        Image gifimg = getImage(getCodeBase(),"magnify.gif");
        Image antimg = getImage(getCodeBase(),"antani2.gif");
        
        // Create the playfield with a max of 1 sprite and 5 animated sprites...
		playfield = new Playfield(jpgimg,1,5);
		
		// Add a Sprite to the playfield...
		playfield.addSprite(0,new Sprite(gifimg));
		
		// Add an AnimatedSprite to the playfield...
		AnimatedSprite antSprite = new AnimatedSprite(antimg,12,2);
		
		// Create the track to make the ant walk...
		int[] frameSequence1 = {0,1,2,1,0,3,4,3};
		int[] dx1 = {-5,-5,-5,-5,-5,-5,-5,-5};
		int[] dy1 = {0,0,0,0,0,0,0,0};
		Track antTrack1 = new Track(frameSequence1,dx1,dy1);		
		antSprite.addTrack(0,antTrack1);
		
		// Create the track to make the ant explode...
		int[] frameSequence2 = {5,6,7,8,9,10,11};
		int[] dx2 = {0,0,0,0,0,0,0};
		int[] dy2 = {0,0,0,0,0,0,0};
		Track antTrack2 = new Track(frameSequence2,dx2,dy2);		
		antSprite.addTrack(1,antTrack2);
		
		antSprite.positionSprite(250,100);
		playfield.addAnimatedSprite(0,antSprite);
		
		// Now add 4 more of the same sprite in different locations...
		AnimatedSprite nextSprite = antSprite.copySprite();
		nextSprite.positionSprite(400,200);
		playfield.addAnimatedSprite(1,nextSprite);
		
		nextSprite = antSprite.copySprite();
		nextSprite.positionSprite(100,320);
		playfield.addAnimatedSprite(2,nextSprite);

		nextSprite = antSprite.copySprite();
		nextSprite.positionSprite(350,20);
		playfield.addAnimatedSprite(3,nextSprite);
		
		nextSprite = antSprite.copySprite();
		nextSprite.positionSprite(200,300);
		playfield.addAnimatedSprite(4,nextSprite);
		
        renderPlayfield( );
    }
    
    public void start() {
        animate = new Thread(this);
        animate.start();
        
		SuperCedeInit();   // Do not remove this line.
	}

    public void paint(Graphics g) {
        g.drawImage(output, 0, 0, this);
    }

    public void update(Graphics g) {
        paint(g);
    }

    
    public boolean mouseMove(Event e, int x, int y) {
    	//Center the magnifying glass around the pointer...
    	playfield.sprite[0].positionSprite(x-55,y-47);
    	// Don't render the playfield yet (Too processor intensive)
    	//   Wait until the next animate.sleep ends.
    	return true;
    }
    
	public void stop()
	{
		// You can add code anywhere in this method.
        animate = null;
      
		SuperCedeStop();   // Do not remove this line.
	}
	
    public void run() {
        while (true) {
            try {
                animate.sleep(100);
            } catch (InterruptedException e) {
            }
            renderPlayfield( );
        }
    }    
    
    public void renderPlayfield() {

		// If an ant is done exploding, start its sequence over again...
		for (int i=0; i<playfield.animatedSprite.length; i++) {
			if (playfield.animatedSprite[i] != null && playfield.animatedSprite[i].inState(1,6)) {
				playfield.animatedSprite[i].positionSprite(550,(int)(java.lang.Math.random()*300)+20);
				playfield.animatedSprite[i].startTrack(0);
			}	
		}

		// If an ant has gone off the right side of the screen,
		//   start its sequence over again...
		for (int i=0; i<playfield.animatedSprite.length; i++) {
			if (playfield.animatedSprite[i] != null && playfield.animatedSprite[i].x < -150) {
				playfield.animatedSprite[i].positionSprite(550,(int)(java.lang.Math.random()*300)+20);
				playfield.animatedSprite[i].startTrack(0);
			}	
		}

		// Did the user click on an ant, and which one?
		int whichAnt = playfield.onAnimatedSprite(playfield.sprite[0].x+55, playfield.sprite[0].y+47);
		if (whichAnt != -1 && playfield.animatedSprite[whichAnt].currentTrack != 1) {
			// If the magnifying glass is over an ant, and the ant is
			// not already exploding, start the exploding-ant track.
			playfield.animatedSprite[whichAnt].startTrack(1);
		}
				
        playfield.Draw();
        output = playfield.toImage();

		//   sprite.nextState();
		
        repaint();
    }
    
	// SuperCede Begin 2.0 Form Members
	// Do not remove the Begin and End markers.
	// The editor will rewrite the contents of this section each time the form is saved.

	private void SuperCedeConstructor() throws IOException, ClassNotFoundException, ClassCastException, SuperCedeInvalidStateException
	{
		// Construct the actual connectors to give to our base class.

		Vector connectors = null;
		super.initializeThis(connectors);
	}

	public Roaster() throws IOException, ClassNotFoundException, ClassCastException, SuperCedeInvalidStateException
	{
		super();

		roasterPreInit();
		SuperCedeConstructor();
		roasterPostInit();
	}

	private void SuperCedeInit()
	{
	}

	private void SuperCedeStart()
	{
	}

	private void SuperCedeStop()
	{
	}

	// SuperCede End 2.0 Form Members
}

// SuperCede Begin 2.0 Form Connectors
// Do not remove the Begin and End markers.
// The editor will rewrite the contents of this section each time the form is saved.

// The following line must be the last line in the file.
// SuperCede End 2.0 Form Connectors
