home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / Gamelication / examples / Boinkaroids / Bullet.java < prev    next >
Encoding:
Java Source  |  1998-04-20  |  1.8 KB  |  93 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. import com.next.gt.*;
  17.  
  18. public class Bullet extends Actor {
  19.  
  20.     //
  21.     // variable used to compare against for auto death
  22.     //
  23.     long startTime;
  24.   
  25.     //
  26.     // time to live
  27.     //
  28.     long ttl= 1750;
  29.   
  30.     //
  31.     // the ship object
  32.     //
  33.     Ship    explodee;
  34.  
  35.    
  36. Bullet(Gamelication 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.     startTime= owner.currentTickTimeMillis;
  59. } /*Bullet()*/
  60.  
  61.  
  62. /**
  63.  * Override tick to implement a timed death.
  64.  */
  65. public void tick(){
  66.     super.tick();
  67.     
  68.     if (owner.currentTickTimeMillis - startTime > ttl) {
  69.         if (explodee.numBullets>0)
  70.             explodee.numBullets--;    
  71.         owner.actorManager.removeActor (this);
  72.     } /*endif*/
  73. } /*tick*/
  74.  
  75.  
  76.  
  77. /**
  78.  * Handle collision with an actor.
  79.  */
  80. protected void collideWithActor (Actor theActor){
  81.     String theActorClassName= theActor.getClass().getName();
  82.   
  83.     if (theActorClassName.equals("Asteroid") ||
  84.             theActorClassName.equals("Bigoobie")) {
  85.         if (explodee.numBullets>0)
  86.             explodee.numBullets--;
  87.         owner.actorManager.removeActor(this);
  88.     } /*endif*/
  89.   
  90. } /*collideWithActor*/
  91.  
  92. } /*Bullet*/
  93.