scg/ch14/gamestuff/GameObject

From FANG

Jump to: navigation, search

001 package scg.ch14.gamestuff;
002 
003 import java.util.ArrayList;
004 import java.util.Scanner;
005 
006 import scg.ch14.util.ReadAndWrite;
007 
008 /**
009  * The base class in the game object hierarchy. GameObject is not
010  * designed to be instantiated directly. Instead it provides the common
011  * base for {@link Location}{@link Item}, and {@link Critter}.
012  *
013  <p>{@link GameObject} has three fields:
014  *
015  <ul>
016  <li>uuid</li> - Universally Unique IDentifier; the id for the object
017  * which must be globally (with in the game) unique. Not typically
018  * displayed to user.
019  <li>name</li> - name of the game object; typically displayed for the
020  * player.
021  <li>description</li> - describes the object; shown to user when they
022  * encounter the object or explicitly look at it.
023  </ul>
024  */
025 public class GameObject {
026   /**
027    * Advance through a record in an input file. The scanner has just
028    * scanned past the class identifier. The endClassID is the tag
029    * marking the end of the record and the go is the game object to read
030    * into.
031    *
032    @param   gameObjectScanner  scanner inside a class record
033    @param   endClassID         the string marking the end of the
034    *                             record
035    @param   go                 object of the given {@link GameObject}
036    *                             extending class
037    *
038    @return
039    */
040   protected static GameObject processAttributes(
041     Scanner gameObjectScanner, String endClassID, GameObject go{
042     String attribute = ReadAndWrite.readAttribute(gameObjectScanner);
043     while (!attribute.equalsIgnoreCase(endClassID)) {
044       /* ignore */ ReadAndWrite.readMatch(gameObjectScanner, "=");
045       String value = ReadAndWrite.readValue(gameObjectScanner);
046       go.handleAttributeValuePair(attribute, value);
047 
048       attribute = ReadAndWrite.readAttribute(gameObjectScanner);
049     }
050     return go;
051   }
052 
053   /** the description of the object */
054   private String description;
055 
056   /** Items held by this Critter; built after load */
057   private final ArrayList<Item> inventory;
058 
059   /** the name; need not be unique */
060   private String name;
061 
062   /** universally unique identifier - the object's id */
063   private String uuid;
064 
065   /**
066    * Protected constructor; {@link GameObject} cannot be constructed
067    * "naked", only subclasses of this class can be constructed.
068    */
069   protected GameObject() {
070     uuid = null;
071     name = null;
072     description = null;
073     inventory = new ArrayList<Item>();
074   }
075 
076   /**
077    * add the given item to the object
078    */
079   public void add(Item item{
080     if (inventory.indexOf(item0{
081       inventory.add(item);
082       item.setOwner(this);
083     }
084   }
085 
086   /**
087    * Get the object's description
088    *
089    @return  the description
090    */
091   public String getDescription() {
092     return description;
093   }
094 
095   /**
096    * Get the full description: The description plus any "ownedDescription" bits contributed by any of the items held.
097    @return the full description.
098    */
099   public String getFullDescription() {
100     String fullDescription = getDescription();
101     String separator = " ";
102     for(int = 0; i != inventory.size()++i{
103       String ownedDescription = inventory.get(i).getOwnedDescription();
104       if (ownedDescription != null{ 
105         fullDescription += separator + ownedDescription;
106       }
107     }
108     return fullDescription;
109   }
110   
111   /**
112    * The list of items in this game object.
113    *
114    @return  the inventory
115    */
116   public ArrayList<Item> getInventory() {
117     return inventory;
118   }
119 
120   /**
121    @return  the name
122    */
123   public String getName() {
124     return name;
125   }
126 
127   /**
128    * Get the Universally Unique Identifier
129    *
130    @return  the uuid
131    */
132   public String getUUID() {
133     return uuid;
134   }
135 
136   /**
137    * Does this object have the given item in its inventory?
138    *
139    @param   name  name of item to find
140    *
141    @return  true if the item is in inventory, false otherwise
142    */
143   public boolean hasItemName(String name{
144     return itemIndexByName(name>= 0;
145   }
146 
147   /**
148    * Does the item exist in the inventory?
149    *
150    @param   uuid  the uuid to check for
151    *
152    @return  true if it is in the inventory, false otherwise
153    */
154   public boolean hasItemUUID(String uuid{
155     return itemIndexByUUID(uuid>= 0;
156   }
157 
158   /**
159    * Find item by name.
160    *
161    @param   itemName  name of item to find
162    *
163    @return  the item or null if there is no such item
164    */
165   public Item itemByName(String itemName{
166     Item retval = null;
167     int ndx = itemIndexByName(itemName);
168     if (ndx >= 0{
169       retval = inventory.get(ndx);
170     }
171     return retval;
172   }
173 
174   /**
175    * Get the item by UUID
176    *
177    @param   itemUUID  uuid to find
178    *
179    @return  reference to item if it is found; null otherwise
180    */
181   public Item itemByUUID(String itemUUID{
182     Item retval = null;
183     int ndx = itemIndexByUUID(itemUUID);
184     if (ndx >= 0{
185       retval = inventory.get(ndx);
186     }
187     return retval;
188   }
189 
190   /**
191    * Remove the named item from the inventory of this object.
192    *
193    @param   itemName  name of object to remove
194    *
195    @return  the item removed or null if none was removed
196    */
197   public Item removeItemName(String itemName{
198     Item retval = null;
199     int ndx = itemIndexByName(itemName);
200     if (ndx >= 0{
201       retval = inventory.remove(ndx);
202     }
203     return retval;
204   }
205 
206   /**
207    * Remove item from the inventory by uuid (if it is here)
208    *
209    @param   itemUUID  the uuid of the object to get
210    *
211    @return  reference to removed object if one was found; null
212    *          otherwise
213    */
214   public Item removeItemUUID(String itemUUID{
215     Item retval = null;
216     int ndx = itemIndexByUUID(itemUUID);
217     if (ndx >= 0{
218       retval = inventory.remove(ndx);
219     }
220     return retval;
221   }
222 
223   /**
224    Set the description of the
225    *
226    @param  description  the description to set
227    */
228   public void setDescription(String description{
229     this.description = description;
230   }
231 
232   /**
233    Set the object's name
234    *
235    @param  name  the name to set
236    */
237   public void setName(String name{
238     this.name = name;
239   }
240 
241   /**
242    * Get a string representation of this object. Note this is NOT the
243    * same as the save format.
244    */
245   @Override
246   public String toString() {
247     return getClass().getName() + " [\n" + toStringGuts() + "\n]";
248   }
249 
250   /**
251    * Find the index of the given item name (for picking up items)
252    *
253    @param   itemName  name to find
254    *
255    @return  index of the matching item (or -1)
256    */
257   private int itemIndexByName(String itemName{
258     for (int = 0; i != inventory.size()++i{
259       if (inventory.get(i).getName().equalsIgnoreCase(itemName)) {
260         return i;
261       }
262     }
263     return -1;
264   }
265 
266   /**
267    * Find the index of the given item UUID (for picking up items)
268    *
269    @param   itemUUID
270    *
271    @return  index of match or -1
272    */
273   private int itemIndexByUUID(String itemUUID{
274     for (int = 0; i != inventory.size()++i{
275       Item item = inventory.get(i);
276       if (item.getUUID().equalsIgnoreCase(itemUUID)) {
277         return i;
278       }
279     }
280     return -1;
281   }
282 
283   /**
284    Set the Universally Unique IDentifier; private because the UUID is
285    * not something that should be changed.
286    *
287    @param  uuid  the new uuid value
288    */
289   private void setUUID(String uuid{
290     this.uuid = uuid;
291   }
292 
293   /**
294    * Handle, if possible, the given name/value pair
295    *
296    @param   attribute  name of the attribute
297    @param   value      the value of the attribute
298    *
299    @return  true if the attribute was handled at this level; false
300    *          otherwise.
301    */
302   protected boolean handleAttributeValuePair(String attribute,
303     String value{
304     if (attribute.equalsIgnoreCase("uuid")) {
305       setUUID(value.toLowerCase());
306       return true;
307     } else if (attribute.equalsIgnoreCase("name")) {
308       setName(value);
309       return true;
310     } else if (attribute.equalsIgnoreCase("description")) {
311       setDescription(value);
312       return true;
313     }
314     return false;
315   }
316 
317   /**
318    * Internal toString helper method.
319    *
320    @return  A string representation of the fields.
321    */
322   protected String toStringGuts() {
323     String invList = "inventory = [";
324     String separator = "";
325     for (int = 0; i != inventory.size()++i{
326       invList += separator + inventory.get(i).toString();
327       separator = ", ";
328     }
329     invList += "]";
330     return "uuid = " + uuid + "\n" + "name = " + name + "\n" +
331       "description = " + description + "\n" + invList;
332   }
333 }
334 
335 //Uploaded on Mon Mar 29 21:40:09 EDT 2010


Download/View scg/ch14/gamestuff/GameObject.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