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