Javadoc, small bugfix in levels.xml
[AntiTD.git] / src / se / umu / cs / dit06ajnajs / level / TurnSquare.java
blob8fabc2e12d7257e78fe259e492ffc22172ae4385
1 package se.umu.cs.dit06ajnajs.level;
3 import java.awt.Image;
5 import se.umu.cs.dit06ajnajs.agent.Direction;
6 import se.umu.cs.dit06ajnajs.agent.Unit;
7 import java.util.List;
8 import java.util.ArrayList;
9 import java.awt.Graphics;
10 import java.util.logging.Logger;
11 import java.awt.Graphics2D;
12 import java.awt.geom.AffineTransform;
14 /**
15 * Class TurnSquare extends PathSquare and represents a square with a turn.
16 * Contains a List with legal directions and a variable containing the current
17 * direction that the unit is given when it is turned
19 * @author Anton Johansson (dit06ajn@cs.umu.se)
20 * @version 1.0
22 public class TurnSquare extends PathSquare {
23 private static Logger logger = Logger.getLogger("AntiTD");
25 private List<Direction> directions;
26 private Direction currentDirection = Direction.NONE;
28 /**
29 * Creates a new TurnSquare, calls extended MapSquares contructor with
30 * supplied parameters.
32 * @param x The x-coordinate of this Square.
33 * @param y The y-coordinate of this Square.
34 * @param image The Image used to display this Square.
36 public TurnSquare(int x, int y, Image image) {
37 super(x, y, image);
40 /**
41 * Setup Directions, this field is unique for every TurnSquare.
43 @Override
44 public void init() {
45 this.directions = new ArrayList<Direction>(4);
48 /**
49 * Add a possible Direction to this TurnSquare. The currentDirection will be
50 * changed to this new Direction.
52 * @param direction The Direction to add.
54 public void addDirection(Direction direction) {
55 logger.info("Added direction: " + direction + " to: " + this);
56 this.currentDirection = direction;
57 if (!directions.contains(direction)) {
58 directions.add(direction);
62 /**
63 * Toggles current direction. Sets current direction to next direction among
64 * possible directions in this TurnSquare.
66 private void toggleDirections() {
67 int index = directions.indexOf(currentDirection);
68 int nextIndex = (index + 1) % directions.size();
69 // TODO, verify. Should get next direction from list.
70 this.currentDirection = directions.get(nextIndex);
73 /**
74 * On call, the direction is toggled
76 @Override
77 public void click() {
78 toggleDirections();
81 /**
82 * Calls super.landon(unit) and changes landing Units position if its center
83 * point has passed this TurnSquares center point.
85 * @param unit The Unit who lands on this TurnSquare.
87 @Override
88 public void landOn(Unit unit) {
89 super.landOn(unit);
90 //TODO continue
91 int unitCenterX = (int) unit.getCenterPoint().getX();
92 int unitCenterY = (int) unit.getCenterPoint().getY();
94 switch(unit.getDirection()) {
95 case UP:
96 if (unitCenterY <= this.getCenterY()) {
97 turnUnit(unit);
99 break;
100 case DOWN:
101 if (unitCenterY >= this.getCenterY()) {
102 turnUnit(unit);
104 break;
105 case LEFT:
106 if (unitCenterX <= this.getCenterX()) {
107 turnUnit(unit);
109 break;
110 case RIGHT:
111 if (unitCenterX >= this.getCenterX()) {
112 turnUnit(unit);
114 break;
115 default:
116 // TODO: Error?
117 throw new Error("Unit hasn't got any Direction");
122 * Calls super.pain(p) and rotates the Image used to draw this TurnSquare to
123 * match the current Direction of this square. Painted Images should always
124 * point right when not rotated.
126 * @param g The Graphics object to paint to.
128 @Override
129 public void paint(Graphics g) {
130 super.paint(g);
131 Graphics2D g2 = (Graphics2D) g;
133 switch (currentDirection) {
134 case UP:
135 g2.rotate(-Math.PI / 2, getCenterX(), getCenterY());
136 break;
137 case DOWN:
138 g2.rotate(Math.PI / 2, getCenterX() , getCenterY());
139 break;
140 case LEFT:
141 g2.rotate(Math.PI, getCenterX(), getCenterY());
142 break;
143 case RIGHT:
144 // Images should always point right
145 break;
146 case NONE:
147 // If this is called from LevelEditor
148 break;
149 default:
150 // TODO: Error
151 throw new Error("No direction in TurnSquare");
153 g2.drawImage(getImage(), getX(), getY(), null);
154 // Reset Transform
155 g2.setTransform(new AffineTransform());
159 * Turns the supplied Unit.
161 * @param unit The Unit to turn.
163 private void turnUnit(Unit unit) {
164 unit.setDirection(currentDirection, this);