Intersects with Rectangle
[AntiTD.git] / src / se / umu / cs / dit06ajnajs / agent / Unit.java
blob09316bb336cf0abe3021b29920df23d995ad7100
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 import java.awt.Rectangle;
13 // TODO: write a testclass to test all get- set-positions with center top left..
14 public abstract class Unit implements Agent, Paintable, Cloneable {
15 private static Logger logger = Logger.getLogger("AntiTD");
17 private int x;
18 private int y;
19 private int centerX;
20 private int centerY;
22 private int width;
23 private int height;
25 private int speed;
26 private int health;
27 private int cost;
29 private Image image;
30 private Direction direction;
31 private Map map;
32 private boolean alive;
33 private boolean pause;
35 public Unit(int width, int height, int speed, int health,
36 int cost, Image image) {
37 this.width = width;
38 this.height = height;
39 setX(x);
40 setY(y);
41 this.speed = speed;
42 this.health = health;
43 this.cost = cost;
44 this.image = image;
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;
249 @Override
250 public String toString() {
251 return this.getClass().getSimpleName();
254 public Rectangle getBounds() {
255 return new Rectangle(x, y, width, height);
258 private Rectangle getFutureBounds() {
259 Point nextPoint = getNextPosition();
260 return new Rectangle(nextPoint.x, nextPoint.y, width, height);
263 public boolean intersects(Unit unit) {
264 return this.getBounds().intersects(unit.getBounds());
267 public boolean intersectsNextMove(Unit unit) {
268 return this.getFutureBounds().intersects(unit.getBounds());