1 package se
.umu
.cs
.dit06ajnajs
.level
;
4 import se
.umu
.cs
.dit06ajnajs
.agent
.Unit
;
5 import java
.util
.logging
.Logger
;
6 import se
.umu
.cs
.dit06ajnajs
.agent
.Direction
;
7 import java
.awt
.Graphics
;
9 import se
.umu
.cs
.dit06ajnajs
.AntiTD
;
10 import java
.util
.Observer
;
11 import java
.util
.Observable
;
12 import java
.util
.ArrayList
;
13 import java
.util
.List
;
16 * Class StartSquare extends MapSquare and implements Traversable and Observer.
17 * A StartSquare can recive units that are about to be released. It has
18 * functionality to release units without causing collisions
20 * @author Anton Johansson (dit06ajn@cs.umu.se)
23 public class StartSquare
extends MapSquare
implements Traversable
, Observer
{
24 private static Logger logger
= Logger
.getLogger("AntiTD");
25 private boolean active
;
26 private List
<Unit
> unitsToStart
;
29 private Direction startDirection
= Direction
.UP
;
32 * Creates a new StartSquare, calls extended MapSquares contructor with
33 * supplied parameters. Sets this StartSquare as not active.
35 * @param x The x-coordinate of this Square.
36 * @param y The y-coordinate of this Square.
37 * @param image The Image used to display this Square.
39 public StartSquare(int x
, int y
, Image image
) {
45 * Used to initialize fields that should be individually set for every
46 * StartSquare, since clone() is shallow.
50 this.unitsToStart
= new ArrayList
<Unit
>();
54 * Returns true if this StartSquare is active, false otherwise.
56 * @return True if this StartSquare is active, false otherwise.
58 public boolean isActive() {
63 * Sets the start Direction of this MapSquare.
65 * @param directioin The start Direction of this MapSquare.
67 public void setStartDirection(Direction directioin
) {
68 this.startDirection
= directioin
;
72 * Returns the start Direction of this StartSquare.
74 * @return The start Direction of this StartSquare.
76 public Direction
getStartDirection() {
77 return this.startDirection
;
81 * Returns the Unit in turn to start from this StartSquare.
83 * @return The Unit in turn to start from this StartSquare. If no Unit is
84 * about to start null is returned.
86 public Unit
getUnitToStart() {
87 if (!unitsToStart
.isEmpty()) {
88 return unitsToStart
.get(0);
95 * Adds a Unit to this StartSquare, sets its position to match the center
96 * point of this square, and sets the Units Direction to the Direction of
99 * @param unit The Unit to add.
101 public void addUnit(Unit unit
) {
102 unitsToStart
.add(unit
);
103 unit
.setCenterX(getCenterX());
104 unit
.setCenterY(getCenterY());
105 unit
.setDirection(getStartDirection(), this);
109 * Removes specified Unit from the start queue.
111 * @param unit The Unit to remove.
113 public void removeUnit(Unit unit
) {
114 unitsToStart
.remove(unit
);
118 * Sets the landing Units Direction to the Direction of this StartSquare.
120 * @param unit The Unit that land on this square.
122 public void landOn(Unit unit
) {
123 unit
.setDirection(startDirection
, this);
127 * Sets this StartSquare as active.
130 public void click() {
140 * Paints this square on the specified Graphics objects. Calls
141 * super.paint(p) and paints a green bar at bottom of this square if the
142 * StartSquare is active.
144 * @param g The Graphics object to paint to.
147 public void paint(Graphics g
) {
151 g
.setColor(Color
.GREEN
);
152 g
.fillRect(getX(), getY() + AntiTD
.SQUARE_SIZE
-2, AntiTD
.SQUARE_SIZE
, 2);
157 * Called when another StartSquare is activated, which means that this
158 * StartSquare should not be active.
160 * @param observer an <code>Observable</code> value
161 * @param obj an <code>Object</code> value
163 public void update(Observable observer
, Object obj
) {
164 logger
.info("Update in StartSquare: active is set to false");