home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / Gamelication / com / next / gt / RCS / Actor.java,v next >
Text File  |  1998-04-15  |  6KB  |  320 lines

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