scg/ch02/BasketBallSprite

From FANG

Jump to: navigation, search

001 package scg.ch02;
002 
003 
004 
005 import fang2.attributes.Vector2D;
006 import fang2.core.Game;
007 import fang2.core.Sprite;
008 import fang2.sprites.OvalSprite;
009 import fang2.sprites.RectangleSprite;
010 import fang2.transformers.AccelerationTransformer;
011 import fang2.transformers.BounceClassTransformer;
012 import fang2.transformers.BounceInsideRectangleTransformer;
013 import fang2.transformers.BounceSpriteTransformer;
014 import fang2.transformers.BounceTransformer;
015 import fang2.transformers.SpinTransformer;
016 import fang2.transformers.VelocityProvider;
017 import fang2.transformers.VelocityTransformer;
018 
019 /**
020  * Models a bouncing ball. This is a sprite: an object with an
021  * on-screen appearance which can be located in different places on the
022  * screen.
023  */
024 public class BasketBallSprite
025   extends OvalSprite {
026   /** the default diameter of a ball, in screens */
027   private static final double DEFAULT_DIAMETER = 0.10;
028 
029   /** bounces the ball off of sprites on the game board */
030   private final BounceClassTransformer spriteBouncer;
031 
032   /** the bouncing transformer; has velocity and bounce */
033   private final BounceInsideRectangleTransformer bouncer;
034 
035   /** the acceleration transformer; wrapped around the bouncer */
036   private final AccelerationTransformer gravity;
037 
038   /**
039    * Construct a brand-new {@link BasketBallSprite} with the default
040    * size and default transformer values.
041    */
042   public BasketBallSprite() {
043     super(DEFAULT_DIAMETER, DEFAULT_DIAMETER);
044     bouncer = new BounceInsideRectangleTransformer(
045         0.00.01.01.0// dimensions of screen 
046         new VelocityTransformer(new Vector2D()));
047     // amount of bounce off edges
048     bouncer.setElasticity(0.66);
049     spriteBouncer = new BounceClassTransformer(this, bouncer);
050     spriteBouncer.add(RectangleSprite.class);
051     // amount of bounce off sprites
052     spriteBouncer.setElasticity(0.45);
053     
054     gravity = new AccelerationTransformer(new Vector2D(), spriteBouncer);
055     addTransformer(gravity);
056     setColor(Game.getColor("misty rose"));
057   }
058 
059   /**
060    * Add a new target class.
061    
062    @param spriteClass the class of sprite to bounce off of
063    */
064   public void addTarget(Class<? extends Sprite> spriteClass{
065     spriteBouncer.add(spriteClass);
066   }
067   
068   /**
069    * Add a new target sprite to the ones this ball will interact with
070    *
071    @param  sprite  the sprite with which to interact
072    */
073   public void addTarget(Sprite sprite{
074     spriteBouncer.add(sprite);
075   }
076 
077   /**
078    * Get the accelerator transformer attached to this sprite. This is
079    * the gravity.
080    *
081    @return  the acceleration transformer attached to this ball
082    */
083   public AccelerationTransformer getAccelerationNG() {
084     return gravity;
085   }
086 
087   /**
088    * Get the bouncer attached to this ball. That way it can be modified.
089    *
090    @return  the bouncer
091    */
092   public BounceInsideRectangleTransformer getBouncer() {
093     return bouncer;
094   }
095 
096   /**
097    Set the velocity of the velocity providers attached to the ball.
098    *
099    @param  newVelocity  the new velocity in screens/second
100    */
101   public void setVelocity(Vector2D newVelocity{
102     bouncer.setVelocity(newVelocity);
103   }
104 
105   /**
106    Set the acceleration of the "gravity" obeyed by this ball
107    *
108    @param  newAcceleration  the new acceleration in screens/second^2
109    */
110   public void setAcceleration(Vector2D newAcceleration{
111     gravity.setAcceleration(newAcceleration);
112   }
113 
114   /**
115    * Get the velocity transformer attached to this sprite. Lets you set
116    * the velocity
117    *
118    @return  the velocity transformer
119    */
120   public VelocityProvider getVelocityNG() {
121     return bouncer;
122   }
123 }
124 
125 //Uploaded on Mon Mar 29 21:41:56 EDT 2010


Download/View scg/ch02/BasketBallSprite.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