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

  1. head    1.5;
  2. access;
  3. symbols;
  4. locks; strict;
  5. comment    @# @;
  6.  
  7.  
  8. 1.5
  9. date    98.04.15.15.44.11;    author jgh8962;    state Exp;
  10. branches;
  11. next    1.4;
  12.  
  13. 1.4
  14. date    98.04.15.15.40.48;    author mds1274;    state Exp;
  15. branches;
  16. next    1.3;
  17.  
  18. 1.3
  19. date    98.04.14.00.56.35;    author jgh8962;    state Exp;
  20. branches;
  21. next    1.2;
  22.  
  23. 1.2
  24. date    98.04.10.18.27.30;    author jgh8962;    state Exp;
  25. branches;
  26. next    1.1;
  27.  
  28. 1.1
  29. date    98.04.10.17.53.01;    author jgh8962;    state Exp;
  30. branches;
  31. next    ;
  32.  
  33.  
  34. desc
  35. @@
  36.  
  37.  
  38. 1.5
  39. log
  40. @*** empty log message ***
  41. @
  42. text
  43. @/**
  44.  *
  45.  * Gamelication.java
  46.  * @@author    Mark G. Tacchi (mtacchi@@next.com) 
  47.  * @@version    0.8
  48.  * Mar 27/1996
  49.  *
  50.  * Gamelication contains a thread which is used for distributing ticks to all
  51.  * manager objects.  A tick is basically an instant in time and represents
  52.  * the time granularity of the game.  
  53.  *
  54.  */
  55.  
  56. package com.next.gt;
  57.  
  58. import java.awt.*;
  59. import java.util.Date;
  60. import java.util.Vector;
  61. //import java.awt.Graphics;
  62. import java.awt.event.*;
  63.  
  64. abstract public class Gamelication extends java.awt.Panel implements Runnable  {
  65.  
  66.   //
  67.   // This is the main thread, used for ticks.
  68.   //
  69.   public Thread runner= null;
  70.   
  71.   //
  72.   // Gamelication is the master time keeper.
  73.   //
  74.   public long currentTickTimeMillis= System.currentTimeMillis();
  75.   public long lastTickTimeMillis;
  76.   
  77.   //
  78.   // Gamelication is the manager of the managers. 
  79.   //
  80.   public ActorManager        actorManager= new ActorManager(this);
  81.   public DisplayManager        displayManager= new DisplayManager(this);
  82.   public ScoreManager        scoreManager;
  83.   //public EventManager        eventManager= new EventManager();
  84.   public AudioManager        audioManager = new AudioManager();
  85.   
  86.   //
  87.   // Store the directory of the codebase.
  88.   //
  89.   private String        codebaseDir;
  90.   
  91.   //
  92.   // Sleep time for the thread.
  93.   //
  94.  public int                SLEEP_MILLIS= 50;
  95.  
  96.  
  97.  
  98. /**
  99.  * Generate a random double between two doubles.
  100.  */
  101. public static double randBetween(double a, double b)
  102. {
  103.   double        val, scale, tmp;
  104.  
  105.   if (a > b) {
  106.     tmp= a; a= b; b= tmp;
  107.   } /*endif*/
  108.   
  109.   scale = (b-a);
  110.   val = scale * Math.random();
  111.   
  112.   return (a + val);
  113.   
  114. } /*randBetween*/
  115.  
  116.  
  117.  
  118. /**
  119.  * Initialize.  Takes the width and height of the window.
  120.  */
  121. public void init( int width, int height ) {
  122.     init( width, height, "." );
  123. } /*init*/
  124.  
  125. /**
  126.  * Another routine for initialization, takes in a width, height, and a 
  127.  * codebase.
  128.  */
  129. public void init( int width, int height, String codebaseDir ) {
  130.     setSize( width, height );
  131.     this.codebaseDir = codebaseDir;
  132. }
  133.  
  134. /**
  135.  * Start the thread.
  136.  */
  137. public void start() {
  138.   if(runner==null) { //start new thread if it doesn't exist
  139.     runner= new Thread(this); 
  140.     runner.start();
  141.     new Thread( audioManager ).start();
  142.     //runner.setPriority (Thread.MAX_PRIORITY);
  143.   } /*endif*/
  144.  
  145. } /*start*/
  146.  
  147.  
  148.  
  149. /**
  150.  * Stop the thread.
  151.  */
  152. public void stop() {
  153.   if (runner != null)
  154.     runner.stop (); //kill thread when applet is stopped
  155.   runner= null;
  156. } /*stop*/
  157.  
  158.  
  159.  
  160. public long sleepMillis () {
  161.   return SLEEP_MILLIS;
  162. }
  163.  
  164.  
  165.  
  166. /**
  167.  * Execution loop.  Used to call tick().
  168.  */
  169. public void run() {
  170.  
  171.   while (runner != null){
  172.       try {
  173.         Thread.sleep (sleepMillis ());
  174.       } catch(InterruptedException e){} //sleep
  175.     tick();
  176.   }/*endwhile*/
  177.   
  178.   runner= null;
  179.   
  180. } /*run*/
  181.  
  182.  
  183.  
  184. /**
  185.  * Distribute tick to the ActorManager and update display.
  186.  */
  187. public void tick() { 
  188.  
  189.   lastTickTimeMillis= currentTickTimeMillis;
  190.   currentTickTimeMillis= System.currentTimeMillis();
  191.  
  192.   actorManager.tick();
  193.   
  194.   repaint();
  195.   
  196. } /*tick*/
  197.  
  198.  
  199. /**
  200.  * Calculater the difference between the current tick and the last one.
  201.  */
  202. public double deltaTickTimeMillis() {
  203.   return (double)(currentTickTimeMillis - lastTickTimeMillis);
  204. } /*deltaTickTimeMillis*/
  205.  
  206.  
  207. /**
  208.  * Override update to avoid screen clears.
  209.  */
  210. public void update(Graphics g) {    
  211.   paint(g);
  212. } /*update*/
  213.  
  214.  
  215.  
  216. /**
  217.  * Pass the Graphics onto the DisplayManager.
  218.  */ 
  219. public void paint(Graphics g) {
  220.   displayManager.paint(g);
  221. } /*paint*/
  222.  
  223.  
  224.  
  225. /**
  226.  * Provide standard Gamelication info.
  227.  */
  228. public String getGamelicationInfo() {
  229.   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.";
  230. } /*getAppletInfo*/
  231.  
  232. /**
  233.  * Get codebase for the directory of the Gamelication.
  234.  */
  235. public String getCodeBase() {
  236.   return codebaseDir;
  237. }
  238.  
  239. /**
  240.  * Displays the current status of the current Gamelication.
  241.  */
  242. public void showStatus( String gamelicationStatus ) {
  243.     System.out.println( gamelicationStatus );
  244. }
  245.  
  246. /**
  247.  * Get an image from a file.
  248.  */
  249. public Image getImage( String codeBase, String imagefile ) {
  250.     return getToolkit().getImage( codeBase+"/"+imagefile );
  251. }
  252.  
  253. /**
  254.  * Play an .au file
  255.  */
  256. public void play( String codeBase, String auFile ) {
  257.     audioManager.play( codeBase+auFile );
  258. }
  259.  
  260. } /*Gamelication*/
  261. @
  262.  
  263.  
  264. 1.4
  265. log
  266. @*** empty log message ***
  267. @
  268. text
  269. @d41 1
  270. a41 1
  271.   public EventManager        eventManager= new EventManager();
  272. @
  273.  
  274.  
  275. 1.3
  276. log
  277. @*** empty log message ***
  278. @
  279. text
  280. @d187 1
  281. a187 2
  282.   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 section
  283. s released with code developed with the Gamelet Toolkit.";
  284. @
  285.  
  286.  
  287. 1.2
  288. log
  289. @*** empty log message ***
  290. @
  291. text
  292. @d42 1
  293. d99 1
  294. d187 2
  295. a188 1
  296.   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.";
  297. d216 1
  298. a216 1
  299.     // Not implemented yet.
  300. @
  301.  
  302.  
  303. 1.1
  304. log
  305. @Initial revision
  306. @
  307. text
  308. @a154 1
  309.  
  310. a155 20
  311.  * Pass the event along to the EventManager for handling.
  312.  */
  313. public void processEvent (AWTEvent theEvent) {
  314.   System.out.println( "Gamelication processEvent called." );
  315.   boolean returnValue= false;
  316.   
  317.   System.out.println( "processEvent called." );
  318.   
  319.   //
  320.   // ignore events which occur before objects are ready for them
  321.   //
  322.   //if (eventManager!=null)
  323.     returnValue= eventManager.handleEvent(theEvent);
  324.   
  325.   //return returnValue;
  326. } /*handleEvent*/
  327.  
  328.  
  329.  
  330. /**
  331. a162 1
  332.  
  333. @
  334.