home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / Gamelication / examples / Boinkaroids / Goobie.java < prev    next >
Encoding:
Java Source  |  1998-04-20  |  2.3 KB  |  117 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.  
  16. import java.lang.Math;
  17. import com.next.gt.*;
  18.  
  19. public class Goobie extends Actor {
  20.  
  21.     //
  22.     // Gravity strength toward the ship.
  23.     //
  24.     private static double    GRAVITATIONAL_PULL= 0.5;
  25.  
  26.     
  27. /**
  28.  * Constructor
  29.  */  
  30. Goobie(Gamelication theOwner, Bigoobie explodee) {
  31.     super();
  32.     java.awt.Image            theImage;
  33.     java.awt.MediaTracker        tracker;
  34.     owner= theOwner;
  35.   
  36.     theImage = owner.getImage(owner.getCodeBase(), "images/goobie.gif");
  37.     tracker = new java.awt.MediaTracker(theOwner);
  38.     tracker.addImage(theImage, 0);
  39.  
  40.     try{
  41.         tracker.waitForID(0);
  42.     } 
  43.     catch (InterruptedException e) {}
  44.   
  45.     x= (int)(explodee.x - (width - explodee.width)/2.0);
  46.     y= (int)(explodee.y - (height - explodee.height)/2.0);
  47.     velocity_x= explodee.velocity_x * Gamelication.randBetween(0.2,1.4);
  48.     velocity_y= explodee.velocity_y * Gamelication.randBetween(0.2,1.4);
  49.   
  50.     setImage (theImage);
  51.  
  52. } /*Goobie()*/
  53.  
  54.  
  55. /**
  56.  * Gravitate torwards player.
  57.  */
  58. public void calculateNewPosition() {
  59.     super.calculateNewPosition();
  60.   
  61.     //
  62.     // Is the player alive?
  63.     //
  64.     if (((Boinkaroids)owner).player==null) return;
  65.   
  66.     //
  67.     // gravitate towards player
  68.     //
  69.     if (x> ((Boinkaroids)owner).player.x) velocity_x-= GRAVITATIONAL_PULL;
  70.     else velocity_x+= GRAVITATIONAL_PULL;
  71.     if (y> ((Boinkaroids)owner).player.y) velocity_y-= GRAVITATIONAL_PULL;
  72.     else velocity_y+= GRAVITATIONAL_PULL;
  73.     
  74. } /*calculateNewPosition*/
  75.  
  76.  
  77. /**
  78.  * Explode goobie.
  79.  */
  80. public void explode(){
  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 Gamelication that there is one less bad guy
  98.     //
  99.     ((Boinkaroids)owner).removeOneBadGuy();
  100.     
  101. } /*explode*/
  102.  
  103.  
  104. /**
  105.  * Handle collision with an actor.
  106.  */
  107. protected void collideWithActor (Actor theActor){
  108.     String theActorClassName= theActor.getClass().getName();
  109.   
  110.     if (theActorClassName.equals("Bullet") ||
  111.             theActorClassName.equals("Ship") ) {
  112.         explode();
  113.     } /*endif*/
  114. } /*collideWithActor*/
  115.  
  116. } /*Goobie*/
  117.