home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / Gamelication / examples / Boinkaroids / com / next / gt / backup / Actor.java next >
Encoding:
Java Source  |  1998-04-07  |  6.3 KB  |  298 lines

  1. /**
  2.  *
  3.  * Actor.java
  4.  * @author    Mark G. Tacchi (mtacchi@next.com) 
  5.  * @version    0.8
  6.  * Feb 23/1996
  7.  *
  8.  * Actor is an abstract superclass which defines behaviour used in the Actors
  9.  * the developer creates.  
  10.  * 
  11.  * Actors are responsible for calculating their positions each tick.  They are
  12.  * also handed a <em>g</em> and expected to draw themselves into it.
  13.  *
  14.  */
  15.  
  16. package com.next.gt;
  17.  
  18. import java.util.Date;
  19. import java.awt.Image;
  20. import java.awt.Graphics;
  21. import java.awt.Rectangle;
  22. import java.applet.Applet;
  23. import java.lang.Math;
  24.  
  25. public abstract class Actor extends java.lang.Object {
  26.  
  27.   //
  28.   // Image and animation variables.
  29.   //
  30.   protected Image    image;
  31.   protected int     numFrames;
  32.   public int     width; 
  33.   public int     height;
  34.   public int     currentFrame;
  35.   protected int     hFrames;
  36.   
  37.   //
  38.   // Position and velocity.
  39.   //
  40.   public double    x, y;
  41.   public double    x_old, y_old;
  42.   public double    velocity_x, velocity_y;
  43.   
  44.   //
  45.   // The object that owns the Actor.
  46.   //
  47.   public Gamelet    owner;
  48.   
  49.   //
  50.   // Flag indicating whether Actor should wrap at edge of screen.
  51.   //
  52.  public boolean wrapAround;
  53.  
  54.  
  55. public Actor() {
  56.   currentFrame= 0;
  57.   wrapAround= true;
  58. }/*Actor()*/
  59.  
  60.  
  61.  
  62. /**
  63.  * Change animation frame, calculate new position, and calculate new velocity..
  64.  */
  65. public void tick() {
  66.   calculateCurrentFrame();
  67.   calculateNewPosition();
  68.   calculateNewVelocity();
  69. } /*tick*/
  70.  
  71.  
  72.  
  73. /**
  74.  * Set the image used for animation.
  75.  */
  76. protected void setImage (Image theImage, 
  77.                  int frameXSize, 
  78.                  int frameYSize, 
  79.                  int framesHorizontally, 
  80.                  int totalFrames){
  81.   width= frameXSize; 
  82.   height= frameYSize;
  83.   numFrames= totalFrames;
  84.   hFrames= framesHorizontally;
  85.   image= theImage;
  86.   
  87. } /*setImage(,,,,,)*/
  88.  
  89.  
  90.  
  91. /**
  92.  * Set the image used for animation.  Good for an image that has all frames
  93.  * within it and none empty.
  94.  */
  95. protected void setImage (Image theImage){
  96.   int imageHeight;
  97.   int imageWidth;
  98.  
  99.   do { 
  100.     imageHeight= theImage.getHeight (owner);
  101.   } while (imageHeight == -1);
  102.   do {
  103.     imageWidth= theImage.getWidth (owner);
  104.   } while (imageWidth == -1);
  105.  
  106.   setImage (theImage, imageWidth, imageHeight, 1, 1);
  107. }/*setImage*/
  108.  
  109.  
  110.  
  111. /**
  112.  * Set the image used for animation.  Good for an image that has some empty
  113.  * frames within it.
  114.  */
  115. protected void setImage (Image theImage, 
  116.                  int horizontalFrames, 
  117.                  int totalFrames){
  118.   int imageHeight;
  119.   int imageWidth;
  120.  
  121.   do {
  122.     imageHeight= theImage.getHeight (owner);
  123.   } while (imageHeight == -1);
  124.   do {
  125.     imageWidth= theImage.getWidth (owner);
  126.   } while (imageWidth == -1);
  127.    
  128.   setImage (theImage, imageWidth/horizontalFrames, 
  129.              imageHeight / (int)Math.ceil((double)totalFrames/(double)horizontalFrames),
  130.              horizontalFrames,
  131.              totalFrames 
  132.             );
  133. }/*setImage,,,*/
  134.  
  135.  
  136.  
  137. /**
  138.  * Calculates the new X and Y position based on velocity and time.  Also may check if
  139.  * Actor needs to wrap around at the edges if the <em>wraparound</em> flag is set.
  140.  */
  141. protected void calculateNewPosition() {
  142.  
  143.   double    deltaTime= owner.deltaTickTimeMillis()/1000.0;
  144.  
  145.   //
  146.   // save old position
  147.   //
  148.   x_old= x;
  149.   y_old= y;
  150.   
  151.   //
  152.   // calculate position based on velocity and time
  153.   //
  154.   x+= velocity_x*deltaTime;
  155.   y+= velocity_y*deltaTime;
  156.   
  157.  if (wrapAround) checkForOutOfBounds();
  158.  
  159. } /*calculateNewPosition*/
  160.  
  161.  
  162.  
  163. /**
  164.  * Override this to provide your own behaviour.
  165.  */
  166. protected void calculateNewVelocity() {
  167. }
  168.     
  169.     
  170.     
  171. /**
  172.  * Calculates the current frame.  Behaviour is to flip through frames sequentially
  173.  * and loop.
  174.  */
  175. protected void calculateCurrentFrame() {
  176.   if (++currentFrame>=numFrames) currentFrame= 0;
  177. } /*calculateCurrentFrame*/
  178.  
  179.  
  180.  
  181. /**
  182.  * Check for out of bounds and wrap if it is.
  183.  */
  184. protected void checkForOutOfBounds() {
  185.  
  186.   //
  187.   // check for out of bounds and wrap
  188.   //
  189.   if (x > (owner.size().width + width + width)) {
  190.     x= -width;
  191.   }
  192.   else if (x < -width) {
  193.     x= owner.size().width + width + width;
  194.   }
  195.   
  196.   if (y > (owner.size().height + height + height)) {
  197.     y= -height;  
  198.   }
  199.   else if (y < -height) {
  200.     y= owner.size().height + height + height;
  201.   }
  202.   
  203. } /*checkForOutOfBounds*/
  204.  
  205.  
  206.  
  207. /**
  208.  * Each Actor is handed a <em>g</em> and is expected to draw itself in it.
  209.  */ 
  210. public void draw (Graphics g){
  211.   double    offsetx= -(currentFrame%hFrames)*width;
  212.   double    offsety= -Math.floor(currentFrame/hFrames) * height;
  213.  
  214.   Graphics     g2= g.create ((int)x, (int)y, width, height);
  215.   g2.drawImage(image, (int)offsetx, (int)offsety, owner);
  216.   g2.dispose ();
  217.  
  218. }/*draw*/
  219.  
  220.  
  221.  
  222. /**
  223.  * Override this method to handle the case when Actor collides with another.
  224.  */ 
  225. protected void collideWithActor (Actor theActor){
  226. } /*collideWithActor*/
  227.  
  228.  
  229.  
  230. /**
  231.  * Bounce off the specified Actor.  Changes it's velocity so that it appears
  232.  * to bounce off.
  233.  */ 
  234. public void bounceOff(Actor theActor) {
  235.   double        myCenterX= width/2 + x_old, myCenterY= height/2 + y_old;
  236.   double        actorCenterX= theActor.width/2 + theActor.x;
  237.   double        actorCenterY= theActor.height/2 + theActor.y;
  238.   double    slope= (myCenterY - actorCenterY)/(myCenterX - actorCenterX);    
  239.   double        b= myCenterY - slope*myCenterX;
  240.   
  241.  
  242.   double        intersectY, intersectX;
  243.     
  244.   //
  245.   // Check if segments intersect.
  246.   //
  247.   
  248.   //
  249.   // Check RIGHT side
  250.   //
  251.   if (theActor.x>=myCenterX) {
  252.     intersectY= slope*theActor.x + b;
  253.     if (intersectY>=theActor.y && intersectY<=theActor.y+theActor.height) {
  254.       velocity_x= theActor.velocity_x - velocity_x;
  255.       x= x_old;
  256.     } /*endif*/
  257.   }
  258.   //
  259.   // Check LEFT side
  260.   //
  261.   else if (theActor.x+theActor.width<=myCenterX) {
  262.     intersectY= slope*(theActor.x + theActor.width) + b;
  263.     if (intersectY>=theActor.y && intersectY<=theActor.y+theActor.height) {
  264.       velocity_x= theActor.velocity_x - velocity_x;
  265.       x= x_old;
  266.     } /*endif*/
  267.   }
  268.   
  269.   //
  270.   // Check BOTTOM side
  271.   //
  272.   else if (theActor.y>=myCenterY) {
  273.     intersectX= (theActor.y - b)/slope;
  274.     if (intersectX>=theActor.x && intersectX<=theActor.x+theActor.width) {
  275.       velocity_y= theActor.velocity_y - velocity_y;
  276.       y= y_old;
  277.     } /*endif*/
  278.   }
  279.   //
  280.   // Check TOP side
  281.   //
  282.   else if (theActor.y+theActor.height<=myCenterY) {
  283.     intersectX= (theActor.y + theActor.height - b)/slope;
  284.     if (intersectX>=theActor.x && intersectX<=theActor.x+theActor.width) {
  285.       velocity_y= theActor.velocity_y - velocity_y;
  286.       y= y_old;
  287.     } /*endif*/
  288.   }
  289.   
  290. } /*bounceOff*/
  291.  
  292. } /*BOActor*/
  293.  
  294.  
  295.  
  296.  
  297.  
  298.