import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;

public class HelloWorld extends Applet implements Runnable, MouseListener
{
  
  //declare all class variables
  public Image myImage;
  public Image myImage2;
  public Image myback;
  public AnimatedSprite guy;
  public Raster backgrnd;
  public Raster backedit;
  public AnimatedSprite spry;
  public Playfield playground;
  public Image output;
  public int x, y;
  public int flag;
  public int lastMouseClickX, lastMouseClickY;
  Thread animate;

  public void init()
  {
    //give applet mouse functions
    this.addMouseListener(this);

    //grab the spinning world graphic and create AnimatedSprite from it.
    myImage = getImage(getCodeBase(),"WrldAni.gif");
    spry = new AnimatedSprite(myImage, 12);

    //grab the JavaGuy image and treat as an AnimatedSprite with one frame.
    myImage2 = getImage(getCodeBase(),"T1.gif");
    guy = new AnimatedSprite(myImage2, 1);

    //grab the background graphic and make it into the playfield.
    myback = getImage(getCodeBase(),"curvgrid.jpg");
    playground = new Playfield(myback, 2);   // 2 is how many sprites we want.
    

    //finish initializing the world sprite
    spry.addState(1, 10, 2, 1, 1, 1);
    spry.setFrame(1);
    
    //finish initializing the JavaGuy sprite
    guy.addState(1,10,0,0,1,0);
    guy.setFrame(0);

    //Add the sprites to playfield
    playground.addSprite(1,spry);
    playground.addSprite(2,guy);
    
    //render the sprites onto the playfield
    playground.Draw();

    //prepare a graphic for writing to the screen
    output = playground.toImage();
    
    //initialize the "earth-hit" flag to a miss.
    flag = 0;
  }
  
  //start threading
  public void start()
  {
    animate = new Thread(this);
    animate.start();
  }

  //remove the thread when finished.
  public void stop()
  {
    animate = null;
  }

  //normal operation of thread, with no events.
  public void run()
  {
    while (true)
      {
	try 
	  {
	    //can be changed to speed/slow the application
	    animate.sleep(100000000000);  
	  }
	catch(InterruptedException e)
	  {}
	update(getGraphics());
      }
   }        


  public void paint(Graphics g)
  {
    //display the rendered sprites and playfield on the screen.
    g.drawImage(output, 0, 0, this);

    //the test for the indicator for the "hit-the-earth" game
    if (flag == 1)
      getAppletContext().showStatus("you hit the earth!");
    else if (flag == 0)
      getAppletContext().showStatus("you missed the earth!");
  }

  public void update(Graphics g)
  {
    //advance sprite animation and movement
    spry.nextState();
    guy.nextState();
    
    //code to stop the JavaGuy if he makes it to where the mouse clicked last.
    if (guy.x == lastMouseClickX - guy.getWidth()/2) 
      guy.moveX = 0;
    if (guy.y == lastMouseClickY - guy.getHeight()/2)
      guy.moveY = 0;
	

    //re-render the new state of the sprites onto the playfield
    playground.Draw();
    output = playground.toImage();
    paint(g);
  }

  //must be present to implement MouseListener
  public void mousePressed(MouseEvent e)
  {;}
  
  //must be present to implement MouseListener
  public void mouseReleased(MouseEvent e)
  {;}

  //must be present to implement MouseListener
  public void mouseEntered(MouseEvent e)
  {;}

  //must be present to implement MouseListener
  public void mouseExited(MouseEvent e)
  {;}

  //must be present to implement MouseListener
  public void mouseClicked(MouseEvent e)
  {
    //test to see if you hit the earth with your mouseclick!
    if (spry.Overlap(e.getX(), e.getY()))
      flag = 1;
    if (!(spry.Overlap(e.getX(), e.getY())))
      flag = 0;

    //remember variables for stopping JavaGuy.
    lastMouseClickX = e.getX();
    lastMouseClickY = e.getY();
      
    //start the JavaGuy moving towards the position clicked
    if (e.getX() - guy.getWidth()/2 > guy.x)
      guy.moveX = 1;
    if (e.getX() - guy.getWidth()/2 < guy.x)
      guy.moveX = -1;
    if (e.getY() - guy.getHeight()/2 > guy.y)
      guy.moveY = 1;
    if (e.getY() - guy.getHeight()/2 < guy.y)
      guy.moveY = -1;

    //re-render the playfield
    playground.Draw();
    output = playground.toImage();
    update(getGraphics());
  }
  
}

