import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

/**
  * This clss is the main class for the game, and it is also the controller
  * class, responsible for controlling all the functions of the game. All
  * the game has four scenes.
  *
  * "http://luc.adolphe.free.fr"
  * All the background images are got from this Web site.
  *
  * "http://members.fortunecity.com/catherine_b/wukong.html"
  * This web site for Wukong images.
  *
  * "http://www.animfactory.com"
  * This website for monster images.
  * @author Weiqun Wang
  */

public class GameApplet extends Applet
{
	/** Help frame in the game where player can get some instructions.*/
	HelpFrame help;

	Panel buttonPanel = new Panel ();

	/** This button is to start the game or restart a new game.*/
	Button runButton = new Button ("  Play  ");

	/** This button lets the player go to the next step.*/
	Button nextButton = new Button ("  Next   ");

	/** This button lets the player to get help.*/
	Button helpButton = new Button ("  Help   ");

 	/** Handle all the animations.*/
 	AnimationHandler animationHandler = new AnimationHandler();

 	/** Handle all the graphics.*/
 	GraphicHandler graphicHandler = new GraphicHandler();

	/** Control the steps of the game.*/
	int step;

	/** Background image directory.*/
	String imageLocation1;
	/** Alive monster image directory.*/
	String imageLocation2;
	/** Dead Monster image directory.*/
	String imageLocation3;

 	/** A seperate thread to do the animation.*/
 	Thread animation;

 	/** Tells if the monster is dead or not*/
 	static boolean monsterAlive;
 	/** Tells if Sand is dead or not*/
 	static boolean sandAlive;

	AudioClip  weaponSound, winGameSound, startSound;

 	Image background, wukongImage, weaponImage, monsterImage,
 		  monsterDieImage, sandImage, sandDieImage;

	/** Generate user interfacean and the control button actions.*/
	public void init()
	{
    	setLayout(new BorderLayout());
    	this.add(BorderLayout.CENTER, graphicHandler);
    	buttonPanel.add (runButton);
		buttonPanel.add (nextButton);
		buttonPanel.add (helpButton);
    	buttonPanel.setBackground(Color.black);
    	this.add (BorderLayout.SOUTH, buttonPanel);


		/*graphicHandler.delay = Integer.valueOf(getParameter("delay")).intValue(); */
		loadSound();
		setNewGame();

    	graphicHandler.animationHandler = animationHandler;
    	graphicHandler.ga = this;

   		graphicHandler.alive = true;

    	animation = new Thread(graphicHandler);
    	animation.start();

    	runButton.addActionListener (new ActionListener ( )
		{
      		public void actionPerformed (ActionEvent event)
			{
				/* Start to play the game.*/
				if( step == 0 )
				{
					step ++;
					startSound.stop();

					imageLocation1="images/background1.gif";
					imageLocation2="images/monster1.gif";
					imageLocation3="images/monsterDie1.gif";
					loadImages(imageLocation1, imageLocation2, imageLocation3);
					graphicHandler.okToRun = true;
					showStatus ("Animation is running.");
					runButton.setLabel ("  New  ");
					animationHandler.initializeLocation();
				}
				else
				{
					/* Restart a new game.*/
					setNewGame();
					runButton.setLabel (" Start ");
					graphicHandler.weaponInHand = false;

					animationHandler.initializeLocation();
					graphicHandler.repaint();
      	 		}
       		}
		});


		nextButton.addActionListener (new ActionListener ( )
		{
			public void actionPerformed (ActionEvent event)
			{
				if( ( step == 0)||(monsterAlive)||(step == 3) )
					return;

				/* After Wukong killed the monster he can go to
				 * the next step */
				winGameSound.stop();
				step++;
				graphicHandler.delay -= 20;
				monsterAlive = true;
				animationHandler.initializeLocation();
				graphicHandler.weaponInHand = false;
				imageLocation1="images/background" + step + ".gif";
				imageLocation2="images/monster" + step + ".gif";
				imageLocation3="images/monsterDie" + step + ".gif";
				loadImages(imageLocation1, imageLocation2, imageLocation3);
			}
		});

		helpButton.addActionListener (new ActionListener ( )
		{
			public void actionPerformed (ActionEvent event)
			{
				/* Display the help frame. */
				if( help == null )
					help = new HelpFrame();
				help.show();
			}
		});

  	}

