scg/ch09/Rescuer

From FANG

Jump to: navigation, search

001 package scg.ch09;
002 
003 import java.awt.Color;
004 import java.awt.event.KeyEvent;
005 
006 import fang2.core.Game;
007 import fang2.sprites.PolygonSprite;
008 
009 /**
010  * The rescuer sprite. Keyboard driven (so advance takes care of
011  * movement).
012  */
013 public class Rescuer
014   extends SpriteWithVelocity {
015   /** body of the rescuer */
016   private final PolygonSprite body;
017 
018   /** the rescue ring (to be thrown by the rescuer) */
019   private final RescueRing ring;
020 
021   /**
022    * Create a new rescuer (along with a RescueRing).
023    *
024    @param  deltaX      initial x-component of velocity for rescuer
025    @param  deltaY      initial y-component of velocity for rescuer
026    @param  ringDeltaX  initial x-component of velocity for rescue ring
027    @param  ringDeltaY  initial y-component of velocity for rescue ring
028    */
029   public Rescuer(double deltaX, double deltaY, double ringDeltaX,
030     double ringDeltaY{
031     super(deltaX, deltaY);
032     body = new PolygonSprite(3);
033     body.setScale(1.0);
034     addSprite(body);
035     ring = new RescueRing(this, ringDeltaX, ringDeltaY);
036   }
037 
038   /**
039    * Advance the rescuer according to the time that has passed
040    *
041    @param  dT  seconds since last frame
042    */
043   @Override
044   public void advance(double dT{
045     Game curr = Game.getCurrentGame();
046     if (curr.keyPressed()) {
047       char key = curr.getKeyPressed();
048       if ((key == KeyEvent.VK_LEFT|| (key == KeyEvent.VK_KP_LEFT)) {
049         setVelocity(-Math.abs(getDeltaX()), getDeltaY());
050         super.advance(dT);
051       } else if ((key == KeyEvent.VK_RIGHT||
052           (key == KeyEvent.VK_KP_RIGHT)) {
053         setVelocity(Math.abs(getDeltaX()), getDeltaY());
054         super.advance(dT);
055       } else if (key == KeyEvent.VK_SPACE{
056         ring.startFlying();
057       }
058     }
059   }
060 
061   /**
062    * Bounce the rescuer off the edge by pushing it back onto the screen
063    * (if necessary)
064    */
065   @Override
066   public void bounceOffEdges() {
067     if (getMinY() 0.0{
068       translateY(0.0 - getMinY());
069     }
070     if (getMaxY() 1.0{
071       translateY(1.0 - getMaxY());
072     }
073     if (getMinX() 0.0{
074       translateX(0.0 - getMinX());
075     }
076     if (getMaxX() 1.0{
077       translateX(1.0 - getMaxX());
078     }
079   }
080 
081   /**
082    * Return a reference to the rescue ring used by this rescuer
083    *
084    @return  reference to the ring; should never be null
085    */
086   public RescueRing getRescueRing() {
087     return ring;
088   }
089 
090   /**
091    Set the color sets the color of the parts of the rescuer
092    */
093   @Override
094   public void setColor(Color color{
095     body.setColor(color);
096   }
097 
098   /**
099    Set the location of this rescuer. Note that the ring moves with the
100    * rescuer if it is ready.
101    */
102   @Override
103   public void setLocation(double x, double y{
104     super.setLocation(x, y);
105     if (ring.isReady()) {
106       ring.setLocation(x, y);
107     }
108   }
109 
110   /**
111    Set the scale of the Rescuer. Note that the ring is scaled to match
112    * the rescuer.
113    */
114   @Override
115   public void setScale(double scale{
116     super.setScale(scale);
117     ring.setScale(scale);
118   }
119 }
120 
121 //Uploaded on Mon Mar 29 21:40:24 EDT 2010


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