MapSquarePrototypeFactory is replaced by MapSquareFactory, since extending Observable...
[AntiTD.git] / src / se / umu / cs / dit06ajnajs / agent / Tower.java
blob3bd76e5ffde9212a4263e7e38f6449e32dccfca6
1 package se.umu.cs.dit06ajnajs.agent;
3 import java.awt.Image;
4 import java.awt.Point;
5 import java.util.ArrayList;
6 import java.util.List;
7 import java.util.Observable;
8 import java.util.Observer;
9 import java.util.logging.Logger;
11 import se.umu.cs.dit06ajnajs.Paintable;
12 import java.awt.Color;
13 import java.awt.Graphics;
15 public abstract class Tower implements Paintable, Agent, Observer, Cloneable {
17 private static Logger logger = Logger.getLogger("AntiTD");
19 private int x;
20 private int y;
21 private int centerX;
22 private int centerY;
24 private int width;
25 private int height;
27 private int damage;
28 private int range;
29 private Image image;
30 private boolean alive;
31 private boolean fiering;
32 private List<Unit> fireingList;
35 public Tower(int width, int height, int damage, int range, Image image) {
36 this.width = width;
37 this.height = height;
38 this.damage = damage;
39 this.range = range;
40 this.image = image;
42 this.alive = true;
43 this.fiering = false;
46 /**
47 * Used to initialize fields that should not be individually set for every
48 * Tower, since clone() is shallow.
50 public void init() {
51 this.fireingList = new ArrayList<Unit>();
54 public void setImage(Image img) {
55 // TODO Implement method
58 public void setPostition(Point p) {
59 this.x = p.x;
60 this.y = p.y;
61 this.centerX = this.x + (this.width/2);
62 this.centerY = this.y + (this.height/2);
65 public int getRange() {
66 return this.range;
69 public Point getCenterPoint() {
70 return new Point(this.centerX, this.centerY);
73 public void act() {
74 if (!fireingList.isEmpty()) {
75 Unit unit = fireingList.get(0);
76 if (unit.isAlive()) {
77 Point unitPoint = unit.getCenterPoint();
79 int unitXPos = unitPoint.x;
80 int unitYPos = unitPoint.y;
81 int distans = (int) Math.hypot((this.centerX - unitXPos), this.centerY - unitYPos);
82 System.out.println("Tower range: " + this.range);
83 System.out.println("Distance to unit: " + distans);
84 if (distans < this.range) {
85 logger.info("UNIT IN RANGE, FIIIIIREEEEEEE!!!");
86 this.fiering = true;
87 unit.damage(damage);
88 } else {
89 logger.info("Unit out of reach, removing from fireingList");
90 fireingList.remove(unit);
92 } else {
93 logger.info("Unit is dead, removing from fireingList");
94 fireingList.remove(unit);
99 public void update(Observable caller, Object arg) {
100 // TODO Auto-generated method stub
101 // Should always be units
102 if (arg instanceof Unit) {
103 Unit unit = (Unit) arg;
104 if (!fireingList.contains(unit)) {
105 fireingList.add(unit);
106 logger.info("Unit >" + unit.getClass().getSimpleName() +
107 "< added to tower...");
112 public boolean isAlive() {
113 return this.alive;
116 public void setAlive(boolean state) {
117 this.alive = state;
120 public int getHeight() {
121 return height;
124 public void setHeight(int height) {
125 this.height = height;
128 public int getWidth() {
129 return width;
132 public void setWidth(int width) {
133 this.width = width;
136 public int getX() {
137 return this.x;
140 public int getY() {
141 return this.y;
144 public int getCenterX() {
145 return centerX;
148 public int getCenterY() {
149 return centerY;
152 public boolean isFiering() {
153 return this.fiering;
156 public void setFiering(boolean fiering) {
157 this.fiering = fiering;
160 public List<Unit> getFireingList() {
161 return this.fireingList;
164 public void paint(Graphics g) {
165 // TODO cast to graphics2D?
166 // g.setColor(Color.RED);
167 // g.fillRect(this.getX(), this.getY(), this.getWidth(), this.getHeight());
168 g.drawImage(image, x, y, null);
170 // TODO Tillfällig implementering
171 if (this.isFiering()) {
172 this.setFiering(false);
173 g.setColor(Color.WHITE);
174 List<Unit> fireingList = this.getFireingList();
175 if (!fireingList.isEmpty()) {
176 Unit unit = this.getFireingList().get(0);
177 g.drawLine(this.getCenterX(), this.getCenterY(),
178 unit.getCenterPoint().x, unit.getCenterPoint().y);
184 * Empty method to handle mouseClicks, override in subclass to implement
185 * behaviour.
188 public void click() {
189 // Overried in subclass
193 * Attemts to clone this Tower.
195 * @return A new instance of the same type as the instantiated Tower.
197 @Override
198 public Object clone() {
199 try {
200 return super.clone();
201 } catch (CloneNotSupportedException e) {
202 e.printStackTrace();
203 throw new Error("Object " + this.getClass().getName()
204 + " is not Cloneable");