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
;
14 public class TurnSquare
extends PathSquare
{
15 private static Logger logger
= Logger
.getLogger("AntiTD");
17 private List
<Direction
> directions
;
18 private Direction currentDirection
= Direction
.NONE
;
20 public TurnSquare(int x
, int y
, Image image
) {
25 * Setup Directions, this field is unique for every TurnSquare.
29 this.directions
= new ArrayList
<Direction
>(4);
33 * Add a possible Direction to this TurnSquare. The currentDirection will be
34 * changed to this new Direction.
36 * @param direction The Direction to add.
38 public void addDirection(Direction direction
) {
39 logger
.info("Added direction: " + direction
+ " to: " + this);
40 this.currentDirection
= direction
;
41 if (!directions
.contains(direction
)) {
42 directions
.add(direction
);
47 * Toggles current direction. Sets current direction to next direction among
48 * possible directions in this TurnSquare.
50 private void toggleDirections() {
51 int index
= directions
.indexOf(currentDirection
);
52 int nextIndex
= (index
+ 1) % directions
.size();
53 // TODO, verify. Should get next direction from list.
54 this.currentDirection
= directions
.get(nextIndex
);
63 public void landOn(Unit unit
) {
66 int unitCenterX
= (int) unit
.getCenterPoint().getX();
67 int unitCenterY
= (int) unit
.getCenterPoint().getY();
69 switch(unit
.getDirection()) {
71 if (unitCenterY
<= this.getCenterY()) {
76 if (unitCenterY
>= this.getCenterY()) {
81 if (unitCenterX
<= this.getCenterX()) {
86 if (unitCenterX
>= this.getCenterX()) {
92 throw new Error("Unit hasn't got any Direction");
98 public void paint(Graphics g
) {
100 Graphics2D g2
= (Graphics2D
) g
;
102 switch (currentDirection
) {
104 g2
.rotate(-Math
.PI
/ 2, getCenterX(), getCenterY());
107 g2
.rotate(Math
.PI
/ 2, getCenterX() , getCenterY());
110 g2
.rotate(Math
.PI
, getCenterX(), getCenterY());
113 // Images should always point right
116 // If this is called from LevelEditor
120 throw new Error("No direction in TurnSquare");
122 g2
.drawImage(getImage(), getX(), getY(), null);
124 g2
.setTransform(new AffineTransform());
127 private void turnUnit(Unit unit
) {
128 unit
.setDirection(currentDirection
, this);