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

  1. /**
  2.  *
  3.  * ScoreManager.java
  4.  * @author    Mark G. Tacchi (mtacchi@next.com) 
  5.  * @version    0.8
  6.  * Mar 15/1996
  7.  *
  8.  * ScoreManager maintains the current score and provides accessor methods
  9.  * for objects to access/update the score.  It also allows objects to register
  10.  * for notification when a specific point level has been reached.  This is
  11.  * most useful for rewarding player with some sort of bonus.
  12.  *
  13. */
  14.  
  15. package com.next.gt;
  16.  
  17. import java.util.Vector;
  18.  
  19. public class ScoreManager extends java.lang.Object {
  20.   public    int                score;
  21.   private    Vector            objectsToNotify;
  22.  
  23. public ScoreManager() {
  24.   objectsToNotify= new Vector();
  25. } /*ScoreManager()*/
  26.  
  27.  
  28.  
  29. /**
  30.  * Add to the score.
  31. */
  32. public void addToScore(int theValue) {
  33.   score+= theValue;
  34.   checkForBonus(theValue);
  35. } /*addToScore*/
  36.  
  37.  
  38.  
  39. /**
  40.  * Set the score.
  41. */
  42. public void setScore(int theValue) {
  43.   score= theValue;
  44. } /*setScore*/
  45.  
  46.  
  47.  
  48. /**
  49.  * Subtract from the score.
  50. */
  51. public void subtractFromScore(int theValue) {
  52.   score-= theValue;
  53. } /*subtractFromScore*/
  54.  
  55.  
  56.  
  57. /**
  58.  * For every `bonus' points, notify requestor.
  59. */
  60. public void registerForBonusNotification (Object theObject, int theValue) {
  61.   Vector    registerVector= new Vector(3);
  62.   
  63.   registerVector.addElement(theObject);                 // the object to notify
  64.   registerVector.addElement(new Integer(theValue));     // bonus every
  65.   registerVector.addElement(new Integer(0));            // bonus counter
  66.   
  67.   objectsToNotify.addElement(registerVector);
  68. } /*registerForBonusNotification*/
  69.  
  70.  
  71.  
  72. /**
  73.  * Check if it's time for a bonus.
  74. */
  75. private void checkForBonus(int theValue) {
  76.   Vector    theObjectToNotify;
  77.   Integer    n1, n2;
  78.   Integer    anInteger;
  79.  
  80.   for (int i= 0; i<objectsToNotify.size(); i++) {
  81.     theObjectToNotify= (Vector) objectsToNotify.elementAt(i);
  82.  
  83.     //
  84.     // increment bonus counter
  85.     //
  86.     anInteger= (Integer) theObjectToNotify.elementAt(2);
  87.     theObjectToNotify.setElementAt(new Integer(anInteger.intValue() + theValue), 2);
  88.     
  89.     //
  90.     // check if bonus counter is greater than the bonus level
  91.     //
  92.     n2= (Integer) theObjectToNotify.elementAt(2);
  93.     n1= (Integer) theObjectToNotify.elementAt(1);
  94.     
  95.     if (n2.intValue() >= n1.intValue()) {
  96.       BonusHandler    theBonusHandler= (BonusHandler) theObjectToNotify.elementAt(0);
  97.       theObjectToNotify.setElementAt(new Integer(n2.intValue()- n1.intValue()), 2);
  98.        theBonusHandler.didAchieveBonus();
  99.    } /*endif*/
  100.     
  101.   } /*nexti*/
  102.  
  103. } /*checkForBonus*/
  104.  
  105.  
  106. } /*ScoreManager*/