001    // Exploder.java
002    // Created: Thursday, September 23, 1999 10:03:26 PM
003    
004    package ballworld;
005    
006    import cs101.lang.Animate;
007    
008    /**
009     * A ball that Explodes.
010     *
011     * @author (original author unknown)
012     * @author Patrick G. Heck
013     */
014    public class Exploder implements Animate, Ball
015    {
016            double x= 0;
017            double y= 0;
018            double dx= 0;
019            double dy= 0;
020            double r= 2;
021            double maxr= Math.random()+2;
022            int generation = 0;
023            
024            World world;
025            
026            /**
027             * Create a new exploder with default position.
028             */        
029            public Exploder() {
030            }
031            
032            /**
033             * Create a new exploder that is aware of it's world from the start.
034             * @param myWorld The world that the ball is being created in
035             */        
036            public Exploder(World myWorld) {
037                world = myWorld;
038                this.x = world.getMaxX()/2;
039                this.y = world.getMaxY()/2;
040    
041            }
042            
043            /**
044             * Create an exploder at given coordinates and a predfined generation.
045             *
046             * @param generation Our generation
047             * @param startx Our starting x coordinate
048             * @param starty Our starting y coordinate
049             */        
050            public Exploder(double startx, double starty, int generation) {
051                    this.x= startx;
052                    this.y= starty;
053                    this.dx= Math.random()-0.5;
054                    this.dy= Math.random()-0.5;
055                    this.generation = generation;
056            }
057            
058            // inherits doc from Ball
059            public double getX() {return x;}
060            
061            // inherits doc from Ball        
062            public double getY() {return y;}
063    
064            // inherits doc from Ball        
065            public double getRadius() {return r;}
066            
067            // inherits doc from Ball        
068            public void setWorld(World theWorld) {
069                    this.world= theWorld;
070            }
071            
072            // inherits doc from Ball        
073            public void userClicked(double x, double y) {}
074    
075            // inherits doc from Ball        
076            public void userTyped(char key) {}
077            
078            // inherits doc from Animate        
079            public void act() {
080                    r+=0.2;
081                    x+= dx;
082                    y+= dy;
083                    if (r>maxr) {
084                            if (this.generation < 6) {
085                                    world.addBall(new Exploder(this.x, this.y, this.generation+1));
086                                    world.addBall(new Exploder(this.x, this.y, this.generation+1));
087                            } else {
088                                    world.addBall(new Shrinker(this.x, this.y));
089                            }
090                            world.removeBall(this);
091                    }
092            }
093            
094            
095    }