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

  1. head    1.2;
  2. access;
  3. symbols;
  4. locks; strict;
  5. comment    @# @;
  6.  
  7.  
  8. 1.2
  9. date    98.04.15.15.48.52;    author jgh8962;    state Exp;
  10. branches;
  11. next    1.1;
  12.  
  13. 1.1
  14. date    98.04.10.17.53.01;    author jgh8962;    state Exp;
  15. branches;
  16. next    ;
  17.  
  18.  
  19. desc
  20. @@
  21.  
  22.  
  23. 1.2
  24. log
  25. @*** empty log message ***
  26. @
  27. text
  28. @/**
  29.  *
  30.  * ActorManager.java
  31.  * @@author    Mark G. Tacchi (mtacchi@@next.com)
  32.  * @@version    0.8
  33.  * Mar 27/1996
  34.  *
  35.  * ActorManager maintains a list of all actors which are participating in
  36.  * the game.  Actors may be added or removed from this list by requesting
  37.  * a change in the list.  When it is safe to make changes to the list,
  38.  * ActorManager will do so.
  39.  *
  40.  * Collision detection is performed in ActorManager.  If Actors collide,
  41.  * a message is sent to each Actor indicating which Actor it has collided
  42.  * with.
  43.  *
  44.  * Queries can be made to ActorManager to determine whether an Actor is in
  45.  * a specific location.
  46. */
  47.  
  48. package com.next.gt;
  49.  
  50. import java.util.Vector;
  51. import java.awt.Rectangle;
  52.  
  53.  
  54. public class ActorManager extends java.lang.Object { 
  55.  
  56.   //
  57.   //  A reference back to the Gamelication this manager is servicing.
  58.   //
  59.   protected Gamelication owner;
  60.   
  61.   //
  62.   //  The list of actors currently alive.  
  63.   //
  64.   public Vector actors= new Vector();
  65.  
  66.   //
  67.   //  The list of actors that will be removed or added when
  68.   //  the current heartbeat is over.
  69.   // 
  70.   private Vector actorsScheduledForRemoval= new Vector();
  71.   private Vector actorsScheduledForInsertion= new Vector();
  72.  
  73.   private boolean    removeAllActors= false;
  74.   
  75. public ActorManager(Gamelication theOwner) {
  76.   owner= theOwner;
  77. }
  78.  
  79.  
  80.  
  81. /**
  82.  * The Actor is added to a list of Actors to be added.
  83.  */
  84. public void addActor (Actor theActor)
  85. {
  86.   actorsScheduledForInsertion.addElement (theActor);
  87. }/*addActor*/
  88.  
  89.  
  90.  
  91. /**
  92.  * The Actor is added to a list of Actors to be removed.
  93.  */
  94. public void removeActor (Actor theActor)
  95. {
  96.   actorsScheduledForRemoval.addElement (theActor);
  97. }/*removeActor*/
  98.  
  99.  
  100.  
  101. /**
  102.  * Dump all references to Actors.  This is used when a game is restarted.
  103.  */
  104. public void removeAllActors ()
  105. {
  106.   //
  107.   // give eventManager a chance to clean up
  108.   //
  109.   for (int i= 0; i< actors.size(); i++) {
  110.     //owner.eventManager.removeFromNotificationRegistry (actors.elementAt (i));
  111.   } /*next_i*/
  112.   
  113.   //
  114.   // destroy pending lists
  115.   //
  116.   actorsScheduledForRemoval.removeAllElements ();
  117.   actorsScheduledForInsertion.removeAllElements ();
  118.   
  119.   //
  120.   // destroy all actors
  121.   //
  122.   actors.removeAllElements ();
  123.  
  124. }/*removeAllActors*/
  125.  
  126.  
  127.  
  128. /**
  129.  * Test if there is an actor at a specified position.
  130.  */
  131. public boolean isActorAt(double theX, double theY) {
  132.   boolean returnValue= false;
  133.      
  134.   for (int i= 0; i < actors.size(); i++){
  135.     Actor anActor= (Actor)actors.elementAt(i);
  136.  
  137.     if (theX >= anActor.x) {
  138.       if ((theX <= (anActor.x + anActor.width)) && 
  139.           (theY >= anActor.y) &&
  140.           (theY <= (anActor.y+anActor.height))) {
  141.         returnValue= true;
  142.       }/*endif*/
  143.     }
  144.     else{
  145.       break;
  146.     }
  147.   }/*next_i*/
  148.     
  149.   return returnValue;
  150. } /*isActorAt*/
  151.  
  152.  
  153.  
  154. /**
  155.  * Test if there is an actor within a specified Rectangle.
  156.  */
  157. public boolean isActorIn(Rectangle theRectangle) {
  158.   boolean returnValue= false;
  159.   double maxxj= theRectangle.x + theRectangle.width;
  160.      
  161.   for (int i= 0; i < actors.size(); i++){
  162.     Actor anActor= (Actor)actors.elementAt(i);
  163.  
  164.     if (maxxj >= anActor.x){
  165.       if (theRectangle.y <= anActor.y){
  166.         if (theRectangle.y + theRectangle.height > anActor.y)
  167.           returnValue= true;
  168.       }
  169.       else{
  170.         if (anActor.y + anActor.height > theRectangle.y)
  171.           returnValue= true;
  172.       }
  173.     }
  174.     else{
  175.       break;
  176.     }
  177.   }/*next_i*/
  178.     
  179.   return returnValue;
  180. } /*isActorIn*/
  181.  
  182.  
  183.  
  184. /**
  185.  * Insertion sort is used to prep Actors for collision detection.  This
  186.  * technique is ideal because actors are almost always already sorted.
  187.  */
  188. private final void sortActorsByXCoordinate()
  189. {
  190.   int i, j;
  191.   int size= actors.size();
  192.  
  193.   for (j= 1; j < size; j++){
  194.      Actor aj= (Actor)actors.elementAt(j);
  195.  
  196.      for (i= j-1; i>=0; i--){        
  197.         Actor ai= (Actor)actors.elementAt(i);
  198.  
  199.         if (aj.x < ai.x)
  200.             actors.setElementAt (ai, i+1);
  201.         else
  202.             break;
  203.      }/*next_i*/
  204.  
  205.      if (j != i+1)
  206.        actors.setElementAt (aj, i+1);
  207.   }/*next_j*/
  208. }/*sortActorsByXCoordinate*/
  209.  
  210.  
  211.  
  212. /*
  213.  * Perform collision detection based on rectangles.  Future versions will
  214.  * detect against circles, and polygons.
  215.  */
  216. private final void detectCollision ()
  217. {
  218.   int i, j;
  219.   int size= actors.size();
  220.  
  221.   for (j= 0; j+1 < size; j++){
  222.     Actor aj= (Actor)actors.elementAt(j);
  223.     double maxxj= aj.x + aj.width;
  224.      
  225.     for (i= j+1; i < size; i++){
  226.       Actor ai = (Actor)actors.elementAt(i);
  227.  
  228.       if (maxxj >= ai.x){
  229.         if (aj.y <= ai.y){
  230.           if (aj.y + aj.height > ai.y)
  231.             handleBBCollision (aj, ai);
  232.         }
  233.         else{
  234.           if (ai.y + ai.height > aj.y)
  235.             handleBBCollision (aj, ai);
  236.         }
  237.       }
  238.       else{
  239.         break;
  240.       }
  241.         
  242.     }/*next_i*/
  243.   }/*next_j*/
  244.   
  245. }/*detectCollision*/
  246.  
  247.  
  248.  
  249. /**
  250.  * Tell each Actor that they've collided with each other.
  251.  */
  252. protected void handleBBCollision (Actor a1, Actor a2)
  253. {
  254.   a1.collideWithActor (a2);
  255.   a2.collideWithActor (a1);
  256. }/*handleBBCollision*/
  257.  
  258.  
  259.  
  260. /**
  261.  * Add/delete Actors, send current Actors a tick message, and then
  262.  * perform collision detection.
  263.  */
  264. public void tick() {
  265.  
  266.   //
  267.   // add all actors which were scheduled for addtion
  268.   //
  269.   if (actorsScheduledForInsertion.size() > 0) {
  270.     for (int i= 0; i < actorsScheduledForInsertion.size(); i++) {
  271.       actors.addElement (actorsScheduledForInsertion.elementAt (i));
  272.     } /*next_i*/      
  273.     actorsScheduledForInsertion.removeAllElements ();
  274.   } /*endif*/
  275.   
  276.   
  277.   //
  278.   // remove all actors which were scheduled for removal
  279.   //
  280.   if (actorsScheduledForRemoval.size() > 0) {
  281.       for (int i= 0; i < actorsScheduledForRemoval.size(); i++) {
  282.         //owner.eventManager.removeFromNotificationRegistry (actorsScheduledForRemoval.elementAt (i));
  283.         actors.removeElement (actorsScheduledForRemoval.elementAt (i));
  284.       } /*next_i*/      
  285.     actorsScheduledForRemoval.removeAllElements ();
  286.   } /*endif*/
  287.  
  288.  
  289.   //
  290.   // send a tick to each actor
  291.   //
  292.   for (int i= 0; i< actors.size (); i++) {
  293.      ((Actor)actors.elementAt(i)).tick();
  294.  } /*next_i*/
  295.  
  296.  
  297.   //
  298.   // perform collision detection
  299.   //
  300.   sortActorsByXCoordinate ();
  301.   detectCollision();
  302.  
  303. } /*tick*/
  304.  
  305. } /*ActorManager*/
  306. @
  307.  
  308.  
  309. 1.1
  310. log
  311. @Initial revision
  312. @
  313. text
  314. @d83 1
  315. a83 1
  316.     owner.eventManager.removeFromNotificationRegistry (actors.elementAt (i));
  317. d255 1
  318. a255 1
  319.         owner.eventManager.removeFromNotificationRegistry (actorsScheduledForRemoval.elementAt (i));
  320. @
  321.