	/** Load sound files.*/
  	public void loadSound()
  	{
   		weaponSound = getAudioClip(getDocumentBase(),
   									"sound/weapon.au");
   		//monsterDieSound = getAudioClip(getDocumentBase(),
   									//"sound/monster.au");
   		startSound = getAudioClip(getDocumentBase(),
   									"sound/sonata.mid");
   		//gameOverSound = getAudioClip(getDocumentBase(),
   									//"sound/gameOver.au");
   		winGameSound = getAudioClip(getDocumentBase(),
   									"sound/kingly.mid");
   		//wukongSound1 = getAudioClip(getDocumentBase(),
   									//"sound/wukong1.au");
   		//wukongSound2 = getAudioClip(getDocumentBase(),
   									//"sound/wukong2.au");
	}

	/** Initialize a new Game */
  	public void setNewGame()
  	{
		//gameOverSound.stop();
		winGameSound.stop();
		step = 0;
		animationHandler.wukongMove = false;
		graphicHandler.wukongNumber = 0;
		monsterAlive = true;
		sandAlive = true;
		graphicHandler.okToRun = false;
		graphicHandler.s = "Magic Monkey Game";

		imageLocation1="images/background0.gif";
		imageLocation2 = "images/monster0.gif";
		imageLocation3 = "images/monsterDie0.gif";
		loadImages(imageLocation1, imageLocation2, imageLocation3);
		graphicHandler.delay = 100;
	}

  	/**
	* Finds the size of the canvas (this can't be done in init()
   	* because the canvas isn't ready yet) and gives the animation
   	* permission to run.
   	*/
  	public void start()
	{
    	Dimension viewSize = graphicHandler.getSize();

    	animationHandler.imageWidth = 100;
    	animationHandler.imageHeight = 100;
    	animationHandler.xLimit = viewSize.width -
    							animationHandler.imageWidth;
    	animationHandler.yLimit = viewSize.height -
    							animationHandler.imageHeight;
  	}

	/** Withdraws the permission for the animation to run.*/
	public void stop()
	{
  	 	graphicHandler.okToRun = false;
  	}

	/** Tells the animation thread to stop running.*/
  	public void destroy()
  	{
  	 	graphicHandler.alive = false;
  	}

  	/** Load all images to the MediaTracker.*/
  	void loadImages(String location1, String location2, String location3)
    {

    	/* Create a MediaTracker to wait for the images to be loaded */

    	MediaTracker tracker = new MediaTracker(this);

    	URL appletLocation = getCodeBase();
    	URL imageURL = null;

    	try
		{
			/*Load background image.*/
       		imageURL = new URL(appletLocation +  location1);
     		background = getImage(imageURL);
      		tracker.addImage(background, 0);

			/*Load Wukong image.*/
			//for (int i =0; i < 24; i++)
			//{
      	 		imageURL = new URL(appletLocation + "images/wukong0.gif");
     	 		wukongImage = getImage(imageURL);
     	 		tracker.addImage(wukongImage, 0);
     	 	//}

			/*Load monster images.*/
      	 	imageURL = new URL(appletLocation +  location2);
     	 	monsterImage = getImage(imageURL);
     	 	tracker.addImage(monsterImage, 0);

      	 	imageURL = new URL(appletLocation +  location3);
     	 	monsterDieImage = getImage(imageURL);
     	 	tracker.addImage(monsterDieImage, 0);

      	 	/*Load Sand images.*/
      	 	imageURL = new URL(appletLocation +  "images/sand.gif");
     	 	sandImage = getImage(imageURL);
     	 	tracker.addImage(sandImage, 0);

      	 	imageURL = new URL(appletLocation +  "images/sandDie.gif");
     	 	sandDieImage = getImage(imageURL);
     	 	tracker.addImage(sandDieImage, 0);

     		/*Load boomrage image.*/
     		imageURL = new URL(appletLocation +  "images/weapon.gif");
     	 	weaponImage = getImage(imageURL);
     	 	tracker.addImage(weaponImage, 0);
     	}
    	catch (MalformedURLException e) { e.printStackTrace(); }

     	// Wait for the images to be loaded; does no error checking

    	try
		{
			tracker.waitForAll(6000);
		}
   		catch (InterruptedException e)  {}
   	}
}





