topical media & game development

talk show tell print

actionscript-zoo-v1-src-zoo-VirtualPet.ax

actionscript-zoo-v1-src-zoo-VirtualPet.ax (swf ) [ flash ] flex


  package zoo {
    import flash.utils.setInterval;
    import flash.utils.clearInterval;
    
    public class actionscript-zoo-v1-src-zoo-VirtualPet {
      private static var maxNameLength = 20;
      private static var maxCalories = 2000;
      private static var caloriesPerSecond = 100;
      
      private var petName;
      private var currentCalories = actionscript-zoo-v1-src-zoo-VirtualPet.maxCalories/2;
      private var digestIntervalID;
  
      public function actionscript-zoo-v1-src-zoo-VirtualPet (name) {
        setName(name);
        digestIntervalID = setInterval(digest, 1000);
      }
  
      public function eat (foodItem) {
        if (currentCalories == 0) {
          trace(getName() + " is dead. You can't feed it.");
          return;
        }
  
        if (foodItem is Apple) {
          if (foodItem.hasWorm()) {
            trace("The " + foodItem.getName() + " had a worm. " + getName() 
                  + " didn't eat it.");
            return;
          }
        }
      
        var newCurrentCalories = currentCalories + foodItem.getCalories();
        if (newCurrentCalories > actionscript-zoo-v1-src-zoo-VirtualPet.maxCalories) {
          currentCalories = actionscript-zoo-v1-src-zoo-VirtualPet.maxCalories;
        } else {
          currentCalories = newCurrentCalories;
        }
        trace(getName() + " ate some " + foodItem.getName() + "." 
              + " It now has " + currentCalories  + " calories remaining.");
      }
  
      public function getHunger () {
        return currentCalories / actionscript-zoo-v1-src-zoo-VirtualPet.maxCalories;
      }
      
      public function setName (newName) {
        // If the proposed new name has more than maxNameLength characters...
        if (newName.length > actionscript-zoo-v1-src-zoo-VirtualPet.maxNameLength) {
          // ...truncate it
          newName = newName.substr(0, actionscript-zoo-v1-src-zoo-VirtualPet.maxNameLength);
        } else if (newName == "") {
          // ...otherwise, if the proposed new name is an empty string,
          // then terminate this method without changing petName
          return;
        }
      
        // Assign the new, validated name to petName
        petName = newName;
      }
  
      public function getName () {
        return petName;
      }
      
      private function digest () {
        // If digesting more calories would leave the pet's currentCalories at 
        // 0 or less...
        if (currentCalories - actionscript-zoo-v1-src-zoo-VirtualPet.caloriesPerSecond <= 0) {
          // ...stop the interval from calling digest()
          clearInterval(digestIntervalID);
          // Then give the pet an empty stomach
          currentCalories = 0;
          // And report the pet's death
          trace(getName() + " has died.");
        } else {
          // ...otherwise, digest the stipulated number of calories
          currentCalories -= actionscript-zoo-v1-src-zoo-VirtualPet.caloriesPerSecond;
          
          // And report the pet's new status
          trace(getName() + " digested some food. It now has " 
                + currentCalories + " calories remaining.");
        } 
      }
    }
  }
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.