home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / Gamelication / examples / Boinkaroids / com / next / gt / backup / Gamelet.java < prev    next >
Encoding:
Java Source  |  1996-03-30  |  3.6 KB  |  194 lines

  1. /**
  2.  *
  3.  * Gamelet.java
  4.  * @author    Mark G. Tacchi (mtacchi@next.com) 
  5.  * @version    0.8
  6.  * Mar 27/1996
  7.  *
  8.  * Gamelet contains a thread which is used for distributing ticks to all
  9.  * manager objects.  A tick is basically an instant in time and represents
  10.  * the time granularity of the game.  
  11.  *
  12.  */
  13.  
  14. package com.next.gt;
  15.  
  16. import java.util.Date;
  17. import java.util.Vector;
  18. import java.awt.Graphics;
  19. import java.awt.Event;
  20.  
  21. abstract public class Gamelet extends java.applet.Applet implements Runnable  {
  22.  
  23.   //
  24.   // This is the main thread, used for ticks.
  25.   //
  26.   public Thread runner= null;
  27.   
  28.   //
  29.   // Gamelet is the master time keeper.
  30.   //
  31.   public long currentTickTimeMillis= System.currentTimeMillis();
  32.   public long lastTickTimeMillis;
  33.   
  34.   //
  35.   // Gamelet is the manager of the managers. 
  36.   //
  37.   public ActorManager        actorManager= new ActorManager(this);
  38.   public DisplayManager        displayManager= new DisplayManager(this);
  39.   public ScoreManager        scoreManager;
  40.   public EventManager        eventManager= new EventManager();
  41.   
  42.   //
  43.   // Sleep time for the thread.
  44.   //
  45.  public int                SLEEP_MILLIS= 50;
  46.  
  47.  
  48.  
  49. /**
  50.  * Generate a random double between two doubles.
  51.  */
  52. public static double randBetween(double a, double b)
  53. {
  54.   double        val, scale, tmp;
  55.  
  56.   if (a > b) {
  57.     tmp= a; a= b; b= tmp;
  58.   } /*endif*/
  59.   
  60.   scale = (b-a);
  61.   val = scale * Math.random();
  62.   
  63.   return (a + val);
  64.   
  65. } /*randBetween*/
  66.  
  67.  
  68.  
  69. /**
  70.  * Initialize.
  71.  */
  72. public void init() {
  73. } /*init*/
  74.  
  75.  
  76.  
  77. /**
  78.  * Start the thread.
  79.  */
  80. public void start() {
  81.   if(runner==null) { //start new thread if it doesn't exist
  82.     runner= new Thread(this); 
  83.     runner.start();
  84.     //runner.setPriority (Thread.MAX_PRIORITY);
  85.   } /*endif*/
  86.  
  87. } /*start*/
  88.  
  89.  
  90.  
  91. /**
  92.  * Stop the thread.
  93.  */
  94. public void stop() {
  95.   if (runner != null)
  96.     runner.stop (); //kill thread when applet is stopped
  97.   runner= null;
  98. } /*stop*/
  99.  
  100.  
  101.  
  102. public long sleepMillis (){
  103.   return SLEEP_MILLIS;
  104. }
  105.  
  106.  
  107.  
  108. /**
  109.  * Execution loop.  Used to call tick().
  110.  */
  111. public void run() {
  112.  
  113.   while (runner != null){
  114.       try {
  115.         Thread.sleep (sleepMillis ());
  116.       } catch(InterruptedException e){} //sleep
  117.     tick();
  118.   }/*endwhile*/
  119.   
  120.   runner= null;
  121.   
  122. } /*run*/
  123.  
  124.  
  125.  
  126. /**
  127.  * Distribute tick to the ActorManager and update display.
  128.  */
  129. public void tick() {
  130.  
  131.   lastTickTimeMillis= currentTickTimeMillis;
  132.   currentTickTimeMillis= System.currentTimeMillis();
  133.  
  134.   actorManager.tick();
  135.   
  136.   repaint();
  137.   
  138. } /*tick*/
  139.  
  140.  
  141.  
  142. /**
  143.  * Pass the event along to the EventManager for handling.
  144.  */
  145. public boolean handleEvent (Event theEvent) {
  146.   boolean returnValue= false;
  147.   
  148.   //
  149.   // ignore events which occur before objects are ready for them
  150.   //
  151.   if (eventManager!=null)
  152.     returnValue= eventManager.handleEvent(theEvent);
  153.   
  154.   return returnValue;
  155. } /*handleEvent*/
  156.  
  157.  
  158.  
  159. /**
  160.  * Calculater the difference between the current tick and the last one.
  161.  */
  162. public double deltaTickTimeMillis() {
  163.   return (double)(currentTickTimeMillis - lastTickTimeMillis);
  164. } /*deltaTickTimeMillis*/
  165.  
  166.  
  167.  
  168. /**
  169.  * Override update to avoid screen clears.
  170.  */
  171. public void update(Graphics g) {    
  172.   paint(g);
  173. } /*update*/
  174.  
  175.  
  176.  
  177. /**
  178.  * Pass the Graphics onto the DisplayManager.
  179.  */ 
  180. public void paint(Graphics g) {
  181.   displayManager.paint(g);
  182. } /*paint*/
  183.  
  184.  
  185.  
  186. /**
  187.  * Provide standard Gamelet info.
  188.  */
  189. public String getAppletInfo() {
  190.   return "The Gamelet Toolkit\nVersion 0.8\nWritten by Mark Tacchi, mtacchi@NeXT.COM\n\nYou are free to use, copy, and modify the source without restriction.  However, it is requested that the author is mentioned in any pertinent credit sections released with code developed with the Gamelet Toolkit.";
  191. } /*getAppletInfo*/
  192. } /*Gamelet*/
  193.  
  194.