scg/ch09/SpriteWithVelocity

From FANG

Revision as of 01:47, 30 March 2010 by Jam Jenkins (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

001 package scg.ch09;
002 
003 import fang2.sprites.CompositeSprite;
004 
005 /**
006  * A class which factors out the velocity and movement fields and
007  * methods that are common among the moving elements in RescueMission.
008  */
009 public class SpriteWithVelocity
010   extends CompositeSprite {
011   /** The sprite's velocity */
012   private double deltaX;
013   private double deltaY;
014 
015   /**
016    * Default constructor makes a sprite with a (0, 0) velocity.
017    */
018   public SpriteWithVelocity() {
019     this(0.00.0);
020   }
021 
022   /**
023    * Construct a new {@code MovingCompositeSprite} with the given
024    * velocity vector. The velocity is set to the given value
025    *
026    @param  deltaX  initial horizontal velocity
027    @param  deltaY  initial vertical velocity
028    */
029   public SpriteWithVelocity(double deltaX, double deltaY{
030     this.deltaX = deltaX;
031     this.deltaY = deltaY;
032   }
033 
034   /**
035    * Default advance behavior: Move according to the current velocity.
036    * That is, the velocity vector (in screens/sec) is multiplied by the
037    * time since the last frame (sec) and the result is the amount to
038    * move this sprite (screens).
039    *
040    @param  dT  seconds since last frame
041    */
042   public void advance(double dT{
043     translate(dT * deltaX, dT * deltaY);
044   }
045 
046   /**
047    * Bounce this sprite off an edge of the screen. This is intended to
048    * be overridden by subclasses to specialize what should happen.
049    */
050   public void bounceOffEdges() {
051     // intentionally does nothing
052   }
053 
054   /**
055    * Get the current horizontal velocity.
056    *
057    @return  current horizontal velocity in screens/second
058    */
059   public double getDeltaX() {
060     return deltaX;
061   }
062 
063   /**
064    * Get the current vertical velocity.
065    *
066    @return  current vertical velocity in screens/second
067    */
068   public double getDeltaY() {
069     return deltaY;
070   }
071 
072   /**
073    * Scale the velocity (in both dimensions) by the given factor. factor
074    * > 1 speeds up; factor < 1 slows down. Positive keeps the direction
075    * the same, negative reverses the direction
076    *
077    @param  factor  the factor to multiply the velocity by
078    */
079   public void multiplyVelocity(double factor{
080     deltaX *= factor;
081     deltaY *= factor;
082   }
083 
084   /**
085    * Reverse the velocity of the object in both dimensions.
086    */
087   public void reverseVelocity() {
088     multiplyVelocity(-1);
089   }
090 
091   /**
092    Set the horizontal velocity
093    *
094    @param  deltaX  new horizontal velocity in screens/second
095    */
096   public void setDeltaX(double deltaX{
097     this.deltaX = deltaX;
098   }
099 
100   /**
101    Set the vertical velocity
102    *
103    @param  deltaY  new vertical velocity in screens/second
104    */
105   public void setDeltaY(double deltaY{
106     this.deltaY = deltaY;
107   }
108 
109   /**
110    Set the velocity of this sprite to the given x and y values.
111    *
112    @param  deltaX  horizontal velocity of the victims (screens/sec)
113    @param  deltaY  vertical velocity of the victims (screens/sec)
114    */
115   public void setVelocity(double deltaX, double deltaY{
116     this.deltaX = deltaX;
117     this.deltaY = deltaY;
118   }
119 
120   /**
121    * Is the sprite moving down the screen?
122    *
123    @return  true if moving down, false otherwise
124    */
125   protected boolean movingDown() {
126     return deltaY > 0;
127   }
128 
129   /**
130    * Is the sprite moving left on the screen?
131    *
132    @return  true if moving left, false otherwise
133    */
134   protected boolean movingLeft() {
135     return deltaX < 0;
136   }
137 
138   /**
139    * Is the sprite moving right on the screen?
140    *
141    @return  true if moving right, false otherwise
142    */
143   protected boolean movingRight() {
144     return deltaX > 0;
145   }
146 
147   /**
148    * Is the sprite moving up the screen?
149    *
150    @return  true if moving up, false otherwise
151    */
152   protected boolean movingUp() {
153     return deltaY < 0;
154   }
155 
156   /**
157    * Has the sprite hit any of the four walls?
158    *
159    @return  true if sprite has hit; false otherwise
160    */
161   protected boolean runIntoAnyWall() {
162     return runIntoVerticalWall() || runIntoHorizontalWall();
163   }
164 
165   /**
166    * Has the sprite hit the bottom of the screen?
167    *
168    @return  true if the sprite has hit the bottom; false otherwise
169    */
170   protected boolean runIntoBottomWall() {
171     return (movingDown() && (1.0 <= getMaxY()));
172   }
173 
174   /**
175    * Has sprite hit either horizontal wall?
176    *
177    @return  true if sprite has hit; false otherwise
178    */
179   protected boolean runIntoHorizontalWall() {
180     return runIntoTopWall() || runIntoBottomWall();
181   }
182 
183   /**
184    * Has the sprite hit the left edge of the screen?
185    *
186    @return  true if the sprite has hit the left edge; false otherwise
187    */
188   protected boolean runIntoLeftWall() {
189     return (movingLeft() && (getMinX() <= 0.0));
190   }
191 
192   /**
193    * Has the sprite hit the right edge of the screen?
194    *
195    @return  true if the sprite has hit the right edge; false otherwise
196    */
197   protected boolean runIntoRightWall() {
198     return (movingRight() && (1.0 <= getMaxX()));
199   }
200 
201   /**
202    * Has the sprite hit the top of the screen?
203    *
204    @return  true if the sprite has hit the top; false otherwise
205    */
206   protected boolean runIntoTopWall() {
207     return (movingUp() && (getMinY() <= 0.0));
208   }
209 
210   /**
211    * Has the sprite hit either vertical wall
212    *
213    @return  true if sprite has hit; false otherwise
214    */
215   protected boolean runIntoVerticalWall() {
216     return runIntoLeftWall() || runIntoRightWall();
217   }
218 }
219 
220 //Uploaded on Mon Mar 29 21:38:39 EDT 2010


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