fixed source code links to go to repo.or.cz repository
[applet-bots.git] / src / appletbots / CarrierAgent.java
blob681651681f99347ba7abb9a961d60e96fd5389d1
1 /*
2 * Copyright (c) 2002 Erik Rasmussen - All Rights Reserved
3 */
4 package appletbots;
6 import java.util.ArrayList;
7 import java.util.List;
9 /**
10 * This class represents an agent that can pick up objects in the
11 * appletbots world.
13 * @author Erik Rasmussen
15 public abstract class CarrierAgent extends Agent
17 /**
18 * The list of objects the agent is carrying
20 private List inventory = new ArrayList();
21 /**
22 * The distance the agents needs to be from an object to be able to pick
23 * it up
25 private int pickUpDistance;
27 /**
28 * Constructs an agent with the given parameters
30 * @param pickUpDistance How close a CarrierAgent must be to an object to
31 * pick it up
32 * @param size The agent's radius
33 * @param sight The distance the agent can see
34 * @param maxSpeed The maximum speed the agent can travel
35 * @param maxAcceleration The maximum acceleration for this agent
37 public CarrierAgent(final int pickUpDistance, final int size, final int sight, final double maxSpeed, final double maxAcceleration)
39 super(size, sight, maxSpeed, maxAcceleration);
40 this.pickUpDistance = pickUpDistance;
43 /**
44 * Picks up an object
46 * @param item The object to pick up
48 protected void pickUp(final CarriableObject item)
50 inventory.add(item);
51 item.setCarriedBy(this);
54 /**
55 * Drops an object
57 * @param item The object to drop
58 * @throws CollisionException Thrown if the item cannot be dropped within
59 * the "pickup distance"
61 protected void drop(final CarriableObject item) throws CollisionException
63 item.setCarriedBy(null);
64 try
66 world.dropItem(this, item);
67 inventory.remove(item);
69 catch (CollisionException e)
71 item.setCarriedBy(this);
72 throw e;
76 /**
77 * Returns whether or not the agent is carrying the given item
79 * @param item The item to check for
80 * @return Whether or not the agent is carrying the given item
82 public boolean hasItem(final CarriableObject item)
84 return inventory.contains(item);
87 /**
88 * Returns whether or not the agent is carrying any items
90 * @return Whether or not the agent is carrying any items
92 public boolean hasItems()
94 return !inventory.isEmpty();
97 /**
98 * Returns the contents of the agent's inventory
100 * @return The contents of the agent's inventory
102 public final CarriableObject[] getInventory()
104 synchronized (inventory)
106 if (inventory.isEmpty())
107 return new CarriableObject[]{};
108 final CarriableObject[] items = new CarriableObject[inventory.size()];
109 for (int i = 0; i < inventory.size(); i++)
110 items[i] = (CarriableObject) inventory.get(i);
111 return items;
116 * Returns the distance the agents needs to be from an object to be able
117 * to pick it up
119 * @return The distance the agents needs to be from an object to be able
120 * to pick it up
122 public int getPickUpDistance()
124 return pickUpDistance;