HashMap with images in prototypefactory
[AntiTD.git] / src / se / umu / cs / dit06ajnajs / agent / Unit.java
blobdd2f57f084cf667924be5f8b264908847ae055ec
1 package se.umu.cs.dit06ajnajs.agent;
3 import java.awt.Graphics;
4 import java.awt.Image;
5 import java.awt.Point;
6 import java.util.HashMap;
7 import java.util.Map;
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");
21 private int x;
22 private int y;
23 private int centerX;
24 private int centerY;
26 private int width;
27 private int height;
29 private String type;
30 private int speed;
31 private int health;
32 private int cost;
34 private Map<Direction, Image> images;
35 private Direction direction;
36 private Level level;
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) {
43 this.type = type;
44 this.width = width;
45 this.height = height;
46 setX(x);
47 setY(y);
48 this.speed = speed;
49 this.health = health;
50 this.cost = cost;
51 this.images = images;
53 this.alive = true;
54 this.pause = false;
57 public void setImage(Image img) {
58 // TODO Auto-generated method stub
61 public String getType() {
62 return this.type;
65 // TODO används denna?
66 public Map<Direction, Image> getImages() {
67 return this.images;
70 public void act() {
71 // Check if this unit has health left
72 if (this.health > 0) {
73 // Check if this unit is active
74 if (!pause) {
75 Point nextPos = getNextPosition();
76 // TODO check for collision on next position
77 move(nextPos);
79 // Land on current square
80 MapSquare currentSquare = level.getMapSquareAtPoint(centerX, centerY);
81 if (currentSquare instanceof Traversable) {
82 ((Traversable) currentSquare).landOn(this);
83 } else {
84 // Unit hitting something not Traversable and therefor dies.
85 this.alive = false;
87 } else {
88 // Toggle pause back to false;
89 pause = false;
91 } else {
92 // Kill unit
93 this.alive = false;
99 public Point getNextPosition() {
100 switch (direction) {
101 case UP:
102 logger.fine("UP");
103 return new Point(x, y - speed);
104 case DOWN:
105 logger.fine("DOWN");
106 return new Point(x, y + speed);
107 case LEFT:
108 logger.fine("LEFT");
109 return new Point(x - speed, y);
110 case RIGHT:
111 logger.fine("RIGHT");
112 return new Point(x + speed, y);
113 default:
114 System.err.println("Unit has not got a valid Direction");
115 return null;
119 public void move(Point p) {
120 setX(p.x);
121 setY(p.y);
124 public int getX() {
125 return x;
128 public void setX(int x) {
129 this.x = x;
130 recalculateCenterX();
133 public int getY() {
134 return y;
137 public void setY(int y) {
138 this.y = y;
139 recalculateCenterY();
142 public void setWidth(int width) {
143 this.width = 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) {
167 setCenterX(point.x);
168 setCenterY(point.y);
171 public Point getCenterPoint() {
172 return new Point(this.centerX, this.centerY);
175 public int getWidth() {
176 return width;
179 public int getHeight() {
180 return height;
183 public int getSpeed() {
184 return speed;
187 public void setSpeed(int speed) {
188 this.speed = speed;
191 public int getCost() {
192 return this.cost;
195 public Direction getDirection() {
196 return direction;
199 public void setDirection(Direction direction, Object caller) {
200 if (this.lastTurnedBy != caller) {
201 this.direction = direction;
202 this.lastTurnedBy = caller;
203 } else {
204 System.err.println("Turned by same caller twice: " + caller);
208 public void setMap(Level level) {
209 this.level = level;
212 public boolean isAlive() {
213 return this.alive;
216 public void setAlive(boolean state) {
217 this.alive = state;
220 public int getHealth() {
221 return health;
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.
252 @Override
253 public Object clone() {
254 try {
255 return super.clone();
256 } catch (CloneNotSupportedException e) {
257 e.printStackTrace();
258 throw new Error("Object " + this.getClass().getName()
259 + " is not Cloneable");
263 public void collision(Unit unit) {
264 switch (direction) {
265 case UP:
266 if (unit.getDirection() == Direction.DOWN) {
267 setDirection(Direction.DOWN, unit);
268 return;
270 break;
271 case DOWN:
272 if (unit.getDirection() == Direction.UP) {
273 setDirection(Direction.UP, unit);
274 return;
276 break;
277 case LEFT:
278 if (unit.getDirection() == Direction.RIGHT) {
279 setDirection(Direction.RIGHT, unit);
280 return;
282 break;
283 case RIGHT:
284 if (unit.getDirection() == Direction.LEFT) {
285 setDirection(Direction.LEFT, unit);
286 return;
288 break;
289 default:
290 System.err.println("Unit hasn't got a valid Direction");
291 break;
293 this.pause = true;
296 public boolean isPaused() {
297 return this.pause;
300 @Override
301 public String toString() {
302 return getType();
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);