2 * Copyright (c) 2002 Erik Rasmussen - All Rights Reserved
6 import java
.util
.ArrayList
;
10 * This class represents an agent that can pick up objects in the
13 * @author Erik Rasmussen
15 public abstract class CarrierAgent
extends Agent
18 * The list of objects the agent is carrying
20 private List inventory
= new ArrayList();
22 * The distance the agents needs to be from an object to be able to pick
25 private int pickUpDistance
;
28 * Constructs an agent with the given parameters
30 * @param pickUpDistance How close a CarrierAgent must be to an object to
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
;
46 * @param item The object to pick up
48 protected void pickUp(final CarriableObject item
)
51 item
.setCarriedBy(this);
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);
66 world
.dropItem(this, item
);
67 inventory
.remove(item
);
69 catch (CollisionException e
)
71 item
.setCarriedBy(this);
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
);
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();
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
);
116 * Returns the distance the agents needs to be from an object to be able
119 * @return The distance the agents needs to be from an object to be able
122 public int getPickUpDistance()
124 return pickUpDistance
;