Merge branch 'master' of ssh://blastura@repo.or.cz/srv/git/AntiTD
[AntiTD.git] / src / se / umu / cs / dit06ajnajs / map / StartSquare.java
blobcfd0bcf05da301302743d4d962c3a2425961fcf8
1 package se.umu.cs.dit06ajnajs.map;
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 public class StartSquare extends MapSquare implements Traversable, Observer {
16 private static Logger logger = Logger.getLogger("AntiTD");
17 private boolean active;
18 private List<Unit> unitsToStart;
20 // TODO, set startDirection dynamicly
21 private Direction startDirection = Direction.UP;
23 public StartSquare(int x, int y, Image image) {
24 super(x, y, image);
25 this.active = false;
28 /**
29 * Used to initialize fields that should be individually set for every
30 * StartSquare, since clone() is shallow.
32 @Override
33 public void init() {
34 this.unitsToStart = new ArrayList<Unit>();
37 public boolean isActive() {
38 return active;
41 public void setActive(boolean active) {
42 this.active = active;
45 public void setStartDirection(Direction directioin) {
46 this.startDirection = directioin;
49 public Direction getStartDirection() {
50 return this.startDirection;
53 public Unit getUnitToStart() {
54 if (!unitsToStart.isEmpty()) {
55 return unitsToStart.get(0);
56 } else {
57 return null;
61 public void addUnit(Unit unit) {
62 unitsToStart.add(unit);
63 unit.setCenterX(getCenterX());
64 unit.setCenterY(getCenterY());
65 unit.setDirection(getStartDirection(), this);
68 public void removeUnit(Unit unit) {
69 unitsToStart.remove(unit);
72 public void landOn(Unit unit) {
73 // TODO copied from GoalSquare, how do we implement a starting Unit?
74 // Set direction...
75 unit.setDirection(startDirection, this);
78 /**
79 * Sets this StartSquare as active.
81 @Override
82 public void click() {
83 if (!active) {
84 setChanged();
85 notifyObservers();
86 clearChanged();
87 this.active = true;
91 @Override
92 public void paint(Graphics g) {
93 super.paint(g);
94 //g.drawString("" + unitsToStart.size(), getX(), getY());
96 if (active) {
97 g.setColor(Color.GREEN);
98 g.drawRect(getX(), getY(),
99 AntiTD.SQUARE_SIZE, AntiTD.SQUARE_SIZE);
100 g.drawRect(getX() + 1, getY() + 1,
101 AntiTD.SQUARE_SIZE - 2, AntiTD.SQUARE_SIZE - 2);
106 * Called when another StartSquare is activated, which means that this
107 * StartSquare should not be active.
109 * @param observer an <code>Observable</code> value
110 * @param obj an <code>Object</code> value
112 public void update(Observable observer, Object obj) {
113 logger.info("Update in StartSquare: active is set to false");
114 this.active = false;