home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / de86gnzn / examples / boinkaroids / goobie.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  2.4 KB  |  121 lines

  1. /**
  2. *
  3.  * Goobie.java
  4.  * @author    Mark G. Tacchi (mtacchi@next.com) 
  5.  * @version    0.8
  6.  * Mar 18/1996
  7. *
  8. * A goobie is just a little green thing that lives within a transparent
  9.  * shell until that shell pops.
  10.  *
  11.  * Goobies harm ships and bullets harm Goobies.
  12.  *
  13. */
  14.  
  15. import java.applet.Applet;
  16. import java.lang.Math;
  17.  
  18. import com.next.gt.*;
  19.  
  20. public class Goobie extends Actor {
  21.  
  22.   //
  23.   // Gravity strength toward the ship.
  24.   //
  25.   private static double    GRAVITATIONAL_PULL= 0.5;
  26.   
  27. Goobie(Gamelet theOwner, Bigoobie explodee) {
  28.   super();
  29.  java.awt.Image            theImage;
  30.   java.awt.MediaTracker        tracker;
  31.  
  32.   owner= theOwner;
  33.   
  34.   theImage = owner.getImage(owner.getCodeBase(), "images/goobie.gif");
  35.   tracker = new java.awt.MediaTracker(theOwner);
  36.  
  37.   tracker.addImage(theImage, 0);
  38.   try{
  39.         tracker.waitForID(0);
  40.   } 
  41.   catch (InterruptedException e) {}
  42.   
  43.   x= (int)(explodee.x - (width - explodee.width)/2.0);
  44.   y= (int)(explodee.y - (height - explodee.height)/2.0);
  45.   velocity_x= explodee.velocity_x * Gamelet.randBetween(0.2,1.4);
  46.   velocity_y= explodee.velocity_y * Gamelet.randBetween(0.2,1.4);
  47.   
  48.   setImage (theImage);
  49.  
  50. } /*Goobie()*/
  51.  
  52.  
  53. /**
  54.  * Gravitate torwards player.
  55.  */
  56. public void calculateNewPosition() {
  57.   super.calculateNewPosition();
  58.   
  59.   //
  60.   // Is the player alive?
  61.   //
  62.   if (((Boinkaroids)owner).player==null) return;
  63.   
  64.   //
  65.   // gravitate towards player
  66.   //
  67.   if (x> ((Boinkaroids)owner).player.x) velocity_x-= GRAVITATIONAL_PULL;
  68.   else velocity_x+= GRAVITATIONAL_PULL;
  69.   if (y> ((Boinkaroids)owner).player.y) velocity_y-= GRAVITATIONAL_PULL;
  70.   else velocity_y+= GRAVITATIONAL_PULL;
  71.     
  72. } /*calculateNewPosition*/
  73.  
  74.  
  75.  
  76. /**
  77.  * Explode goobie.
  78.  */
  79. public void explode()
  80. {
  81.   //
  82.   // play explode sound
  83.   //
  84.   owner.play(owner.getCodeBase(), "sounds/smack.au");
  85.   
  86.   //
  87.   // give credit for hitting me, increase score
  88.   //
  89.   owner.scoreManager.addToScore(1000);
  90.   
  91.   //
  92.   // i'm dead, i should schedule to be removed
  93.   //
  94.   owner.actorManager.removeActor(this);
  95.   
  96.   //
  97.   // tell the gamelet that there is one less bad guy
  98.   //
  99.   ((Boinkaroids)owner).badGuyCount--;
  100.     
  101. } /*explode*/
  102.  
  103.  
  104.  
  105.  
  106. /**
  107.  * Handle collision with an actor.
  108.  */
  109. protected void collideWithActor (Actor theActor)
  110. {
  111.   String theActorClassName= theActor.getClass().getName();
  112.   
  113.   if (theActorClassName.equals("Bullet") ||
  114.       theActorClassName.equals("Ship") ) {
  115.          explode();
  116.   } /*endif*/
  117.   
  118. } /*collideWithActor*/
  119.  
  120. } /*Goobie*/
  121.