1 package se
.umu
.cs
.dit06ajnajs
.level
;
5 import se
.umu
.cs
.dit06ajnajs
.agent
.Direction
;
6 import se
.umu
.cs
.dit06ajnajs
.agent
.Unit
;
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
;
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)
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
;
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
) {
41 * Setup Directions, this field is unique for every TurnSquare.
45 this.directions
= new ArrayList
<Direction
>(4);
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
);
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
);
74 * On call, the direction is toggled
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.
88 public void landOn(Unit unit
) {
91 int unitCenterX
= (int) unit
.getCenterPoint().getX();
92 int unitCenterY
= (int) unit
.getCenterPoint().getY();
94 switch(unit
.getDirection()) {
96 if (unitCenterY
<= this.getCenterY()) {
101 if (unitCenterY
>= this.getCenterY()) {
106 if (unitCenterX
<= this.getCenterX()) {
111 if (unitCenterX
>= this.getCenterX()) {
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.
129 public void paint(Graphics g
) {
131 Graphics2D g2
= (Graphics2D
) g
;
133 switch (currentDirection
) {
135 g2
.rotate(-Math
.PI
/ 2, getCenterX(), getCenterY());
138 g2
.rotate(Math
.PI
/ 2, getCenterX() , getCenterY());
141 g2
.rotate(Math
.PI
, getCenterX(), getCenterY());
144 // Images should always point right
147 // If this is called from LevelEditor
151 throw new Error("No direction in TurnSquare");
153 g2
.drawImage(getImage(), getX(), getY(), null);
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);