home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / Gamelication / examples / Boink / RCS / Ball.java,v next >
Encoding:
Text File  |  1998-04-13  |  2.0 KB  |  105 lines

  1. head    1.1;
  2. access;
  3. symbols;
  4. locks
  5.     jgh8962:1.1; strict;
  6. comment    @# @;
  7.  
  8.  
  9. 1.1
  10. date    98.04.13.23.33.29;    author jgh8962;    state Exp;
  11. branches;
  12. next    ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19. 1.1
  20. log
  21. @Initial revision
  22. @
  23. text
  24. @/**
  25.  *
  26.  * Ball.java
  27.  * @@author    Mark G. Tacchi (mtacchi@@next.com) 
  28.  * @@version    1.0
  29.  * Feb 23/1996
  30.  *
  31.  * A bouncy ball.
  32.  *
  33.  */
  34.  
  35. import java.lang.Math;
  36. import java.awt.Image;
  37. import com.next.gt.*;
  38.  
  39. public class Ball extends Actor {
  40.   int            bounceCount;
  41.   Image            theImage;
  42.   int            MAX_VELOLCITY_X= 200;
  43.   int            MAX_VELOLCITY_Y= 1200;
  44.   
  45. Ball(Gamelication theOwner) {
  46.  
  47.   owner= theOwner;
  48.   x= (Math.random()*owner.getSize().width);
  49.   y= (Math.random()*owner.getSize().height);
  50.       velocity_x= (double)((int)owner.randBetween(0.5,1.5)*2 - 1) * Math.random()*MAX_VELOLCITY_X;
  51.   velocity_y= Math.random()*MAX_VELOLCITY_Y;
  52.   bounceCount= 0;
  53.  
  54.   theImage= owner.getImage(owner.getCodeBase(), "images/pingPong.gif");
  55.  
  56.   setImage (theImage, 4, 16);
  57.   currentFrame= (int) owner.randBetween(0., (double)numFrames);
  58.   wrapAround= false;
  59.   
  60. } /*Ball()*/
  61.  
  62.  
  63.  
  64. /**
  65.  * Handle left rotation.
  66.  */
  67. public void calculateNewPosition() {
  68.   super.calculateNewPosition();
  69.   //
  70.   // check for out of bounds and rebound
  71.   //
  72.   if (x > (owner.getSize().width - width)) {
  73.       velocity_x= - velocity_x;
  74.       x= owner.getSize().width - width;
  75.   }
  76.   else if (x < 0) {
  77.     velocity_x= - velocity_x;
  78.     x= 0;
  79.   }
  80.   if (y > (owner.getSize().height - height)) {
  81.     velocity_y= - velocity_y * 0.75;
  82.       System.out.println( "Outside the height of the screen." );
  83.       System.out.println( "    Bottom Y coord is: " + (y+height)  );
  84.       System.out.println( "    Owners height is: " + owner.getSize().height );
  85.       System.out.println( "    My height is: " + height );
  86.     y= owner.getSize().height - height;
  87.     if(bounceCount++ > 8) {        // give it a kick of energy after a while
  88.       bounceCount= 0;
  89.       velocity_y= -Math.random() * MAX_VELOLCITY_Y;
  90.     } /*endif*/
  91.   
  92.   }
  93.   else if (y < 0) {
  94.     velocity_y= - velocity_y * 0.75;
  95.     y= 0;
  96.   }
  97.   
  98.   velocity_y+= 5; 
  99.  
  100. } /*calculateNewPosition*/
  101.  
  102.  
  103. } /*Ball*/
  104. @
  105.