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

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