001    /*
002     * SimuCS - Simulator to use with Classifier Systems 
003     * MSc project - Oxford University 
004     * by Benoit Isaac - Summer 2005
005     */
006    
007    package simuLCS;
008    import java.awt.Color;
009    import java.awt.Graphics2D;
010    import java.awt.geom.Point2D;
011    
012    /**
013     * Superclass of all the entities, providing basic functions, but relying a lot on the implementation given by subclasses.
014     * @author Benoit
015     * 
016     */
017    public abstract class Entity {
018    
019            /**
020             * To obtain the unique id
021             */
022            private static int nbInstances = 0;
023            
024            /**
025             * unique for each entity
026             */
027            private int id;
028            protected String name;
029            
030            /**
031             * Current position of the entity
032             */ 
033            protected Point2D coord;
034            protected int size;
035    
036            /**
037             * Pointer to the Random generator
038             * @see java.util.Random
039             */
040            protected java.util.Random generator;
041    
042            /**
043             * The color inside the entity.
044             */
045            protected Color color_int;
046    
047            /**
048             * The color of the border of the entity.
049             */
050            protected Color color_ext;
051    
052            /**
053             * Boolean which indicates whether or not the name of the Agent should be painted.
054             */
055            protected boolean paintName;
056    
057            public Entity() {
058                    nbInstances++;
059                    id = nbInstances;
060                    name = "NoName";
061            }
062    
063            /**
064             * Paint the entity. 
065             * Default painting is a circle, should be overriden for a different drawing.
066             */
067            public void paint(Graphics2D g) {
068    
069                    g.setColor(color_int);
070                    int x = (int) (coord.getX() - (size / 2));
071                    int y = (int) (coord.getY() - (size / 2));
072                    g.fillOval(x, y, size, size);
073                    g.setColor(color_ext);
074                    g.drawOval(x, y, size, size);
075                    if (paintName)
076                            g.drawString("" + getName(), x + size / 2 - 6, y + size / 2 + 4);
077    
078            }
079    
080            /**
081             * Clear the drawing of the agent at the indicated coordinates
082             * (Basically, a white circle is drawn)
083             * @param g Graphics
084             * @param c The coordinates where to clear the drawing of the agent.
085             */
086    
087            public void clear(Graphics2D g, Point2D c) {
088                    g.setColor(Color.WHITE);
089                    int x = (int) (c.getX() - (size / 2));
090                    int y = (int) (c.getY() - (size / 2));
091                    g.fillOval(x - 2, y - 2, size + 5, size + 5);
092            }
093    
094            /**
095             * By default, the closest coordinates of an entity are its normal coordinates.
096             * Should be overriden for special subclasses (ex: the Arena)
097             */
098            public Point2D getCoordNearTo(Entity otherEntity) {
099                    return getCoord();
100            }
101    
102            /**
103             * Calculates the distance between the CENTER of this entity and another point p.
104             * Should be overriden to handle walls (Arena).
105             * @param p Point2D
106             * @return the distance between the point and the center of the agent
107             */
108            public double distanceTo(Point2D p) {
109                    double dx = p.getX() - this.getCoord().getX();
110                    double dy = p.getY() - this.getCoord().getY();
111    
112                    double distToPoint = Math.sqrt(dx * dx + dy * dy);
113                    return distToPoint;
114            }
115    
116            /**
117             * By default, returns <code>false</code>.
118             * Should be overriden by subclasses
119             * @param a
120             * @return
121             */
122            public boolean collideWithMe(Agent a) {
123                    return false;
124            }
125    
126            /**
127             * Should be defined by subclasses
128             * @return
129             */
130            public abstract boolean isDangerous();
131            
132    
133            /**
134             * Should be defined by subclasses
135             * @return
136             */
137            public abstract boolean isMoving();
138            
139            /**
140            * Subclasses must explain how the entity moves.
141            * In subclasses, an Agent might move according to the positions of the 
142            * other agents, while the Arena or some Food might not move at all.
143            * @param a The Arena inside which to move
144            * @param en The list of other Entities in the arena
145            * @param nbEntitities Number of entities to consider in the list <code>ag</code>
146            * @param g Graphics
147            * @see AgentDuck#move(Arena, Entity[], int, Graphics2D)
148            * @see AgentClassifier#move(Arena, Entity[], int, Graphics2D)
149            */
150            public abstract void move(Arena a, Entity[] others, int nbEntities, Graphics2D g);
151    
152            //      public abstract Point2D getCoord(Entity otherAgent);
153    
154            /**
155             * @return
156             */
157            public int getId() {
158                    return id;
159            }
160    
161            /**
162             * @return
163             */
164            public String getName() {
165                    return name;
166            }
167    
168            /**
169             * @param string
170             */
171            public void setName(String string) {
172                    name = string;
173            }
174    
175            public Point2D getCoord() {
176                    return coord;
177            }
178    
179            public void setSize(int s) {
180                    size = s;
181            }
182    
183            public int getSize() {
184                    return size;
185            }
186            
187            public double getRadiusBody() {
188                    return getSize()/2;
189            }
190    
191            /**
192                     * @return The color of the outer circle.
193                     */
194            public Color getColor_ext() {
195                    return color_ext;
196            }
197    
198            /**
199             * @return The color of the inner circle.
200             */
201            public Color getColor_int() {
202                    return color_int;
203            }
204    
205            /**
206             * Set the color of the outer circle.
207             * @param color
208             */
209            public void setColor_ext(Color color) {
210                    color_ext = color;
211            }
212    
213            /**
214             * Set the color of the inner circle.
215             * @param color
216             */
217            public void setColor_int(Color color) {
218                    color_int = color;
219            }
220    
221            /**
222             * Indicates if the name should be painted
223             * @return <code>true</code> if it should be painted, <code>false</code> otherwise.
224             * @see #paintName
225             */
226            public boolean isPaintName() {
227                    return paintName;
228            }
229    
230            /**
231             * Set the parameter paintName to the boolean b.
232             * @param b
233             * @see #paintName
234             */
235            public void setPaintName(boolean b) {
236                    paintName = b;
237            }
238    
239            /**
240             * Returns a string describing the Entity.
241             */
242            public String toString() {
243                    return "Entity " + name + " " + coord;
244            }
245    }