1 package se
.umu
.cs
.dit06ajnajs
.agent
;
5 import java
.util
.ArrayList
;
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");
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
) {
47 * Used to initialize fields that should not be individually set for every
48 * Tower, since clone() is shallow.
51 this.fireingList
= new ArrayList
<Unit
>();
54 public void setImage(Image img
) {
55 // TODO Implement method
58 public void setPostition(Point p
) {
61 this.centerX
= this.x
+ (this.width
/2);
62 this.centerY
= this.y
+ (this.height
/2);
65 public int getRange() {
69 public Point
getCenterPoint() {
70 return new Point(this.centerX
, this.centerY
);
74 if (!fireingList
.isEmpty()) {
75 Unit unit
= fireingList
.get(0);
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!!!");
89 logger
.info("Unit out of reach, removing from fireingList");
90 fireingList
.remove(unit
);
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() {
116 public void setAlive(boolean state
) {
120 public int getHeight() {
124 public void setHeight(int height
) {
125 this.height
= height
;
128 public int getWidth() {
132 public void setWidth(int width
) {
144 public int getCenterX() {
148 public int getCenterY() {
152 public boolean isFiering() {
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
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.
198 public Object
clone() {
200 return super.clone();
201 } catch (CloneNotSupportedException e
) {
203 throw new Error("Object " + this.getClass().getName()
204 + " is not Cloneable");