1 package se
.umu
.cs
.dit06ajnajs
.agent
;
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");
30 private Direction direction
;
32 private boolean alive
;
33 private boolean pause
;
35 public Unit(int width
, int height
, int speed
, int health
,
36 int cost
, Image image
) {
50 public void setImage(Image img
) {
51 // TODO Auto-generated method stub
54 public Image
getImage() {
60 // Check if this unit has health left
61 if (this.health
>= 0) {
62 // Check if this unit is active
64 Point nextPos
= getNextPosition();
65 // TODO check for collision on next position
68 // Land on current square
69 MapSquare currentSquare
= map
.getMapSquareAtPoint(centerX
, centerY
);
70 if (currentSquare
instanceof Traversable
) {
71 ((Traversable
) currentSquare
).landOn(this);
75 // Toggle pause back to false;
86 public Point
getNextPosition() {
90 return new Point(x
, y
- speed
);
93 return new Point(x
, y
+ speed
);
96 return new Point(x
- speed
, y
);
99 return new Point(x
+ speed
, y
);
101 System
.err
.println("Unit has not got a valid Direction");
106 public void move(Point p
) {
115 public void setX(int x
) {
117 recalculateCenterX();
124 public void setY(int y
) {
126 recalculateCenterY();
129 public void setWidth(int 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
) {
158 public Point
getCenterPoint() {
159 return new Point(this.centerX
, this.centerY
);
162 public int getWidth() {
166 public int getHeight() {
170 public int getSpeed() {
174 public void setSpeed(int speed
) {
178 public void setCost(int cost
) {
182 public Direction
getDirection() {
186 public void setDirection(Direction direction
) {
187 this.direction
= direction
;
190 public void setMap(Map map
) {
194 public boolean isAlive() {
198 public void setAlive(boolean state
) {
202 public int getHealth() {
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.
235 public Object
clone() {
237 return super.clone();
238 } catch (CloneNotSupportedException e
) {
240 throw new Error("Object " + this.getClass().getName()
241 + " is not Cloneable");
245 public void setPause(boolean state
) {
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());