First try in collision detection
[AntiTD.git] / src / se / umu / cs / dit06ajnajs / agent / Unit.java
blobf4941de7e0f730cea478d2ea2da37fd88278a6e4
1 package se.umu.cs.dit06ajnajs.agent;
3 import java.awt.Image;
4 import java.awt.Point;
5 import java.util.logging.Logger;
7 import se.umu.cs.dit06ajnajs.Paintable;
8 import se.umu.cs.dit06ajnajs.map.Map;
9 import se.umu.cs.dit06ajnajs.map.MapSquare;
10 import se.umu.cs.dit06ajnajs.map.Traversable;
11 // TODO: write a testclass to test all get- set-positions with center top left..
12 public abstract class Unit implements Agent, Paintable, Cloneable {
13 private static Logger logger = Logger.getLogger("AntiTD");
15 private int x;
16 private int y;
17 private int centerX;
18 private int centerY;
20 private int width;
21 private int height;
23 private int speed;
24 private int health;
25 private int cost;
27 private Image image;
28 private Direction direction;
29 private Map map;
30 private boolean alive;
31 private boolean pause;
33 public Unit(int x, int y, int width, int height, int speed, int health,
34 int cost, Image image, Direction direction, Map map) {
35 this.width = width;
36 this.height = height;
37 setX(x);
38 setY(y);
39 this.speed = speed;
40 this.health = health;
41 this.cost = cost;
42 this.image = image;
43 this.direction = direction;
44 this.map = map;
46 this.alive = true;
47 this.pause = false;
50 public void setImage(Image img) {
51 // TODO Auto-generated method stub
54 public Image getImage() {
55 return this.image;
58 public void act() {
60 // Check if this unit has health left
61 if (this.health >= 0) {
62 // Check if this unit is active
63 if (!pause) {
64 Point nextPos = getNextPosition();
65 // TODO check for collision on next position
66 move(nextPos);
68 // Land on current square
69 MapSquare currentSquare= map.getMapSquareAtPoint(centerX, centerY);
70 if (currentSquare instanceof Traversable) {
71 ((Traversable) currentSquare).landOn(this);
74 else {
75 // Toggle pause back to false;
76 pause = false;
78 } else {
79 // Kill unit
80 this.alive = false;
86 public Point getNextPosition() {
87 switch (direction) {
88 case UP:
89 logger.fine("UP");
90 return new Point(x, y - speed);
91 case DOWN:
92 logger.fine("DOWN");
93 return new Point(x, y + speed);
94 case LEFT:
95 logger.fine("LEFT");
96 return new Point(x - speed, y);
97 case RIGHT:
98 logger.fine("RIGHT");
99 return new Point(x + speed, y);
100 default:
101 System.err.println("Unit has not got a valid Direction");
102 return null;
106 public void move(Point p) {
107 setX(p.x);
108 setY(p.y);
111 public int getX() {
112 return x;
115 public void setX(int x) {
116 this.x = x;
117 recalculateCenterX();
120 public int getY() {
121 return y;
124 public void setY(int y) {
125 this.y = y;
126 recalculateCenterY();
129 public void setWidth(int width) {
130 this.width = width;
131 recalculateCenterX();
134 public void setHeight(int height) {
135 this.height = height;
136 recalculateCenterY();
139 public void setCenterX(int centerX) {
140 this.centerX = centerX;
141 setX(centerX - (width / 2));
144 public void setCenterY(int centerY) {
145 this.centerY = centerY;
146 setY(centerY - (width / 2));
149 public void setCenterPoint(int centerX, int centerY) {
150 setCenterPoint(new Point(centerX, centerY));
153 public void setCenterPoint(Point point) {
154 setCenterX(point.x);
155 setCenterY(point.y);
158 public Point getCenterPoint() {
159 return new Point(this.centerX, this.centerY);
162 public int getWidth() {
163 return width;
166 public int getHeight() {
167 return height;
170 public int getSpeed() {
171 return speed;
174 public void setSpeed(int speed) {
175 this.speed = speed;
178 public void setCost(int cost) {
179 this.cost = cost;
182 public Direction getDirection() {
183 return direction;
186 public void setDirection(Direction direction) {
187 this.direction = direction;
190 public void setMap(Map map) {
191 this.map = map;
194 public boolean isAlive() {
195 return this.alive;
198 public void setAlive(boolean state) {
199 this.alive = state;
202 public int getHealth() {
203 return health;
207 /** Used to calculate and set a new x-center after x is changed. */
208 private void recalculateCenterX() {
209 this.centerX = this.x + this.width / 2;
212 /** Used to calculate and set a new y-center after y is changed. */
213 private void recalculateCenterY() {
214 this.centerY = this.y + this.height / 2;
218 * Decrease the units health
219 * @param damage The number of health points to substract
221 public void damage(int damage) {
222 this.health -= damage;
225 public void setHealth(int health) {
226 this.health = health;
230 * Attemts to clone this Unit.
232 * @return A new instance of the same type as the instantiated Unit.
234 @Override
235 public Object clone() {
236 try {
237 return super.clone();
238 } catch (CloneNotSupportedException e) {
239 e.printStackTrace();
240 throw new Error("Object " + this.getClass().getName()
241 + " is not Cloneable");
245 public void setPause(boolean state) {
246 this.pause = state;