home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / Gamelication / examples / Boinkaroids / Asteroid.java < prev    next >
Encoding:
Java Source  |  1998-04-20  |  2.9 KB  |  131 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.  
  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(Gamelication 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)Gamelication.randBetween(0.5,1.5)*2 - 1) * Gamelication.randBetween(8.,32.);
  50.         velocity_y= (double)((int)Gamelication.randBetween(0.5,1.5)*2 - 1) * Gamelication.randBetween(8.,32.);
  51.     }
  52.     else {
  53.         x= explodee.x;
  54.         y= explodee.y;
  55.             velocity_x= explodee.velocity_x * Gamelication.randBetween(0.6,1.4);
  56.             velocity_y= explodee.velocity_y * Gamelication.randBetween(0.6,1.4);
  57.     
  58.     }
  59.     theImage = owner.getImage(owner.getCodeBase(), "images/" +theName + theImageName[theSize] + ".gif");
  60.     setImage (theImage, 4, 32);
  61.     currentFrame= (int)Gamelication.randBetween(0, numFrames);
  62. } /*Asteroid()*/
  63.  
  64.  
  65. /**
  66.  * Explode asteroid.
  67.  */
  68. public void explode()
  69. {
  70.     Explosion anExplosion;
  71.   
  72.     //
  73.     // create explosion if smallest asteroid
  74.     //
  75.     if (size==SMALL_SIZE) {
  76.         anExplosion= new Explosion(owner, this);
  77.         owner.actorManager.addActor(anExplosion);
  78.     }
  79.     else { 
  80.         owner.play(owner.getCodeBase(), "sounds/explode1.au");
  81.     }
  82.  
  83.     //
  84.     // split off into smaller bits
  85.     //
  86.     if (size==LARGE_SIZE) {
  87.         for (int i=0; i<2; i++) {
  88.             owner.actorManager.addActor(new Asteroid(owner, this, name, 
  89.                                  MEDIUM_SIZE));
  90.             ((Boinkaroids)owner).addOneBadGuy();
  91.         } /*next_i*/
  92.     }
  93.     else if (size==MEDIUM_SIZE) {
  94.         for (int i=0; i<2; i++) {
  95.             owner.actorManager.addActor(new Asteroid(owner, this, name, 
  96.                                  SMALL_SIZE));
  97.             ((Boinkaroids)owner).addOneBadGuy();
  98.         } /*next_i*/
  99.     }
  100.  
  101.     //
  102.     // give credit for hitting me, increase score
  103.     //
  104.     owner.scoreManager.addToScore((2-size) * 200 + 100);
  105.   
  106.     //
  107.     // i'm dead, i should schedule to be removed
  108.     //
  109.     owner.actorManager.removeActor(this);
  110.   
  111.     //
  112.     // tell the Gamelication that there is one less bad guy
  113.     //
  114.     ((Boinkaroids)owner).removeOneBadGuy();
  115. } /*explode*/
  116.  
  117.  
  118. /**
  119.  * Handle collision with an actor.
  120.  */
  121. protected void collideWithActor (Actor theActor){
  122.     String theActorClassName= theActor.getClass().getName();
  123.     if (theActorClassName.equals("Bullet") ||
  124.             theActorClassName.equals("Ship") ) {
  125.         explode();
  126.     } /*endif*/
  127.   
  128. } /*collideWithActor*/
  129.  
  130. } /*Asteroid*/
  131.