51857e20b60fa6b9f7ce3b12770693dbe22b61c8
[applet-bots.git] / src / appletbots / balls / Kicker.java
blob51857e20b60fa6b9f7ce3b12770693dbe22b61c8
1 /*
2 * Copyright (c) 2002 Erik Rasmussen - All Rights Reserverd
3 */
4 package appletbots.balls;
6 import appletbots.Agent;
7 import appletbots.WorldObject;
8 import appletbots.geometry.Vector;
10 import java.awt.*;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Iterator;
14 import java.util.List;
16 /**
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
24 /**
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;
38 /**
39 * Observes the world, and follows the Kicker Algorithm.
41 public void observeWorld()
43 final Ball ball = chooseBall();
44 if (ball != null)
46 setAcceleration(world.getVectorToObject(this, ball));
48 else
50 // continue in straight path until we find a ball to kick
51 accelerationVectorColor = null;
52 setAcceleration(new Vector(0, 0));
56 /**
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)
69 balls.add(object);
71 if (!balls.isEmpty())
72 return (Ball) balls.get((int) Math.floor(Math.random() * balls.size()));
73 else
74 return null;