scg/ch08/Person

From FANG

Jump to: navigation, search

001 package scg.ch08;
002 
003 import java.awt.Color;
004 
005 import fang2.core.Game;
006 import fang2.sprites.CompositeSprite;
007 import fang2.sprites.OvalSprite;
008 import fang2.sprites.RectangleSprite;
009 
010 /**
011  * A {@code Person} for use in a flu simulator; a person is just their
012  * current state of health and the number of days they have been that
013  * way (dead and healthy people don't track how long they have had their
014  * condition)
015  */
016 public class Person
017   extends CompositeSprite {
018   /** Number of days spent in each of the states of health */
019   public static final int DAYS_SICK = 1;
020   public static final int DAYS_CONTAGIOUS = 3;
021   public static final int DAYS_RECOVERING = 3;
022 
023   /** Chance of infection and death */
024   public static final double INFECTION_CHANCE = 0.50;
025   public static final double MORTALITY_RATE = 0.20;
026 
027   /** states of health. */
028   private static final int DEAD = -1;
029   private static final int HEALTHY = 0;
030   private static final int SICK = 1;
031   private static final int CONTAGIOUS = 2;
032   private static final int RECOVERING = 3;
033 
034   /** Color constants for each of the states of health */
035   private static final Color COLOR_DEAD = Game.getColor("dark gray");
036   private static final Color COLOR_HEALTHY = Game.getColor("green");
037   private static final Color COLOR_SICK = Game.getColor("green yellow");
038   private static final Color COLOR_CONTAGIOUS = 
039     Game.getColor("goldenrod");
040   private static final Color COLOR_RECOVERING = 
041     Game.getColor("yellow green");
042 
043   /** visible body parts; colored to indicate health state */
044   private final RectangleSprite body;
045   private final OvalSprite head;
046 
047   /** current state of health; drawn from states above */
048   private int health;
049 
050   /** days a person has been sick (or greater) */
051   private int sickDay;
052 
053   /**
054    * Initialize a new {@code Person}. Creates visible shapes and uses
055    * makeHealty to initialize fields and colors.
056    */
057   public Person() {
058     body = new RectangleSprite(0.50.5);
059     body.setLocation(0.00.25);
060     head = new OvalSprite(0.50.5);
061     head.setLocation(0.0, -0.25);
062     addSprite(head);
063     addSprite(body);
064     makeHealthy();
065   }
066 
067   /**
068    * This person has been exposed to the disease. Either they get it or
069    * they don't.
070    */
071   public void expose() {
072     if (isHealthy() &&
073         (Game.getCurrentGame().randomDouble() < INFECTION_CHANCE)) {
074       makeSick();
075     }
076   }
077 
078   /**
079    * Randomly determine if killed by disease. Transition to healthy or
080    * dead depending on mortality.
081    */
082   public void finishRecovery() {
083     if (Game.getCurrentGame().randomDouble() < MORTALITY_RATE{
084       makeDead();
085     } else {
086       makeHealthy();
087     }
088   }
089 
090   /**
091    * Is this person CONTAGIOUS?
092    *
093    @return  true if health is CONTAGIOUS; false otherwise
094    */
095   public boolean isContagious() {
096     return health == CONTAGIOUS;
097   }
098 
099   /**
100    * Is this person DEAD?
101    *
102    @return  true if health is DEAD; false otherwise
103    */
104   public boolean isDead() {
105     return health == DEAD;
106   }
107 
108   /**
109    * Is this person HEALTHY?
110    *
111    @return  true if health is HEALTHY; false otherwise
112    */
113   public boolean isHealthy() {
114     return health == HEALTHY;
115   }
116 
117   /**
118    * Is this person RECOVERING?
119    *
120    @return  true if health is RECOVERING; false otherwise
121    */
122   public boolean isRecovering() {
123     return health == RECOVERING;
124   }
125 
126   /**
127    * Is this person SICK?
128    *
129    @return  true if health is SICK; false otherwise
130    */
131   public boolean isSick() {
132     return health == SICK;
133   }
134 
135   /**
136    Set the person's state to CONTAGIOUS. Update health, sickDay, and
137    * color.
138    */
139   public void makeContagious() {
140     sickDay = 0;
141     health = CONTAGIOUS;
142     setColor(COLOR_CONTAGIOUS);
143   }
144 
145   /**
146    Set this person's state to DEAD. Update health, sickDay, and color.
147    */
148   public void makeDead() {
149     sickDay = 0;
150     health = DEAD;
151     setColor(COLOR_DEAD);
152   }
153 
154   /**
155    Set this person's state to HEALTHY. Update health, sickDay, and
156    * color.
157    */
158   public void makeHealthy() {
159     sickDay = 0;
160     health = HEALTHY;
161     setColor(COLOR_HEALTHY);
162   }
163 
164   /**
165    Set the state to RECOVERING. Update health, sickDay, and color.
166    */
167   public void makeRecovering() {
168     sickDay = 0;
169     health = RECOVERING;
170     setColor(COLOR_RECOVERING);
171   }
172 
173   /**
174    Set the person's state to SICK. Update health, sickDay, and color.
175    */
176   public void makeSick() {
177     sickDay = 0;
178     health = SICK;
179     setColor(COLOR_SICK);
180   }
181 
182   /**
183    * Move forward one simulated day: if (healthy or dead) do nothing;
184    else add one to sickDay and advance illness accordingly.
185    */
186   public void nextDay() {
187     if (!isHealthy() && !isDead()) {
188       ++sickDay;
189 
190       if (isSick() && (sickDay >= DAYS_SICK)) {
191         makeContagious();
192       } else if (isContagious() && (sickDay >= DAYS_CONTAGIOUS)) {
193         makeRecovering();
194       } else if (isRecovering() && (sickDay >= DAYS_RECOVERING)) {
195         finishRecovery();
196       }
197     }
198   }
199 
200   /**
201    Override the parent {@code setColor} method so that color of the
202    * component parts is set.
203    *
204    @param  color  the {@link java.awt.Color Color} to which the
205    *                components should be set
206    */
207   @Override
208   public void setColor(Color color{
209     body.setColor(color);
210     head.setColor(color);
211   }
212 }
213 
214 //Uploaded on Mon Mar 29 21:42:32 EDT 2010


Download/View scg/ch08/Person.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