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

  1. /**
  2. *
  3.  * Bullet.java
  4.  * @author    Mark G. Tacchi (mtacchi@next.com) 
  5.  * @version    0.8
  6.  * Mar 12/1996
  7. *
  8.  * A Bullet is created by the Ship object when it is sent a fire message.
  9.  * Bullets live for a specified time to live (ttl).
  10.  *
  11.  * This Actor collides with all but Ships and Explosions.
  12.  *
  13.  */
  14.  
  15. import java.awt.*;
  16.  
  17. import com.next.gt.*;
  18.  
  19. public class Bullet extends Actor {
  20.  
  21.   //
  22.   // variable used to compare against for auto death
  23.   //
  24.   long startTime;
  25.   
  26.   //
  27.   // time to live
  28.   //
  29.   long ttl= 1500;
  30.   
  31.   //
  32.   // the ship object
  33.   //
  34.   Ship    explodee;
  35.    
  36. Bullet(Gamelet theOwner, Ship theExplodee) {
  37.   super();
  38.   
  39.   double            explodeeVelocity= theExplodee.getSpeed();
  40.   double            explodeeTheta= theExplodee.getTheta();
  41.   Image                theImage;
  42.   
  43.   owner= theOwner;
  44.   explodee= theExplodee;
  45.   
  46.   x= (explodee.x + (explodee.width/2.0));
  47.   y= (explodee.y + (explodee.height/2.0));
  48.   
  49.   theImage= owner.getImage(owner.getCodeBase(), "images/bullet.gif");    
  50.   setImage (theImage, 4, 16);
  51.       
  52.   velocity_x= Math.cos(explodeeTheta)*(explodeeVelocity + 150.);
  53.   velocity_y= Math.sin(explodeeTheta+Math.PI)*(explodeeVelocity + 150.);
  54.  
  55.   x+= (velocity_x * .1);
  56.   y+= (velocity_y * .1);
  57.  
  58.  
  59.   startTime= owner.currentTickTimeMillis;
  60.  
  61. } /*Bullet()*/
  62.  
  63.  
  64.  
  65. /**
  66.  * Override tick to implement a timed death.
  67.  */
  68. public void tick()
  69. {
  70.   super.tick();
  71.     
  72.   if (owner.currentTickTimeMillis - startTime > ttl) {
  73.     if (explodee.numBullets>0)explodee.numBullets--;
  74.       owner.actorManager.removeActor (this);
  75.   } /*endif*/
  76.   
  77. } /*tick*/
  78.  
  79.  
  80.  
  81. /**
  82.  * Handle collision with an actor.
  83.  */
  84. protected void collideWithActor (Actor theActor)
  85. {
  86.   String theActorClassName= theActor.getClass().getName();
  87.   
  88.   if (theActorClassName.equals("Asteroid") ||
  89.       theActorClassName.equals("Bigoobie")) {
  90.         if (explodee.numBullets>0)explodee.numBullets--;
  91.     owner.actorManager.removeActor(this);
  92.   } /*endif*/
  93.   
  94. } /*collideWithActor*/
  95. } /*Bullet*/
  96.