home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / Gamelication / examples / Boinkaroids / RCS / Ship.java,v < prev   
Encoding:
Text File  |  1998-04-15  |  9.9 KB  |  615 lines

  1. head    1.7;
  2. access;
  3. symbols;
  4. locks; strict;
  5. comment    @# @;
  6.  
  7.  
  8. 1.7
  9. date    98.04.15.16.07.58;    author mds1274;    state Exp;
  10. branches;
  11. next    1.6;
  12.  
  13. 1.6
  14. date    98.04.15.15.48.12;    author mds1274;    state Exp;
  15. branches;
  16. next    1.5;
  17.  
  18. 1.5
  19. date    98.04.15.15.41.04;    author mds1274;    state Exp;
  20. branches;
  21. next    1.4;
  22.  
  23. 1.4
  24. date    98.04.14.16.33.47;    author jgh8962;    state Exp;
  25. branches;
  26. next    1.3;
  27.  
  28. 1.3
  29. date    98.04.11.16.20.09;    author jgh8962;    state Exp;
  30. branches;
  31. next    1.2;
  32.  
  33. 1.2
  34. date    98.04.10.18.25.57;    author jgh8962;    state Exp;
  35. branches;
  36. next    1.1;
  37.  
  38. 1.1
  39. date    98.04.10.17.53.49;    author jgh8962;    state Exp;
  40. branches;
  41. next    ;
  42.  
  43.  
  44. desc
  45. @@
  46.  
  47.  
  48. 1.7
  49. log
  50. @*** empty log message ***
  51. @
  52. text
  53. @/*
  54.  *
  55.  * Ship.java
  56.  * @@author    Mark G. Tacchi (mtacchi@@next.com) 
  57.  * @@version    0.8
  58.  * Mar 11/1996
  59.  *
  60.  * The ship is controlled by the user, it registers for specific keyboard
  61.  * events to handle control.
  62.  *
  63.  * This Actor collides with Asteroids and Goobies.  It is responsible for
  64.  * creating an explosion object.
  65.  *
  66.  */
  67.  
  68. import java.awt.event.*;
  69. import java.lang.Math;
  70. import java.awt.*;
  71.  
  72. import com.next.gt.*;
  73.  
  74. public class Ship extends Actor implements KeyListener{
  75.  
  76.     //
  77.     // Limit number of bullets on the screen at once
  78.     //
  79.     private static int         MAX_NUM_BULLETS= 5;
  80.     public int                numBullets= 0;
  81.   
  82.     //
  83.     // Animation.  ccw= 1, cw= -1
  84.     //
  85.     public int                animationDirection= 1;
  86.     public boolean            isAnimating= true; 
  87.   
  88.     //
  89.     // Is thrusting
  90.      //
  91.     public boolean            thrusting= false; 
  92.   
  93. Ship(Gamelication theOwner) {
  94.     super();
  95.  
  96.     Image theImage; 
  97.     owner = theOwner;  
  98.   
  99.     //owner.addKeyListener( this );
  100.   
  101.     //
  102.     // play warp in sound
  103.     //
  104.     owner.play(owner.getCodeBase(), "sounds/warp.au");
  105.   
  106.     x= (owner.getSize().width/2.0);
  107.     y= (owner.getSize().height/2.0);
  108.     velocity_x= 0;
  109.     velocity_y= 0;
  110.     String    theImageName= "images/ship.gif";
  111.  
  112.     theImage= owner.getImage(owner.getCodeBase(), "images/ship.gif");
  113.     setImage (theImage, 4, 24);
  114.     isAnimating= false;  
  115.   
  116. } /*Ship()*/
  117.  
  118.  
  119.  
  120. /**
  121.  * Handle keyboard events that control ship.
  122.  */
  123. public void keyPressed( KeyEvent k ){
  124.     if(k.getKeyCode() == k.VK_RIGHT )
  125.         this.rotateRight(true);
  126.     if(k.getKeyCode() == k.VK_LEFT )
  127.         this.rotateLeft(true);
  128.     if(k.getKeyCode() == k.VK_UP )
  129.         this.thrust(true);
  130.     if( k.getKeyCode() == k.VK_SPACE ) {
  131.         this.fire();
  132.     }
  133. }
  134.  
  135. public void keyReleased( KeyEvent k ){
  136.     if(k.getKeyCode() == k.VK_RIGHT )
  137.         this.rotateRight(false);
  138.     if(k.getKeyCode() == k.VK_LEFT )
  139.         this.rotateLeft(false);
  140.     if(k.getKeyCode() == k.VK_UP )
  141.         this.thrust(false);
  142. }
  143.  
  144. public void keyTyped( KeyEvent k ){
  145.  
  146. }
  147.  
  148.  
  149.  
  150. /**
  151.  * If ship is thrusting, then velocity is increasing.  Use friction if
  152.  * not thrusting.
  153.  */
  154. public void calculateNewVelocity() {
  155.     if (thrusting) {
  156.         velocity_x+= Math.cos(currentFrame*2*Math.PI/numFrames + Math.PI/2)*10;
  157.         velocity_y+= Math.sin(currentFrame*2*Math.PI/numFrames - Math.PI/2)*10;
  158.     }
  159.     else {
  160.         velocity_x*= 0.99;
  161.          velocity_y*= 0.99;
  162.     }
  163.   
  164. } /*calculateNewVelocity*/
  165.  
  166. /**
  167.  * Animation of the ship is based on theta, display accordingly.
  168.  */
  169. public void calculateCurrentFrame() {
  170.     if (isAnimating) {
  171.         if (animationDirection== -1) {
  172.             if (--currentFrame<=0) currentFrame= numFrames - 1;
  173.         }
  174.         else {
  175.             if (++currentFrame>=numFrames) currentFrame= 0;
  176.         }
  177.     } /*endif*/
  178.     
  179. } /*calculateCurrentFrame*/
  180.  
  181.  
  182. /**
  183.  * Handle left rotation.
  184.  */
  185. public void rotateLeft (boolean keydown) {
  186.     if (keydown) {
  187.         isAnimating= true;
  188.         animationDirection= 1;
  189.     }
  190.     else {
  191.         isAnimating= false;
  192.     }
  193.   
  194. } /*rotateLeft*/
  195.  
  196.  
  197.  
  198. /**
  199.  * Handle right rotation.
  200.  */
  201. public void rotateRight (boolean keydown) {
  202.     if (keydown) {
  203.         animationDirection= -1;
  204.         isAnimating= true;
  205.     }
  206.     else {
  207.         isAnimating= false;
  208.     }
  209.   
  210. } /*rotateRight*/
  211.  
  212.  
  213.  
  214. /**
  215.  * Handle thrust.
  216.  */
  217. public void thrust (boolean keydown) {
  218.     if (keydown) {
  219.         thrusting= true;
  220.     }
  221.     else {
  222.         thrusting= false;
  223.     }
  224.   
  225. } /*thrust*/
  226.  
  227.  
  228.  
  229. /**
  230.  * Fire bullet.
  231.  */
  232. public void fire() {
  233.     if (numBullets<MAX_NUM_BULLETS&&!((Boinkaroids)owner).createNewPlayer) {
  234.         Bullet aBullet = new Bullet(owner, this);
  235.     
  236.         numBullets++;
  237.         owner.play(owner.getCodeBase(), "sounds/bullet.au");
  238.         owner.actorManager.addActor(aBullet);
  239.     } /*endif*/
  240.   
  241. } /*fire*/
  242.  
  243.  
  244.  
  245. /**
  246.  * Accessor methods (bullet uses this).
  247.  */
  248.  
  249. /**
  250.  * Ship's angle.
  251.  */
  252. public double getTheta() {
  253.     return (currentFrame*2*Math.PI/numFrames + Math.PI/2);
  254. } /*getTheta*/
  255.  
  256.  
  257.  
  258. /**
  259.  * Ship's speed.
  260.  */
  261. public double getSpeed() {
  262.     return Math.sqrt(velocity_x*velocity_x + velocity_y*velocity_y);
  263. } /*getSpeed*/
  264.  
  265.  
  266.  
  267. /**
  268.  * Handle collision with an actor.
  269.  */
  270. protected void collideWithActor (Actor theActor)
  271. {
  272.     String theActorClassName= theActor.getClass().getName();
  273.   
  274.     if (theActorClassName.equals("Asteroid") ||
  275.             theActorClassName.equals("Goobie") ||
  276.             theActorClassName.equals("Bigoobie") ) {
  277.         explode();
  278.     } /*endif*/
  279.   
  280. } /*collideWithActor*/
  281.  
  282.  
  283.  
  284. /**
  285.  * Explode ship.
  286.  */
  287. public void explode()
  288. {
  289.     Explosion anExplosion;
  290.   
  291.     //
  292.     // Tell the ActorManager that I'm gone, and an Explosion Actor
  293.     // should be added.
  294.     //
  295.     owner.actorManager.removeActor(this);
  296.     anExplosion= new Explosion(owner, this);  
  297.     owner.actorManager.addActor(anExplosion);
  298.   
  299.     //
  300.     // Lower ship counter.
  301.     //
  302.     ((Boinkaroids)owner).decrementShipCount();
  303.   
  304. } /*explode*/
  305.  
  306.  
  307. } /*Ship*/
  308. @
  309.  
  310.  
  311. 1.6
  312. log
  313. @*** empty log message ***
  314. @
  315. text
  316. @d47 1
  317. a47 1
  318.     owner.addKeyListener( this );
  319. @
  320.  
  321.  
  322. 1.5
  323. log
  324. @*** empty log message ***
  325. @
  326. text
  327. @a16 1
  328. import java.applet.AudioClip;
  329. d24 5
  330. a28 5
  331.   //
  332.   // Limit number of bullets on the screen at once
  333.   //
  334.   private static int         MAX_NUM_BULLETS= 5;
  335.   public int                numBullets= 0;
  336. d30 5
  337. a34 5
  338.   //
  339.   // Animation.  ccw= 1, cw= -1
  340.   //
  341.   public int                animationDirection= 1;
  342.   public boolean            isAnimating= true; 
  343. d36 4
  344. a39 4
  345.   //
  346.   // Is thrusting
  347.   //
  348.   public boolean            thrusting= false; 
  349. d42 1
  350. a42 1
  351.   super();
  352. d44 2
  353. a45 2
  354.   Image theImage; 
  355.   owner = theOwner;  
  356. d47 1
  357. a47 1
  358.   owner.addKeyListener( this );
  359. d49 4
  360. a52 4
  361.   //
  362.   // play warp in sound
  363.   //
  364.   owner.play(owner.getCodeBase(), "sounds/warp.au");
  365. d54 5
  366. a58 5
  367.   x= (owner.getSize().width/2.0);
  368.   y= (owner.getSize().height/2.0);
  369.   velocity_x= 0;
  370.   velocity_y= 0;
  371.   String    theImageName= "images/ship.gif";
  372. d60 3
  373. a62 3
  374.   theImage= owner.getImage(owner.getCodeBase(), "images/ship.gif");
  375.   setImage (theImage, 4, 24);
  376.   isAnimating= false;  
  377. d103 8
  378. a110 8
  379.   if (thrusting) {
  380.     velocity_x+= Math.cos(currentFrame*2*Math.PI/numFrames + Math.PI/2)*10;
  381.     velocity_y+= Math.sin(currentFrame*2*Math.PI/numFrames - Math.PI/2)*10;
  382.   }
  383.   else {
  384.     velocity_x*= 0.99;
  385.     velocity_y*= 0.99;
  386.   }
  387. d118 8
  388. a125 8
  389.    if (isAnimating) {
  390.      if (animationDirection== -1) {
  391.        if (--currentFrame<=0) currentFrame= numFrames - 1;
  392.      }
  393.      else {
  394.       if (++currentFrame>=numFrames) currentFrame= 0;
  395.      }
  396.     } /*endif*/
  397. d134 7
  398. a140 7
  399.   if (keydown) {
  400.     isAnimating= true;
  401.     animationDirection= 1;
  402.   }
  403.   else {
  404.     isAnimating= false;
  405.   }
  406. d150 7
  407. a156 7
  408.   if (keydown) {
  409.     animationDirection= -1;
  410.     isAnimating= true;
  411.   }
  412.   else {
  413.     isAnimating= false;
  414.   }
  415. d166 6
  416. a171 6
  417.   if (keydown) {
  418.     thrusting= true;
  419.   }
  420.   else {
  421.     thrusting= false;
  422.   }
  423. d181 2
  424. a182 3
  425.  
  426.   if (numBullets<MAX_NUM_BULLETS&&!((Boinkaroids)owner).createNewPlayer) {
  427.     Bullet aBullet = new Bullet(owner, this);
  428. d184 4
  429. a187 5
  430.     numBullets++;
  431.     owner.play(owner.getCodeBase(), "sounds/bullet.au");
  432.     //aBullet= new Bullet(owner, this);  
  433.     owner.actorManager.addActor(aBullet);
  434.   } /*endif*/
  435. d201 1
  436. a201 1
  437.   return (currentFrame*2*Math.PI/numFrames + Math.PI/2);
  438. d210 1
  439. a210 1
  440.   return Math.sqrt(velocity_x*velocity_x + velocity_y*velocity_y);
  441. d220 1
  442. a220 1
  443.   String theActorClassName= theActor.getClass().getName();
  444. d222 5
  445. a226 5
  446.   if (theActorClassName.equals("Asteroid") ||
  447.       theActorClassName.equals("Goobie") ||
  448.       theActorClassName.equals("Bigoobie") ) {
  449.     explode();
  450.   } /*endif*/
  451. d237 1
  452. a237 1
  453.   Explosion anExplosion;
  454. d239 7
  455. a245 7
  456.   //
  457.   // Tell the ActorManager that I'm gone, and an Explosion Actor
  458.   // should be added.
  459.   //
  460.   owner.actorManager.removeActor(this);
  461.   anExplosion= new Explosion(owner, this);  
  462.   owner.actorManager.addActor(anExplosion);
  463. d247 4
  464. a250 4
  465.   //
  466.   // Lower ship counter.
  467.   //
  468.   ((Boinkaroids)owner).decrementShipCount();
  469. @
  470.  
  471.  
  472. 1.4
  473. log
  474. @*** empty log message ***
  475. @
  476. text
  477. @d44 3
  478. a46 3
  479.     
  480.   Image            theImage; 
  481.   owner= theOwner;  
  482. d63 1
  483. a63 1
  484.  isAnimating= false;  
  485. d79 1
  486. a79 1
  487.     if( k.getKeyCode() == k.VK_SPACE )
  488. d81 1
  489. a130 1
  490.  
  491. @
  492.  
  493.  
  494. 1.3
  495. log
  496. @*** empty log message ***
  497. @
  498. text
  499. @d44 1
  500. a44 3
  501.   
  502.   System.out.println( "Making a ship." );
  503.   
  504. d79 1
  505. a79 2
  506.     if( k.getKeyCode() == k.VK_SPACE ) {
  507.         System.out.println( "keyPressed calling fire." );
  508. a80 1
  509.     }
  510. a182 1
  511.   System.out.println( "Fire called." );
  512. @
  513.  
  514.  
  515. 1.2
  516. log
  517. @Blah.
  518. @
  519. text
  520. @d45 2
  521. d81 2
  522. a82 1
  523.     if( k.getKeyCode() == k.VK_SPACE )
  524. d84 1
  525. d187 1
  526. d189 1
  527. a189 1
  528.     Bullet aBullet;
  529. d193 1
  530. a193 1
  531.     aBullet= new Bullet(owner, this);  
  532. @
  533.  
  534.  
  535. 1.1
  536. log
  537. @Initial revision
  538. @
  539. text
  540. @d16 1
  541. a16 1
  542. //import java.applet.Applet;
  543. d23 1
  544. a23 1
  545. public class Ship extends Actor implements EventHandler{
  546. d48 2
  547. a63 6
  548.  
  549.   int events[]=    {    Event.KEY_ACTION, 
  550.                       Event.KEY_ACTION_RELEASE,
  551.                     Event.KEY_PRESS,
  552.                     Event.KEY_RELEASE
  553.                 };
  554. a64 2
  555.  owner.eventManager.registerForEventNotification(this,events);
  556.  
  557. d72 10
  558. a81 46
  559. public boolean handleRequestedEvent (AWTEvent theEvent) {
  560.   switch(theEvent.getID) {
  561.   case Event.KEY_ACTION:
  562.     switch(theEvent.key) {
  563.       case Event.RIGHT:
  564.         this.rotateRight(true);
  565.         return true;
  566.       case Event.LEFT:
  567.         this.rotateLeft(true);
  568.         return true;
  569.       case Event.UP:                    //THRUST ON
  570.         this.thrust(true);
  571.         return true;
  572.     } /*endSwitch*/
  573.     break;
  574.   case Event.KEY_ACTION_RELEASE:
  575.     switch(theEvent.key) {
  576.       case Event.RIGHT:
  577.         this.rotateRight(false);
  578.         return true;
  579.       case Event.LEFT:
  580.         this.rotateLeft(false);
  581.         return true;
  582.       case Event.UP:                    //THRUST OFF
  583.         this.thrust(false);
  584.         return true;
  585.     } /*endSwitch*/
  586.     break;
  587.     case Event.KEY_PRESS:
  588.       switch(theEvent.key) {
  589.         case 32:
  590.           this.fire();
  591.           return true;
  592.       } /*endSwitch*/
  593.     break;  
  594.     case Event.KEY_RELEASE:
  595.       switch(theEvent.key) {
  596.         case 32:
  597.           return true;
  598.       } /*endSwitch*/
  599.     break;
  600.   } /*endSwitch*/
  601.   
  602.   return false;
  603.   
  604. } /*handleRequestedEvent*/
  605. d83 8
  606. d92 1
  607. d94 4
  608. a113 2
  609.  
  610.  
  611. d183 1
  612. a183 1
  613.   if (numBullets<MAX_NUM_BULLETS) {
  614. @
  615.