/*
  Class Project1
  Written by Bern Walker for 6.837
  
  This java applet implements a procedure where the location of a
  'target' sprite is set by the location of a mouseclick in a playing
  field.  After the target location changes, a 'seeker' sprite is moved
  towards the location of the target, by means of a constant movement back
  and forth along the Y-axis modified by intelligent movement towards the
  target in the X-axis.  Once the seeker has found the targer, the target
  and seeker are represented by a single sprite, representing the seeker
  in a rest state.
  
  The icon used for the seeker in this exercise in the files "peebo.gif"
  and "pblit.gif" are based on the "Peebo" robotic mini-bombs from the
  comic book "Gold Digger."  The seeker behavior can be explained by this
  being a very rough AI test - just as the code quality can be explained
  by this being my first Java program.
  
  Bern Walker
  9/28/98
  */

import java.awt.*;
import java.applet.*; 
import java.lang.*; 
 
public class Project1 extends Applet implements java.lang.Runnable 
{ 
  public Thread runner; 
  public Image output; // The object containing each newly created frame
  public Raster playfield, background; // objects for use in sprite manipulation
  public Sprite A, B1, B2;// The objects holding the targer and seeker sprites
  public boolean found = false;// Is the seeker at the target?(T/F)

  //Initialize the position variables to the center of a 400x400 playfield
  public int A_x = 200;
  public int A_y = 200;
  public int B_x = 200;
  public int B_y = 200;
 
  public void init() 
  { 
    //Init loads the contents of the various sprites and the
    //generic background raster.
    Raster background = new Raster(getImage(getCodeBase(),"playfield.gif")); 
    A = new Sprite(getImage(getCodeBase(),"target.gif"),0);
    B1 = new Sprite(getImage(getCodeBase(),"peebo.gif"),0);
    B2 = new Sprite(getImage(getCodeBase(),"pblit.gif"),0);
  } 
   
  //Generic thread start
  public void start() 
  { 
    if (runner == null) { 
      runner = new Thread(this); 
      runner.start(); 
    }
  }
  
  public void run() 
  { 
    while (true) { 
      //The Boolean down stores the current direction of the seeker's
      // bounce.
      boolean down = true; 
      if ((A_x == B_x) & (A_y == B_y)) { 
    	repaint(); 
      } 
      else { 
	    int y_max = 324;
    	int x_max = 324;
	    repaint(); 
        	if (down) { 
        	  if (B_y == y_max) { 
        	    down = false; 
        	    B_y -= 1; 
        	  } else { 
           	    B_y += 1; 
        	  } 
    	} 
    	else { 
    	  if (B_y == 0) { 
    	    down = true; 
    	    B_y += 1; 
    	  } else { 
    	    B_y -= 1; 
    	  } 
    	} 
	 
	if ((B_x < A_x) & (B_x < x_max)) { 
	  B_x += 1; 
	} else if ((B_x > A_x) & (B_x > 0)) { 
	  B_x -= 1; 
	} 
      } 
    } 
  } 
   
   
  public boolean mouseDown(Event e, int x, int y) 
  { 
    A_x = x; 
    A_y = y; 
    return true; 
  } 
   
  public void paint(Graphics g) 
  { 
    if (found) {
      playfield = B2.Overlay(background, A_x, A_y);
    } else {
      Raster scratch;
      scratch = A.Overlay(background, A_x, A_y);
      playfield = B1.Overlay(scratch, B_x, B_y);
    }
    output = playfield.toImage();
    g.drawImage(output,0,0,this); 
  } 
   
  public void stop() 
  { 
    if (runner != null) { 
      runner.stop(); 
      runner = null; 
    }
  }
  
}
