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

  1. /*
  2.  *
  3.  * Ship.java
  4.  * @author    Mark G. Tacchi (mtacchi@next.com) 
  5.  * @version    0.8
  6.  * Mar 11/1996
  7.  *
  8. * The ship is controlled by the user, it registers for specific keyboard
  9.  * events to handle control.
  10.  *
  11.  * This Actor collides with Asteroids and Goobies.  It is responsible for
  12.  * creating an explosion object.
  13.  *
  14.  */
  15.  
  16. import java.applet.Applet;
  17. import java.applet.AudioClip;
  18. import java.lang.Math;
  19. import java.awt.*;
  20.  
  21. import com.next.gt.*;
  22.  
  23. public class Ship extends Actor implements EventHandler{
  24.  
  25.   //
  26.   // Limit number of bullets on the screen at once
  27.   //
  28.   private static int         MAX_NUM_BULLETS= 5;
  29.   public int                numBullets= 0;
  30.   
  31.   //
  32.   // Animation.  ccw= 1, cw= -1
  33.   //
  34.   public int                animationDirection= 1;
  35.   public boolean            isAnimating= true; 
  36.   
  37.   //
  38.   // Is thrusting
  39.   //
  40.   public boolean            thrusting= false; 
  41.   
  42. Ship(Gamelet theOwner) {
  43.   super();
  44.   
  45.   Image            theImage; 
  46.   owner= theOwner;  
  47.   
  48.   //
  49.   // play warp in sound
  50.   //
  51.   owner.play(owner.getCodeBase(), "sounds/warp.au");
  52.   
  53.   x= (owner.size().width/2.0);
  54.   y= (owner.size().height/2.0);
  55.   velocity_x= 0;
  56.   velocity_y= 0;
  57.   String    theImageName= "images/ship.gif";
  58.  
  59.   theImage= owner.getImage(owner.getCodeBase(), "images/ship.gif");
  60.   setImage (theImage, 4, 24);
  61.  isAnimating= false;  
  62.  
  63.   int events[]=    {    Event.KEY_ACTION, 
  64.                       Event.KEY_ACTION_RELEASE,
  65.                     Event.KEY_PRESS,
  66.                     Event.KEY_RELEASE
  67.                 };
  68.   
  69.  owner.eventManager.registerForEventNotification(this,events);
  70.  
  71. } /*Ship()*/
  72.  
  73.  
  74.  
  75. /**
  76.  * Handle keyboard events that control ship.
  77.  */
  78. public boolean handleRequestedEvent (Event theEvent) {
  79.   switch(theEvent.id) {
  80.   case Event.KEY_ACTION:
  81.     switch(theEvent.key) {
  82.       case Event.RIGHT:
  83.         this.rotateRight(true);
  84.         return true;
  85.       case Event.LEFT:
  86.         this.rotateLeft(true);
  87.         return true;
  88.       case Event.UP:                    //THRUST ON
  89.         this.thrust(true);
  90.         return true;
  91.     } /*endSwitch*/
  92.     break;
  93.   case Event.KEY_ACTION_RELEASE:
  94.     switch(theEvent.key) {
  95.       case Event.RIGHT:
  96.         this.rotateRight(false);
  97.         return true;
  98.       case Event.LEFT:
  99.         this.rotateLeft(false);
  100.         return true;
  101.       case Event.UP:                    //THRUST OFF
  102.         this.thrust(false);
  103.         return true;
  104.     } /*endSwitch*/
  105.     break;
  106.     case Event.KEY_PRESS:
  107.       switch(theEvent.key) {
  108.         case 32:
  109.           this.fire();
  110.           return true;
  111.       } /*endSwitch*/
  112.     break;  
  113.     case Event.KEY_RELEASE:
  114.       switch(theEvent.key) {
  115.         case 32:
  116.           return true;
  117.       } /*endSwitch*/
  118.     break;
  119.   } /*endSwitch*/
  120.   
  121.   return false;
  122.   
  123. } /*handleRequestedEvent*/
  124.  
  125.  
  126.  
  127. /**
  128.  * If ship is thrusting, then velocity is increasing.  Use friction if
  129.  * not thrusting.
  130.  */
  131. public void calculateNewVelocity() {
  132.   if (thrusting) {
  133.     velocity_x+= Math.cos(currentFrame*2*Math.PI/numFrames + Math.PI/2)*10;
  134.     velocity_y+= Math.sin(currentFrame*2*Math.PI/numFrames - Math.PI/2)*10;
  135.   }
  136.   else {
  137.     velocity_x*= 0.99;
  138.     velocity_y*= 0.99;
  139.   }
  140.   
  141. } /*calculateNewVelocity*/
  142.  
  143.  
  144.  
  145. /**
  146.  * Animation of the ship is based on theta, display accordingly.
  147.  */
  148. public void calculateCurrentFrame() {
  149.    if (isAnimating) {
  150.      if (animationDirection== -1) {
  151.        if (--currentFrame<=0) currentFrame= numFrames - 1;
  152.      }
  153.      else {
  154.       if (++currentFrame>=numFrames) currentFrame= 0;
  155.      }
  156.     } /*endif*/
  157.     
  158. } /*calculateCurrentFrame*/
  159.  
  160.  
  161.  
  162. /**
  163.  * Handle left rotation.
  164.  */
  165. public void rotateLeft (boolean keydown) {
  166.   if (keydown) {
  167.     isAnimating= true;
  168.     animationDirection= 1;
  169.   }
  170.   else {
  171.     isAnimating= false;
  172.   }
  173.   
  174. } /*rotateLeft*/
  175.  
  176.  
  177.  
  178. /**
  179.  * Handle right rotation.
  180.  */
  181. public void rotateRight (boolean keydown) {
  182.   if (keydown) {
  183.     animationDirection= -1;
  184.     isAnimating= true;
  185.   }
  186.   else {
  187.     isAnimating= false;
  188.   }
  189.   
  190. } /*rotateRight*/
  191.  
  192.  
  193.  
  194. /**
  195.  * Handle thrust.
  196.  */
  197. public void thrust (boolean keydown) {
  198.   if (keydown) {
  199.     thrusting= true;
  200.   }
  201.   else {
  202.     thrusting= false;
  203.   }
  204.   
  205. } /*thrust*/
  206.  
  207.  
  208.  
  209. /**
  210.  * Fire bullet.
  211.  */
  212. public void fire() {
  213.  
  214.   if (numBullets<MAX_NUM_BULLETS) {
  215.     Bullet aBullet;
  216.     
  217.     numBullets++;
  218.     owner.play(owner.getCodeBase(), "sounds/bullet.au");
  219.     aBullet= new Bullet(owner, this);  
  220.     owner.actorManager.addActor(aBullet);
  221.   } /*endif*/
  222.   
  223. } /*fire*/
  224.  
  225.  
  226.  
  227. /**
  228.  * Accessor methods (bullet uses this).
  229.  */
  230.  
  231. /**
  232.  * Ship's angle.
  233.  */
  234. public double getTheta() {
  235.   return (currentFrame*2*Math.PI/numFrames + Math.PI/2);
  236. } /*getTheta*/
  237.  
  238.  
  239.  
  240. /**
  241.  * Ship's speed.
  242.  */
  243. public double getSpeed() {
  244.   return Math.sqrt(velocity_x*velocity_x + velocity_y*velocity_y);
  245. } /*getSpeed*/
  246.  
  247.  
  248.  
  249. /**
  250.  * Handle collision with an actor.
  251.  */
  252. protected void collideWithActor (Actor theActor)
  253. {
  254.   String theActorClassName= theActor.getClass().getName();
  255.   
  256.   if (theActorClassName.equals("Asteroid") ||
  257.       theActorClassName.equals("Goobie") ||
  258.       theActorClassName.equals("Bigoobie") ) {
  259.     explode();
  260.   } /*endif*/
  261.   
  262. } /*collideWithActor*/
  263.  
  264.  
  265.  
  266. /**
  267.  * Explode ship.
  268.  */
  269. public void explode()
  270. {
  271.   Explosion anExplosion;
  272.   
  273.   //
  274.   // Tell the ActorManager that I'm gone, and an Explosion Actor
  275.   // should be added.
  276.   //
  277.   owner.actorManager.removeActor(this);
  278.   anExplosion= new Explosion(owner, this);  
  279.   owner.actorManager.addActor(anExplosion);
  280.   
  281.   //
  282.   // Lower ship counter.
  283.   //
  284.   ((Boinkaroids)owner).decrementShipCount();
  285.   
  286. } /*explode*/
  287.  
  288.  
  289. } /*Ship*/
  290.