2 * Copyright (c) 2002 Erik Rasmussen - All Rights Reserverd
6 import appletbots
.geometry
.Vector
;
9 import java
.util
.ArrayList
;
10 import java
.util
.List
;
13 * This class represents an Agent in a appletbots world.
15 * @author Erik Rasmussen
17 public abstract class Agent
extends WorldObject
20 * The distance the agent can see
24 * The maximum amount of acceleration for this agent
26 protected double maxAcceleration
;
28 * The world in which the agent exists
30 protected World world
;
32 * The agent's acceleration
34 private Vector acceleration
= new Vector(0, 0);
37 * The color to paint the velocity vector
39 protected Color velocityVectorColor
;
41 * The color to paint the acceleration vector
43 protected Color accelerationVectorColor
;
46 * Constructs a new agent with the following default values:<br>
50 * maxAcceleration = 3.0<br>
58 * Constructs an agent with the given parameters
60 * @param size The agent's radius
61 * @param sight The distance the agent can see
62 * @param maxSpeed The maximum speed the agent can travel
63 * @param maxAcceleration The maximum acceleration for this agent
65 public Agent(final int size
, final int sight
, final double maxSpeed
, final double maxAcceleration
)
67 super(size
, maxSpeed
, size
, Color
.white
);
69 this.maxAcceleration
= maxAcceleration
;
73 * Returns the distance this agent can see
75 * @return The distance this agent can see
83 * Tells the agent what world he is in. This is called by
86 * @param world The world the agent exists in
88 public void setWorld(final World world
)
94 * Returns a random acceleration vector
96 * @return A random acceleration vector
98 public final Vector
getRandomAcceleration()
100 return Vector
.getRandom(Math
.random() * maxAcceleration
);
104 * Returns the agent's acceleration. Note that the vector returned is a
105 * clone of the internal object, so changing the returned vector will not
106 * affect the agent's acceleration. To modify the agent's acceleration,
107 * use setAcceleration(Vector).
109 * @return The agent's acceleration
111 public Vector
getAcceleration()
113 return (Vector
) acceleration
.clone();
117 * Sets the agent's acceleration vector. If the given vector has a
118 * magnitude greater than the agent's maxAcceleration, then the vector is
119 * scaled down to the maxAcceleration.
121 * @param acceleration The agent's acceleration
123 protected void setAcceleration(final Vector acceleration
)
125 this.acceleration
= (Vector
) acceleration
.clone();
126 if (this.acceleration
.getLength() > maxAcceleration
)
127 this.acceleration
= this.acceleration
.setLength(maxAcceleration
);
131 * Returns the agent's maximum acceleration
133 * @return The agent's maximum acceleration
135 public double getMaxAcceleration()
137 return maxAcceleration
;
141 * Returns an array of vectors that should be drawn at the time this agent
144 * @return An array of vectors that should be drawn at the time this agent
147 public VectorToDraw
[] getVectorsToDraw()
149 final List vectorsToDraw
= new ArrayList();
150 if (accelerationVectorColor
!= null)
151 vectorsToDraw
.add(new VectorToDraw(getAcceleration().multiply(size
), accelerationVectorColor
));
152 if (velocityVectorColor
!= null)
153 vectorsToDraw
.add(new VectorToDraw(world
.getVelocity(this).multiply(size
), velocityVectorColor
));
154 final VectorToDraw
[] vectorsToDrawArray
= new VectorToDraw
[vectorsToDraw
.size()];
155 for (int i
= 0; i
< vectorsToDraw
.size(); i
++)
156 vectorsToDrawArray
[i
] = (VectorToDraw
) vectorsToDraw
.get(i
);
157 return vectorsToDrawArray
;
161 * Sets a flag to let the agent know whether or not to draw his
162 * acceleration vector
164 * @param showAcceleration Whether or not to draw the acceleration vector
166 public void setShowAcceleration(final boolean showAcceleration
)
168 accelerationVectorColor
= showAcceleration ? Color
.magenta
: null;
172 * Returns whether or not the agent's acceleration vector will be drawn
174 * @return Whether or not the agent's acceleration vector will be drawn
176 public boolean getShowAcceleration()
178 return accelerationVectorColor
!= null;
182 * Sets a flag to let the agent know whether or not to draw his velocity
185 * @param showVelocity Whether or not to draw the velocity vector
187 public void setShowVelocity(final boolean showVelocity
)
189 velocityVectorColor
= showVelocity ? Color
.orange
: null;
193 * Returns whether or not the agent's velocity vector will be drawn
195 * @return Whether or not the agent's velocity vector will be drawn
197 public boolean getShowVelocity()
199 return velocityVectorColor
!= null;
203 * The method invoked to allow the agent to observe the world and
204 * optionally modify his acceleration to try to achieve a goal.
206 public abstract void observeWorld();