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 fireingList
.remove(unit
);
98 public void update(Observable caller
, Object arg
) {
99 // TODO Auto-generated method stub
100 // Should always be units
101 if (arg
instanceof Unit
) {
102 Unit unit
= (Unit
) arg
;
103 if (!fireingList
.contains(unit
)) {
104 fireingList
.add(unit
);
105 logger
.info("Unit >" + unit
.getClass().getSimpleName() +
106 "< added to tower...");
111 public boolean isAlive() {
115 public void setAlive(boolean state
) {
119 public int getHeight() {
123 public void setHeight(int height
) {
124 this.height
= height
;
127 public int getWidth() {
131 public void setWidth(int width
) {
143 public int getCenterX() {
147 public int getCenterY() {
151 public boolean isFiering() {
155 public void setFiering(boolean fiering
) {
156 this.fiering
= fiering
;
159 public List
<Unit
> getFireingList() {
160 return this.fireingList
;
163 public void paint(Graphics g
) {
164 // TODO cast to graphics2D?
165 // g.setColor(Color.RED);
166 // g.fillRect(this.getX(), this.getY(), this.getWidth(), this.getHeight());
167 g
.drawImage(image
, x
, y
, null);
169 // TODO Tillfällig implementering
170 if (this.isFiering()) {
171 this.setFiering(false);
172 g
.setColor(Color
.WHITE
);
173 List
<Unit
> fireingList
= this.getFireingList();
174 if (!fireingList
.isEmpty()) {
175 Unit unit
= this.getFireingList().get(0);
176 g
.drawLine(this.getCenterX(), this.getCenterY(),
177 unit
.getCenterPoint().x
, unit
.getCenterPoint().y
);
183 * Empty method to handle mouseClicks, override in subclass to implement
187 public void click() {
188 // Overried in subclass
192 * Attemts to clone this Tower.
194 * @return A new instance of the same type as the instantiated Tower.
197 public Object
clone() {
199 return super.clone();
200 } catch (CloneNotSupportedException e
) {
202 throw new Error("Object " + this.getClass().getName()
203 + " is not Cloneable");