//******************************************************************************
// SpriteDemo.java:	Applet
//
//******************************************************************************
import java.applet.*;
import java.awt.*;

//==============================================================================
// Main Class for applet SpriteDemo
//
//==============================================================================
public class SpriteDemo extends Applet implements Runnable
{
    Playfield playfield;    
	Image output;
	CarSprite carsprite;
	RandomSprite hurricane, cloudsprite1, cloudsprite2, cloudsprite3;

	final static int HEIGHT = 400;
	final static int WIDTH = 200;

	// THREAD SUPPORT:
	//		m_SpriteDemo	is the Thread object for the applet
	//--------------------------------------------------------------------------
	private Thread	 m_SpriteDemo = null;


	// SpriteDemo Class Constructor
	//--------------------------------------------------------------------------
	public SpriteDemo()
	{
	}

	// APPLET INFO SUPPORT:
	//		The getAppletInfo() method returns a string describing the applet's
	// author, copyright date, or miscellaneous information.
    //--------------------------------------------------------------------------
	public String getAppletInfo()
	{
		return "Name: SpriteDemo\r\n" +
		       "Author: Charles B. Lee\r\n" +
		       "Created with Microsoft Visual J++ Version 1.1";
	}


	// The init() method is called by the AWT when an applet is first loaded or
	// reloaded.  Override this method to perform whatever initialization your
	// applet needs, such as initializing data structures, loading images or
	// fonts, creating frame windows, setting the layout manager, or adding UI
	// components.
    //--------------------------------------------------------------------------
	public void init()
	{
        // If you use a ResourceWizard-generated "control creator" class to
        // arrange controls in your applet, you may want to call its
        // CreateControls() method from within this method. Remove the following
        // call to resize() before adding the call to CreateControls();
        // CreateControls() does its own resizing.
        //----------------------------------------------------------------------

		resize(HEIGHT, WIDTH);

		MyRaster myrasterBackground = new MyRaster(getImage(getCodeBase(), "usa.gif"));

		playfield = new Playfield(myrasterBackground, 5);

		carsprite = new CarSprite(getImage(getCodeBase(), "car.gif"), getImage(getCodeBase(), "car2.gif"), myrasterBackground);
		playfield.addSprite(0, carsprite);

		hurricane = new RandomSprite(getImage(getCodeBase(), "hurricane1.gif"), myrasterBackground, 4, true);
		hurricane.addSprite(1, new Sprite(getImage(getCodeBase(), "hurricane2.gif"), true));
		hurricane.addSprite(2, new Sprite(getImage(getCodeBase(), "hurricane3.gif"), true));
		hurricane.addSprite(3, new Sprite(getImage(getCodeBase(), "hurricane4.gif"), true));
		playfield.addSprite(1, hurricane);

		cloudsprite1 = new RandomSprite(getImage(getCodeBase(), "clouds1.gif"), myrasterBackground, true);
		playfield.addSprite(2, cloudsprite1);

		cloudsprite2 = new RandomSprite(getImage(getCodeBase(), "clouds2.gif"), myrasterBackground, true);
		playfield.addSprite(3, cloudsprite2);
		
		cloudsprite3 = new RandomSprite(getImage(getCodeBase(), "clouds3.gif"), myrasterBackground, true);
		playfield.addSprite(4, cloudsprite3);

		renderPlayfield();

	}

	// Place additional applet clean up code here.  destroy() is called when
	// when you applet is terminating and being unloaded.
	//-------------------------------------------------------------------------
	public void destroy()
	{
	}

    public void renderPlayfield()
	{
		playfield.Draw();
        output = playfield.toImage();
        repaint();

		hurricane.nextFrame();
		playfield.nextState();
	}
	
	// Overload repaint so that it doesn't clear the screen
	public void update(Graphics g)
	{
		paint(g);
	}

	// SpriteDemo Paint Handler
	//--------------------------------------------------------------------------
	public void paint(Graphics g)
	{
		g.drawImage(output, 0, 0, this);
	}

	//		The start() method is called when the page containing the applet
	// first appears on the screen. The AppletWizard's initial implementation
	// of this method starts execution of the applet's thread.
	//--------------------------------------------------------------------------
	public void start()
	{
		if (m_SpriteDemo == null)
		{
			m_SpriteDemo = new Thread(this);
			m_SpriteDemo.start();
		}
	}
	
	//		The stop() method is called when the page containing the applet is
	// no longer on the screen. The AppletWizard's initial implementation of
	// this method stops execution of the applet's thread.
	//--------------------------------------------------------------------------
	public void stop()
	{
		if (m_SpriteDemo != null)
		{
			m_SpriteDemo.stop();
			m_SpriteDemo = null;
		}

	}

	// THREAD SUPPORT
	//		The run() method is called when the applet's thread is started. If
	// your applet performs any ongoing activities without waiting for user
	// input, the code for implementing that behavior typically goes here. For
	// example, for an applet that performs animation, the run() method controls
	// the display of images.
	//--------------------------------------------------------------------------
	public void run()
	{
		while (true)
		{
			try
			{
				repaint();
				Thread.sleep(100);
			}
			catch (InterruptedException e)
			{
				stop();
			}
			renderPlayfield();
		}
	}

	// MOUSE SUPPORT:
	//		The mouseDown() method is called if the mouse button is pressed
	// while the mouse cursor is over the applet's portion of the screen.
	//--------------------------------------------------------------------------
	public boolean mouseDown(Event evt, int x, int y)
	{
		// TODO: Place applet mouseDown code here
		carsprite.setDestination(x, y);
		return true;
	}
}
