home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / Gamelication / examples / Boinkaroids / Ship.java < prev   
Encoding:
Java Source  |  1998-04-20  |  4.4 KB  |  256 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.awt.event.*;
  17. import java.lang.Math;
  18. import java.awt.*;
  19.  
  20. import com.next.gt.*;
  21.  
  22. public class Ship extends Actor implements KeyListener{
  23.  
  24.     //
  25.     // Limit number of bullets on the screen at once
  26.     //
  27.     private static int         MAX_NUM_BULLETS= 5;
  28.     public int                numBullets= 0;
  29.   
  30.     //
  31.     // Animation.  ccw= 1, cw= -1
  32.     //
  33.     public int                animationDirection= 1;
  34.     public boolean            isAnimating= true; 
  35.   
  36.     //
  37.     // Is thrusting
  38.      //
  39.     public boolean            thrusting= false; 
  40.   
  41. Ship(Gamelication theOwner) {
  42.     super();
  43.  
  44.     Image theImage; 
  45.     owner = theOwner;  
  46.   
  47.     //owner.addKeyListener( this );
  48.   
  49.     //
  50.     // play warp in sound
  51.     //
  52.     owner.play(owner.getCodeBase(), "sounds/warp.au");
  53.   
  54.     x= (owner.getSize().width/2.0);
  55.     y= (owner.getSize().height/2.0);
  56.     velocity_x= 0;
  57.     velocity_y= 0;
  58.     String    theImageName= "images/ship.gif";
  59.  
  60.     theImage= owner.getImage(owner.getCodeBase(), "images/ship.gif");
  61.     setImage (theImage, 4, 24);
  62.     isAnimating= false;  
  63.   
  64. } /*Ship()*/
  65.  
  66.  
  67.  
  68. /**
  69.  * Handle keyboard events that control ship.
  70.  */
  71. public void keyPressed( KeyEvent k ){
  72.     if(k.getKeyCode() == k.VK_RIGHT )
  73.         this.rotateRight(true);
  74.     if(k.getKeyCode() == k.VK_LEFT )
  75.         this.rotateLeft(true);
  76.     if(k.getKeyCode() == k.VK_UP )
  77.         this.thrust(true);
  78.     if( k.getKeyCode() == k.VK_SPACE ) {
  79.         this.fire();
  80.     }
  81. }
  82.  
  83. public void keyReleased( KeyEvent k ){
  84.     if(k.getKeyCode() == k.VK_RIGHT )
  85.         this.rotateRight(false);
  86.     if(k.getKeyCode() == k.VK_LEFT )
  87.         this.rotateLeft(false);
  88.     if(k.getKeyCode() == k.VK_UP )
  89.         this.thrust(false);
  90. }
  91.  
  92. public void keyTyped( KeyEvent k ){
  93.  
  94. }
  95.  
  96.  
  97.  
  98. /**
  99.  * If ship is thrusting, then velocity is increasing.  Use friction if
  100.  * not thrusting.
  101.  */
  102. public void calculateNewVelocity() {
  103.     if (thrusting) {
  104.         velocity_x+= Math.cos(currentFrame*2*Math.PI/numFrames + Math.PI/2)*10;
  105.         velocity_y+= Math.sin(currentFrame*2*Math.PI/numFrames - Math.PI/2)*10;
  106.     }
  107.     else {
  108.         velocity_x*= 0.99;
  109.          velocity_y*= 0.99;
  110.     }
  111.   
  112. } /*calculateNewVelocity*/
  113.  
  114. /**
  115.  * Animation of the ship is based on theta, display accordingly.
  116.  */
  117. public void calculateCurrentFrame() {
  118.     if (isAnimating) {
  119.         if (animationDirection== -1) {
  120.             if (--currentFrame<=0) currentFrame= numFrames - 1;
  121.         }
  122.         else {
  123.             if (++currentFrame>=numFrames) currentFrame= 0;
  124.         }
  125.     } /*endif*/
  126.     
  127. } /*calculateCurrentFrame*/
  128.  
  129.  
  130. /**
  131.  * Handle left rotation.
  132.  */
  133. public void rotateLeft (boolean keydown) {
  134.     if (keydown) {
  135.         isAnimating= true;
  136.         animationDirection= 1;
  137.     }
  138.     else {
  139.         isAnimating= false;
  140.     }
  141.   
  142. } /*rotateLeft*/
  143.  
  144.  
  145.  
  146. /**
  147.  * Handle right rotation.
  148.  */
  149. public void rotateRight (boolean keydown) {
  150.     if (keydown) {
  151.         animationDirection= -1;
  152.         isAnimating= true;
  153.     }
  154.     else {
  155.         isAnimating= false;
  156.     }
  157.   
  158. } /*rotateRight*/
  159.  
  160.  
  161.  
  162. /**
  163.  * Handle thrust.
  164.  */
  165. public void thrust (boolean keydown) {
  166.     if (keydown) {
  167.         thrusting= true;
  168.     }
  169.     else {
  170.         thrusting= false;
  171.     }
  172.   
  173. } /*thrust*/
  174.  
  175.  
  176.  
  177. /**
  178.  * Fire bullet.
  179.  */
  180. public void fire() {
  181.     if (numBullets<MAX_NUM_BULLETS&&!((Boinkaroids)owner).createNewPlayer) {
  182.         Bullet aBullet = new Bullet(owner, this);
  183.     
  184.         numBullets++;
  185.         owner.play(owner.getCodeBase(), "sounds/bullet.au");
  186.         owner.actorManager.addActor(aBullet);
  187.     } /*endif*/
  188.   
  189. } /*fire*/
  190.  
  191.  
  192.  
  193. /**
  194.  * Accessor methods (bullet uses this).
  195.  */
  196.  
  197. /**
  198.  * Ship's angle.
  199.  */
  200. public double getTheta() {
  201.     return (currentFrame*2*Math.PI/numFrames + Math.PI/2);
  202. } /*getTheta*/
  203.  
  204.  
  205.  
  206. /**
  207.  * Ship's speed.
  208.  */
  209. public double getSpeed() {
  210.     return Math.sqrt(velocity_x*velocity_x + velocity_y*velocity_y);
  211. } /*getSpeed*/
  212.  
  213.  
  214.  
  215. /**
  216.  * Handle collision with an actor.
  217.  */
  218. protected void collideWithActor (Actor theActor)
  219. {
  220.     String theActorClassName= theActor.getClass().getName();
  221.   
  222.     if (theActorClassName.equals("Asteroid") ||
  223.             theActorClassName.equals("Goobie") ||
  224.             theActorClassName.equals("Bigoobie") ) {
  225.         explode();
  226.     } /*endif*/
  227.   
  228. } /*collideWithActor*/
  229.  
  230.  
  231.  
  232. /**
  233.  * Explode ship.
  234.  */
  235. public void explode()
  236. {
  237.     Explosion anExplosion;
  238.   
  239.     //
  240.     // Tell the ActorManager that I'm gone, and an Explosion Actor
  241.     // should be added.
  242.     //
  243.     owner.actorManager.removeActor(this);
  244.     anExplosion= new Explosion(owner, this);  
  245.     owner.actorManager.addActor(anExplosion);
  246.   
  247.     //
  248.     // Lower ship counter.
  249.     //
  250.     ((Boinkaroids)owner).decrementShipCount();
  251.   
  252. } /*explode*/
  253.  
  254.  
  255. } /*Ship*/
  256.