Added textures and agent boolean alive
[AntiTD.git] / src / se / umu / cs / dit06ajnajs / agent / Unit.java
blob219ce5c46fca0a0c2fa420d1d81aa2046df03dad
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;
12 public abstract class Unit implements Agent, Paintable{
13 private static Logger logger = Logger.getLogger("AntiTD");
15 private int xPos;
16 private int yPos;
18 private int width;
19 private int height;
21 private int speed;
22 private int health;
24 private Direction direction;
25 private Map map;
26 private boolean alive;
28 public Unit(int xPos, int yPos, int width, int height, int speed, Direction direction, Map map) {
29 this.xPos = xPos;
30 this.yPos = yPos;
31 this.width = width;
32 this.height = height;
33 this.speed = speed;
34 this.direction = direction;
35 this.map = map;
37 this.alive = true;
40 public void setImage(Image img) {
41 // TODO Auto-generated method stub
45 public void act() {
46 // Check if this unit has health left
47 if (this.health >= 0) {
48 Point nextPos = getNextPosition();
49 // TODO check for collision on next position
50 move(nextPos);
52 // Land on current square
53 MapSquare currentSquare= map.getMapSquareAtPoint(xPos, yPos);
54 if (currentSquare instanceof Traversable) {
55 ((Traversable) currentSquare).landOn(this);
57 } else {
58 // Kill unit
59 this.alive = false;
64 public Point getNextPosition() {
65 Point nextPos = null;
66 switch (direction) {
67 case UP:
68 logger.fine("UP");
69 nextPos = new Point(xPos, yPos - speed);
70 break;
71 case DOWN:
72 logger.fine("DOWN");
73 nextPos = new Point(xPos, yPos + speed);
74 break;
75 case LEFT:
76 logger.fine("LEFT");
77 nextPos = new Point(xPos - speed, yPos);
78 break;
79 case RIGHT:
80 logger.fine("RIGHT");
81 nextPos = new Point(xPos + speed, yPos);
82 break;
84 return nextPos;
87 public void move(Point p) {
88 this.xPos = p.x;
89 this.yPos = p.y;
92 public int getXPos() {
93 return xPos;
96 public void setXPos(int pos) {
97 xPos = pos;
100 public int getYPos() {
101 return yPos;
104 public void setYPos(int pos) {
105 yPos = pos;
108 public Point getCenterPoint() {
109 return new Point(xPos + (width / 2),
110 yPos - (height / 2));
113 public int getWidth() {
114 return width;
117 public void setWidth(int width) {
118 this.width = width;
121 public int getHeight() {
122 return height;
125 public void setHeight(int height) {
126 this.height = height;
129 public int getSpeed() {
130 return speed;
133 public void setSpeed(int speed) {
134 this.speed = speed;
137 public Direction getDirection() {
138 return direction;
141 public void setDirection(Direction direction) {
142 this.direction = direction;
145 public boolean isAlive() {
146 return this.alive;
149 public void setAlive(boolean state) {
150 this.alive = state;
153 public int getHealth() {
154 return health;
158 * Decrease the units health
159 * @param damage The number of health points to substract
161 public void damage(int damage) {
162 this.health -= damage;
165 public void setHealth(int health) {
166 this.health = health;