1 package se
.umu
.cs
.dit06ajnajs
.agent
;
3 import java
.awt
.Graphics
;
7 import java
.util
.logging
.Logger
;
9 import se
.umu
.cs
.dit06ajnajs
.Paintable
;
10 import se
.umu
.cs
.dit06ajnajs
.map
.Level
;
11 import se
.umu
.cs
.dit06ajnajs
.map
.MapSquare
;
12 import se
.umu
.cs
.dit06ajnajs
.map
.Traversable
;
13 import java
.awt
.Rectangle
;
14 import java
.awt
.Color
;
15 import java
.awt
.geom
.Area
;
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");
32 private int currentHealth
;
35 private Map
<Direction
, Image
> images
;
36 private Direction direction
;
38 private boolean alive
;
39 private boolean pause
;
40 private Object lastTurnedBy
;
42 public Unit(String type
, int width
, int height
, int speed
, int health
,
43 int cost
, Map
<Direction
, Image
> images
) {
51 this.currentHealth
= health
;
59 public void setImage(Image img
) {
60 // TODO Auto-generated method stub
63 public String
getType() {
67 // TODO används denna?
68 public Map
<Direction
, Image
> getImages() {
73 // Check if this unit has health left
74 if (this.currentHealth
> 0) {
75 // Check if this unit is active
77 Point nextPos
= getNextPosition();
78 // TODO check for collision on next position
81 // Land on current square
82 MapSquare currentSquare
= level
.getMapSquareAtPoint(centerX
, centerY
);
83 if (currentSquare
instanceof Traversable
) {
84 ((Traversable
) currentSquare
).landOn(this);
86 // Unit hitting something not Traversable and therefor dies.
90 // 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 /** Used to calculate and set a new x-center after x is changed. */
221 private void recalculateCenterX() {
222 this.centerX
= this.x
+ this.width
/ 2;
225 /** Used to calculate and set a new y-center after y is changed. */
226 private void recalculateCenterY() {
227 this.centerY
= this.y
+ this.height
/ 2;
231 * Decrease the units currentHealth.
232 * @param damage The number of health points to substract
234 public void damage(int damage
) {
235 this.currentHealth
-= damage
;
239 * Attemts to clone this Unit.
241 * @return A new instance of the same type as the instantiated Unit.
244 public Object
clone() {
246 return super.clone();
247 } catch (CloneNotSupportedException e
) {
249 throw new Error("Object " + this.getClass().getName()
250 + " is not Cloneable");
254 public void collision(Unit unit
) {
257 if (unit
.getDirection() == Direction
.DOWN
) {
258 setDirection(Direction
.DOWN
, unit
);
259 unit
.setDirection(Direction
.UP
, this);
264 if (unit
.getDirection() == Direction
.UP
) {
265 setDirection(Direction
.UP
, unit
);
266 unit
.setDirection(Direction
.DOWN
, this);
271 if (unit
.getDirection() == Direction
.RIGHT
) {
272 setDirection(Direction
.RIGHT
, unit
);
273 unit
.setDirection(Direction
.LEFT
, this);
278 if (unit
.getDirection() == Direction
.LEFT
) {
279 setDirection(Direction
.LEFT
, unit
);
280 unit
.setDirection(Direction
.RIGHT
, this);
285 System
.err
.println("Unit hasn't got a valid Direction");
291 public boolean isPaused() {
296 public String
toString() {
300 public Rectangle
getBounds() {
301 return new Rectangle(x
, y
, width
, height
);
304 public Rectangle
getFutureBounds() {
305 Point nextPoint
= getNextPosition();
306 return new Rectangle(nextPoint
.x
, nextPoint
.y
, width
, height
);
309 public boolean intersects(Unit unit
) {
310 return this.getBounds().intersects(unit
.getBounds());
313 public boolean intersectsNextMove(Unit unit
) {
314 return this.getFutureBounds().intersects(unit
.getBounds());
317 public void paint(Graphics g
) {
318 Image image
= images
.get(direction
);
319 g
.drawImage(image
, x
, y
, null);
321 int healthHeight
= 2;
322 int yHealth
= y
+ height
- healthHeight
;
323 g
.setColor(Color
.RED
);
324 g
.fillRect(x
, yHealth
, width
, healthHeight
);
325 g
.setColor(Color
.GREEN
);
326 int healthBar
= (int) (width
* (new Double(currentHealth
) / health
));
327 g
.fillRect(x
, yHealth
, healthBar
, healthHeight
);