scg/ch09/RescueRing

From FANG

Jump to: navigation, search

001 package scg.ch09;
002 
003 import java.awt.Color;
004 
005 import fang2.core.Game;
006 import fang2.core.Sprite;
007 import fang2.sprites.LineSprite;
008 import fang2.sprites.OvalSprite;
009 
010 /**
011  * The "projectile" for the RescueMission game. The RescueRing is the
012  * ring thrown out to rescue a drowning victim in the water. The ring is
013  * a {@link fang2.sprites.CompositeSprite CompositeSprite} with two
014  * circles in it. The inner circle serves as the "hole" through a
015  * doughnut. It is not, in fact, transparent. The projectile also keeps
016  * track of the rescue rope between itself and the rescuer. This is
017  * simply a {@link fang2.sprites.LineSprite LineSprite} that is colored
018  * the color of a "rope". Each time the ring moves, the rope is updated
019  * to have end points at the {@link Rescuer} and the ring. A RescueRing
020  * is in one of two states: it is ready to be thrown or it is flying.
021  * The methods isReady and isFlying permit the state of the ring to be
022  * checked.
023  */
024 public class RescueRing
025   extends SpriteWithVelocity {
026   /**
027    * the default color of the hole; use setBackgroundColor to change
028    * after ring is constructed.
029    */
030   public static final Color DEFAULT_BACKGROUND_COLOR = Game.getColor(
031       "cyan");
032 
033   /**
034    * the default color of the ring itself; use setColor to change after
035    * ring is constructed
036    */
037   public static final Color DEFAULT_RING_COLOR = Game.getColor(
038       "misty rose");
039 
040   /**
041    * the default color of the rope; use setRopeColor to change after
042    * ring is constructed
043    */
044   public static final Color DEFAULT_ROPE_COLOR = Game.getColor(
045       "goldenrod");
046 
047   private final OvalSprite inner;
048 
049   /** outer/inner are the appearance of the ring. inner is the hole */
050   private final OvalSprite outer;
051 
052   /** is the ring ready for launch? */
053   private boolean readyForLaunch;
054 
055   /**
056    * the rescuer with which this ring is associated; the rope goes back
057    * to the rescuer _and_ the ring returns to the rescuer when it goes
058    * off the screen or rescues an individual
059    */
060   private final Rescuer rescuer;
061 
062   /** the rope connecting the ring to the rescuer */
063   private final LineSprite rescueRope;
064 
065   /**
066    * Construct a brand new {@code RescueRing}. The inner/outer ring are
067    * part of the {@link fang2.sprite.CompositeSprite CompositeSprite}
068    while the rope is outside of the ring (so that it can connect the
069    * location of the {@link Rescuer} and the {@code RescueRing}.
070    *
071    @param  rescuer  the {@link Rescuer} with which this ring is
072    *                  associated
073    */
074   public RescueRing(Rescuer rescuer, double deltaX, double deltaY{
075     this.rescuer = rescuer;
076 
077     outer = new OvalSprite(11);
078     outer.setColor(DEFAULT_RING_COLOR);
079     inner = new OvalSprite(0.50.5);
080     inner.setColor(DEFAULT_BACKGROUND_COLOR);
081     addSprite(outer);
082     addSprite(inner);
083 
084     rescueRope = new LineSprite();
085     rescueRope.setColor(DEFAULT_ROPE_COLOR);
086     rescueRope.hide();// make it invisible (only visible after launch)
087 
088     setVelocity(deltaX, deltaY);
089 
090     startReady();
091   }
092 
093   /**
094    * Advance the rescue ring one frame. If flying, move according to
095    * velocity and update rope's end points; otherwise do nothing.
096    *
097    @param  dT  seconds elapsed since last frame
098    */
099   @Override
100   public void advance(double dT{
101     if (isFlying()) {
102       super.advance(dT);
103       rescueRope.setStart(rescuer.getLocation());
104       rescueRope.setEnd(getLocation());
105     } else {
106       setLocation(rescuer.getLocation());
107     }
108   }
109 
110   /**
111    * Finish flying when the ring moves off the screen.
112    */
113   @Override
114   public void bounceOffEdges() {
115     if (isFlying() && runIntoAnyWall()) {
116       startReady();
117     }
118   }
119 
120   /**
121    * Return the rope connecting the ring to the rescuer
122    *
123    @return  reference to the rope used to connect this ring to the
124    *          rescuer
125    */
126   public LineSprite getRope() {
127     return rescueRope;
128   }
129 
130   /**
131    * The ring is either ready or flying; returns true if the ring is not
132    * ready but rather in flight.
133    *
134    @return  true if ring is not ready; false otherwise
135    */
136   public boolean isFlying() {
137     return !readyForLaunch;
138   }
139 
140   /**
141    * Is the ring in the ready state? Is it ready to be thrown?
142    *
143    @return  true if ring is ready, false otherwise
144    */
145   public boolean isReady() {
146     return readyForLaunch;
147   }
148 
149   /**
150    * Is the rescue ring in the process of rescuing a given victim? If
151    * the victim is not null then an intersection test is done; if this
152    * ring intersects the victim, then the victim is being rescued and
153    this method returns true.
154    *
155    @param   victim  the sprite we _might_ be rescuing
156    *
157    @return  true if ring is flying and victim is non-null and ring
158    *          intersects the victim; false otherwise
159    */
160   public boolean isRescuing(Sprite victim{
161     return isFlying() && (victim != null&& intersects(victim);
162   }
163 
164   /**
165    Set the color of the inner ring.
166    *
167    @param  color  the color to set
168    */
169   public void setBackgroundColor(Color color{
170     inner.setColor(color);
171   }
172 
173   /**
174    Set the color of the outer ring.
175    *
176    @param  color  the color to set the outer ring
177    */
178   @Override
179   public void setColor(Color color{
180     outer.setColor(color);
181   }
182 
183   /**
184    Set the color of the rope
185    *
186    @param  color  the color of the rope to set
187    */
188   public void setRopeColor(Color color{
189     rescueRope.setColor(color);
190   }
191 
192   /**
193    * Move from ready to flying state.
194    */
195   public void startFlying() {
196     if (isReady()) {
197       readyForLaunch = false;
198       rescueRope.show();
199       rescueRope.setStart(rescuer.getLocation());
200       rescueRope.setEnd(getLocation());
201     }
202   }
203 
204   /**
205    * Move from flying to ready state.
206    */
207   public void startReady() {
208     if (isFlying()) {
209       readyForLaunch = true;
210       rescueRope.hide();
211       setLocation(rescuer.getLocation());
212     }
213   }
214 }
215 
216 //Uploaded on Mon Mar 29 21:41:12 EDT 2010


Download/View scg/ch09/RescueRing.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