001    package breakout;
002    
003    import java.awt.*;
004    import java.io.Serializable;
005    
006    /**
007     * class WorldEvent embodies a change to state of the World
008     * as reported to a WorldListener.
009     *
010     * @see breakout.World
011     * @see breakout.WorldListener
012     *
013     * @author benmv@olin.edu
014     * @version <tt>$Id: WorldEvent.java,v 1.2 2004/03/26 20:39:33 gus Exp $</tt>
015     */
016    public class WorldEvent implements Serializable {
017        /** brick added to world event */
018        public static final int ADD_BRICK = 0;
019        /** brick removed from world event */
020        public static final int REMOVE_BRICK = 1;
021        /** ball transferred out of world event */
022        public static final int BALL_TRANSFER = 3;
023    
024        /** action drawn from above constants */
025        private int action;
026        /** location of component */
027        private Point location;
028        /** direction of component if it was active */
029        private Point direction;
030        /** size of component */
031        private Dimension size;
032        
033        /**
034         * builds new WorldEvent with the given parameters.
035         *
036         * @param action Constant drawn from ADD_BRICK,REMOVE_BRICK,
037         * or BALL_TRANSFER.
038         * @param location component location
039         * @param size component size
040         * @param direction component direction (only valid for BALL_TRANSFER
041         * action normally)
042         */
043        WorldEvent(int action, Point location, Dimension size, Point direction) {
044            this.action = action;
045            this.location = location;
046            this.direction = direction;
047            this.size = size;
048        }
049        
050        /** getter for action
051         * @return What happened. */
052        public int getAction() { return this.action; }
053    
054        /** getter for location
055         * @return Point location of component */
056        public Point getLocation() { return new Point(this.location); }
057    
058        /** getter for direction
059         * @return Point direction of component
060         * @see breakout.ActiveBreakoutComponent
061         */
062        public Point getDirection() { return new Point(this.direction); }
063    
064        /** getter for size
065         * @return Dimension size of component */
066        public Dimension getSize() { return new Dimension(this.size); }
067    
068        /** factory method for building WorldEvent given a
069         * Breakout component.  Fills in direction if given an
070         * ActiveBreakoutComponent.
071         *
072         * @param action action that happened.
073         * @param bc Component the action relates to.
074         */
075        public static WorldEvent createEvent(int action, BreakoutComponent bc) {
076            if (bc instanceof ActiveBreakoutComponent) {
077                ActiveBreakoutComponent abc = (ActiveBreakoutComponent)bc;
078                return new WorldEvent(action,abc.getLocation(),abc.getSize(),
079                                      abc.getDirection());
080            } else
081                return new WorldEvent(action,bc.getLocation(),bc.getSize(),
082                                      new Point(0,0));
083        }
084    }
085