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

  1. /**
  2. *
  3.  * Asteroid.java
  4.  * @author    Mark G. Tacchi (mtacchi@next.com) 
  5.  * @version    0.8
  6.  * Mar 11/1996
  7. *
  8.  * A generic Asteroid class which handles varying sizes of asteroids.  Images
  9.  * should have filenames with corresponding S, M, L, and G appended to them
  10.  * (example asteroidM.gif).
  11.  * This Actor collides with Bullets and Ships.  It is responsible for creating
  12.  * an explosion object.
  13.  *
  14. */
  15.  
  16. import java.applet.Applet;
  17. import java.lang.Math;
  18.  
  19. import com.next.gt.*;
  20.  
  21. public class Asteroid extends Actor {
  22.  
  23.   //
  24.   // Size of the Asteroid.
  25.   //
  26.   public static final     int    SMALL_SIZE= 0,
  27.                       MEDIUM_SIZE= 1,
  28.                     LARGE_SIZE= 2;
  29.   int                size;
  30.   
  31.   //
  32.   // The filename prefix.
  33.   //
  34.   String            name;
  35.           
  36. Asteroid(Gamelet theOwner, Asteroid explodee, String theName, int theSize) {
  37.   super();
  38.  
  39.   String[]                    theImageName= {"S", "M", "L", "G"};
  40.  java.awt.Image            theImage;
  41.  
  42.   owner= theOwner;
  43.   size= theSize;
  44.   name= theName;
  45.   
  46.   if (theSize==LARGE_SIZE) {
  47.     x= (Math.random()*512);
  48.     y= (Math.random()*512);
  49.     velocity_x= (double)((int)Gamelet.randBetween(0.5,1.5)*2 - 1) * Gamelet.randBetween(8.,32.);
  50.     velocity_y= (double)((int)Gamelet.randBetween(0.5,1.5)*2 - 1) * Gamelet.randBetween(8.,32.);
  51.   }
  52.   else {
  53.     x= explodee.x;
  54.     y= explodee.y;
  55.     velocity_x= explodee.velocity_x * Gamelet.randBetween(0.6,1.4);
  56.     velocity_y= explodee.velocity_y * Gamelet.randBetween(0.6,1.4);
  57.     
  58.   }
  59.   theImage = owner.getImage(owner.getCodeBase(), "images/" +theName + theImageName[theSize] + ".gif");
  60.   setImage (theImage, 4, 32);
  61.  
  62.   currentFrame= (int)Gamelet.randBetween(0, numFrames);
  63.   
  64. } /*Asteroid()*/
  65.  
  66.  
  67.  
  68. /**
  69.  * Explode asteroid.
  70.  */
  71. public void explode()
  72. {
  73.   Explosion anExplosion;
  74.   
  75.   //
  76.   // create explosion if smallest asteroid
  77.   //
  78.   if (size==SMALL_SIZE) {
  79.     anExplosion= new Explosion(owner, this);
  80.     owner.actorManager.addActor(anExplosion);
  81.   }
  82.   else { 
  83.     owner.play(owner.getCodeBase(), "sounds/explode1.au");
  84.   }
  85.   
  86.  
  87.   //
  88.   // split off into smaller bits
  89.   //
  90.   if (size==LARGE_SIZE) {
  91.     for (int i=0; i<2; i++) {
  92.       owner.actorManager.addActor(new Asteroid(owner, this, name, MEDIUM_SIZE));
  93.       ((Boinkaroids)owner).badGuyCount++;
  94.     } /*next_i*/
  95.  
  96.   }
  97.   else if (size==MEDIUM_SIZE) {
  98.     for (int i=0; i<2; i++) {
  99.       owner.actorManager.addActor(new Asteroid(owner, this, name, SMALL_SIZE));
  100.       ((Boinkaroids)owner).badGuyCount++;
  101.     } /*next_i*/
  102.   }
  103.   
  104.   
  105.   //
  106.   // give credit for hitting me, increase score
  107.   //
  108.   owner.scoreManager.addToScore((2-size) * 200 + 100);
  109.   
  110.   //
  111.   // i'm dead, i should schedule to be removed
  112.   //
  113.   owner.actorManager.removeActor(this);
  114.   
  115.   //
  116.   // tell the gamelet that there is one less bad guy
  117.   //
  118.   ((Boinkaroids)owner).badGuyCount--;
  119.   
  120. } /*explode*/
  121.  
  122.  
  123.  
  124. /**
  125.  * Handle collision with an actor.
  126.  */
  127. protected void collideWithActor (Actor theActor)
  128. {
  129.   String theActorClassName= theActor.getClass().getName();
  130.   
  131.   if (theActorClassName.equals("Bullet") ||
  132.       theActorClassName.equals("Ship") ) {
  133.     explode();
  134.   } /*endif*/
  135.   
  136. } /*collideWithActor*/
  137.  
  138. } /*Asteroid*/
  139.