Changed package name map to level
[AntiTD.git] / src / se / umu / cs / dit06ajnajs / level / TurnSquare.java
blob1dca5fed97691060d588bb1e1a7164c93661ae4c
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 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) {
21 super(x, y, image);
24 /**
25 * Setup Directions, this field is unique for every TurnSquare.
27 @Override
28 public void init() {
29 this.directions = new ArrayList<Direction>(4);
32 /**
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);
46 /**
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);
57 @Override
58 public void click() {
59 toggleDirections();
62 @Override
63 public void landOn(Unit unit) {
64 super.landOn(unit);
65 //TODO continue
66 int unitCenterX = (int) unit.getCenterPoint().getX();
67 int unitCenterY = (int) unit.getCenterPoint().getY();
69 switch(unit.getDirection()) {
70 case UP:
71 if (unitCenterY <= this.getCenterY()) {
72 turnUnit(unit);
74 break;
75 case DOWN:
76 if (unitCenterY >= this.getCenterY()) {
77 turnUnit(unit);
79 break;
80 case LEFT:
81 if (unitCenterX <= this.getCenterX()) {
82 turnUnit(unit);
84 break;
85 case RIGHT:
86 if (unitCenterX >= this.getCenterX()) {
87 turnUnit(unit);
89 break;
90 default:
91 // TODO: Error?
92 throw new Error("Unit hasn't got any Direction");
97 @Override
98 public void paint(Graphics g) {
99 super.paint(g);
100 Graphics2D g2 = (Graphics2D) g;
102 switch (currentDirection) {
103 case UP:
104 g2.rotate(-Math.PI / 2, getCenterX(), getCenterY());
105 break;
106 case DOWN:
107 g2.rotate(Math.PI / 2, getCenterX() , getCenterY());
108 break;
109 case LEFT:
110 g2.rotate(Math.PI, getCenterX(), getCenterY());
111 break;
112 case RIGHT:
113 // Images should always point right
114 break;
115 case NONE:
116 // If this is called from LevelEditor
117 break;
118 default:
119 // TODO: Error
120 throw new Error("No direction in TurnSquare");
122 g2.drawImage(getImage(), getX(), getY(), null);
123 // Reset Transform
124 g2.setTransform(new AffineTransform());
127 private void turnUnit(Unit unit) {
128 unit.setDirection(currentDirection, this);