2 * Copyright (c) 2002 Erik Rasmussen - All Rights Reserverd
4 package appletbots
.balls
;
6 import appletbots
.Agent
;
7 import appletbots
.WorldObject
;
8 import appletbots
.geometry
.Vector
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Collection
;
13 import java
.util
.Iterator
;
14 import java
.util
.List
;
17 * A kicker agent chooses a visible ball at random and accelerates towards it.
18 * If no ball is visible the agent sets his acceleration to zero.
20 * @author Erik Rasmussen
22 public class Kicker
extends Agent
25 * Constructs a new Kicker with the given parameters
27 * @param size The agent's radius
28 * @param sight The distance the agent can see
29 * @param maxSpeed The maximum speed the agent can travel
30 * @param maxAcceleration The maximum acceleration for this agent
32 public Kicker(final int size
, final int sight
, final double maxSpeed
, final double maxAcceleration
)
34 super(size
, sight
, maxSpeed
, maxAcceleration
);
35 this.color
= Color
.cyan
;
39 * Observes the world, and follows the Kicker Algorithm.
41 public void observeWorld()
43 final Ball ball
= chooseBall();
46 setAcceleration(world
.getVectorToObject(this, ball
));
50 // continue in straight path until we find a ball to kick
51 accelerationVectorColor
= null;
52 setAcceleration(new Vector(0, 0));
57 * Selects a ball at random from those visible
59 * @return The chosen ball
61 protected Ball
chooseBall()
63 final Collection seenObjects
= world
.getSeenObjects(this);
64 final List balls
= new ArrayList();
65 for (Iterator iterator
= seenObjects
.iterator(); iterator
.hasNext();)
67 final WorldObject object
= (WorldObject
) iterator
.next();
68 if (object
instanceof Ball
)
72 return (Ball
) balls
.get((int) Math
.floor(Math
.random() * balls
.size()));