scg/ch14/gamestuff/Item

From FANG

Jump to: navigation, search

001 package scg.ch14.gamestuff;
002 
003 import java.util.Scanner;
004 
005 public class Item
006   extends GameObject {
007   /**
008    * Read one item from the given scanner
009    *
010    @param   gameObjectScanner  scanner open for input
011    *
012    @return  the new Item or null if there is a problem
013    */
014   public static Item readObjectFromFile(Scanner gameObjectScanner{
015     String classID = gameObjectScanner.next();
016     if (!classID.equalsIgnoreCase("Item")) {
017       return null;// not the expected type of object; punt
018     }
019 
020     String endClassID = "/" + classID;
021 
022     Item it = new Item();
023     GameObject.processAttributes(gameObjectScanner, endClassID, it);
024     return it;
025   }
026 
027   /** how much does this item hurt critters susceptible to it? */
028   private int attackStrength;
029 
030   /** Description added to owner's description */
031   private String ownedDescription;
032 
033   /** object to which this item belongs */
034   private GameObject owner;
035 
036   /**
037    * the uuid of the owner of this item; the item is either in a
038    * Location or on a Critter
039    */
040   private String ownerID;
041 
042   
043   /**
044    * Initialize the item. 
045    */
046   public Item() {
047     attackStrength = 0;
048     ownedDescription = null;
049   }
050   
051   /**
052    * Get the current attack strength of this item.
053    *
054    @return  attack strength
055    */
056   public int getAttackStrength() {
057     return attackStrength;
058   }
059 
060   /**
061    @return  the ownedDescription
062    */
063   public String getOwnedDescription() {
064     return ownedDescription;
065   }
066 
067   /**
068    @return  the owner
069    */
070   public GameObject getOwner() {
071     return owner;
072   }
073 
074   /**
075    * Get the UUID of the current owner of this item.
076    *
077    @return  the UUID of the owner
078    */
079   public String getOwnerID() {
080     if (owner != null{
081       return owner.getUUID();
082     }
083     return ownerID;
084   }
085 
086   /**
087    * set the owner of this item
088    */
089   public void give(GameObject owner{
090     this.owner = owner;
091   }
092 
093   /**
094    Set the attackStrength.
095    *
096    @param  attackStrength  new attackStrength value
097    */
098   public void setAttackStrength(int attackStrength{
099     this.attackStrength = attackStrength;
100   }
101 
102   /**
103    @param  ownedDescription  the ownedDescription to set
104    */
105   public void setOwnedDescription(String ownedDescription{
106     this.ownedDescription = ownedDescription;
107   }
108 
109   /**
110    @param  owner  the owner to set
111    */
112   public void setOwner(GameObject owner{
113     this.owner = owner;
114   }
115 
116   /**
117    * Handle, if possible, the given name/value pair
118    *
119    @param   attribute  name of the attribute
120    @param   value      the value of the attribute
121    *
122    @return  true if the attribute was handled at this level; false
123    *          otherwise.
124    */
125   @Override
126   protected boolean handleAttributeValuePair(String attribute,
127     String value{
128     if (super.handleAttributeValuePair(attribute, value)) {
129       return true;
130     }
131 
132     if (attribute.equalsIgnoreCase("owner")) {
133       ownerID = value.toLowerCase();
134       return true;
135     } else if (attribute.equalsIgnoreCase("ownedDescription")) {
136       setOwnedDescription(value);
137       return true;
138     } else if (attribute.equalsIgnoreCase("attackStrength")) {
139       setAttackStrength(Integer.parseInt(value));
140       return true;
141     }
142 
143     return false;
144   }
145 
146   /**
147    * Internal toString helper method.
148    *
149    @return  A string representation of the fields.
150    */
151   @Override
152   protected String toStringGuts() {
153     return super.toStringGuts() + "\n" + "ownerID = " + getOwnerID() +
154       "\n" + "attackStrength = " + attackStrength + "\n";
155   }
156 }
157 
158 //Uploaded on Mon Mar 29 21:41:43 EDT 2010


Download/View scg/ch14/gamestuff/Item.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter

Games
Games