home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / Gamelication / examples / Boink / com / next / gt / Gamelication.java < prev    next >
Encoding:
Java Source  |  1998-04-15  |  4.3 KB  |  219 lines

  1. /**
  2.  *
  3.  * Gamelication.java
  4.  * @author    Mark G. Tacchi (mtacchi@next.com) 
  5.  * @version    0.8
  6.  * Mar 27/1996
  7.  *
  8.  * Gamelication 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.awt.*;
  17. import java.util.Date;
  18. import java.util.Vector;
  19. //import java.awt.Graphics;
  20. import java.awt.event.*;
  21.  
  22. abstract public class Gamelication extends java.awt.Panel implements Runnable  {
  23.  
  24.   //
  25.   // This is the main thread, used for ticks.
  26.   //
  27.   public Thread runner= null;
  28.   
  29.   //
  30.   // Gamelication is the master time keeper.
  31.   //
  32.   public long currentTickTimeMillis= System.currentTimeMillis();
  33.   public long lastTickTimeMillis;
  34.   
  35.   //
  36.   // Gamelication is the manager of the managers. 
  37.   //
  38.   public ActorManager        actorManager= new ActorManager(this);
  39.   public DisplayManager        displayManager= new DisplayManager(this);
  40.   public ScoreManager        scoreManager;
  41.   //public EventManager        eventManager= new EventManager();
  42.   public AudioManager        audioManager = new AudioManager();
  43.   
  44.   //
  45.   // Store the directory of the codebase.
  46.   //
  47.   private String        codebaseDir;
  48.   
  49.   //
  50.   // Sleep time for the thread.
  51.   //
  52.  public int                SLEEP_MILLIS= 50;
  53.  
  54.  
  55.  
  56. /**
  57.  * Generate a random double between two doubles.
  58.  */
  59. public static double randBetween(double a, double b)
  60. {
  61.   double        val, scale, tmp;
  62.  
  63.   if (a > b) {
  64.     tmp= a; a= b; b= tmp;
  65.   } /*endif*/
  66.   
  67.   scale = (b-a);
  68.   val = scale * Math.random();
  69.   
  70.   return (a + val);
  71.   
  72. } /*randBetween*/
  73.  
  74.  
  75.  
  76. /**
  77.  * Initialize.  Takes the width and height of the window.
  78.  */
  79. public void init( int width, int height ) {
  80.     init( width, height, "." );
  81. } /*init*/
  82.  
  83. /**
  84.  * Another routine for initialization, takes in a width, height, and a 
  85.  * codebase.
  86.  */
  87. public void init( int width, int height, String codebaseDir ) {
  88.     setSize( width, height );
  89.     this.codebaseDir = codebaseDir;
  90. }
  91.  
  92. /**
  93.  * Start the thread.
  94.  */
  95. public void start() {
  96.   if(runner==null) { //start new thread if it doesn't exist
  97.     runner= new Thread(this); 
  98.     runner.start();
  99.     new Thread( audioManager ).start();
  100.     //runner.setPriority (Thread.MAX_PRIORITY);
  101.   } /*endif*/
  102.  
  103. } /*start*/
  104.  
  105.  
  106.  
  107. /**
  108.  * Stop the thread.
  109.  */
  110. public void stop() {
  111.   if (runner != null)
  112.     runner.stop (); //kill thread when applet is stopped
  113.   runner= null;
  114. } /*stop*/
  115.  
  116.  
  117.  
  118. public long sleepMillis () {
  119.   return SLEEP_MILLIS;
  120. }
  121.  
  122.  
  123.  
  124. /**
  125.  * Execution loop.  Used to call tick().
  126.  */
  127. public void run() {
  128.  
  129.   while (runner != null){
  130.       try {
  131.         Thread.sleep (sleepMillis ());
  132.       } catch(InterruptedException e){} //sleep
  133.     tick();
  134.   }/*endwhile*/
  135.   
  136.   runner= null;
  137.   
  138. } /*run*/
  139.  
  140.  
  141.  
  142. /**
  143.  * Distribute tick to the ActorManager and update display.
  144.  */
  145. public void tick() { 
  146.  
  147.   lastTickTimeMillis= currentTickTimeMillis;
  148.   currentTickTimeMillis= System.currentTimeMillis();
  149.  
  150.   actorManager.tick();
  151.   
  152.   repaint();
  153.   
  154. } /*tick*/
  155.  
  156.  
  157. /**
  158.  * Calculater the difference between the current tick and the last one.
  159.  */
  160. public double deltaTickTimeMillis() {
  161.   return (double)(currentTickTimeMillis - lastTickTimeMillis);
  162. } /*deltaTickTimeMillis*/
  163.  
  164.  
  165. /**
  166.  * Override update to avoid screen clears.
  167.  */
  168. public void update(Graphics g) {    
  169.   paint(g);
  170. } /*update*/
  171.  
  172.  
  173.  
  174. /**
  175.  * Pass the Graphics onto the DisplayManager.
  176.  */ 
  177. public void paint(Graphics g) {
  178.   displayManager.paint(g);
  179. } /*paint*/
  180.  
  181.  
  182.  
  183. /**
  184.  * Provide standard Gamelication info.
  185.  */
  186. public String getGamelicationInfo() {
  187.   return "The Gamelication Toolkit\nVersion 0.1\nWritten by Jeremy Hutchins, jgh8962@cs.rit.edu\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.";
  188. } /*getAppletInfo*/
  189.  
  190. /**
  191.  * Get codebase for the directory of the Gamelication.
  192.  */
  193. public String getCodeBase() {
  194.   return codebaseDir;
  195. }
  196.  
  197. /**
  198.  * Displays the current status of the current Gamelication.
  199.  */
  200. public void showStatus( String gamelicationStatus ) {
  201.     System.out.println( gamelicationStatus );
  202. }
  203.  
  204. /**
  205.  * Get an image from a file.
  206.  */
  207. public Image getImage( String codeBase, String imagefile ) {
  208.     return getToolkit().getImage( codeBase+"/"+imagefile );
  209. }
  210.  
  211. /**
  212.  * Play an .au file
  213.  */
  214. public void play( String codeBase, String auFile ) {
  215.     audioManager.play( codeBase+auFile );
  216. }
  217.  
  218. } /*Gamelication*/
  219.