scg/ch09/FastSwimmer

From FANG

Jump to: navigation, search

001 package scg.ch09;
002 
003 
004 import fang2.core.Game;
005 
006 /**
007  * The fast swimmer at the top of the screen. Waves and all that.
008  */
009 public class FastSwimmer
010   extends Swimmer {
011   /** max seconds between appearances of fast swimmer */
012   public static double MAX_TIME_OFF_SCREEN = 6.0;
013 
014   /** The velocity to be set when the fast swimmer launches */
015   private double speedX;
016   private double speedY;
017 
018   /** state flag: is the swimmer waiting to launch? */
019   private boolean waiting;
020 
021   /**
022    * Create a new fast swimmer with the given score value. Score is
023    * passed to the {@link Swimmer} constructor.
024    *
025    @param  score
026    */
027   public FastSwimmer(int score, double speedX, double speedY{
028     super(score, 00)// initial velocity is (0, 0)
029     setLaunchSpeed(speedX, speedY);
030     startWaiting();
031   }
032 
033   /**
034    * Advance the fast swimmer. If swimming, move. If waiting, keep
035    * waiting unless it is time to launch.
036    *
037    @param  dT  seconds since last frame
038    */
039   @Override
040   public void advance(double dT{
041     decrementTimer(dT);
042     if (isSwimming()) {
043       translate(dT * getDeltaX(), dT * getDeltaY());
044     }
045   }
046 
047   /**
048    * Fast swimmers don't really bounce; instead, when striking an edge
049    * of the screen they disappear and go into waiting.
050    */
051   @Override
052   public void bounceOffEdges() {
053     if (runIntoLeftWall() || runIntoRightWall()) {
054       startWaiting();
055     }
056   }
057 
058   /**
059    * Is the swimmer swimming?
060    *
061    @return  true if swimmer is swimming; false if swimmer is waiting
062    */
063   public boolean isSwimming() {
064     return !waiting;
065   }
066 
067   /**
068    * Is the swimmer waiting to launch?
069    *
070    @return  true if the swimmer is waiting to launch; false if swimmer
071    *          is swimming
072    */
073   public boolean isWaiting() {
074     return waiting;
075   }
076 
077   /**
078    Set the speed (launch velocity) of this swimmer to the given x and
079    * y values.
080    *
081    @param  speedX  horizontal velocity of the swimmers (screens/sec)
082    @param  speedY  vertical velocity of the swimmers (screens/sec)
083    */
084   public void setLaunchSpeed(double speedX, double speedY{
085     this.speedX = speedX;
086     this.speedY = speedY;
087   }
088 
089   /**
090    * Start swimmer swimming
091    */
092   public void startSwimming() {
093     show();
094     waiting = false;
095     setFrame(0);
096   }
097 
098   /**
099    * Take swimmer off the screen.
100    */
101   public void startWaiting() {
102     hide();
103     waiting = true;
104     setLocation(00);
105     setVelocity(00);
106     startTimer();
107   }
108 
109   /**
110    * Launch the swimmer. Go from waiting to swimming state. There is a
111    * 50-50 chance the swimmer begins on either side of the screen; this
112    * could be changed to launch from the side closest to (or farthest
113    * from) the rescuer.
114    */
115   private void launch() {
116     startSwimming();
117     if (Game.getCurrentGame().randomDouble() 0.5{
118       launchLeftToRight();
119     } else {
120       launchRightToLeft();
121     }
122   }
123 
124   /**
125    Position the fast swimmer to move from left to right on the screen.
126    * Sets frame to 0, puts swimmer in upper-left corner, pointing to the
127    * right.
128    */
129   private void launchLeftToRight() {
130     setLocation(0.00.1);
131     setRotation(90);
132     setVelocity(speedX, speedY);
133   }
134 
135   /**
136    Position the fast swimmer to move from right to left on the screen.
137    * Sets frame to 0, puts swimmer in upper-right corner, pointing to
138    * the left.
139    */
140   private void launchRightToLeft() {
141     setLocation(1.00.1);
142     setRotation(-90);
143     setVelocity(-speedX, speedY);
144   }
145 
146   /**
147    * Return the amount of time the timer should countdown; when moving
148    * it should be just like swimmer. When waiting, when should it launch
149    * again?
150    *
151    @return  seconds until timer should expire
152    */
153   @Override
154   protected double getTimerCountdownTime() {
155     if (isSwimming()) {
156       return super.getTimerCountdownTime();
157     } else {
158       return Game.getCurrentGame().randomDouble(MAX_TIME_OFF_SCREEN);
159     }
160   }
161 
162   /**
163    * If swimming, just animate (like any other swimmer); if waiting, it
164    * is time to launch.
165    */
166   @Override
167   protected void timerExpired() {
168     if (isSwimming()) {
169       super.timerExpired();
170     } else {
171       launch();
172     }
173   }
174 }
175 
176 //Uploaded on Mon Mar 29 21:40:07 EDT 2010


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