scg/ch06/GeneralPongBall

From FANG

Jump to: navigation, search

001 package scg.ch06;
002 
003 import fang2.attributes.Location2D;
004 import fang2.core.Game;
005 import fang2.core.Sprite;
006 import fang2.sprites.OvalSprite;
007 
008 /**
009  * The PongBall is an augmented {@link fang2.sprites.OvalSprite
010  * OvalSprite} which has a velocity, an advance method which applies
011  * that velocity, and the ability to bounce off of the walls of the game
012  * and other sprites.
013  */
014 public class GeneralPongBall
015   extends OvalSprite {
016   /**
017    * current velocity in screens/second; decomposed into x-coordinate
018    * and y-coordinate velocities
019    */
020   private double deltaX;
021   private double deltaY;
022 
023   public final int LEFT_EDGE = 0x0001;
024   public final int RIGHT_EDGE = 0x0002;
025   public final int TOP_EDGE = 0x0004;
026   public final int BOTTOM_EDGE = 0x0008;
027 
028   private int bounceEdges;
029 
030   /**
031    * Create a new PongBall. Specify the size and the initial velocity of
032    * the ball.
033    *
034    @param  width   width of the ball in screens
035    @param  height  height of the ball in screens
036    @param  deltaX  initial horizontal velocity in screens per second
037    @param  deltaY  initial vertical velocity in screens per second
038    */
039   public GeneralPongBall(double width, double height, double deltaX,
040     double deltaY{
041     super(width, height);
042     /*
043      * A scope hole: to which deltaX (line 9 or line 25) does the
044      * variable deltaX refer? The closest (furthest in in curly braces),
045      * so line 25 How to refer to the field? Use this and a dot.
046      */
047     this.deltaX = deltaX;// set FIELD to the parameter value
048     this.deltaY = deltaY;// set FIELD to the parameter value
049     this.bounceEdges = LEFT_EDGE | RIGHT_EDGE | TOP_EDGE | BOTTOM_EDGE;
050   }
051 
052   /**
053    * Create a new PongBall. Specify the size and the initial velocity
054    * will default to 0.0, 0.0
055    *
056    @param  width   width of the ball in screens
057    @param  height  height of the ball in screens
058    */
059   public GeneralPongBall(double width, double height{
060     /*
061      * The first line of a constructor can pass the job to a different
062      * constructor of the current object by calling a method "this".
063      */
064     this(width, height, 0.00.0);
065   }
066 
067   public Location2D getVelocity() {
068     return new Location2D(deltaX, deltaY);
069   }
070 
071   /**
072    Set the velocity of this ball to the given x and y values.
073    *
074    @param  deltaX  horizontal velocity of the ball (screens/sec)
075    @param  deltaY  vertical velocity of the ball (screens/sec)
076    */
077   public void setVelocity(double deltaX, double deltaY{
078     this.deltaX = deltaX;
079     this.deltaY = deltaY;
080   }
081 
082   /**
083    * Update this ball's position on the screen. This moves the
084    * responsibility for updating the ball's position out of the {@link
085    * fang2.core.Game Game} class's {@code advance} method. This is a good
086    * division of responsibilities: the ball knows its speed and whatever
087    else it needs to know.
088    *
089    @param  deltaT  time elapsed since the last call to advance in
090    *                 seconds
091    */
092   public void advance(double deltaT, Sprite paddle{
093     translate(deltaX * deltaT, deltaY * deltaT);
094     bounceOffEdges();
095     if ((paddle != null&& intersects(paddle)) {
096       bounceOffSprite(paddle);
097       Game.getCurrentGame().setScore(Game.getCurrentGame().getScore() +
098         1);
099     }
100   }
101 
102   /**
103    @return  the bounceEdges
104    */
105   public int getBounceEdges() {
106     return bounceEdges;
107   }
108 
109   /**
110    @param  bounceEdges  the bounceEdges to set
111    */
112   public void setBounceEdges(int bounceEdges{
113     this.bounceEdges = bounceEdges;
114   }
115 
116   /**
117    * Did the ball hit the right edge?
118    *
119    @return  true if it has hit the right edge
120    */
121   private boolean hitRightEdge() {
122     return (getMaxX() >= 1.0);
123   }
124 
125   /**
126    * Did the ball hit the left edge?
127    *
128    @return  true if it hit the left edge
129    */
130   private boolean hitLeftEdge() {
131     return (getMinX() <= 0.0);
132   }
133 
134   /**
135    * Did the ball hit the bottom edge?
136    *
137    @return  true if the ball hit the bottom edge
138    */
139   private boolean hitBottomEdge() {
140     return (getMaxY() >= 1.0);
141   }
142 
143   /**
144    * Did the ball hit the top edge?
145    *
146    @return  true if the ball hit the top edge
147    */
148   private boolean hitTopEdge() {
149     return (getMinY() <= 0.0);
150   }
151 
152   /**
153    * Respond to hitting a horizontal edge; reverse the up/down direction
154    * (by changing the sign of deltaY).
155    */
156   public void bounceOffHorizontalEdge() {
157     deltaY = -deltaY;
158   }
159 
160   /**
161    * Respond to hitting a vertical edge; reverse the left/right
162    * direction (by changing the sign of deltaX).
163    */
164   public void bounceOffVerticalEdge() {
165     deltaX = -deltaX;
166   }
167 
168   /**
169    * Has the ball hit a scoring/out of bounds line?
170    *
171    @return  true if out of bounds; false otherwise
172    */
173   public boolean isOutOfBounds() {
174     return (((bounceEdges & TOP_EDGE== 0&& hitTopEdge()) ||
175       (((bounceEdges & BOTTOM_EDGE== 0&& hitBottomEdge()) ||
176       (((bounceEdges & LEFT_EDGE== 0&& hitLeftEdge()) ||
177       (((bounceEdges & RIGHT_EDGE== 0&& hitRightEdge());
178   }
179 
180   /**
181    * Bounce the ball off all walls off which it bounces.
182    */
183   public void bounceOffEdges() {
184     if ((((bounceEdges & TOP_EDGE!= 0&& hitTopEdge()) ||
185         (((bounceEdges & BOTTOM_EDGE!= 0&& hitBottomEdge())) {
186       bounceOffHorizontalEdge();
187     }
188     if ((((bounceEdges & LEFT_EDGE!= 0&& hitLeftEdge()) ||
189         (((bounceEdges & RIGHT_EDGE!= 0&& hitRightEdge())) {
190       bounceOffVerticalEdge();
191     }
192   }
193 
194   /**
195    * A ball collides with any other sprite, treat the other as a
196    * rectangle, bounce off the horizontal/vertical surfaces (or both).
197    *
198    @param  other  the other sprite which we hit
199    */
200   public void bounceOffSprite(Sprite other{
201     // we hit a horizontal edge so reflect the y-velocity
202     if ((getY() < other.getMinY()) || (other.getMaxY() < getY())) {
203       deltaY = -deltaY;
204     }
205 
206     // we hit a vertical edge so reflect the x-velocity
207     if ((getX() < other.getMinX()) || (other.getMaxX() < getX())) {
208       deltaX = -deltaX;
209     }
210   }
211 }
212 
213 //Uploaded on Mon Mar 29 21:39:42 EDT 2010


Download/View scg/ch06/GeneralPongBall.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