scg/ch14/gamestuff/Critter

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.core.Game;
007 
008 public class Critter
009   extends GameObject {
010   /**
011    * Read one critter from the given scanner. The {@link Critter}
012    * factory.
013    *
014    @param   gameObjectScanner  scanner open for input
015    *
016    @return  the new Critter or null if there is a problem
017    */
018   public static Critter readObjectFromFile(Scanner gameObjectScanner{
019     String classID = gameObjectScanner.next();
020     if (!classID.equalsIgnoreCase("Critter")) {
021       return null;// not the expected type of object; punt
022     }
023 
024     String endClassID = "/" + classID;
025 
026     Critter cr = new Critter();
027     GameObject.processAttributes(gameObjectScanner, endClassID, cr);
028     return cr;
029   }
030 
031   /** Critter dies at 0 health */
032   private int health;
033 
034   /** the list of UUIDs of things that reduce critter's health */
035   private final ArrayList<String> hurtBy;
036 
037   /** where is this critter? */
038   private Location location;
039   private String locationID;
040 
041   /** list of all items this critter will trade for */
042   private final ArrayList<TradesFor> tradesFor;
043 
044   /**
045    * Construct a new, empty critter.
046    */
047   public Critter() {
048     health = 0;
049     locationID = Location.NO_SUCH_LOCATION;
050     location = null;
051     hurtBy = new ArrayList<String>();
052     tradesFor = new ArrayList<TradesFor>();
053   }
054 
055   /**
056    * Add the given item to this critter's inventory.
057    */
058   public void add(Item item{
059     super.add(item);
060     if (hurtBy.indexOf(item.getUUID()) >= 0{
061       health -= item.getAttackStrength();
062       Game.getCurrentGame().addMessage(getName() + " hurt by " + 
063           item.getName() + " for " + item.getAttackStrength());
064     }
065   }
066   
067   /**
068    * Get the critter's current health.
069    @return the health
070    */
071   public int getHealth() {
072     return health;
073   }
074 
075   /**
076    * Add a new uuid to the list of things that can hurt this critter
077    @param uuid the uuid of item
078    */
079   public void addHurtBy(String uuid{
080     if (hurtBy.indexOf(uuid0{
081       hurtBy.add(uuid);
082     }
083   }
084   
085   /**
086    * Add the given TradesFor pair of items to the critter's list of such
087    * pairs.
088    @param tf the item to add.
089    */
090   public void addTradesFor(TradesFor tf{
091     if (tradesFor.indexOf(tf0{
092       tradesFor.add(tf);
093     }
094   }
095   
096   /**
097    * Get this critter's current location
098    @return  the location
099    */
100   public Location getLocation() {
101     return location;
102   }
103   
104   /**
105    * Get the UUID of the current owner of this item.
106    *
107    @return  the UUID of the owner
108    */
109   public String getLocationID() {
110     if (location != null{
111       return location.getUUID();
112     }
113     return locationID;
114   }
115 
116   /**
117    * Check if this critter is hurt by the element "named" by the uuid.
118    @param uuid the item which might hurt us
119    @return true if it can hurt us, false otherwise
120    */
121   public boolean isHurtBy(String uuid{
122     return (hurtBy.indexOf(uuid>= 0);
123   }
124 
125   /**
126    @param  location  the location to set
127    */
128   public void setLocation(Location location{
129     this.location = location;
130   }
131 
132   /**
133    * Determine what, if anything, this critter will trade for the given
134    * item. This NOT modify the critter's inventory.
135    @param getUUID the offered uuid
136    @return null if nothing offered, the offered trade item otherwise
137    */
138   public String willTradeFor(String getUUID{
139     for (int = 0; t != tradesFor.size()++t{
140       TradesFor curr = tradesFor.get(t);
141       if (curr.getGetsUUID().equalsIgnoreCase(getUUID)) {
142         return curr.getGivesUUID();
143       }
144     }
145     return null;
146   }
147 
148   /**
149    @param  health  the health to set
150    */
151   private void setHealth(int health{
152     this.health = health;
153   }
154 
155   /**
156    * Handle, if possible, the given name/value pair
157    *
158    @param   attribute  name of the attribute
159    @param   value      the value of the attribute
160    *
161    @return  true if the attribute was handled at this level; false
162    *          otherwise.
163    */
164   @Override
165   protected boolean handleAttributeValuePair(String attribute,
166     String value{
167     if (super.handleAttributeValuePair(attribute, value)) {
168       return true;
169     }
170 
171     if (attribute.equalsIgnoreCase("location")) {
172       locationID = value.toLowerCase();
173       return true;
174     } else if (attribute.equalsIgnoreCase("health")) {
175       setHealth(Integer.parseInt(value));
176       return true;
177     } else if (attribute.equalsIgnoreCase("hurtBy")) {
178       addHurtBy(value.toLowerCase());
179       return true;
180     } else if (attribute.equalsIgnoreCase("tradesFor")) {
181       String gets = value.substring(0, value.indexOf(':')).trim().toLowerCase();
182       String gives = value.substring(value.indexOf(':')+1).trim().toLowerCase();
183       
184       addTradesFor(new TradesFor(gets, gives));
185       return true;
186     }
187     return false;
188   }
189   
190   /**
191    * Internal toString helper method.
192    *
193    @return  A string representation of the fields.
194    */
195   @Override
196   protected String toStringGuts() {
197     return super.toStringGuts() + "\n" + "locationID = " +
198       getLocationID() + "\n" + "health = " + health;
199   }
200 }
201 
202 //Uploaded on Mon Mar 29 21:38:44 EDT 2010


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