001    // Faller.java
002    // Created: Wednesday, September 22, 1999 10:02:24 PM
003    
004    package ballworld;
005    
006    import cs101.lang.Animate;
007    
008    /**
009     * A Ball that falls and bounces.
010     * @author (original author unknown)
011     * @author Patrick G. Heck gus.heck@olin.edu
012     * @version $Id: Faller.java,v 1.4 2004/02/09 21:06:45 gus Exp $
013     */
014    public class Faller implements Ball, Animate
015    {
016            // fields:
017            double x=0;                     // current x position
018            double y=0;                     // current y position
019            double dx=0;                    // current x velocity
020            double dy=0;                    // current y velocity
021            
022            World world;
023    
024            /** Create a faller that starts at the top, center of the screen
025             * @param w The world we are being creted in
026             */        
027            public Faller(World w) {
028                this.world = w;
029                this.x = 0;
030                this.y = world.getMaxY()-1;
031            }
032    
033            // contract methods from Ball
034    
035            // inherits doc from Ball        
036            public double getX() {
037                    return x;
038            }
039            
040            // inherits doc from Ball        
041            public double getY() {
042                    return y;
043            }
044            
045            // inherits doc from Ball        
046            public double getRadius() {
047                    return 5;
048            }
049            
050            // inherits doc from Ball        
051            public void setWorld(World theWorld) {
052                    this.world= theWorld;
053                    this.dx=(Math.random()-0.5)*2;
054                    this.dy= -Math.random()*2;
055            }
056            
057            // inherits doc from Ball        
058            public void userClicked(double atX, double atY) {
059            }
060            
061            // inherits doc from Ball        
062            public void userTyped(char key) {
063            }
064            
065            /**
066             * Make sure that we stay inside the ball world.
067             */
068            private void checkBounds() {
069                    if (x<=world.getMinX() || x>=world.getMaxX()) {
070                            dx= -dx;
071                    }
072                    if (y<world.getMinY() || y>=world.getMaxY()) {
073                            dy= -dy;
074                    } else {
075                            dy= dy - 0.01;
076                    }
077            }
078            
079            // inherits doc from Animate
080            public void act() {
081                            this.checkBounds();
082                            x= x+dx;
083                            y= y+dy;
084                            Ball other= world.getClosestBall(this);
085                            if (other != null) {
086                                    double dx= other.getX()-this.getX();
087                                    double dy= other.getY()-this.getY();
088                                    if (dx*dx+dy*dy < 1) {
089                                            world.removeBall(this);
090                                            world.removeBall(other);
091                                    }
092                            }
093            }
094    }