1 package se
.umu
.cs
.dit06ajnajs
.agent
;
3 import java
.awt
.Graphics
;
6 import java
.util
.HashMap
;
8 import java
.util
.logging
.Logger
;
10 import se
.umu
.cs
.dit06ajnajs
.Paintable
;
11 import se
.umu
.cs
.dit06ajnajs
.map
.Level
;
12 import se
.umu
.cs
.dit06ajnajs
.map
.MapSquare
;
13 import se
.umu
.cs
.dit06ajnajs
.map
.Traversable
;
14 import java
.awt
.Rectangle
;
15 import se
.umu
.cs
.dit06ajnajs
.map
.TurnSquare
;
17 // TODO: write a testclass to test all get- set-positions with center top left..
18 public abstract class Unit
implements Agent
, Paintable
, Cloneable
{
19 private static Logger logger
= Logger
.getLogger("AntiTD");
34 private Map
<Direction
, Image
> images
;
35 private Direction direction
;
37 private boolean alive
;
38 private boolean pause
;
39 private Object lastTurnedBy
;
41 public Unit(String type
, int width
, int height
, int speed
, int health
,
42 int cost
, Map
<Direction
, Image
> images
) {
57 public void setImage(Image img
) {
58 // TODO Auto-generated method stub
61 public String
getType() {
65 // TODO används denna?
66 public Map
<Direction
, Image
> getImages() {
71 // Check if this unit has health left
72 if (this.health
> 0) {
73 // Check if this unit is active
75 Point nextPos
= getNextPosition();
76 // TODO check for collision on next position
79 // Land on current square
80 MapSquare currentSquare
= level
.getMapSquareAtPoint(centerX
, centerY
);
81 if (currentSquare
instanceof Traversable
) {
82 ((Traversable
) currentSquare
).landOn(this);
84 // Unit hitting something not Traversable and therefor dies.
88 // Toggle pause back to false;
99 public Point
getNextPosition() {
103 return new Point(x
, y
- speed
);
106 return new Point(x
, y
+ speed
);
109 return new Point(x
- speed
, y
);
111 logger
.fine("RIGHT");
112 return new Point(x
+ speed
, y
);
114 System
.err
.println("Unit has not got a valid Direction");
119 public void move(Point p
) {
128 public void setX(int x
) {
130 recalculateCenterX();
137 public void setY(int y
) {
139 recalculateCenterY();
142 public void setWidth(int width
) {
144 recalculateCenterX();
147 public void setHeight(int height
) {
148 this.height
= height
;
149 recalculateCenterY();
152 public void setCenterX(int centerX
) {
153 this.centerX
= centerX
;
154 setX(centerX
- (width
/ 2));
157 public void setCenterY(int centerY
) {
158 this.centerY
= centerY
;
159 setY(centerY
- (width
/ 2));
162 public void setCenterPoint(int centerX
, int centerY
) {
163 setCenterPoint(new Point(centerX
, centerY
));
166 public void setCenterPoint(Point point
) {
171 public Point
getCenterPoint() {
172 return new Point(this.centerX
, this.centerY
);
175 public int getWidth() {
179 public int getHeight() {
183 public int getSpeed() {
187 public void setSpeed(int speed
) {
191 public int getCost() {
195 public Direction
getDirection() {
199 public void setDirection(Direction direction
, Object caller
) {
200 if (this.lastTurnedBy
!= caller
) {
201 this.direction
= direction
;
202 this.lastTurnedBy
= caller
;
204 System
.err
.println("Turned by same caller twice: " + caller
);
208 public void setMap(Level level
) {
212 public boolean isAlive() {
216 public void setAlive(boolean state
) {
220 public int getHealth() {
225 /** Used to calculate and set a new x-center after x is changed. */
226 private void recalculateCenterX() {
227 this.centerX
= this.x
+ this.width
/ 2;
230 /** Used to calculate and set a new y-center after y is changed. */
231 private void recalculateCenterY() {
232 this.centerY
= this.y
+ this.height
/ 2;
236 * Decrease the units health
237 * @param damage The number of health points to substract
239 public void damage(int damage
) {
240 this.health
-= damage
;
243 public void setHealth(int health
) {
244 this.health
= health
;
248 * Attemts to clone this Unit.
250 * @return A new instance of the same type as the instantiated Unit.
253 public Object
clone() {
255 return super.clone();
256 } catch (CloneNotSupportedException e
) {
258 throw new Error("Object " + this.getClass().getName()
259 + " is not Cloneable");
263 public void collision(Unit unit
) {
266 if (unit
.getDirection() == Direction
.DOWN
) {
267 setDirection(Direction
.DOWN
, unit
);
272 if (unit
.getDirection() == Direction
.UP
) {
273 setDirection(Direction
.UP
, unit
);
278 if (unit
.getDirection() == Direction
.RIGHT
) {
279 setDirection(Direction
.RIGHT
, unit
);
284 if (unit
.getDirection() == Direction
.LEFT
) {
285 setDirection(Direction
.LEFT
, unit
);
290 System
.err
.println("Unit hasn't got a valid Direction");
296 public boolean isPaused() {
301 public String
toString() {
305 public Rectangle
getBounds() {
306 return new Rectangle(x
, y
, width
, height
);
309 public Rectangle
getFutureBounds() {
310 Point nextPoint
= getNextPosition();
311 return new Rectangle(nextPoint
.x
, nextPoint
.y
, width
, height
);
314 public boolean intersects(Unit unit
) {
315 return this.getBounds().intersects(unit
.getBounds());
318 public boolean intersectsNextMove(Unit unit
) {
319 return this.getFutureBounds().intersects(unit
.getBounds());
322 public void paint(Graphics g
) {
323 Image image
= images
.get(direction
);
324 g
.drawImage(image
, x
, y
, null);