Javadoc, small bugfix in levels.xml
[AntiTD.git] / src / se / umu / cs / dit06ajnajs / level / StartSquare.java
blob72d91dd01c58ffebf0c710db4c6aea919195ffbb
1 package se.umu.cs.dit06ajnajs.level;
3 import java.awt.Image;
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;
8 import java.awt.Color;
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;
15 /**
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)
21 * @version 1.0
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;
31 /**
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) {
40 super(x, y, image);
41 this.active = false;
44 /**
45 * Used to initialize fields that should be individually set for every
46 * StartSquare, since clone() is shallow.
48 @Override
49 public void init() {
50 this.unitsToStart = new ArrayList<Unit>();
53 /**
54 * Returns true if this StartSquare is active, false otherwise.
56 * @return True if this StartSquare is active, false otherwise.
58 public boolean isActive() {
59 return active;
62 /**
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;
71 /**
72 * Returns the start Direction of this StartSquare.
74 * @return The start Direction of this StartSquare.
76 public Direction getStartDirection() {
77 return this.startDirection;
80 /**
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);
89 } else {
90 return null;
94 /**
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
97 * this StartSquare.
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.
129 @Override
130 public void click() {
131 if (!active) {
132 setChanged();
133 notifyObservers();
134 clearChanged();
135 this.active = true;
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.
146 @Override
147 public void paint(Graphics g) {
148 super.paint(g);
150 if (active) {
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");
165 this.active = false;